C程序设计 谭浩强 期末考试题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
一、填空题
1.输入一个不包含空格的字符串,将字符串反序输出,如:“abc12”的输出为“21cba”。
#include <>
void f(char *p)
{
char *p1, *p2;
char c;
p1=p2= ;
while( *p2++)
;
;
while ( )
{
c=*p1;
*p1= ;
*p2= ;
p1++;
p2--;
}
}
void main()
{
char s[200];
printf(“输入一个字符串:”);
scanf( );
f(s);
printf(“字符串反序:%s\n”,s);
}
2.输入1个长整数,求各位数字的平方和。
例如:输入 123,输出14。
#include <>
void main( )
{ int digit;
long in,s;
scanf("%ld", &in);
;
;
while(in>0){
;
s=s+digit*digit;
;
}
printf("sum=%ld\n", s);
}
二、程序阅读题
1.写出下面程序运行结果(5分)
#include <>
#define MSIZE 8
void main()
{
char im[MSIZE][MSIZE+1]={ “********”,
“########”,
“#**#***#”,
“####***#”,
“********”,
“#*******”,
“********”,
“########”
};
int i,j;
for(j= MSIZE -1;j>=0;j--)
{
for(i=0;i printf(“%c”,im[i][j]); printf(“\n”); } } 2.写出下面程序运行的输出结果(5分) #include <> void main() { char *str1="", *str2=”123424315”; int x=0, i; for(i=0;str1[i]!='\0'&& str2[i]!='\0';i++) if(str1[i]==str2[i]) x++; printf("%d\n",x); 3.写出下列程序的输出结果(4分) #include <> main() { int a=4,b=6; printf("a=%d\n",a<<1); printf("b=%d\n",b>>1); } 4. 写出调用函数f(-123)的输出结果是多少。(6分) void f(int n) { if(n<0) { printf(“-”); f(-n); } else if(n<2) printf(“%d”,n); else { f(n/2); printf(“%d”,n%2); } } 三、读程序,找出其中存在的10个错误并修改。 #include <> #define NUM 3 struct problems{ char problem[20]; int answer; } mypros[NUM]={{”2+3=”,5},{”2*3-13=”,-7},{“(2*31*5)%3=”,1}}; int main( void ) { int i=0,a,score; printf(“test begin:\n”) for(i=0,i { printf(“problem %d:\n”,i+1); printf(“%c\n”, problems[i]. problem); scanf(“%d”,a); if(a=problems[i].answer) score++; } score=10×score; printf(“your score: %d\n”,score); return 0; /* indicates successful termination */ } /* end main */ 错误的语句: 修改为: 1、 2、 3、 4、 5、 6、 7、 8、 9、 10、 四、编程。 1、首先按要求生成两个有序链表p和q如下图所示: p …… q …… 然后将这两个链表合并生成一个新的链表head,使得head仍然从大到小有序,并且不允许有重复的元素。最后将新的链表输出。(本题25分)比如,p、q链表中节点data的值若分别为: p: 12,11,10,9,8,7,6,5,4,3 q: 15,13,11,9,7 则生成的head链表中节点data的值为: 15,13,12,11,10,9,8,7,6,5,4,3 #include <> #include <> struct node {int data; struct node *next; }; void main() { int a[]={12,11,10,9,8,7,6,5,4,3}; int b[]={15,13,11,9,7}; struct node *p,*q,*head; /* 以下程序实现生成链表p和q,使得p链表中节点data的值依次为a数组, q链表中节点data的值依次为b数组*/ /*按题意要求合并生成新的链表head*/ /*输出链表head*/ } 2.把一英文文章中的字母全部转换成密文字母输出到另一个文件中,文中其他字符以及换行不变。字母转换规则为: A->E,B->F,…,V->Z,W->A,…Z->D, a->e,b->f,…,v->z,w->a,…z->d,即变成其后面第四个字母。文件名用命令行参数给出。假设源程序名,命令行写法如下: encrypt 其中为原始的英文文章,为完成加密后的新文件。(本题15分)