C程序设计谭浩强期末考试题

  • 格式:doc
  • 大小:63.12 KB
  • 文档页数:15

一、填空题1.输入一个不包含空格的字符串,将字符串反序输出,如:“abc12”的输出为“21cba”。

#include <stdio.h>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 <stdio.h>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 <stdio.h>#define MSIZE 8void main(){char im[MSIZE][MSIZE+1]={ “********”,“########”,“#**#***#”,“####***#”,“********”,“#*******”,“********”,“########”};int i,j;for(j= MSIZE -1;j>=0;j--){for(i=0;i<MSIZE;i++)printf(“%c”,im[i][j]);printf(“\n”);}}2.写出下面程序运行的输出结果(5分)#include <stdio.h>void main(){char *str1="12123434", *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 <stdio.h>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 <stdio.h>#define NUM 3struct 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<num;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,3q: 15,13,11,9,7则生成的head链表中节点data的值为:15,13,12,11,10,9,8,7,6,5,4,3#include <stdio.h>#include <malloc.h>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.c,命令行写法如下:encrypt script.txt cipher.txt其中script.txt为原始的英文文章,cipher.txt为完成加密后的新文件。

(本题15分)二一、填空题1.输入10个点的坐标(设坐标为整数值), 输出距原点最远的点(设唯一)的坐标及该点距原点的距离.#include <math.h>void main(){struct{int x, y;float length;} point[10];int k, sub=0;for(k = 0; k <10; k++){scanf("%d%d", &point[k].x, &point[k].y);point[k].length =}for(k=1; k<10; k++)if( )printf("(%d,%d) %f\n", point[sub].x, point[sub].y, point[sub].length); }2.以下程序统计从终端输入的字符中大写字母的个数,num[0]中统计字母A的个数,num[1]中统计字母B的个数,其它依次类推.用#号结束输入,请填空.#include <stdio.h>#include <ctype.h>main(){ int num[26]={0},i; char c;while((________)!='#')if(isupper(c)) num[c-‘A’]+= ________;for(i=0;i<26;i++)printf("大写字母%c的个数是%d\n", );}3.函数main()的功能是:在带头结点的单链表中查找数据域中值最小的结点.请填空#include <stdio.h>struct node{ int data;struct node *next;};int min(struct node *first)/*指针first为链表头指针*/{ strct node *p; int m;p= ; m= ; p=p->next;for(;p!=NULL;p=________)if(p->data<m) ;return m;}二、程序阅读题1.写出下面程序运行的输出结果(6 分)#include <stdio.h>void main(){int i,j;for(i=3;i>=1;i--){ for(j=1;j<=2;j++)printf("%d",i+j);printf("\n");}}2.写出下面程序的输出结果(3 分)#include <stdio.h>int fun(int a,int b){ if(b==0)return a;elsereturn (fun(--a,--b));}main(){ printf("%d\n",fun(4,2));}3. 写出输出结果是多少。

(5 分)#include <stdio.h>main(){ int j,a[]={1,3,5,7,9,11,13,15},*p=a+5;for(j=3; j ;j--){switch(j){ case 1:case 2: printf("%d\n",(*p)++);break;case 3: printf("%d\n",*(p--));}}}4. 写出输出结果是多少。

(6 分)#include <stdio.h>void fun1(char*p){ char *q;q=p;while(*q!='\0'){ (*q)++;q++;}}main(){char a[]="Program",*p;p=&a[3];fun1(p);printf("%s\n",a);}三、改错题(20分)include <stdio.h>int isprime(){int i;for(i=2,i<=n;i++)if(n%i=0) return 0;return 1;}void main(){int i,j;printf("primes from 1 to 100:\n");for(i=2;j=0;i<=100;i++);if(isprime(i)){printf("%d ,i ");j++;IF(j%5==0) printf("\n");}四、编程(共40 分)。