1. #include<iostream.h>
const int T=6;
void main()
{
int i,j,k=0;
for(i=1;i<=T;i+=2){
for(j=2;j<=T;j+=2) k++;
}
cout<<”k=”<<k<<endl;
}
运行结果:
2. #include<iostream.h>
void main()
{
int x=5;
switch(2*x-3) {
case 4: cout<<x<<’ ’;
case 7: cout<<2*x+1<<’ ’;
case 10: cout<<3*x-1<<’ ’; break;
default: cout<<"default"<<endl;
}
cout<<"switch end."<<endl;
}
运行结果:
3. #include<iomanip.h>
const int M=20;
void main()
{
int c2,c3,c5;
c2=c3=c5=0;
for(int i=1; i<=M; i++) {
if(i%2==0) c2++;
if(i%3==0) c3++;
if(i%5==0) c5++;
}
cout<<c2<<' '<<c3<<' '<<c5<<endl;
}
运行结果:
4. #include<iostream.h>
const int T=6;
void main()
{
int i,j;
for(i=1;i<=T;i+=2)
for(j=2;j<=T;j+=2) {
if(i+j<T) cout<<’+’;
else cout<<’*’;
}
}
运行结果:
5. #include<iostream.h>
class A {
int a,b;
public:
A() {a=b=0;}
A(int aa, int bb) {a=aa; b=bb;}
int Sum() {return a+b;}
int* Mult() {
int *p=new int(a*b);
return p;
}
};
void main() {
A x(2,3), *p;
p=new A(4,5);
cout<<x.Sum()<<’ ’<<*(x.Mult())<<endl;
cout<<p->Sum()<<’ ’<<*(p->Mult())<<endl;
}
运行结果:
6. #include<iostream.h>
void f2(int& x, int& y)
{
int z=x; x=y; y=z;
}
void f3(int* x, int* y)
{
int z=*x; *x=*y; *y=z;
}
void main()
{
int x=10,y=26;
cout<<"x,y="<<x<<", "<<y<<endl;
f2(x,y);
cout<<"x,y="<<x<<", "<<y<<endl;
f3(&x,&y);
cout<<"x,y="<<x<<", "<<y<<endl;
x++; y--;
f2(y,x);
cout<<"x,y="<<x<<", "<<y<<endl;
}
7. #include<iostream.h>
void main() {
int a[10]={76,83,54,62,40,75,90,92,77,84};
int b[4]={60,70,90,101};
int c[4]={0};
for(int i=0;i<10;i++) {
int j=0;
while(a[i]>=b[j]) j++;
c[j]++;
}
for(i=0;i<4;i++) cout<<c[i]<<’ ’;
cout<<endl;
}
8. #include<iostream.h>
void main()
{
char s[3][5]={"1234","abcd","+-*/"};
char *p[3];
for(int I=0;I<3;I++) p[I]=s[I];
for(I=2;I>=0;I--) cout<<p[I]<<' ';
cout<<endl;
}
9. #include<iostream.h>
class CE {
private:
int a,b;
int getmax() {return (a>b? a:b);}
public:
int c;
void SetValue(int x1,int x2, int x3) {
a=x1; b=x2; c=x3;
}
int GetMax();
};
int CE::GetMax() {
int d=getmax();
return (d>c? d:c);
}
void main()
{
int x=5,y=12,z=8;
CE ex, *ep=&ex;
ex.SetValue(x,y,z);
cout<<ex.GetMax()<<endl;
ep->SetValue(x+y,y-z,20);
cout<<ep->GetMax()<<endl;
}
10. #include<iostream.h>
class Franction { //定义分数类
int nume; //定义分子
int deno; //定义分母
public:
//把*this化简为最简分数,具体定义在另外文件中实现
void FranSimp();
//返回两个分数*this和x之和,具体定义在另外文件中实现
Franction FranAdd(const Franction& x);
//置分数的分子和分母分别0和1
void InitFranction() {nume=0; deno=1;}
//置分数的分子和分母分别n和d
void InitFranction(int n, int d) {nume=n; deno=d;}
//输出一个分数
void FranOutput() {cout<<nume<<'/'<<deno<<endl;}
};
void main()
{
Franction a,b,c,d;
a.InitFranction(6,15);
b.InitFranction(3,10);
c.InitFranction();
c=a.FranAdd(b);
d=c.FranAdd(a);
cout<<"a: "; a.FranOutput();
cout<<"b: "; b.FranOutput();
cout<<"c: "; c.FranOutput();
cout<<"d: "; d.FranOutput();
}
11. #include<iostream.h>
class A {
int a,b;
public:
A() {a=b=0;}
A(int aa, int bb) {a=aa; b=bb;}
int Sum() {return a+b;}
int* Mult() {
int *p=new int(a*b);
return p;
}
};
void main() {
int *k;
A x(2,3), *p;
p=new A(4,5);
cout<<x.Sum()<<' '<<*(x.Mult())<<endl;
cout<<p->Sum()<<' '<<*(k=p->Mult())<<endl;
delete k;
}
程序运行结果参考解答
1. k=9
2. 11 14 switch end.
3. 10 6 4
4. ++*+*****
5. 5 6
9 20
6. x,y=10, 26
x,y=26, 10
x,y=10, 26
x,y=25, 11
7. 2 1 5 2
8. +-*/ abcd 1234
9. 12
20
10. a: 6/15
b: 3/10
c: 7/10
d: 11/10
11. 5 6
9 20
来源:网络整理 免责声明:本文仅限学习分享,如产生版权问题,请联系我们及时删除。
相关文章:
电大《园林规划》期末考试复习资料04-27
电大《政府经济学》期末复习题(名词解释)04-27
电大《政府经济学》期末复习题(多选)04-27
浙江电大《宪法学》期末复习指导04-27
电大《现代教育思想》期末复习资料(2)04-27