文档库 最新最全的文档下载
当前位置:文档库 › 大学CC++程序设计案例教程上机指导与习题解答

大学CC++程序设计案例教程上机指导与习题解答

实验2答案
1程序阅读
(1)The sum of 50 and 25 is :75
(2) *
***
*****
(3)x=8 y=7 a=14
2程序改错:请改正以下程序的错误。
(1)#include
#define PRICE 30
void main()
{int x=5;
x=PRICE*x;
cout<}
(2)#include
void main()
{double F,c;
cin>>F;
c=5.0/9*(F-32.);
cout<<"F="<}
(3)#include
#include
#define pi 3.14159
void main()
{long d;double x;
cin>>d;
x=1.0/2*sin(d*pi/180.0);
cout<}
3程序填空
(1)①"二十一世纪来到了!"
②"\a"
③"\a"
④"\a"
(2)①,t
②cin>>a>>b>>c;
③t=a
④a=b
⑤b=c
⑥c=t
(3)①a += b;
②b = a-b; ③a -= b;
4编写程序[C级]
#include
void main()
{
int a,b,x,y;
cout<<"输入任意两个整数:"<cin>>a>>b;
x=a+b;
y=a*b;
cout<<"两数和为:"<cout<<"两数积为:"<}
实验3答案
程序阅读
(1)运行结果:
a=6
b=4
c=2
(2)运行结果:
请输入r:1
l=6.28
s=3.14
(3)运行结果:
请输入n的值:123
y=321
程序改错
(1)cin>>a,b;应为cin>>a>>b;
运行结果:
请输入a,b的值:4 9
a=4
b=9
c=20
3、程序填空
(1)① average=(n1+n2+n3)/3.0;
运行结果:
请输入n1,n2,n3的值:1 3 9
average=4.33333
(2)① ch=(ch>='A'&&ch<='Z')?(ch+32):ch;
② cout<运行结果:
H
h
(3)① s=(a+b+c)/2; ② Area=sqrt(s*(s-a)*(s-b)*(s-c));
运行结果:
Area=7.51463
4、编写程序
#include
#include
#include
void main()
{
float a,b,c,x1,x2;
cout<<"输入a b c:";
cin>>a>>b>>c;
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
cout<<"x1="<cout<<"x2="<}
运行结果:
输入a b c:2.5 9.4 4.3
x1=-0.533003
x2=-3.227
实验4答案
1、程序阅读
(1)运行结果:
a=6
b=2
c=8
(2)运行结果:
2(回车)
i
3(回车)
3
6(回车)
OK!
(3)运行结果:
i=1,j=1,k=3
2、程序改错
(1)不能。在“case 1:”语句前加break;语句
3、程序填空
(1)① if(x>=0)
运行结果:
输入数据:0
x=0
输入数据:2 x=1.41421
输入数据:-9
输入数据错误!
(2)① k==0
② k!=0
运行结果:
输入测试数据:5
测试数据是奇数:
输入测试数据:8
测试数据是偶数:
(3)if(a>c)
{t=a;a=c;c=t;}
if(a>d)
{t=a;a=d;d=t;}
if(b>c)
{t=b;b=c;c=t;}
if(b>d)
{t=b;b=d;d=t;}
if(c>d)
{t=c;c=d;d=t;}
运行结果:
输入4个数:4 3 2 1
a=4,b=3,c=2,d=1

从小到大的顺序是:
1,2,3,4
4、编写程序
#include
void main()
{
int x,y;
cout<<"输入x:";
cin>>x;
if(x<1)
{
y=x;
cout<<"x="<}
else if(x<10) //1<=x<10
{
y=2*x-1;
cout<<"x="<}
else //x>=10
{ y=3*x-11;
cout<<"x="<}
cout<}
运行结果:
输入x:4
x=4,y=2*x-1=7
输入x:-1
x=-1,y=x=-1
输入x:20
x=20,y=3*x-11=49
实验5答案
1、程序阅读
(1)运行结果:
a=10
(2) 3
(3)运行结果:
a=32
2、程序改错
(1)第二个cout<<"#";改为cout<<"*";
3、程序填空
(1)① i=100
② i>0
③ i%13==0
运行结果:
i=91
(2)① t=t*n;
② s=s+t;
运行结果:
1!+2!+...+20!=2.56133e+018
(3)
① r=m;
② m=n;
③ n=r;
④ r=m%n;
⑤ m=n;
⑥ n=r;
运行结果:
请输入两个正整数:9 45
最大公约数是9
4、编写程序
#include
#include void main()
{
int m,i,k;
cout<<"输入一个数:";
cin>>m;
k=sqrt(m);
for(i=2;i<=k;i++)
if(m%i==0)
break;
if(i>k)
cout<else
cout<}
运行结果:
输入一个数:4
4不是素数:
输入一个数:7
7是素数:
实验6答案
1、程序阅读
运行结果:
x=8
(2)
运行结果:
a=4
b=10
(3)
运行结果:
pi=3.14159
2、程序改错
(1)正确语句:
for(i=1;i<=100;i++)
if(i%2)
运行结果:
100以内的偶数和:2550
3、程序填空
(1)① i%5==0
② continue
运行结果:
1
2
3
4 6
7
8
9
11
12
13
14
16
17
18
19
(2)运行结果:
123 132 213 231 312
321
the total number is:6
① i=1;i<4;i++
② j=1;j<4;j++
③ k=1;k<4;k++
④ i!=k&&i!=j&&j!=k
⑤ n++
(3)
① j<=n-i
② j<2*i
③ j<=i
④ j<2*(n-i)
运行结果:
Enter n:4
*
***
*****
*******
*****
***
*
4、编写程序
#include
void main()
{
double s,t;
int n=0;
s=0.0;
do
{ t=s;
n=n+1;
s=1.0/n+s;
}while(8>s);
if(s-8<8-t)
{
cout<<"n="<cout<<"s="<}
else
{
cout<<"n="<cout<<"s="<}
}
运行结果:
n=1673
s=7.99989
实验7答案
1程序阅读
(1)20...f1()
30...f2()
10...main()
(2)a+b+j=13
a+b+j=14
a+b+j=15
a+b+j=16

(3)210
2程序改错:请改正以下程序的错误。
(1)#include
#include
int Prime(int m);
void main()
{
int m;
for (m=2;m<=99;m=m+1)
{
if (Prime(m))
cout << m <<" "; //输出素数
}}
int Prime(int m)
{
int i,k;
k = (int)sqrt(m); // 计算m的平方根 for(i=2;i<=k;i++)
if (m%i==0) return 0;
return 1;
}
3程序填空
(1)①x--
②int c
③return c
(2)①t=t*j
②return t
(3)①b%i==0
②i++
③b>=i
4编写程序[C级]
#include
void f(int k);
void main()
{int x;
cout<<"请输入一个整数,若小于3则重输:";
do cin>>x;while(x<3);
cout<f(x);
}
void f(int k)
{int i=2;
do{
while(k%i==0){
cout<k/=i;
}
i++;
}while(iif(k!=1)cout<}
实验8答案
程序阅读答案
(1) 0 1 1 2 3 5 8 13 21 34
(2)1
1
2
4
8
16 32
64
128
256
(3)90
(4)12 2 32 42 0↙
最小数的位置是:4
最大数的位置是:3
12 2 32 0 42
2、程序改错答案
正确的程序为:
#include
void main()
{
int i, a[10];
for (i=0; i<10; i++)
a[i]=i;
for (i=9; i>0; i--)
cout << a[i] << " ";
}
程序填空答案
(1)cin >> a[i] i%8==0 cout << endl
(2)SIZE-1 a[i]=a[j] a[j]=t j--
(3)a[i] i++ b[j] j++
(4)i>0 i j-1 -- -- i%5==0
4、编写程序答案
程序清单:
#include
#include
#define N 10
void main()
{
int a[N], b[N], i, j, k=0, n=0, s=0, t;
for (i=0; icin >> a[i];
for (i=0; iif (a[i]>=0)
{
n++;
s+=a[i];
b[k++]=a[i];
}
cout << setw(4) << n << setw(4) << s << endl;
for (i=0; iif (b[j]>b[j+1])
{t=b[j]; b[j]=b[j+1]; b[j+1]=t; }
for (i=0; icout << b[i] << " ";
}
实验9答案
程序阅读答案
(1)19
(2)cmue
(3) 11 22 33 44
1 2 3 4
10 20 30 40
12 22 32 42
(4)58
2、程序改错答案
(1)修改后的程序段
int a[3][8];
for (int i=0; i<=2; i++)
for (int j=0; j<8; j++)
a[i][j]=i+j;
(2)修改后的程序段
char str1[8]= "country", str2[8]= "morning", temp[8];
if (str1>str2)
{ strcpy(temp,str1); strcpy(str1,str2); strcpy(str2,temp); }
cout << str1 << " " << str2;
3、程序填空答案
(1)i=0 str[i]!= '\0' n++
(2)i

; s2[n]='\0';
(3)i==j || i+j==6 a[i][j]=2 i6
4、编写程序答案
程序清单:
#include
#include
#define M 10
#define N 3
void main()
{
int i, j, a[M][N], n[N];
float s, aver[N];
cout << "请输入每名学生成绩:" << endl;
for (i=0; i{
s=0; for (j=0; j{
cin >> a[i][j];
s+=a[i][j];
}
aver[i]=s/N;
}
cout << setw(5) << "语文" << setw(5) << "数学" << setw(5) << "外语" << setw(7) << "平
均分"<< endl;
for (i=0; i{
for (j=0; jcout << setw(5) << a[i][j];
cout << setw(7)<< setprecision(3) << aver[i] <}
for (j=0; j{
n[j]=0;
for (i=0; iif (a[i][j]>85)
n[j]++;
}
cout << setw(16) << "语文优秀率" << setw(16) << "数学优秀率" << setw(16) << "外语优秀率"
<< endl;
for (j=0; jcout << setw(16) << setprecision(3) <<100.0*n[j]/M << "%";
}
实验10答案
1、程序阅读
(1)
2
4
(2)
abcd
abc
ab
a
Press any key to continue
(3)请写出下面程序的执行结果。
程序输出结果:
100 200 300
400 500 600
700 800 900
数组转换后: 100 400 700
200 500 800
300 600 900
(4)
2 1 2 1
(5)请写出下面程序的执行结果。
程序输出结果:
p[2]=5
p[-2]=1
执行p=a;后
(*p)++ 1
*p 2
*p++ 2
*p 2
数组 a
a[0]= 2
a[1]= 4
a[2]= 3
a[3]= 4
a[4]= 5
a[5]= 6
2、程序改错
(1)
for(;pcout<<*p; 错误
在语句前加入 p=a;
(2)
float s; 错误,应改正为:float s(0);
for(int i=0;i{
s*=(float)a[i];
}
(3)
if(substr[k+1]!='\0') 语句中条件错误,应改正为:
if(substr[k+1]=='\0')
实验11答案
1、程序阅读
(1)
请输入两个整数:23 32
a 和 b中较大数为:32
a + b = 55
a - b = -9
(2)
1 2 3 4 5 6 7 8 9 10 11 40 41 42 43 44 45 46 47 48 49 12
39 72 73 74 75 76 77 78 79 50 13
38 71 96 97 98 99 100 101 80 51 14
37 70 95 112 113 114 115 102 81 52 15
36 69 94 111 120 121 116 103 82 53 16
35 68 93 110 119 118 117 104 83 54 17
34 67 92 109 108 107 106 105 84 55 18
33 66 91 90 89 88 87 86 85 56 19
32 65 64 63 62 61 60 59 58 57 20
31 30 29 28 27 26 25 24 23 22 21
(3)
输入待查字符f
fghijklmnopqrstuvwxyz
程序输出结果:
输入待查字符F
本字符串中无此字符
2、程序改错
(1)void swap_a(int *xp,int *yp)
{
int *temp;
temp=*xp;
*xp=*yp;
*yp=temp;


}
(2)return temp;

4、编写程序
#include
#include
int main()
{

int num, a, b, c, count = 0;
while (num != 0)
{
cout << "请选择题型(1代表加法,2代表减法,3代表乘法,0代表结束):";
cin >> num;
cout << endl << endl;
srand( time(NULL) );
a = rand()%100;
b = rand()%100;
if (num == 1)
{
cout << "请计算" << a << "和" << b << "的和,结果为:";
cin >> c;
if (c == a + b)
{
cout << "正确!\n\n";
count++;
}
else
cout << "错误!正确答案是:" << a + b << endl << endl;
}
if (num == 2)
{
cout << "请计算" << a << "和" << b << "的差,结果为:";
cin >> c;
if (c == a - b)
{
cout << "正确!\n\n";
count++;
}
else
cout << "错误!正确答案是:" << a - b << endl << endl;
}

if (num == 3)
{
cout << "请计算" << a << "和" << b << "的积,结果为:";
cin >> c;
if (c == a * b)
{
cout << "正确!\n\n";
count++;
}
else
cout << "错误!正确答案是:" << a * b << endl << endl;
}
if (num != 0 && num != 1 && num != 2 && num != 3)
cout << "请按要求输入!\n\n";
}
cout << endl << endl << endl;
cout << "你总共做对题的个数为:" << count << endl;
return 0;
}
实验12答案
1、程序阅读 (1)
6
7
(2)
请输入两个加数:23 56
23+56=79
请输入两个加数:43256 78675
43256+78675=121931
(3)
请输入待计算数列前几项:6
数列为:0 1 1 2 3 5
2、程序改错
(1)清指出错误语句和出错原因
编译无问题,执行出错
*p++; 错误,改变指向分配空间的指针变量的指向。
(2)清指出错误语句和出错原因
delete data ; 错误 正确应为:
delete []data ;
3、程序填空
(1)
= new float[n];
s+= fdata[i]
delete data ;
4、编写程序
#include
#include
char *creatline(char *c)
{
char *p;
int l;
l=strlen(c);
p = new char[l+1];
strcpy(p,c);
return p;
}
int main()
{
char **ch_p;
int n;
cout<<"请输入待输入文本的行数:";
cin>>n; //输入文本行数
ch_p = new char *[n]; //建立指向字符串的指针数组
char ctemp[255];
cin.ignore(255,'\n'); //跳过输入缓冲区内的无用的字符。 for(int i=0;i<=n;i++)
{
cout<<"输入第"<cin.getline(ctemp, sizeof ctemp); //输入一行文本
ch_p[i] = creatline(ctemp);
}
for

(i=0;i{
cout<<"第"<}
for(i=0;i{
delete []ch_p[i];
}
delete []ch_p;
return 0;
}

实验13答案
1、程序阅读
(1)
0 2 4 6 8 10 12 14 16 18
0 1 2 3 4 5 6 7 8 9

(2)
交换前的数组:1 3 5 7 9 2 4 6 8 10
交换后的数组:10 8 6 4 2 9 7 5 3 1
(3)
请输入一个整数:24
24 = 2*2*2*3
请输入一个整数:840
840 = 2*2*2*3*5*7

2、程序改错
(1)
函数add(long a,long b)改正为:
long add(long a,long b)
{
long t;
t=a+b;
return t;
}

(2)下面函数fun()的功能是:将长整型数中偶数位置上的数依次取出,构成一个新数返回, 函数fun()中多条语句错误,改正为:
long fun(long s)
{
long t , sl=1;
int d ;
t = 0 ;
while( s > 0 )
{
d = s%10;
if (d%2==0)
{
t=d* sl+ t;
sl *= 10;
}
s/= 10;
}
return (t);
}
修正后程序运行结果:
请输入一个正整数:3542678
重新组合的整数为:4268
(3)下面函数fun的功能是:依次取出字符串中所有数字字符,形成新的字符串,并取代
原字符串;判断下面程序的正误,如果错误请改正过来。
函数fun()中多条语句错误,改正为:
void fun_c(char *s)
{
int i,j;
for(i=0,j=0;s[i]!='\0';i++)
if(s[i]>='0'&&s[i]<='9')
s[j++]=s[i];
s[j]='\0';
}
修正后程序运行结果:
请输入一个包含数字的字符串:a1b2cd3efg4hijk5
重新组合的新字符串为:12345
3、程序填空
(1)avg函数的作用是计算数组array的平均值返回,请在横线上填上适当的语句使程序完
整。
#include
float avg(float *array,int n)
{
float average, sum=0 ;
for(int i=0; isum= sum+ *(array+i); average = sum / n ;
return average;
}
void main()
{
float a[10];
int n;
cout<<"请输入求平均值数的数量:";
cin>>n;
cout<<"请输入一组实数,数量为"<for(int i=0;icin>>a[i];
float t;
t=avg(a,n);
cout<<"这组数的平均值为:"<}
(2)以下程序是将字符串b的内容连接字符数组a的内容后面,形成新字符串a,请填空
使程序完整。
#include
void fun_strcat(char *s,char *t)
{
int i=0,j=0 ;
while(*(s+i)!='\0')
i++;
while(*(t+j)!='\0')
s[i++]=t[j++];
s[i]='\0';
}
void main( )
{
char a[40]="Hello ", b[ ]="World";
fun_strcat(a,b);
cout<}
程序执行结果为:
Hello World
(3)下面程序用“顺序查找法”查找数组a中是否存在某一关键字,请在横线上填上语句使
程序完整。
#include

#define N 10
int fun_find(int *s,int t)
{
int i=0;
while((*(s+i)!=t)&&ireturn i;
}
void main( )
{
int a[N]={66,77,53,89,91,82,75,63,97,34};
int x,k;
cout<<"请输入待查数据:";
cin>>x;
k=fun_find(a,x);
if(k>=N)
cout<<"没有在数组中找到待查数据!";
else
cout<<"待查数据"<}
程序执行结果为:
请输入待查数据:63
待查数据63在数组中第8位
请输入待查数据:23
没有在数组中找到待查数据!

4、编写程序
问题描述:
编写程序实现:使用“两路合并法”把两个已按升序(由小到大)排列的数组合并成一个新的
数组,并在主函数中输出这个新数组。
#include
int fun_cat(int *c,int *s,int m,int *t,int n)
{
int i(0),j(0),k(0);
while(iif(s[i]{
c[k]=s[i]; k++; i++;
}
else
{
c[k]=t[j]; k++; j++;
}
while(j{
c[k]=t[j]; k++; j++;
}
while (i{ c[k]=s[j]; k++; i++;
}
return k;
}
void main( )
{
int a[3]={5,9,10} ;
int b[5]={6,24,26,37,48} ;
int c[10];
int k;
k=fun_cat(c,a,3,b,5);
for(int i=0; icout<cout<}
程序执行结果:
5 6 9 10 24 26 37 48

实验14答案
1、程序阅读
(1)[B级]
程序输出结果:
暂停20000
暂停10000
(2)[B级]
程序输出结果:
1 -2.7
(3)[c级]
程序输出结果:
|-21|= 21 8
2、程序改错
(1)[B级]
cout<<"sub(b,a,a+b)= "<< sub(b,a,a+b)<cout<<"add()= "<(2)[B级]
D.int fun4(int a=400, int b,int c,int d=100);
函数缺省参数定义在中间,定义错误。
3、程序填空
(1)
#include
int sub(int x,int y) //函数定义,形参缺省值为表达式
{
return x-y;
} double sub(double x,double y) //函数定义,形参缺省值为表达式
{
return x-y;
}
void main()
{
double a(1.0),b(10.1);
int i(1),j(10);
cout<<"sub("<cout<<"sub("<}
4、编写程序
(1)
#include
int max(int *p,int n)
{
int max,i;
for(max=p[0],i=1;iif(maxmax=p[i];
return max;
}
long max(long *p,int n)
{
long max,i;
for(max=p[0],i=1;iif(maxmax=p[i];
return max;
}
char max(char *p,int n)
{
char max;
int i;
for(max=p[0],i=1;iif(maxmax=p[i];
return max;
}
double max(double *p,int n)
{
double max;
int i;
for(max=p[0],i=1;

imax=p[i];
return max;
}
void main()
{
double a[100];
int n,i(0);
cout<<"请输入待输入数据个数:";
cin>>n;
for(i=0;icin>>a[i];
cout<<"输入数据最大值为:"<}
实验15答案
1、程序阅读答案
(1)11
(2)1 9
(3)9 9 11
2、程序改错答案
将宏定义改为:
#define mtex1(x) (x)*(x)+3*(x)-1
3、程序填空答案
(1)#define PRN3(x,y,z) cout << "x,y,z has value " \
<< setprecision(1) << x << "," \
<< setprecision(1) << y << "," \
<< setprecision(1) << z
(2)x>y count++ number(x, n, k)
(3)max=f(1) max4、编写程序答案
(1)用函数实现。
/* 用函数方法,从三个数中找出最大数 */
# include
int max (int x, int y)
{
int z;
z=x>y? x: y;
return z;
}
void main()
{
int a, b, c;
cout << "请输入三个整数:" ;
cin >> a >> b >> c; cout << "三个数中最大值为:" << max(max(a,b),c);
}
(2)用带参的宏完成。
/* 用宏定义方法,从三个数中找出最大数 */
#include
#define MAX(a,b) ((a) > (b) ? (a) : (b))
void main()
{
int a, b, c;
cout << "请输入三个整数:" ;
cin >> a >> b >> c;
cout << "三个数中最大值为:" << MAX(MAX(a,b),c);
}

实验16答案:
1、阅读程序
(1)
80.5
请按任意键继续. . .
(2)
99,pple请按任意键继续. . .
(3)
A 80.7
请按任意键继续. . .
2、修改程序
#include
using namespace std;
struct date
{
int month;
int day;
int year;
};
struct student
{
int num;
char name[20];
char sex;
date birthday;
float score;
};
student sty={10001,"Li Lin",'M',2009,10,1,98.5};
void main()
{ cout << "学生的情况:"<< sty.num<cout<<"出生日期:
"<}
3、程序填空
(1) ① name[10], ② student *, ③ (t+i), ③ (t+i), ③ (t+i)
④ void
(2) ① boy, ② student, ③ *p, ④ s+=p->scor, ⑤ ave
(3) ① struct, ② person, ③ leader_name, ④ leader[j].count
4、编写程序
#include
using namespace std;
struct date
{
int year;
int month;
int day;
};
struct person
{
char *name;
int age;
date workday;
float money;
};
void add(struct person *p)
{
for(int i=0;i<10;i++)
{
int x=2009-(*(p+i)).workday.year;
if(x>=10)
(p+i)->money=(p+i)->money+20;
else
(p+i)->money=(p+i)->money+10;
}
}
void print(struct person *p)
{
for(int i=0;i<10;i++)
{
cout<<(

p+i)->money<<"--"<}
}
void main()
{
struct person a[10]={
{"zhang",20,2005,10,1,500},
{"wang",21,1990,10,1,500},
{"li",40,2003,10,1,500},
{"liu",25,1990,10,1,500},
{"wu",30,1980,10,1,500}};
add(a);
print(a);

}
实验17答案:
1、阅读程序
(1)
显示对象a的数据成员:
.5--3.5
显示对象a的数据成员:
.5--5.5
请按任意键继续. . .
(2)
进入main()函数
第0个test对象的构造函数被调用
第1个test对象的构造函数被调用
第2个test对象的构造函数被调用
第3个test对象的构造函数被调用
退出main()函数
第3个test对象的析构函数被调用
第2个test对象的析构函数被调用
第1个test对象的析构函数被调用
第0个test对象的析构函数被调用
请按任意键继续. . .
(3)
abcdefg7
请按任意键继续. . .
2、修改程序
#include
using namespace std;
class point
{
private:
float x,y;
public:
void init(float,float); void show();
};
void point::init(float a, float b)
{
x=a;
y=b;
}
void point::show()
{
cout<}
void main()
{
point p;
p.init(10.5,50.5);
p.show();
}

3、程序填空
(1) ① strcpy(name,n), ② delete []name, ③ stu.getout(),
(2) ① test&, ② test::test(int n, double f), ③ t.num, ④ t.f1,
⑤ int test::
(3) ① arr[5], ② void inc::, ③ n, ④ inc inc1(x)
4、编写程序
//bc连续的合数
#include
using namespace std;
class num
{
private:
int n;
int *p;
public:
num(int n1)
{
n=n1;
p=new int[10];
}
int yes(int x)
{
for(int i=2;i<=x/2;i++)
{
if(x%i==0)
return 1; }
return 0;
}
void fun(void)
{
int j;
for(int i=3;1;i++)
{
j=0;
while(yes(i)&&j{
p[j]=i;
j++;
i++;

}
if(j==n)
break;
cout<}
}
void print()
{int i;
for(i=0;icout<cout<}
~num()
{
if(p)
delete []p;
}
};
void main()
{
num num1(10);
num1.fun();
num1.print();
}
实验18答案:
阅读程序
(1)
the first:
x=11y=22 the second:
x=21y=22
请按任意键继续. . .
(2)
name=wang num=00001 total=3
name=yang num=00002 total=3
name=liu num=00003 total=3
distruction!
total=2
distruction!
total=1
distruction!
total=0
请按任意键继续. . .
(3)
90
请按任意键继续. . .
2、修改程序[A级]
#include
using namespace std;
class sample
{
int n;
static int m;
public:
sample(int i)
{
n=i;
m+=i

;
}
void disp()
{
cout<<"n="<}
};
int sample::m=0;
void main()
{
sample s1(1),s2(3),s3(5);
s1.disp();
s2.disp();
s3.disp();
}
3、程序填空 (1) ① total, ② double score::total=0, ③ score::get()
(2) ① static float all, ② all+=salary, ③ em::getout()
(3) ② aa tmp, ② aa tmp, ① friend class bb
4、编写程序
#include
using namespace std;
class Y
{
friend class YB;
private:
int t;
public:
Y(int t1=0)
{
t=t1;
}
int gett()
{
return t;
}
};
class YB
{
public:
YB()
{
}
void change(Y &yc);
};
void YB::change(Y &yc)
{
yc.t++;
}
void main()
{
Y yc;
YB yc1;
yc1.change(yc);
cout<}
实验19答案
1程序阅读
(1,2)
5,6 (3,4)
2程序改错
class Square : private Rectangle
应改为
class Square : public Rectangle
3程序填空
b.AA::show()
实验20答案
1程序阅读[A级]
B::show() called. 158
D::show() called. 208
2程序改错[A级]
AA a=b,*p=&b;
应改为:
AA &a=b,*p=&b;
3程序填空[B级]
virtual void speak()=0
实验21答案
1程序阅读[A级]
(4,8,6)
2程序改错[B级]
vector operator +(vector v)
应改为
vector vector::operator +(vector v)
3程序填空[B级]
friend Force
实验22答案
1程序阅读[A级]
dec:30 75
oct:36 113
hex:1e 4b
2程序改错[A级]
cout << setiosflags(ios::scientific);
应改为
cout << setiosflags(ios::fixed);
3程序填空[B级]
[1]out.open("abc.txt")
[2] out.close()

相关文档
相关文档 最新文档