C语言程序设计A期末模拟试题三(3)
时间:2026-01-16
时间:2026-01-16
C语言程序设计A期末模拟试题三
分)
1. #include<iostream.h>
void main()
{
int a=2,b=5,c=0;
if(a+b>10) c=a*b; else c=3*a+b;
if(c<=20) cout<<c*c; else cout<<4+c-5;
cout<<endl;
a=a+b; b=a+b;c+=a+b;
cout<<"a,b,c="<<a<<','<<b<<','<<c<<endl;
}
2. #include<iostream.h>
#include<string.h>
void main() {
char a[5][10]={"student","worker","soldier","cadre","peasant"};
char s1[10], s2[10];
strcpy(s1,a[0]); strcpy(s2,a[0]);
for(int i=1;i<5;i++) {
if(strcmp(a[i], s1)>0) strcpy(s1,a[i]);
if(strcmp(a[i], s2)<0) strcpy(s2,a[i]);
}
cout<<s1<<’ ’<<s2<<endl;
}
3. #include<iostream.h>
#include<math.h>
void main()
{
int x,y;
cin>>x;
y=int(sqrt(x)); //sqrt(x)为求x的算术平方根
for(int i=1;i<=y;i++)
if(x%i==0) cout<<”x=”<<i<<’*’<<x/i<<endl;
}
4. IntNode* FindMax(IntNode *f)
{
if(!f) return NULL;
IntNode *p=f;
f=f->next;
while(f) {
if(f->data>p->data) p=f;
f=f->next;
}
return p;
}
假定IntNode的类型定义为:
struct IntNode {
int data; //结点值域
IntNode* next; //结点指针域
};
五、编程题(每小题6分,共12分)
1. 计算1+3+32+...+310的值并输出,假定分别用i,p,s作为循环变量、累乘变量和累加变量的标识符。
2. 根据下面类中Reverse1函数成员的原型和注释写出它的类外定义。
class AA {
int* a;
int n;
int MS;
public:
void InitAA(int aa[], int nn, int ms) {
if(nn>ms) {cout<<"Error!"<<endl; exit(1);}
MS=ms;
n=nn;
a=new int[MS];
for(int i=0; i<MS; i++) a[i]=aa[i];
}
AA* Reverse1(); //通过动态存储分配得到一个对象,并动态分配
//a[MS]数组空间,要求该对象中的n和MS的值与*this中的
//对应成员的值相同,数组元素的值是按照*this中数组元
//素的相反次序排列得到的,要求该函数返回动态对象的地址。
};
C语言程序设计A期末模拟试题三
参考答案
单选题
1. D2. A3. D4.B5.A6.D7.C8.B9.A10.A
填空题
1. cpp、 程序 2. 2293. !x4. cin>>a 5. 总和6. *p
7. ple8. this9. 成员10. 继承(派生)
三、程序填充题
1. (1) x<3 (或x<=2) (2) x%i==0 (3) i++
2. (1) void fun(char ss[]) (2) fun(s) (3) i<n/2
3. (1) ARRAY:: (2) new int[n] (3) v[i]=a[i]
4. (1) a=new int[MaxLen] (2) {return a[i];} (3) s+=r.
GetValue(i)
四、理解问答题
1.
121
a,b,c=7,12,30
2.
worker cadre
3. 把从键盘上输入的一个整数x分解为所有可能的每两个因子之积。
4. 实现strcat函数的功能,把str2所指字符串连接到str1
上一篇:校友会主持稿(串词)
下一篇:气动工具使用培训