阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
[说明]
编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。
[Java 代码]
import java. io.* ;
public class abc
{
public static void main ((1))
{int i, n=10, max=0, min=0, temp=0;
try {
BufferedReader br = new BufferedReader (
new InputStreamReader (System.in ) );
max = min =Integer. parselnt (br. readLine () );
}. (2) (IOExccption e ) {} ;
for (i=2 ; i<=n ; i++ ) {
try {
BufferedReader br = new (3) (
new InputStreamReader (System. in ) );
temp =(4) (br. readLine ());
if (temp>max ) max=temp;
if (temp<min) (5)
} catch (IOException e ) {};
}
System.out.println ("max="+max+"\nmin="+min );
}
}
第1题
阅读下列函数说明和C函数,将应填入(n)处的字句写在对应栏内。
【函数2.1说明】
函数palindrome(char s[])的功能是,判断字符串s是否为回文字符串,若是,则返回0,否则返回-1。若一个字符串顺读和倒读都一样时,称该字符串是回文字符串,例如:“LEVEL”是回文字符串,而“LEVAL”不是。
【函数2.1】
int palindrome( char s[ ] )
{ char * pi, * pj;
pi=s; pj=s+strlen(s)-1;
while( pi<pj&&(1)){
pi ++ ;pj --
}
if((2))return -1;
else return 0;
}
【函数2.2说明】
函数f(char * str,char del)的功能是:将非空字符串str分割成若干个子字符串并输出,del表示分割时的标志字符。
例如若str的值为“33123333435”,del的值为“3”,调用此函数后,将输出3个子字符串,分别为“12”、“4”和“5”。
【函数2.2】
void f( char * str, char del)
{ int i ,j ,len;
len = strlen (str)
i=0;
while(i < len) {
while((3))i++; /*忽略连续的标志字符*/
/*寻找从str[i]开始直到标志字符出现的一个子字符串*/
j=i+1;
while(str[j] != del && str[j] ! = '\0')j ++
(4): '\0'; /*给找到的字符序列置字符串结束标志*/
printf("%s\t",&str[i]);
(5);
}
}
第2题
阅读以下技术说明及Java程序,将Java程序中(1)~(5)空缺处的语句填写完整。
【说明】
用创建Thread类的子类的方法实现多线程,判断一个数是否是素数。如果是,打印“是素数”,如果不是,则打印“不是素数”;如果没有参数输入,显示“请输入一个命令行参数”。
【Java程序】
import java.io.* ;
public class TestThread{ //Java Application主类
public static void main(Sting args[ ]){
if (args length<l) { //要求用户输入一个命令行,否则程序不能进行下去
system.out.println("请输入一个命令行参数");
system.exit(0) ;
} //创建用户Thread子类的对象实例,使其处于NewBorn状态
primeThread getPrimes = new primeThread (Integer.parseInt(args[0]));
getPrimes.start () ; //启动用户线程,使其处于Runnable状态
while(getPrimes.isAlive()&& getPrimes.ReadyToGoOn() ) {
system.out.println("Counting the prime number..\n"); //说明主线程在运行
try {
Thread. sleep (500); //使主线程挂起指定毫秒数,以便用户线程取得控制权,
//sleep是static的类方法
}
Catch(InterruptedException e) { //sleep方法可能引起的异常,必须加以处理
return ;
}
} //while循环结束
System.out.println ("按任意键继续……") ; //保留屏幕,以便观察
try {
(1);
}
Catch(IOException e) { }
} //main方法结束
}
class primeThread extends Thread {
//创建用户自己的Thread子类run()中实现程序子线程操作
boolean m_bContinue=true; //标志本线程是继续
int m_nCircleNum ; /循环的上限
prime Thread(int Num){ //构造函数
m_nCircleNum =Nam;
}
boolean ReadyToGoOn () { //判断本线程是否继续执行
return ( (2) );
}
public void run () {
//继承并重载父类Thread的run ()方法,在该线程被启动时自动执行
int number =3;
boolean flag=true;
while (true) { //无限循环
for((3); i++) //检查number是否为素数
if(number %i==0)
(4);
system, out. println (flag);
if (flag) //打印该数是否为素数的信息
system,out.print in (number+ "是素数") ;
else
sys rem.out.print In (number+ "是素数") ;
number++ ; &nb
第3题
阅读以下技术说明和C代码,将C程序中(1)~(5)空缺处的内容填写完整。
[说明]
某种传感器的输出值Ratio依赖于环境温度temp(-40℃≤temp≤50℃)。对一组环境温度值(ITEMS个),已经测量得到了相应的Ratio值(如表4-10表格所示)。表4-10粗略地描述了曲线Ratio(temp)。
校正系数K是Ratio的倒数,因此也依赖于环境温度temp。在数据处理中,需要用更多的列表值细致地描述曲线K(temp),如表4-11所示。
在表4-11中,各温度值所对应的K值是对表4-10进行线性插值再求倒数得到的,具体的计算方法如下。
1) 根据temp值,在表4-10中用二分法查找;
2) 若找到相应的温度值,则按相应的Ratio值求倒数得到K值;
3) 若没找到相应的温度值,则可确定temp所在的温度区间[Tp1,Tp2],同时获得了相应的Ratio1和 Ratio2,再按如下公式计算K值:
在程序中,当temp高于50℃或低于-40℃C时,设定K=0。
[C程序]
include
typedef struct {
int Temp; /* 环境温度 */
double Ratio; /* 传感器的输出值 */
}CURVE;
define ITEMS 7
double GetK(int Temp,CURVE *p,int n)
{ /* 用二分法在n个元素的有序表p中查找与Temp对应的传感器输出值 */
int low, high, m;
double Step;
low = 0;
high = n-1;
if ((Temp<p->Temp) || (Temp>(p+high)->Temp))
return 0.0; /* 超出温度范围时返回 0.0 */
while (low<=high)
{ m=(1);
if (Temp==(p+m)->Temp)
return (2);
if (Temp<(p+m) >Temp)
high=m-1;
else
low=(3);
}
p+=high;
Step=( (4) )/((p+1)->Temp-p->Temp);
return 1.0/ (p->Ratio + Step*( (5) ) ;
}
void main()
{ int Degree;
double k;
CURVE Curve [ITEMS]={{-40,0.2},{-20,0.60.},{-10,0.8},{0,1.0},{10,1.17},{30,1.50},{50,1.8}};
printf ("环境温度 校正系数\n");
for (Degree=-40;Degree<=50;Degree++)
{ k=GetK ( Degree, Curve, ITEMS);
printf("%3d %4.2f\n",Degree,k);
}
}
第4题
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
下面的Java程序演示了程序竞争资源(Mutex的实例对象)而引起程序死锁的一种例子。
【Java程序】
import java.applet.*;
import java.awt.*;
//此处声明一个互斥类
class Mutex { }
class A extends (1)
{
private Mutex first,second;
public A(Mutex f,Mutex s)
{
first = f;
second = s;
}
public void run()
{
//锁定first变量
(2) (first)
{
try
{ //本线程挂起,等待重新调度
Thread.sleep(1); //注意此处(1)不是小题序号
}
catch(InterruptedException e){}
System. out. println("threadA got first mutex");
(2) (second) //锁定second变量
{ //do something
System. out. println("threadA got second mutex");
} //释放second变量
} //释放first变量
}
}
class B extends (1)
{
private Mutex first,second;
public B(Mutex f,Mutex s)
{
(3) ;
second = s;
}
public void run()
{
(2) (second) //锁定second变量
{
//do something
try
{
Thread.sleep(((int)(3*Math.random()))*1000);
//本线程挂起,等待重新调度
}
catch(InterruptedException e){}
System.out.println("threadB got second mutex");
(2) (first) //锁定first变量
{
//do something
System.out.println("threadB got first mutex");
} //释放first变量
} //释放second变量
}
}
public class DeadlockExample
{
public static void main(String arg[])
{
Mutex mutexX = new Mutex();
Mutex mutexY = new Mutex();
AthreadA = new A(mutexX,mutexY);
B threadB = new B (4);
threadA.(5);
threadB.start();
}
}
第5题
阅读以下说明和Java源程序,将应填入(n)处的字句写在对应栏内。
【说明】
以下程序能够计算三角形、矩形和正方形的周长并输出。
程序由5个类组成:AreaTest是主类,类Triangle、Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算周长的抽象方法。
【程序】
public class girthTest{
public static void main (String args[]){
Figure[]figures={
new Triangle (2,3,3),new Rectangle(5,8),new Square(5)
};
for(int i=0;i<figures.length;i++){
System.out.println(figures[i]+"girth="+figures[i].getGirth());
}
}
}
public abstract class Figure{
public abstract double getGirth();
}
public class Rectangle extends (1) {
double height;
double width;
public Rectangle(double height,double width){
this.height=height;
this.width=width;
}
public String toString(){
return "Rectangle:height="+height+",width="+width+":";
}
public double getGirth(){
return (2);
}
}
public class Square extends (3) {
public Square(double width){
(4);
}
public Stdng toString(){
return "Square:width='+width+":";
}
}
public class Triangle extends (5) {
double la;
double lb;
double lc;
public Triangle(double la,double lb,double lc){
this.la=la;this.lb=lb;this.lc=lc;
}
public String toString(){
return "Triangle:sides=" +la+"," +lb+"," +lc+":";
}
public double getGirth(){
return la+lab+lc;
}
}
第6题
阅读以下说明和java代码,将应填入(n)处的字句写在对应栏内。
[说明]
本程序中预设了若干个用户名和口令。用户输入正确的用户名后,可以查找对应的口令,一旦输入结束标记“end”,程序结束。
[Java代码]
import java. io. *:
public class User {
public String user;
public Siring pass;
public User() { }
public User( String u,String p) {
user=u;
pass=p;
}
public String (1) () { return this. user; }
public String (2) () { return this. pass; }
public static void main(String[] args) {
User ua[]={new User("Li","123456"), new User("wang","654321"),
new User("Song","666666")};
while(true) {
InputStreamReader reader = new InputStreamReader(System. in);
BufferedReader inpul = new BnfferedReader(reader);
System. out. print("Enter your name:");
String name = null;
try { name = input. readLine();}
catch (IOException ex) {}
if((3)) break;
int i;
for (i=0;i<3;i++) {
if (name. equals(ua[i]. getUser())){
System. out. println("密码:"+ua[i].getPass());
(4);
}
}
if ((5)) System. out. println("该用户不存在!");
}
}
}
第7题
阅读以下说明和流程图,回答问题1~2,将解答填入答题纸对应的解答栏内。
[说明]
给定一个十进制整数A,将其转换为R进制数的方法是:将A的整数部分逐次除以R,直到商等于0为止,将所得的余数由低位到高位排列在一起,就得到了对应R的进制数。以A=11,R=2为例,11÷2=5…1,5÷2=2…1,2÷2=1…0,1÷2=0…1中各式的余数依次为:1,1,0,1,于是与A对应的二进制数为1011。
下面的流程图实现了将十进制数2597转换为八进制数的功能,其中:
(1)循环1执行除法运算并将余数依次记录在数组a中(假定数组长度足够长),如a[1], a[2],……,a[k];
(2)循环2则用于将这些余数按逆序输出,即a[k],a[k-1],……,a[1];
(3)图中i,j分别是循环1和循环2中的循环变量;
(4)图中q用于记录每次除法所得的商值。
[流程图]
[问题1]
将流程图中的(1)~(4)处补充完整,其中(1)~(3)处要求使用C语言格式书写:(1)处为逻辑表达式,(2)、(3)两处为算术表达式;(4)则须按照“循环变量名:循环初值,循环终值,增量”格式描述。
[问题2]该算法运行的结果(5)。
第8题
阅读以下程序说明和C++程序,将程序段中(1)~(5)空缺处的语句填写完整。
【说明】
以下【C++程序】实现一个简单的小型复数类MiniComplex,该复数类能进行输入、输出、复数的加法、减法、乘法和除法运算,还可以进行复数的相等比较。
【C++程序】
ifndef H_MiniComplex
define H_MiniComplex
include <iostream>
using namespace std;
class MiniComplex{
public: //重载流插入和提取运算符
(1) ostream&operator<<(ostream &osObject,const MiniComplex&complex){
osObject<<"("<<complex.realPart<<"+"<<complex.imagPart<<"i"<<")";
return osObject;
}
(2) istream&operator>>(istream&isObject, MiniComplex&complex){
char ch;
isObject >>complex.realPart>>ch>>complex.imagPart>>ch;
return isObject;
}
MiniComplex(double real=0,double imag=0); //构造函数
MiniComplex operator+(const MiniComplex&otherComplex)const; //重载运算符+
MiniComplex operator-(const MiniComplex&otherComplex)const; //重载运算符-
MiniComplex operator*(const MiniComplex&otherComplex)const; //重载运算符*
MiniComplex operator/(const MiniComplex&otherComplex)const; //重载运算符/
bool perator==(const MiniComplex&otherComplex)const; //重载运算符==
private :
double (3);
double imagPart;
};
end if
include "MiniComplex.h"
bool MiniComplex::operator==(const MiniComplex&otherComplex)const{
return(realPart==otherComplex.realPart&&imagPart==ortherComplex.imagPart);
}
MiniComplex::MiniComplex(double real,double imag){
realPart== real; imagPart==imagPart;
}
MiniComplex MiniComplex::operator+(const MiniComplex&otherComplex)const{
MiniComplex temp;
temp.realPart = realPart+ortherComplex. realPart;
temp.imagPart = imagPart +ortherComplex. imagPart;
return temp;
}
(4)
{ MiniComplex temp;
temp.realPart= realPart-ortherComplex. realPart;
temp.imagPart = imagPart-ortherComplex. imagPart;
return temp;
}
MiniComplex MiniComplex::operator*(const MiniComplex&otherComplex)const{
MiniComplex temp;
temp.realPart = (realPart*ortherComplex. realPart)-(imagPart *ortherComplex.imagPart);
temp.imagPart = (realPart*ortherComplex. imagPart)+(imagPart *ortherComplex.realPart);
return temp;
}
MiniComplex MiniComplex::operator/(const MiniComplex&otherComplex)const{
MiniComplex temp;
float tt;
tt=1/(ortherComplex.realPart*ortherComplex.realPart+ortherComplex.imagPart *ortherComplex. imagPart);
temp.realPart=((realPart*ortherComplex, realPart)+(imagPart *ortherComplex. imagPart))*tt;
temp.imagPart =((imagPart *ortherComplex. realPart)-(realPart*ortherComplex. imagPart))*tt;
return temp;
}
include <iostream>
include <MiniComplex.h>
using namespace std;
int main(){
MiniComplex numl(23, 34),num2(56, 35);
cout<<"Initial Value of num1="<<num1<<"\n Initial Value of num2="<<num2<<end1;
cout<<num1<<"+"<<num2<<"="<<num1+num2<<end1; //使用重载的加号运算符
cout<<num1<<"-"<<num2<<"="<<num
第9题
阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。
【说明】
设计一个评选优秀教师和学生的程序,其类结构如图6所示。当输入一系列教师或学生的记录后,将优秀学生及教师的姓名列出来。
【程序】
include<iostream.h>
include<stdio.h>
enum boolean {False,True}
class base
{
protected:
char name[8];
public:
void getname() {cout<<"姓名:" ;cin>>name; }
void printname() {cout<<"姓名:"<<name<<endU3
virtual boolean isgood() =0;
}
class student:(1)
{
int num;
public:
void getnum()
cout<<"考试成绩:" cin>>num;
boolean isgood() {return (2);{
};
class teacher:(3) public base
int num;
public:
void getnum()
cout<<"每年发表论文数:" ;cin>>num;
boolean isgood() {return (4);}
};
void main()
base* p[50];
student * pstud;
teacher * ptech;
char ch;
int count =0;
do
cout<<"输入教师(t)或学生(s):"
cin>>ch;
if(ch =='s')
{
pstud = new student;
pstud ->getname();
pstud ->getnum();
p[count ++ ] = pstud;
}
else if(ch == 't')
{
ptech = newteacher;
ptech - >getname( )
ptech ->getnum();
p[count++]=ptech;
}
else
cout<<"输入错误<<endl;
cout<<"继续输入码(Y/n)";
cin>>ch;
} while(ch == 'y')
for(int i=0;i<count;i++)
{
if((5)) //若为优秀,则输出
p[i]->printname();
}
}
第10题
阅读下列函数说明和C函数,将应填入______处的语句写在答题纸的对应栏内。
[函数6说明]
函数DelA_InsB(LinkedList La,LinkedList Lb,int key1,int key2,int len)的功能是:将线性表A中关键码为key1的结点开始的len个结点,按原顺序移至线性表B中关键码为key2的结点之前,若移动成功,则返回0;否则返回-1。线性表的存储结构为带头结点的单链表,La为表A的头指针,Lb为表B的头指针。单链表结点的类型定义为:
typedef struct node {
int key;
struct node * next;
} * LinkedList;
[函数6]
int DelA InsB(LinkedList La,LinkedList Lb,int key1,int key2,int len)
{ LinkedListp,q,s,prep,pres;
int k;
if(! La->next‖! Lb->next‖->next‖len<=0)return-1;
p=La->next;prep=La;
while(p&&p->key!=key1){ / * 查找表A中键值为key1的结点 * /
prep=p;p=p->next;
}
if(! p)return -1; / * 表A中不存在键值为key1的结点 * /
q=p;k=1;
while(q&& (1) ){ / * 在表A中找出待删除的len个结点 * /
(2);k++;
}
if(! q)return-1: / * 表A中不存在要被删除的len个结点 * /
s=Lb->next; (3);
while(s s && s->key!=key2){ / * 查找表B中键值为key2的结点 * /
pres=s;s=s->next;
}
if(! s)return-1; / * 表B中不存在键值为key2的结点 * /
(4)=q->next; / * 将表A中的len个结点删除 * /
q->next=(5);
pres->next=p; / * 将len个结点移至表B * /
return 0;
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!