问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中定义的Matrix是一个用于表示矩阵的类。成员函数max_value的功能是求出所有矩阵元素中的最大值。例如,若有3×3矩阵则调用max_value函数,返回值为3。请编写成员函数max_value.要求:补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为oN文件,并且在本程序中调用。//Matrix.h#include#includeusingnamespacestd;constintM=18;constintN=18;classMatrix{intarray[M][N];public:Matrix(){)intgetElement(inti,intj)const{returnarray[i][j];)voidsetElement(inti,intj,intvalue){,array[ij[j]=value;)intmaxvalue()const;voidshow(constchar*s)const{coutvoidreadFromFile(constchar*f,Matrix&m){ifstreaminfile(f);if(infile.fail()){cerr<>k;m.setElement(i,j,k);}}intMatrix::maxvalue()const{//********333********//********666********}intmain(){Matrixm;readFromFile(””,m);m.show(”Matrix:”);cout
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有坐标点类point、线段类Line和三角形类Triangle的定义,还有main函数的定义。程序中两点间距离的计算是按公式d=实现的,三角形面积的计算是按公式f=。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:Side1:9.43398Side2:5Side3:8area:20注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include#includeusingnamespacestd;classPoint{//坐标点类public:constdoublex,y;Point(doublex=0.0,doubley=0.0):x(x),y(x){}//**********found**********doubledistanceTo(_________)const{//到指定点的距离returnsqrt((x—p.x)*(x—p.x)+(y—p.y)*(y—p.y));}};classLine{//线段类public:constPointp1,p2;//线段的两个端点//**********found**********Line(Pointp1,Pointp2):_________{}doublelength()const{returnp1.distanceTo(p2);}//线段的长度};classTriangle{//三角形类public:constPointp1,p2,p3;//三角形的三个顶点//**********found**********Triangle(__________):p1(p1),p2(p2),p3(p3){}doublelengthl()const{//边p1,p2的长度returnLine(p1,p2).1ength();}doublelength2()const{//边p2,p3的长度returnLine(p2,p3).length();}doublelength3()const{//边p3,p1的长度returnLine(p3,p1).length();}doublearea()const{//三角形面积//**********found**********doubles=_________;returnsqrt(s*(s—lengthl())*(s—length2())*(s—length3()));}};intmain(){Triangler(Point(0.0,8.O),Point(5.0,0.0),Point(0.0,0.0));cout
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中包含源程序文件main.cpp,其中有ElectricFan(“电风扇”)类和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
品牌:清风牌,电源:关,风速:0
品牌:清风牌,电源:开,风速:3
品牌:清风牌,电源:关,风速:0
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class ElectricFan { //"电扇"类
char * brand;
int intensity; //风速: 0-关机 1-弱, 2-中, 3-强
public:
ElectricFan (const char * the_brand) : intensity(0) {
brand = new char [strlen (the_brand) +1];
strcpy(brand, the_brand);
}
~ElectricFan() { delete []brand; }
const char * theBrand () const { return brand; } //返回电扇品牌
int theIntensity () const { return intensity; } //返回风速
// ERROR ********** found**********
bool isOn()const{ return intensity==0;} //返回电源开关状态
// ERROR ********** found**********
void turnOff () const { intensity =0;} //关电扇
void setIntensity (int inten) { //开电扇并设置风速
if(inten>=1
}
void show () {
cout <<"品牌:" << theBrand () << "牌"
<<",电源:" << (isOn () ? "开": "关")
<<",风速:" <<theIntensity() <<
endl;
}
};
int main(){
ElectricFan fan("清风");
fan.show();
fan.setIntensity(3);
fan.show();
fan.turnOff();
fan.show();
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中包含了日期类Date、人员类Person及排序函数sortByAge和主函数main的定义。其中Person的成员函数compareAge的功能是:将当前Person对象和参数Person对象进行比较,若前者的年龄大于后者的年龄,则返回一个正数;若前者的年龄小于后者的年龄,则返回一个负数;若年龄相同则返回0。注意,出生13期越大,年龄越小。请编写成员函数compareAge。在main函数中给出了一组测试数据,此时程序的正确输出结果应为:
按年龄排序
排序前:
张三 男 出生日期:1978年4月20日
王五 女 出生日期:1965年6月3日
杨六 女 出生日期:1965年9月5日
李四 男 出生日期:1973年5月30日
排序后:
张三 男 出生日期:1978年4月20日
李四 男 出生日期:1973年5月30日
杨六 女 出生日期:1965年9月5日
王五 女 出生日期:1965年6月3日
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//Person.h
#include <iostream>
using namespace std;
class Date{ //日期类
int year,month, day; //年、月、日
public:
Date(int year, int month, int day):year(year), month(month), day(day){}
int getYear()const{ return year; }
int getMonth () const { return month; }
int getDay()const{ return day; }
};
class Person{ //人员类
char name[14]; //姓名
boom is male; //性别,为true时表示男性
Date birth date; //出生日期
public:
Person (char * name, bool is_male, Date birth_date);
const char * getName () const { return name; }
bool isMale()const{ return is male; }
Date getBirthdate () const { return birth date; }
int compareAge (const Person
void show()const;
};
void sortByAge(Personps[], int size);
void writeToFile(char * );
//main.cpp
#include"Person.h"
Person::Person(char * name, boom is_male, Date birth_date):is_male(is_male),birth_date(birth_date) {
strcpy(this->name, name);
}
int Person::compareAge(const Person
cout << name << " //显示姓名
<< (is_male? "男" : "女") //显示性别("男"或"女")
<<"出生日期:" //显示出生日期
<<birth_date.getYear () << "年"
<<birthdate.getMonth() <<"月"
<<birthdate.getDay() <<"日";
}
void sortByAge(Person ps[], int size)
for(int i=0; i<size-1; i++) { //
int m=i;
for (int j=i+1; j<size; j++)
if (ps[j].compareAge (ps[m])<0)
m=j;
if (m>i) {
Person p=ps[m];
ps[m]=ps[i];
ps[i]=p;
}
}
}
int main() {
Person staff[] = {
Person ("张三", true, Date (1978, 4, 20)),
Person ("王五", false, Date (1965, 6, 3)),
Person("杨六", false, Date (1965, 9, 5)),
Person ("李四", true, Date (1973, 5, 30))
};
const int size = sizeof (staff)/sizeof(staff[0]);
int i;
cout << "按年龄排序" << endl <<"排序前:";
for(i=0; i<size; i++) staff[i]. show ();
sortByAge (staff, size);
cout << endl << endl << "排序后:";
for(i=0; i<size; i++) staff[i].
show ();
cout << endl;
writeToFile ("");
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeToFile.obj。补充完成重载赋值运算符函数,完成深复制功能。 屏幕上输出的正确结果应该是: Hello! Happy new year! 要求: 补充编制的内容写在“//**********333**********”与“//**********666**********”两行之间。不得修改程序的其他部分。 注意: 程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。//proj3.h#include<iostream>#include<iomanip>using namespace std;class MiniString{ public: friend ostream//如果string2是空指针,则为空字符串 } MiniString&operator=(const Mini—String&otherString); MiniString(const char*s =””):length((S!=0)?strlen(S):0)//构造函数 { sPtr=0; if(length!=0) setString(S); } 一MiniString()//析构函数 {delete[]sPtr;} private: int length;//字符串长度 char*sPtr; //指向字符串起始位置 void setString( const char *string2} //辅助函数 { sPtr=new char[strlen(string2)+1]; //分配内存 if(string2!=0)strcpy(SPtr,string2);//如果string2不是空指针,则复制内容 else sPtr[0]=’\0’;//如果string2是空指针,则为空字符串 }};//proj3.cpp#include<iostream>#include<iomanip>using namespace std;#include”proj 3.h”MiniString//使用重载的赋值运算符str2.modString(”Happy new year!”); cout<<str1<<’\n’;cout<<str2<<’\n’;writeToFile(””);return 0;}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,使它能得出正确的结果。
本程序要求屏幕输出:n=99
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构, 错误的语句在
∥********error********的下面。
#include
∥********error********
class TestClass()
{
public:
∥********error********
void~TestClass(){};
TeStClass(int n)
{
cout<<‘n’<<‘=’<
问答题请使用VC6或使用[答题]菜单打开考生文件夹prog2下的工程prog2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序展示,当该班级换教室后,这两个人的教室也同时得到改变。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
改换教室前:
学号:0789 姓名:张三 班级:062113 教室:521
学号:0513 姓名:李四 班级:062113 教室:521
改换教室后:
学号:0789 姓名:张三 班级:062113 教室:311
学号:0513 姓名:李四 班级:062113 教室:311
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Class{ //"班级"类
public:
Class(const char * id, const char *room){
strcpy (class_id, id);
//********** found**********
______
}
const char * getClassID()const{ return class id; } //返回班号
//********** found**********
const char * getClassroom ()const{______} //返回所在教室房号
void changeRoomTo (const char * new_room {//改换到另一个指定房号的教室
strcpy(classroom, new room);
}
private:
char class id[20]; //班号
char classroom[20]; //所在教室房号
};
class Student{ //"学生"类
char my_id [10]; //学号
char my_name[20 ]; //姓名
Class //所在教室
public:
//**********found**********
Student (const char * the_id, constchar * the_name, Class
strcpy (my_name, the_name );
}
const char * getID () const { returnmy_id; }
const char * getName ()const { return my_name ; }
Class getClass () const { return my_class; }
};
void showStudent (Student * stu) {
cout <<"学号:" <<stu->getID () <<"";
cout <<"姓名:" <<stu ->getName () <<"";
cout <<"班级:" <<stu ->getClass (). getClassID () <<"";
cout <<"教室:" <<stu ->getClass (). getClassroom() <<endl;
}
int main() {
Class cla ("062113", "521");
Student Zhang ("0789", "张三", cla),
Li ("0513", "李四", cla);
cout <<"改换教室前:" <<endl;
showStudent (
showStudent (
//062113班的教室由521改换到311
//**********found**********
______
cout <<"改换教室后: " <<endl;
showStudent (
showStudent (
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h.proj3.cpp、writeToFile.obj。补充完成proj3.h,重载复合赋值运算符+=。要求:补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFih已经编译为obj文件,并且在本程序中调用。//proj3.h#include<iostream>#include<iomanip>usingnamespacestd;classMiniString{public:friendostream&operator<<(ostream&output,constMiniString&s)//重栽流插入运算符{output<<s.sPtr;returnoutput;}friendistream&operator>>(istream&input,MiniString&s)//重载流提取运算符{chartemp[i00];//用于输入的临时数组temp[0]='\0';input>>setw(100)>>temp;intinLen=strlenktemp);//输入字符串长度if(inLen!=0){s.length=inLen;//赋长度if(s.sPtr!=0)delete[]s.sPtr;//避免内存泄漏s.sPtr=newchar[s.length+1];strcpy(s.sPtr,temp);//如果s不是空指针,则复制内容}elses.sPtr[0]='\0';//如果s是空指针,则为空字符串returninput;}MiniString(constchar*S="":length((s!=0)?strlen(S):0)(setString(S);}~MiniString()fdelete[]sPtr;)//析构函数//*************333***********//+=运算符重载//*************666***********private:intlength;//字符串长度char*sPtr;//指向字符串起始位置voidsetString(constchar*string2}//辅助函数{sPtr=newchar[length+1];//分配内存if(string2!=0)strcpy(sPtr,string2);//如果string2不是空指针,则复制内容elsesPtr[0]='\0';//如果string2是空指针,则为空字符串 }};//proj3.cpp#include<iostream>#include<iomanip>usingnamespacestd;#include"proj3.h"intmain(){MiniStringstrl("World"),str2("Hello");voidwriteToFile(char*);str2+=strl;//使用重载的+=运算符cout<<str2<<"/n";writeToFile("");return0;}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使程序正确运行,并且使程序输出的结果为:
30
130
注意:错误的语句在 ∥********error********的下面,修改该语句即可。
#include
int a=10;
class CObj
{
public:
CObj()
{
a=b=0;
}
void di splay()
{
∥********error********
cout<
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中含有一个源程序文件proj2.cpp。函数char * GetNum(char*src,char*buf)从src开始扫描下—个数字字符序列,并将其作为一个字符串取出放入字符串空间buf中。函数返回扫描的终止位置,如果返回NULL表示没有扫描到数字字符序列。 运行程序时,如果输入的一行字符序列是 ABC012XY2378MN274WS 则输出为: Digit string 1 is 012 Digit string 2 is 378 Digit string 3 is 274 注意:只在横线处编写适当代码,不要删除或移动“//****found****”。 //proj2.cpp #include <iostream> using namespace std; char*GetNum(char*src,char*buf) while(*src!='/0') if(isdigit(*src)break; if (*src=="/0") //********found******** ______; while(*src!='/0' buf++; src++; *buf='/0'; return src; int main() char str[100l,digits[20]; cin.getline(str,100); char*p=str; int i=1; while((p=GetNum(p,digits))!=NULL) cout<<"Digit string "<<i<<"is "<<digits<<endl; //********found******** ______; return 0;
问答题请使用VC6或使用【答题】菜单打开考生文件夹progl下的工程progl,该工程中包含程序文件main.cpp,其中有Salary(“工资”)类和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句行有错误,请加以改正。改正后程序的输出结果应为: 应发合计:3500应扣合计:67.5实发工资.3432.5注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;class Salary{public: Salary(const char*id,double thebase,double the—bonus,double the—tax)//ERROR***********found***********:the base(base),the bonus(bonus),the tax(tax){staff id=new char[ strlen(id)+1];strcpy(staff_id,id);} //ERROR***********found*********** ~Salary(){delete*staff—id;) double getGros s Pay()const{return base+bonus;}//返回应发项合计 double getNetPay()const{return getGrossPay()一tax;)//返回实发工资额private: char*staff id; //职工号 double base; //基本工资 double bonus; //奖金 double tax; //代扣个人所得税};int main(){ Salary pay(“888888”,3000.0,500.0,67.50);cout<<“应发合计:”<<pay.getGrossPay()<<“”;cout<<“应扣合计:”<<pay.getGrossPay()一pay.getNetPay()<<””;//ERROR***********found*************cout<<“实发工资:”<<pay::getNetPay()<<endl;return 0;}
问答题使用VC6打开
下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:
Number=7
Number=12
注意:错误的语句在//******error******的下面,修改该语句即可。
#include<iostream.h>
class CMyClass
{
public:
//******error******
CMyClass(int i): Number=i
{
//******error******
return Number;
}
void set(int i)
{
Number=i;
}
void display()
{
cout
"Number="
Number
endl;
}
private:
int Number;
};
void main()
{
//******error******
CMyClass* p=new CMyClass;
p->display();
p->set(12);
p->display();
return;
}
问答题请编写一个函数int Count(double a[],int n),统计出具有n个元素的一维数组中大于等于所有元素平均值的元素个数并返回这个值。注意:请使用for循环实现该函数。 注意:部分源程序已存在文件test12_2.cpp中。 请勿修改主函数main和其他函数中的任何内容,仅在函数Count的花括号中填写若干语句。 文件test12_2的内容如下: #include<iostream.h> int Count(double a[], int n) void main() double a[5]; cout<<"请输入5个double型的数字"<<endl; for(int i=0;i<5;i++) cin>>a[i]; int result=Count(a,5); cout<<"大于等于所有元素平均值的元素个数:"<<result<<endl;
问答题
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵unitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:1 2 3 4 52 3 4 5 63 4 5 6 71 0 0 0 0 00 1 0 0 0 00 0 1 0 0 00 0 0 1 0 00 0 0 0 1 00 0 0 0 0 1注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>using namespace std;//矩阵基础类,一个抽象类class MatrixBase{int rows,cols;public:MatrixBase(int rows,int cols):rows(rows),cols(cols){}int getRows()const/return rows;}//矩阵行数int getCols()const{return cols;}//矩阵列数virtual double getElement(int r,int c)const=O;//取第i个元素的值void show()const{//分行显示矩阵中所有元素for(int i=0;i<rows;i++){cout<<endl:for(int j=0;j<cols;j++)//**********found**********cout<<_______<<" ";}}};//矩阵类class Matrix:public MatrixBase{double*val:public://**********found**********Matrix(int rows,int cols,double m[]=NULL):______________{//**********found**********val=______________;for(int i=0;i<rows*cols;i++)val[i]=(m==NULL?0.0:m[i]);}~Matrix(){delete[]val;}double getElement(int r,int c)const{return val[r*getCols()+c];}};//单位阵(主对角线元素都是1,其余元素都是0的方阵)类class UnitMatrix:public MatrixBase{public:UnitMatrix(int rows):MatrixBase(rows,rows){}//单位阵行数列数相同double getElement(int r,int c)const{//**********found**********if(______________)return1.0;return0.0;}};int main(){MatrixBase*m:double d[][5]={{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}};m=new Matrix(3,5,(double*)d);m->show();delete m;cout<<endl:m=new UnitMatrix(6);m->show();delete m;return0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明的CDeepCopy是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数operator=,以实现深层复制。 要求: 补充编制的内容写在“//**********333**********”与“//**********666**********”之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 //CDeepCopy.h #include<iostream> #include<string> using namespace std; class CDeepCopy public: int n; //动态数组的元素个数 int*p; //动态数组首地址 CDeepCopy(int); ~CDeepCopy(); CDeepCopy//赋值运算符函数 ; void writeToFile (char*); //main.cpp #include"CDeepCopy.h" CDeepCopy::~CDeepCopy()delete[]p; CDeepCopy::CDeepCopy(int k)n=k;p=new int[n];)//构造函数实现 CDeepCopy a.p[0]=1;d.p[0]=666;//对象a,d数组元素的赋值 CDeepCopy b(3); a.p[0]=88;b=a;//调用赋值运算符函数 cout<<b.p[0];//显示内层局部对象的数组元素 cout<<d.p[0];//显示d数组元素a.p[0]的值 cout<<"d fade away;/n"; cout<<a.p[0];//显示a数组元素a.p[0]的值 writeToFile(" "); return 0;
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。程序的功能是寻找1~500以内的亲和数并显示出来,函数amicableNum(int m,int n)判定两个数是否是亲和数。
亲和数的定义为:两个数m和n,如果n的所有因子之和(因子除掉自身)等于m,且m的所有因子等于n,则m、n是互为亲和数。
注意:不能修改程序的其他部分,只能补充amicableNum (int m,int n)函数。
#include <iostream.h>
int amicableNum(int n)
{
int sum=0;
for(int i=1;i<n;i++)
{
if( n%i==0)
{
sum+=i;
}
}
return sum;
}
bool amicableNum(int m,int n)
{
}
void main()
{
cout
"1-500以内的亲和数有: "
endl;
for(int i=1;i<500;i++)
{
for(int j=i+l;j<500;j++)
{
if(i!=j)
{
if (amicableNum (i, j)==i)
{
cout
i
""
j
endl;
}
}
}
}
return;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数sum(int A[NUM][NUM],int n)实现的功能是计算矩阵中所有非质数数字的和。 提示:函数isPrime(int n)的功能是判定当前数字是否为质数,如果是则返回true。 注意:不能修改程序的其他部分,只能修改sum()函数。#include<iostream.h>#include<cmath>#define NUM 50int A[NUM][NUM]={ {10,13,59,70,6}, {2,40,89,92,9}, {14,55,71,11,19}, {79,68,83,97,101}, {102,10001,23,45}};bool isPrime(int n){ if(n==1) return false; if(n==2) return true; for(int i=2;i<n/2;i++) { if(n%i==0) return false; } return true;}int sum(int A[NUM][NUM],int n){}int main(){ cout<<sum(A,5)<<endl; return 0;}
问答题使用VC++6.0打开考生文件夹下的源程序文件2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数fun(int N[4])的功能是用4个数字,组成互不相同且无重复数字的3位数,并将满足所有条件的数字输出到屏幕,并且每输出一个3位数就换一行。 程序分析:可填在百位、十位、个位的数字都是1、2、3、0。组成所有的排列后再去掉不满足条件的排列。如果判定第一个数字是0,则也去掉。 试题程序: #include<iostream.h> void fun(int N[4]) int main() int N[4]=1,2,3,0; fun(N); return 0;
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码。函数show()的功能是将1、2、3、4四个数字,组成互不相同且无重复数字的四位数,并将这些数输出到屏幕,输出的内容如下:
1234 1243 1324 1342 1423 1432 2134 2143 2314
2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132
4213 423l 4312 432l
将函数show()补充完整。
注意:请勿改动主函数。
#include
void show()
{
}
int main()
{
show();
return 0 ;
}
