一、单项选择题
4、强制类型转换格式如下:
(type_name) expression
不能写成float(1/2)!而应该是(float)(1/2)
5、我就觉得题目答案错了,果真不对。。。应该说选项中就没有正确答案
17、
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int f(int m){ 5 static int i=0; 6 int s=0; 7 for(;i<=m;i++) 8 s+=i; 9 return s; 10 } 11 12 int main() 13 { 14 int sum=f(5)+f(3); 15 printf("%d",sum); 16 return 0; 17 }
运行结果:
为什么?
三、填空题
2、关于求最大公约数的填空题
算法:辗转相减
算法很简单,对于m和n 不妨假设m>n 若是m<n则交换这两者的值(m=n+m;n=m-n;m=m-n;可以实现交换,你可以试试,比如m=2,n=3; m=2+3为5 ->n=5-3=2->m=5-2=3)
总是把大的减去小的,得到的一个数m-n=>r 把这个数直接给m就行,至于m与n的大小,有处理机制,直到m-n为0结束。
给的参考答案和我写的不一样,机试了一下,我的是对的。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 int m,n; 7 scanf("%d%d",&m,&n); 8 while(m-n!=0){ 9 if(m>n) m=m-n; 10 else{ 11 m=m+n;n=m-n;m=m-n; 12 } 13 } 14 printf("gcd(m,n)=%d",n); 15 return 0; 16 }
运行结果
5、backmove()是字符指针x所指长度为n的字符串后移m个位置,移出的字符移到串首,m、n为非负整数。
如"abcdefghij"后移3个位置成"hijabcdefg"。
填空的算法其实很简单,移动3个位置,就一步一步的走!而不是所有的一次性跳3步
第一次:
j=0 jabcdefghi
第二次在第一次的基础上,再移动一步
j=1 ijabcdefgh
第三次在第二次的基础上,继续移动一步
j=2 hijabcdefg
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<string.h> 4 #define M 20 5 6 void backmove(char *x,int n,int m){ 7 int i,j; 8 char w; 9 for(j=0;j<m;j++){ 10 w=*(x+n-1); 11 for(i=0;i<n-1;i++) 12 *(x+n-1-i)=*(x+n-2-i); 13 *x=w; 14 } 15 } 16 int main() 17 { 18 char *s; 19 int m; 20 s=(char *)malloc(sizeof(char)*M); 21 printf("m:"); 22 scanf("%d",&m); 23 getchar();//这个一定要加! 24 gets(s); 25 backmove(s,strlen(s),m); 26 puts(s); 27 return 0; 28 }
运行结果:
五、程序阅读题
1、设文件A.DAT为文本文件,其内容为1234567890。写出以下程序的运行结果。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 char ifname[]="A.DAT"; 7 char ofname[]="B.DAT"; 8 FILE *fpi,*fpo; 9 int i=0; 10 char ch; 11 fpi=fopen(ifname,"r"); 12 fpo=fopen(ofname,"w"); 13 if(fpi==NULL||fpo==NULL){ 14 printf("Error open file"); 15 exit(1); 16 } 17 while(1){ 18 19 ch=fgetc(fpi); 20 if(feof(fpi)) 21 break; 22 if(i==0){ 23 fputc(ch,fpo); 24 i=1; 25 }else 26 i=0; 27 } 28 fclose(fpi); 29 fclose(fpo); 30 return 0; 31 }
运行结果:
六、改错题
#include <stdio.h> #include <stdlib.h> #include<math.h> void fun(int a,int *b,int *c){ int i,j,d,y; for(i=2;i<=a/2;i=i+3){//从2开始,因为2也是素数,而2的倍数一定不是素数,即,素数一定是奇数,但是奇数不一定是素数,所以还要判断 y=1; for(j=2;j<=sqrt((double)i);j++) if(i%j==0) y=0; if(y==1){ d=a-i; for(j=2;j<=sqrt((double)d);j++) if(d%j==0) y=0; if(y==1){ *b=i;*c=d; } } } } int main() { int a,b,c; do{ printf("\nInput a:"); scanf("%d",&a); }while(a%2); fun(a,&b,&c); printf("\n\n%d=%d+%d\n",a,b,c); return 0; }
运行结果:
七、编程题
有10种商品,每种商品的数据包括:品名、单价和出厂日期,从键盘输 有10种商品,每种商品的数据包括:品名、单价和出厂日期,从键盘输入10种商品的数据,统计价格在30元以上的商品数目,并输出它们的全
部信息。
要求:
①在主函数中输入、统计和输出;
②用指向结构体变量的指针进行处理。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define M 20 4 #define N 3 5 struct good{ 6 char name[M]; 7 float price; 8 char date[M]; 9 }goods[N]; 10 11 int main() 12 { 13 int i; 14 int count=0; 15 struct good *pro; 16 printf("Please enter %d product information...\n",N); 17 for(i=0;i<N;i++){ 18 printf("No.%d goods info:\n",i+1); 19 printf("name:"); 20 gets(goods[i].name); 21 printf("price:"); 22 scanf("%f",&goods[i].price); 23 printf("date:"); 24 getchar(); 25 gets(goods[i].date);//凡是遇到gets函数之前有输入的,一律在前面加上getchar()来吸收前面输入的回车符号。 26 } 27 28 printf("====List products of price more than 30 RMB===\n"); 29 for(pro=goods;pro<goods+N;pro++){ 30 if(pro->price>30){ 31 count++; 32 printf("name:"); 33 puts(pro->name); 34 printf("price:"); 35 printf("%0.2f\n",pro->price); 36 printf("date:"); 37 puts(pro->date); 38 printf("\n\n"); 39 } 40 } 41 printf("numbers(more than 30RMB):%d\n",count); 42 return 0; 43 }
2.将MxN的矩阵转置。
要求:
①自定义一个函数完成矩阵的转置:
②在主函数中输入原矩阵,输出转置后的矩阵:
③用指向二维数组的指针进行处理。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define M 2 4 #define N 4 5 void f(int (*p)[N],int (*b)[M]){ 6 int i,j,t; 7 for(i=0;i<M;i++){ 8 for(j=0;j<N;j++) 9 *(*(b+j)+i)=*(*(p+i)+j); 10 } 11 } 12 13 int main() 14 { 15 int a[M][N]; 16 int b[N][M]; 17 int i,j; 18 printf("请输入一个%dX%d的矩阵:\n",M,N); 19 for(i=0;i<M;i++) 20 for(j=0;j<N;j++) 21 scanf("%d",&a[i][j]); 22 f(a,b); 23 printf("\n转置后的矩阵为:\n"); 24 for(i=0;i<N;i++){ 25 for(j=0;j<M;j++) 26 printf("%3d",b[i][j]); 27 printf("\n"); 28 } 29 return 0; 30 }
运行结果:
3.将两个字符串s和t的前n个字符拼接成新的字符串,结果存放s中。如果s或t中字符串的长度不足n,按实际长度处理。
要求: 要
①自定义一个函数完成字符串的拼接;
②在主函数中完成输入和输出;
③不能使用string.h中的任何字符串操作函数。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 20 4 5 void stringcat(char *s,char *t,int n){ 6 char *p=s,*q=t; 7 while(*p&&((p-s)<n)) 8 p++; 9 10 while(*q&&(q-t<n)){ 11 *p=*q; 12 q++; 13 p++; 14 } 15 *p='\0'; 16 17 } 18 19 int main() 20 { 21 char s[N*2],t[N]; 22 int n; 23 printf("Input string s:"); 24 gets(s); 25 printf("Input string t:"); 26 gets(t); 27 printf("n:"); 28 scanf("%d",&n); 29 stringcat(s,t,n); 30 puts(s); 31 return 0; 32 }
运行结果:
4、从键盘输入若干行字符(每行长度不等),输入后把它们进行加密存储到 ,输入后把它们进行加密存储到 一个磁盘文件中,再从该文件中读入加密字符进行解密,在显示屏上输出。
加密规则是:非字母字符不变,字母字符的第i个字母变成第(26-i+1)
要求:
①自定义函数完成字符的加密和解密;
②在主函数中完成输入、存储、读取和输出;
③以字符#作为输入结束。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 5 void encode(char *s){ 6 int flag=-1;//-1代表非字母,0代表小写,1代表大写 7 char ch; 8 9 flag=-1; 10 if(*s>='a'&&*s<='z') 11 flag=0; 12 if(*s>='A'&&*s<='Z') 13 flag=1; 14 15 switch(flag){ 16 case 0:ch='a';break; 17 case 1:ch='A';break; 18 } 19 20 if(flag!=-1) 21 *s=2*ch+25-*s; 22 23 24 } 25 26 void decode(char *s){ 27 encode(s); 28 } 29 int main() 30 { 31 FILE *fp; 32 char ch; 33 34 if((fp=fopen("encode.txt","w"))==NULL){ 35 printf("Can't open the file.\n"); 36 exit(1); 37 } 38 while((ch=getchar())!='#'){ 39 encode(&ch); 40 fputc(ch,fp); 41 } 42 printf("saved in encode.txt...\n"); 43 fclose(fp); 44 printf("\n\nbegin read the file..\n"); 45 if((fp=fopen("encode.txt","r"))==NULL){ 46 printf("Can't open the file.\n"); 47 exit(1); 48 } 49 printf("\n\nthe context in encode.txt is:\n\n"); 50 ch=fgetc(fp); 51 52 while(!feof(fp)){ 53 printf("%c",ch); 54 ch=fgetc(fp); 55 } 56 57 rewind(fp);//返回到文件开头 58 printf("\n\n\ndecode the context above:\n\n"); 59 ch=fgetc(fp); 60 while(!feof(fp)){ 61 decode(&ch); 62 printf("%c",ch); 63 ch=fgetc(fp); 64 } 65 fclose(fp); 66 return 0; 67 }
运行结果: