问答题

【说明】 用创建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++ ; //修改number的数值,为下一轮素数检查做准备 if (number> m_nCircleNum) { //到达要求检查数值的上限 m_bCont inue= false ; //则准备结束此线程 return ; //结束run()方法,结束线程 } (5) ; try{ //经过一轮检查之后,暂时休眠一段时间 sleep(500); //使主线程挂起指定毫秒数,以便父线程取得控制权 } Catch(InterruptedException e) { Return; } } //for循环结束 } //while循环结束 } //run()方法结束 } //primeThread类定义结束

【参考答案】

(1)system.in.read0 (2)m_bContinue (3)int i=2;i<number (4)fla......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
【说明】以下【C++程序】用于实现两个多项式的乘积运算。多项式的每一项由类Item描述,而多项式由类List描述。类List的成员函数主要有:createList():创建按指数降序链接的多项式链表,以表示多项式:reverseList():将多项式链表的表元链接顺序颠倒:multiplyList(ListL1,ListL2)计算多项式L1和多项式L2的乘积多项式。【C++程序】#include <iostream.h>class List;class Item {friend class List;private:double quot ;int exp ;Item *next;Public:Item(double_quot,int_exp){ (1) ;}};class List{private:Item *list;Public:List(){list=NULL:}void reverseList();void multiplyList(List L1,List L2);void createList();};void List::createList(){ Item *p,*U,*pre;int exp;double quot;list = NULL;while (1) {cout << 输入多项式中的一项(系数、指数) : << endl;cin >> quot >> exp:if ( exp<0 )break ; 指数小于零,结束输入if ( quot=0 )continue;p = list;while ( (2) ) { 查找插入点pre = p;p = p->next;}if ( p != NULL && exp = p->exp ) {p->quot += quot;continue ;}u = (3) ;if (p == list)list = u;elsepre->next = u;u ->next = p;}}void List::reverseList(){ Item *p, *u;if ( list==NULL )return;p = list ->next;list -> next = NULL;while ( p != NULL) {u = p -> next;p ->next = list;list = p;p = u;}}void List::multiplyList ( List L1, List L2 ){ Item *pL1,*pL2,*u;int k, maxExp;double quot;maxExp = (4) :L2.reverseList();list=NULL;for ( k = maxExp;k >= 0;k-- ){pL1 = L1.list;while ( pL1 != NULL && pL1 -> exp > k )pL1 = pL1 ->next;pL2 = L2.1ist;while (pL2 NULL && (5) )pL2 = pL2 -> next;quot = 0.0;while (pL1 != NULL && pL2 != NULL){if(pL1->exp+pL2->exp==k) {(6) pL1 = pL1 -> next;pL2 = pL2 -> next;} else if ( pL1 -> exp + pL2 -> exp > k )pL1 = pL1 -> next;elsepL2 = pL2 -> next;}if ( quot !=0.0 ) {u = new item( quot, k );u -> next = list;list = u;}}reverseList ();L2. reverseList ():}void main(){ List L1,L2,L;cout << 创建第一个多项式链表 n ;L1.createList();cout << 创建第二个多项式链表 n ;L2.createList();L.multiplyList (L1,L2);}
问答题
【说明】以下【C程序】的功能是从文件text_01.ini中读入一篇英文短文,统计该短文中不同单词和它的出现次数,并按词典编辑顺序将单词及它的出现次数输出到文件word_xml.out中。该C程序采用一棵有序二叉树存储这些单词及其出现的次数,一边读入一边建立。然后中序遍历该二叉树,将遍历经过的二叉树上节点的内容输出。程序中的外部函数int getword(FILE *fpt,char *word)从与fpt所对应的文件中读取单词置入word,并返回1;若已无单词可读,即到文件尾部时,则函数返回0。【C程序】#include <stdio.h>#include <malloc.h>#include <ctype.h>#include <string.h>#define INF TEXT_01.INI #define OUTF WORD_XML.OUT typedef struct treenode {char *word;int count;struct treenode *left, *right;} BNODE;int getword(FILE *fpt,char *word);void binary tree(BNODE **t,char *word){ BNODE *ptr, *p;int cmpres;p = NULL;(1) ;while (ptr) { *寻找插入位置* cmpres = strcmp(word, (2) ); * 保存当前比较结果* if (!cmpres) {(3) return;}else {(4) ;ptr = cmpres > 0 ptr->right : ptr->left;}}ptr = (BNODE *)malloc(sizeof(BNODE));ptr->right = ptr->left = NULL;ptr->word = (char *)malloc(strlen(word)+1);strcpy(ptr->word,word);ptr->count = 1;if (p == NULL)(5) ;elseif (cmpres > 0)p->right = ptr;elsep->left = ptr; }}void midorder(FILE *fpt, BNODE *t){ if ( (6) )return;midorder(fpt , t->left);fprintf(fpt , %s %d n , t->word , t->count);midorder(fpt , t->right); }void main(){ FILE *fpt;char word[40];BNODE *root = NULL;if ((fpt = fopen(INF , r )) == NULL) {printf( Can’t open file %s n ,INF);return;}while (getword(fpt,word) == 1)binary_tree( (7) );fclose(fpt);fopen(OUTF, w );midorder(fpt, root);fclose(fpt);}