请打开考生文件夹下的解决方案文件proj3,该文件中定义了用于表示日期的类Date、表示人员的类Person和表示职员的类Staff;程序应当显示: 张小丽123456789012345 但程序中有缺失部分,请按以下提示把缺失部分补充完整: (1)在“//**1** ****found****”的下方是析构函数定义中的语句,它释放两个指针成员所指向的动态空间。 (2)在“//**2** ****found****”的下方是rename函数中的一个语句,它使指针name指向申请到的足够容纳字符串new_name的空间。 (3)在“//**3** ****found****”的下方是构造函数定义的一个组成部分,其作用是利用参数表中前几个参数对基类Person进行初始化。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。填写的内容必须在一行中完成,否则评分将产生错误。//proj3.cpp#includeiostreamusing namespace std;Class Person{ char*idcardno;//用动态空间存储的身份证号 char*name; //用动态空间存储的姓名bool ismale;//性别:true为男,false为女public: Person(const char*pid,constchar*pname,bool pmale); ~Person(){//**1** *******found******* _____________; } const char*getIDCardNO()const{return idcardno;) const char*getName()const{return name;} void rename(const char * new name); bool isMale()const{return is-male;}};class Staff:public Person{ char*department; double salary;public: Staff(const char*id_card_no,const char*p_name,bool is male,const char:Ic dept,double sal); ~Staff(){delete[]department;) const char *getDepartment()const{return department;} void setDepartment(const char*d); double getSalary()const{return salary;}void setSalary(double S){salary=s;}};Person::Pemson(const char*id card no,const char*p name,bool is male):ismale(is_male){ idcardno=new char[strlen(id card no)+1]; strcpy(idcazdno,id_card_no); name=new char[strlen(pname)+1]; strcpy(name,p_name);}void Person:: rename (constchar*new_name){ delete[]name;//**1** *******found*******____________;strcpy(name,new_name);}Staff::Staff(conSt char*id card no,const char*P_name,bool is male,//**3** *******found******* const char*dept,double sal):{ department=new char[strlen(dept)+1]; strcpy(department,dept); salary=sal;}void Staff:: setDepartment(const char*dept){ delete[]department ; department=new char[strlen(dept)+1]; strcpy(department,dept);}int main(){ Staff Zhangsan("123456789012345","张三",false,"人事部",1234.56);Zhangsan.rename("张小丽"); coutZhangsan.getName()Zhangsan.getIDCardNO()endl; return 0;}
使用VC6打开考生文件夹下的源程序文件modi2.epp。阅读下列函数说明和代码。函数sort(intm,intn,mt1)实现将三个整数m、n、1由大到小输出。m最大,1最小。 程序分析:程序实现时,可以把最大的数放到m上,先将m与n进行比较,如果mn则将m与n的值进行交换,然后再用m与1进行比较,如果m1则将m与1的值进行交换,这样能使m最大。然后再将n与1进行比较,若n1则将n与1的值互换,互换后则1最小。 将函数sort(int&m,intn,int1)补充完整,实现三个数的排序。 注意:请勿改动主函数。#includeiostream.hvoid sort(int&m,int&n,int&1){}int main(){ int×=9 ; int y=13; int z=一3; sort(x,y,z); coutx‘,。y‘,’Zend1; retumR 0;}
请打开考生文件夹下的解决方案文件proj2,此工程包含有一个源程序文件proj2.cpp,其中定义了Stack类和ArrayStack类。 Stack是一个用于表示数据结构“栈”的类,栈中的元素是字符型数据。Stack为抽象类,它只定义了栈的用户接口,如下所示: 公有成员函数功能 push 入栈:在栈顶位置添加一个元素 pop 退栈:取出并返回栈顶元素 ArrayStack是Stack的派生类,它实现了Stack定义的接口。ArrayStack内部使用动态分配的字符数组作为栈元素的存储空间。数据成员maxSize表示的是栈的最大容量,top用于记录栈顶的位置。成员函数push和pop分别实现具体的入栈和退栈操作。 请在程序中的横线处填写适当的代码,然后删除横线,以实现上述功能。此程序的正确输出结果应为: a,b,c c,b,a 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 1 //proj2.cpp 2 #includeiostream 3 using namespace std; 4 class Stack{ 5 Dublic: 6 virtual void push(char c)=0; 7 virtual char pop()=0; 8 }; 9 10 class ArrayStack:public Stack{ ll char * p; 12 int maxsize; 13 int top; 14 public: 15 ArrayStack(int s) 16 { 17 top=0; 18 maxsize=s; 19 //********found******** 20 p=__________; 21 } 22 ~ArrayStack() 23 { 24 //********found******** 25 ____________; 26 } 27 void push(char c) 28 { 29 if(top==maxSize){ 30 cerr"Overflow!\n"; 31 return; 32 } 33 //********found******** 34 ; 35 top++; 36 } 37 char pop() 38 { 39 if(top=0){ 40 cerr"Underflow!\n"; 41 return'\0'; 42 } 43 top__________; 44 //********found******** 45 ___________; 46 } 47 }; 48 void f(StacksRef) 49 { 50 char ch[]={'a','b','c'); 51 cout ch[0] ", " ch [1] ", " ch[2] endl ; 52 sRef.push(ch[0]);sRef.push(ch[1]);sRef.push(ch[2]); 53 coutsRef.pop()","; 54 coutsRef.pop()","; 55 coutsRef.pop()endl ; 56 } 57 int main() 58 { 59 ArrayStack as(10); 60 f(as); 61 return 0; 62 }
请打开考生文件夹下的解决方案文件proj3,此工程包含一个源程序文件proj3.epp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象myArray中,然后对整数序列按非递减排序,最后由函数writeToFile选择序列中的部分数据输出到文件out.dat中。文件in.dat中的整数个数不大于300个。 要求: 补充编制的内容写在“//************333************”与“//************666************”两行之间。实现对整数序列按非递减排序,并将排序结果在屏幕上输出。不得修改程序的其他部分。 注意:程序最后已将结果输出到文件out.dat中。输出函数writeToFile已经给出并且调用。 1 //proj3.cpp 2 #includelostream 3 #includefstream 4 #includecstring 5 using namespace std; 6 7 class intArray 8 { 9 private: 10 int * array;//整数序列首地址 11 int length;//序列中的整数个数 12 public: 13 //构造函数,从文件中读取数据用于初始化新对象。参数是文件名 14 intArray(char*ilename); 15 void sort();//对整数序列按非递减排序 16 ~intArray(); 17 void writeToFi le(char * filename); 18 }; 19 20 intArray::intArray(char * filename) 2l { 22 ifstream myFile(filename); 23 int len=300; 24 array=new int[len]; 25 length=0; 26 while(myFilearray[length++]); 27 length--; 28 myFile.close(); 29 } 30 31 void intArray::sort(){ 32 //*************** 333*************** 33 34 //***************666*************** 35 } 36 intArray::~intArray() 37 { 38 delete[]array; 39 } 40 41 void intArray::writeToFile 42 (char * filename) 43 { 44 int step=0; 45 ofstream outFile(filename); 46 for(int i=0;ilength;i=i+step) 47 { 48 outFilearray[i]endl; 49 step++; 50 } 5l outFile.close(); 52 } 53 54 void main() 55 { 56 intArray myArray("in.dat"); 57 myArray.sort(); 58 myArray.writeToFile("out.dat"); 59 }
请打开考生文件夹下的解决方案文件proj1,该工程中包含程序文件main.cpp,其中有类Door(“门”)和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 打开503号门…门是锁着的,打不开。 打开503号门的锁…锁开了。 打开503号门…门打开了。 打开503号门…门是开着的,无须再开门。 锁上503号门…先关门…门锁上了。 注意:只修改每个“//ERROR ********found********”下的那一行,不要改动程序中的其他内容。#includeiostreamusing namespace std;class Door{ int num; //门号 bool closed; //true表示门关着 bool locked; //true表示门锁着public: Door(int num){//ERROR ********found******** num=this-num; closed=locked=true; } bool isClosed()corlst{return closed;}//门关着时返回true,否则返回false bool isOpened()const{return !closed;}//门开着时返回true,否则返回false bool isLocked()const{return locked;}//门锁着时返回true,否则返回false bool isUnlocked()COlISt(return!locked;}//门未锁时返回true,否则返回false void open(){ //开门 toutendl"打开"num"号门…"; //ERROR ****found**** if(closed) cout"门是开着的,无须再开门。”; elSe if(locked) cout"门是锁着的,打不开。"; else{ closed=false; tout;"门打开了。"; } } void close(){ //关门 coutendl"关上"num"号门…"; if(closed) tout"门是关着的,无须再关门。"; else{ closed=true; cout"门关上了。"; } }//ERROR ********found******** void lock()const{ //锁门 coutendl"锁上"num"号门…"; if(locked) tout"门是锁着的,无须再锁门。"; else{ if(!closecl){ cout"先关门…"; c10sed=true; } locked=true; cout"门锁上了。"; } } void unlock(){ //开锁 coutendl"开"num"号门的锁…"; if(!locked) cout"门没有上锁,无须再开锁。”; else{ locked=false; tOUt"锁开了。"; } } }; int main(){ Door door(503); door.open(); door.unlock(); door.open(); door.open(); door.lock(); return 0;}
请打开考生文件夹下的解决方案文件proj3,其中使用友元函数访问类的私有数据成员,求出两个数据成员的大于1的最小公因子。请编写友员函数FriFun,使其输出结果为: Common denominator is 2 要求:补充编制的内容写在“//*********333*********”与“//*********666*********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//proj3.hClass FriFunClass{ int a,b;public: FriFunClass(int i,int j){a=i;b=j;} friend int FriFun(FriFun-Class x); //友元函数}; void writeToFile(const char*),//proj3.cpp#includeiostreamusing namespace std;#include "prj3.h"int FriFun(FriFunclass X)(//********333******** //由于函数FriFun()是类FriFunClass的友元函数,所以它可以直接访问a和b//********666********}int main(){ FriFunclass n(10,2 0); if(FriFun(n)) cout"Common denominator is" FriFun(n) "\n"; else cout"NO common denominator.\n"; writeToFile(" "); return 0;}
