问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:(1)定义类的私有变量a、b,请在注释//********1********后添加适当的语句。(2)完成类的构造函数,对变量a、b赋值,缺省两个变量值为1,在注释//********2********之后添加语句。(3)完成打印函数display(),打印出“a*b=c”格式的一个乘法算式,在注释//********3********之后添加语句。(4)补充主函数,使得打印9×9的乘法口诀。在注释//********4********后添加语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>class CMyFormula{private://********1********public://********2********{this->a=a;this->b=b;}void set(int x,int y){a=x;b=y;}void display(){//********3********}};int main(){CMyFormula formular(1,1);for(inti=1;i<=9;i++){//********4********for(int j=1;j++){formular.set(j,i);formular.dispiay();cout<<" ";}cout<<endl;}return0;}
问答题基本操作
请使用"答题"菜单或使用VC6打开考生文件夹proj1下的工程proj1。此工程包含程序文件main.cpp。
程序中位于每个// ERROR **********found**********下的语句行有错误,请加以改正。更正后程序的输出应该是:1.41421
注意:只能修改每个// ERROR **********found**********下的那一行,不要改动程序中的其他内容。
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错误,请改正其中的错误,使程序正常运行,并且输出以下结果: (4,5) 7,8 (4,8) 注意:错误的语句在//********error********的下面,修改该语句即可。#include<iostream.h>class CObj0.{public: CObj0(int i,int j) { x=i; y=j; } //********error******** virtual void move(int a;int b) { x+=a; y+=b; } void print() { cout<<"("<<x<<","<<y<<")"<<endl; }public: int x , y;};class CObj1:public CObj0{public: //********error******** CObj1(int i,int j,int k):(i,j) { m=k; n=1; } void print() { cout<<m<<","<<n<<endl; } void func() { move(3,5); } void display() { //******error****** print(); }private: int m , n;};void main(){ CObj0 obj(4,5); obj.print(); CObj1 obj1(1,3,7,8); obj1.func(); obj1.print(); obj1.display();}
问答题用VC++6.0打开
下的源程序文件3.cpp,其中定义了多个类,其中定义的类并不完整,按照要求完成下列操作,将类的定义补充完整;使其输出如下:
TC2
TC3
TC1
(1)类TC2公共继承于TC1,请在注释1后添加适当的语句。
(2)print函数为虚函数,请在注释2后添加适当的语句。
(3)修改语句,使得p指定对象obj3,请在注释3后添加适当的语句。
(4)修改语句,利用p调用print()函数,完成输出信息,在注释4后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
试题程序:
#include <iostream>
using namespace std;
class TC1
{
public:
void print()
{
cout<<"TC1"<<endl;
}
};
//********1********
class TC2
{
public:
//********2********
void print()
{
cout<<"TC2"<<endl;
}
};
class TC3:public TC2
{
public:
void print()
{
cout<<"TC3"<<endl;
}
};
int main()
{
TC2 obj2;
TC3 obj3;
TC1* p;
obj2.print();
obj3.print();
//********3********
p=obj3;
//********4********
p.print();
return 0;
}
问答题用VC6打开下的源程序文件modi.3.cpp。其中定义的类并不完整,按照要求完成下列操作,将类的定义补充完整。在屏幕和程序modi3.txt文件中输出以下结果:HelloTest出现异常情况其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整:(1)以追加的方式打开文件modi3.txt,请在注释//********1********后添加适当的语句。(2)定义一个类对象s,请在注释//********2********后添加适当的语句。注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。#include<iostream.h>#include<fstream>usingnamespacestd;voidWriteFile(char*x){ofstreamout1;//********1********out1.open("modi3.txt",);out1x"";out1.close();}voidClearFile(){ofstreamout1;out1.open("modi3.txt");out1.close();}classTestClass{public:TestClass(){cout"Hello"endl;WriteFile("Hello");}~TestClass(){cout"Test"endl;WriteFile("Test");}};voidmain(){ClearFile();try{//********2********throw1;}catch(int){cout"出现异常情况"endl;WriteFile("出现异常情况");}}
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2。该工程中包含一个程序文件main.cpp,其中有类Quadritic、类Root及主函数main的定义。一个Quadritic对象表示一个ax
2
+bx+c的一元二次多项式。一个Root对象用于表示方程ax
2
+bx+c=0的一组根,它的数据成员num_of_roots有3种可能的值,即0、1和2,分别表示根的3种情况:无实根、有两个相同的实根和有两个不同的实根。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为(注:输出中的X^2表示x
2
):
3X^2+4X+5=0.0 无实根
4.5X^2+6X+2=0.0 有两个相同的实根:-0.666667和-0.666667
1.5X^2+2X-3=0.0 有两个不同的实根:0.896805和-2.23014
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Root{ //一元二次方程的根
public:
const double x1; //第一个根
const double x2; //第二个根
const int num_of_roots; //不同根的数量:0、1或2
//创建一个“无实根”的Root对象
Root(): x1 (0.0), x2 (0.0), num_of_roots(0) {}
//创建一个“有两个相同的实根”的Root对象
Root (double root)
// ********found********
:______{}
//创建一个“有两个不同的实根”的Root对象
Root (double root1, double root2): x1 (root1), x2 (root2), num_of_roots(2) {}
void show() const { //显示根的信息
cout << "/t/t";
switch(num_of_roots) {
case 0:
// ********found********
______
case 1:
cout << "有两个相同的实根:" << x1 << "和" << x2; break;
default:
cout << "有两个不同的实根:" << x1 << "和" << x2; break;
}
}
};
class Quadratic { //二次多项式
public:
const double a,b,c; //分别表示二次项、一次项和常数项等3个系数
Quadratic (double a, double b, double c)
//构造函数
// **********found**********
:______{}
Quadratic (Quadratic
}
Quadratic sub (Quadratic x) const {
//求两个多项式的差
// **********found**********
______
}
double value (double x) const {
//求二次多项式的值
return a*x*x+b*x+c;
}
Root root() const { //求一元二次方程的根
double delta = b *b - 4 * a * c;
//计算判别式
if (delta < 0.0) return Root();
if (delta == 0.0)
return Root(-b/(2 * a));
double sq = sqrt(delta);
return Root((-b+sq)/(2 * a), (-b-sq)/(2*a));
}
void show() const { //显示多项式
cout << endl << a << "X^2" << showpos << b << "X" << c << noshowpos;
}
void showFunction() { //显示一元二次方程
show();
cout << "=0.0";
}
};
int main() {
Quadratic q1(3.0, 4.0, 5.0), q2(4.5, 6.0, 2.0), q3(q2.sub(q1));
q1.showFunction();
q1.root().show();
q2.showFunction();
q2.root().show();
q3.showFunction();
q3.root().show();
cout << endl;
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件mo(1i2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(char*des,char*str)的功能是去掉字符串str中相同的字母,并将处理后的结果存到des所指的字符串中。
例如:
输入:T1lis is great!
输出:111is grea!
注意:不能修改程序的其他部分,只能补充fun()函数。
#include
#define MAXLEN 1024
void convert(char*des,char*
str)
{
}
void main()
{
char sour[MAXLEN];
char dest[MAXLEN];
cout<<”Please input a string:
”<
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正确运行,并且使程序输出的结果为: 30 130 注意:错误的语句在//********error********的下面,修改该语句即可。#include<iostream.h>int a=10;Class CObj{public: CObj() { a=b=0; } Void display() { //********error******** cout<<a<<b<endl; } Void funC(int a) { //********error******** a+=a; } void func2() { //********error******** a+=a: }Private: int a , b; }; Void main() { CObj obj; obj.func(3); obj.display(); obj.func2(); obj.display();}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。sort成员函数的功能是将当前数据表中的元素升序排列。请编写这个sort函数。程序的正确输出应为:排序前:7,1,3,11,6,9,12,10,8,4,5,2排序后:1,2,3,4,5,6,7,8,9,10,11,12要求:补充编制的内容写在“**********333**********”与“//**********666**********”两行之间。不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序调用。//DataList.h#include<iostream>using namespace std;class DataList{//数据表类int len;double*d;public:DataList(int len,double data[]=NULL);~DataList(){delete[]d;}int length()const{return len;}//数据表长度(即数据元素的个数)double getElement(int i)const{return d[i];}void sort();//数据表排序void show()const;//显示数据表};void writeToFile(char*,const DataList&);//main.cpp#include"DataList.h"DataList::DataList(int len,double data[]):len(len){d=new double[len];for(int i=0;i<len;i++)d[i]=(data=NULL?0.0:data[i]);}void DataList::sort(){//数据表排序//**********333**********//**********666**********}void DataList::show()const{//显示数据表for(int i=0;i<len-1;i++)cout<<d[i]<<",";cout<<d[len-1]<<endl;}int main(){double s[]={7,1,3,11,6,9,12,10,8,4,5,2};DataList list(12,s);cout<<"排序前:";list.show();list.sort();cout<<endl<<"排序后:";list.show();writeToFile(" ",list);return0;}
问答题请使用VC6或使用[答题]菜单打开
proj3下的工程proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成员函数add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数remove。在main函数中给出了一组测试数据,此时程序的正确输出结果应为:
2 3 4 5 27 28 31 66 75
2 3 4 5 6 27 28 31 66 75
2 3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out. dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//IntegerSet. h
#ifndef INTEGERSET
#define INTEGERSET
#include<iostream>
using namespace std;
const int MAXELEMENTS=100;
//集合最多可拥有的元素个数
class IntegerSet {
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter;//用于记录集合中元素个数的计数器
public:
IntegerSet(): counter(0) {}
//创建一个空集合
IntegerSet(int data[], int size);
//利用数组提供的数据创建一个整数集合
void add(int element);
//添加一个元素到集合中
void remove(int element);
//删除集合中指定的元素
int getCount()const {return counter;}
//返回集合中元素的个数
int getElement(int i)const {return elem[i];}//返回集合中指定的元素
void show()const;
};
void WriteToFile(char*);
#endif
//main. cpp
#include"IntegerSet. h"
#include<iomanip>
IntegerSet::IntegerSet(int data[], int size): counter(0) {
for(int i=0; i<size; i++)
add(data[i]);
void IntegerSet::add(int element) {
int j;
//从后往前寻找第一个小于等于element的元素
for(j=counter; j>0; j--)
if(element>=elem[j-1]) break;
//如果找到的是等于element的元素,说明要添加的元素已经存在,直接返回
if(j>0)
if(element==elem[j-1])return;
//如果找到的是小于element的元素,j就是要添加的位置
//该元素及其后面的元素依次后移,腾出插入位置
for(int k=counter; k>j; k--)
elem[k]=elem[k-1];
elem[j]=element;//将element插入到该位置
counter++;//计数器加1
}
void IntegerSet::remove(int element) {
//********333********
//********666********
}
void IntegerSet::show()const {
for(int i=0; i<getCount(); i++)
cout<<setw(4)<<getElement(i);
cout<<endl;
}
int main() {
int d[]={5, 28, 2, 4, 5, 3, 2, 75, 27, 66, 31};
IntegerSet s(d, 11); s. show();
s. add(6); s. show();
s. add(19); s. show();
s. remove(2); s. show();
s. add(4); s. show();
WriteToFile(" ");
return 0;
}
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp。其中定义了类Set和用于测试该类的主函数main。类Set是一个用于描述字符集合的类,在该字符集合中,元素不能重复(将“a”和“A”视为不同元素),元素最大个数为100。为该类实现一个构造函数Set(char * s),它用一个字符串来构造一个集合对象,当字符串中出现重复字符时,只放入一个字符。此外,还要为该类实现另一个成员函数InSet(char c),用于测试一个字符c是否在一个集合中,若在,则返回true;否则返回false。
构造函数Set和成员函数InSet的部分实现代码已在文件proj2.cpp中给出,请在标有注释“// TODO:”的行中添加适当的代码,将这两个函数补充完整,以实现其功能。
提示:在实现构造函数时,可以调用InSet函数来判断一个字符是否已经在集合中。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
//proj2.cpp
#include <iostream>
using namespace std;
const int MAXNUM = 100;
class Set {
private:
int num; //元素个数
char setdata[MAXNUM]; //字符数组,用于存储集合元素
public:
Set(char *s); //构造函数,用字符串s构造一个集合对象
bool InSet(char c);
//判断一个字符c是否在集合中,若在,返回true,否则返回false
void Print() const; //输出集合中所有元素
};
Set::Set(char * s)
{
num = 0;
while (* s) {
// **********found**********
if (______) //TODO:添加代码,测试元素在集合中不存在
// **********found**********
______; //TODO:添加一条语句,加入元素至集合中
s++;
}
}
bool Set::InSet (char c)
{
for (int i = 0; i < num; i++)
// **********found**********
if (______) //TODO:添加代码,测试元素c是否与集合中某元素相同
// **********found**********
______; //TODO:添加一条语句,进行相应处理
return false;
}
void Set::Print () const
{
cout << "Set elements:" << endl;
for(int i = 0; i < num; i++)
cout << setdata[i] << "";
cout << endl;
}
int main()
{
char s[MAXNUM];
cin.getline (s, MAXNUM - 1); //从标准输入中读入一行
Set setobj(s); //构造对象setobj
setobj.Print(); //显示对象setobj中内容
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程中声明的Array是一个表示数组的类。一个Array对象可以包含多个整型元素。Array的成员说明如下: 成员函数add用于向数组的末尾添加一个元素; 成员函数get用于获取数组中指定位置的元素; 数据成员a表示实际用于存储数据的整型数组; 数据成员size表示数组的容量,数组中的元素个数最多不能超过size; 数据成员num表示当前数组中的元素个数。 SortedArray是Array的派生类,表示有序数组。SortedAr-ray重新定义了Array中的add函数,以确保有序数组中的元。素始终按照升序排列。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: 10,9,8,7,6,5,4,3,2,1, 1,2,3,4,5,6,7,8,9,10, 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>using namespace std;class Array{public: Array(unsigned int s) { Size=s; num=0; a=new int[s]; } virtual—Array(){delete[]a; } virtual void add(int e) { if(num<size){//**********found************ num++; } } int get(unsigned int i)const { if(i<size) return a[i]; return 0 ; }protected: int*a; unsigned int size,num;};class SortedArray:public Array{public://**********found*********** SortedArray(unsigned int S) :________{} virtual void add(int e) { if(num>=Size) return; int i=0,j; while(i<num){ if(e<a[i]){ for(j=num;j>i;j—一){//**********found********** _________; }//**********found********** ______; break; } i++; } if(i==num) a[i]=e; num++; }};void fun(Array } for(i=0;i<10;i++){ cout<<a.get(i)<<”,”; } cout<<endl;}int main(){ Array a(10); fun(a); SortedArray sa(10); fun(sa); return 0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
Base:Good Luck!
Derived:Good Luck!
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Base
{
// ERROR ******** found********
private:
char* msg;
public:
Base(char* str)
{
// ERROR ******** found********
msg=new char[strlen(str)];
strcpy (msg, str);
cout << "Base: " <<msg << endl;
}
// ERROR ******** found********
~Base() { delete msg; }
};
class Derived:public Base
{
public:
Derived (char* str):Base(str) {}
void Show () { cout <<"Derived:" <<msg << endl; }
};
int main ()
{
Derived obj ("Good Luck! ");
obj.Show();
return 0;
}
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数remove的作用是从集合中删除指定的元素(如果集合中存在该元素),成员函数sort的作用是将集合中的整数按升序进行排序。请编写这个sort函数。此程序的正确输出结果应为:
5 28 2 4 5 3 2 75 27 66 31
5 28 2 4 5 3 2 75 27 66 31 6
5 28 2 4 5 3 2 75 27 66 31 6 19
5 28 4 5 3 2 75 27 66 31 6 19
5 28 4 5 3 2 75 27 66 31 6 19 4
2 3 4 4 5 5 6 19 27 28 31 66 75
要求:
补充编制的内容写在“// *******333*******”与“// *******666*******”之间。不得修改程序的其他部分。
注意:相关文件包括:main.cpp、Integers.h。
程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。
//Integers.h
#ifndef INTEGERS
#define INTEGERS
#include <iostream>
using namespace std;
const int MAXELEMENTS = 100;
//集合最多可拥有的元素个数
class Integers{
int elem[ MAXELEMENTS];
//用于存放集合元素的数组
int counter;
//用于记录集合中元素个数的计数器
public:
Integers(): counter(0) {}
//创建一个空集合
Integers (int data[], int size);
//利用数组提的数据创建一个整数集合
void add (int element);
//添加一个元素到集合中
void remove (int element);
//删除集合中指定的元素
int getCount() const {return counter;}
//返回集合中元素的个数
int getElement (int i) const {return elem[i];}
//返回集合中指定的元素
void sort();
//将集合中的整数按由小到大的次序进行排序
void show() const;
//显示集合中的全部元素
};
void writeToFile(const char * path);
#endif
//main.cpp
#include "Integers.h"
#include <iomanip>
Integers::Integers (int data[], int size): counter(0) {
for (int i=0; i<size; i++)
add(data[i]);
}
void Integers::add (int element) {
if(counter < MAXELEMENTS)
elem [counter + +] = element;
}
void Integers::remove(int element) {
int j;
for(j = counter-1; j >=0; j--)
if(elem[j] == element)
break;
for(int i=j; i<counter-1; i++)
elem[i] = elem[i+1];
counter--;
}
void Integers::sort() {
// ********333********
// *******666*******
}
void Integers::show() const {
for(int i=0; i<getCount(); i++)
cout << setw (4) << getElement(i);
cout << endl;
}
int main() {
int d[] = {5,28,2,4,5,3,2,75,27,66,31};
Integers s (d,11);
s.show();
s.add(6); s.show();
s.add(19); s.show();
s.remove(2); s.show();
s.add(4); s.show();
s.sort(); s.show();
writeToFile(" ");
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序,使该程序输出倒9×9口诀。
程序分析:分行与列考虑,共9行9列,设置两个变量i和j,i控制行,j控制列。
程序运行结果如下:
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45
6*9=54 7*9=
63 8*9=72 9*9=8 1
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40
6*8=48 7*8=
56 8*8=64
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=
42 7*7=49
1*6=6 2*6=12 3*6=1 8 4*6=24 5*6=30
6*6=36
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*4=4 2*4=8 3*4=12 4*4=16
1*3=3 2*3=6 3*3=9
1*2=2 2*2=4
1*1=1
注意:只能补充函数show(),请勿改动其他部分的内容。
#include
void show()
{
}
void main()
{
cout<<“9*9倒乘法口诀”<
问答题使用VC++6.0打开下的源程序文件2.cpp。请补充完整程序fun(int i),使其完成以下功能:如果i=5,则输出如下5行#号。
#
##
###
####
#####
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<iostream.h>
vold fun(int i)
{
}
int main()
{
fun(5);
return 0;
}
问答题请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2。此工程定义了一个人员类Person,然后派生出学生类Student和教授类Professor。请在程序中的画线处填写适当的代码,然后删除横线,以实现上述定义。此程序的正确输出结果应为: My name is Zhang. my name is Wang and my G.P.A.is 3.88 My name is Li,I have 8 publications.. 注意:只能在画线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动“//************found************”。 //源程序 #include <iostream> using namespace std; class Person public: //************found************ ______name=NULL; Person(char*s) name=new char[strlen(s)+1];strcpy(name,s); ~Person() if(name!=NULL) delete[]name; //************found************ ______Disp() cout<<"My name is"<<name<<"./n"; //声明虚函数 void setName(char*s) name=new char[strlen(s)+1];strcpy(name,s); protected: char*name: ; class Student: public Person public: //************found************ Student(char*s,double g)______ void Disp() cout<<"my name is"<<name<<"and my G.P.A.is il<<gpa<<"./n"; private: float gpa; ; class Professor: public Person public: void setPubls(int n)publs=n; void Disp() cout<<"My name is"<<name<<",I have"<<publs<<"publications./n"; private: int publs; ; int main() //************found************ ______; Person x("Zhang"); p=&x;p->Disp(); Student y("Wang",3.88); p=&y;p->Disp(); Professor z: z.setName("Li"); z.setPubls(8); p=&z;p->Disp(); return 0:
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为: sizeof(str1)=5 sizeof(str2)=10 sizeoffstr3)=1 注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。 (1)不能删除assert()语句; (2)只能修改后面的数字。 提示:assert()函数如果为假,则会产生一个中断异常。#include<iostream.h>#include<assert.h>Void main(){ char*str1="abc"; //********error******** assert(sizeof(str1)==3); cout<<"sizeof(str1) = 5"<<endl; char str2[10]="ab"; //********error******** assert(sizeof(str2)==2); cout<<"siZeof(str2)=10"<<endl; char str3=2 3; **********error******** assert(sizeof(str3)==4); cout<<"sizeof(str3)=1"<<endl; return; }
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。(1)定义类CPlanet的保护数据成员distance和revolvePeriod,它们分别表示行星距太阳的距离和行星的公转周期。其中,distance为double型,revolvePeriod为int型。请在注释//********1********之后添加适当的语句。(2)定义类CEarth的构造函数CEarth(double d,int r),并在其中计算地球绕太阳公转的轨道周长。假定:circumference=2*d*3.1416。请在注释//********2********之后添加适当的语句。(3)定义类Earth的成员display(),用于显示所有信息。包括地球距太阳的距离,地球的公转周期,以及地球绕太阳公转的轨道周长。请在注释//********3********之后添加适当的语句。注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream>using namespace std;class CPlanet{protected://********1********public:CPlanet(double d,int r){diStance=d;revolvePeriod=r;}};class CEarth:public CPlanet{double circumference;public://********2********CEarth(double d,int r){circumference=2*d*3.1416;}//********3********{cout<<"diStance="<<distance<<endl;cout<<"revolutionPeriod="<<revolvePeriod<<endl;cout<<"revolu.tioncircumference="<<circumference<<endl;}};void main(){CEarth earth(9300000,365);earth.display();}
问答题请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2,函数void Insert(node*q)使程序能够完成如下功能:从键盘输入一行字符,调用该函数建立反序的无头结点的单链表,然后输出整个链表。
注意:请勿修改主函数main和其他函数中的任何内容,只需在画线处编写适当代码,也不能删除或移动//************found************。
//源程序proj2.cpp
#include<iostream>
using namespace std;
struct node{
char data;
node*link:
}*head; //链表首指针
void Insert(node*q){ //将节点插入链表首部
//************found************
______;
head=q;
}
int main(){
char ch;
node *p;
head=NULL:
cout<<"Please input the string"<<endl;
while((ch=cin.get())!='/n'){
//************found************
______;//用new为节点p动态分配存储空间
p->data=ch;
//************found************
______; //在链表首部插入该节点
}
p=head;
while(p!=NULL){
cout<<p->data;
p=p->link;
}
cout<<endl;
return 0:
}
