《C语言实训上机训练习题.docx》由会员分享,可在线阅读,更多相关《C语言实训上机训练习题.docx(23页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、C语言上机练习题1.给定一个整数,判断其二进制形式的每一位的值,当某一位为1时,输出其所在的的位数,右端开始为第0位。# include # include # include int main (void) int num1,num2; int init; printf(input a number:); scanf(%d,&num1); for (init=0;init1; return 0;2.从键盘输入若干行字符串,每输入一行,输出行号和该行字符串到文件dx.txt中,当一行的第一个字符输入“0”时结束。# include # include # include int main (v
2、oid) FILE *fp; char ch; if (fp=fopen(/tmp/dx.txt,wt+)=NULL) printf(Cannot open file strike any key exit!);getchar();exit(1); printf(input a string:n); ch=getchar(); while(ch!=0) fputc(ch,fp);ch=getchar(); fclose (fp); return 0;3.用指针方式实现:从键盘输入10个2位整数给数组a10,计算并输出其中奇数和偶数的个数。# include # include # includ
3、e int main (void) int *p1; int a10; int i,n1=0,n2=0; printf(input 10 numbers:n); for(i=0;i10;i+) scanf(%d,&ai); p1=a; for(i=0;i10;i+) if (*(p1+i)%2=0) n1+; else n2+; printf(number1=%dn,n1); printf(number2=%dn,n2); return 0;6.输入一行字符,字符个数最多为30,写一函数,把输入的字符按ASCII值的顺序有小到大输出。例:输入cdfga 输出acdfg。# include #
4、include # include void order (char a,int m) char t; int n,i; i=m; for (n=i;n1;n-) for (i=0;iai+1) t=ai; ai=ai+1; ai+1=t; am=0;int main (void) char a30; int i; printf(please input char:n); for (i=0;i30;i+) scanf(%c,&ai); if (ai=n) break; order(a,i); printf(order by:n); printf(%sn,a); return 0;4.列出010
5、000之间的所有其各位上的数字之和等于10的数。编写一个判断各位上的数字之和等于10的函数,并在主函数中调用它。# include # include # include int main (void) int i,j; int fun1(int i); i=0;j=0; for(i=0;i=10000;i+) j=fun1(i); if (j=10) printf(result=%dn,i); return 0; int fun1(int i) int k,j,s,n; k=0;j=0;s=0;n=i; s=n%10000;k=n/10000;j=j+k;n=s; s=n%1000;k=n/
6、1000;j=j+k;n=s; s=s%100;k=n/100;j=j+k;n=s; s=s%10;k=n/10;j=j+k+s; return (j); 11.输入10个学生成绩,成绩取值范围为0到100分,编程实现:去掉最高成绩和最低成绩,计算并显示平均成绩。# include # include # include int main (void) int i; float max=0,min=0,avg,sum=0; float a10; printf(input student score:n); for(i=1;imax) max=ai; if (aimin) min=ai; sum
7、=sum+ai; avg=(sum-max-min)/8; printf(average:%fn,avg); return 0;12.先定义一个排好序的数组, 然后输入一个数, 插入其中后使其还有序并输出。# include # include # include int main (void) int a11=0,1,2,3,4,5,6,7,8,9; int i,j; int n; printf(input number:n); scanf(%d,&n); for (i=0;i10;i+) if (n=i;j-) aj=aj-1; ai=n; break; printf(order by n
8、umber:n); for(i=0;i11;i+) printf(%d ,ai); printf(n); return 0;13.用主函数中输入一组数,调用函数用选择法排序后,再用主函数输出排序后的数。# include # include # include void sort(int b10,int n) int i,j,t; int min; for(j=0;jn;j+) min=bj; for(i=j;in;i+) if (bimin) min=bi; t=bj; bj=bi; bi=t; int main (void) int i,k; int a10; printf(input 1
9、0 number:n); for (i=0;i10;i+) scanf(%d,&ai); sort(a,i); printf(order by 10 nums:n); for (k=0;k10;k+) printf(%d ,ak); return 0;14.用主函数输入4个字符串,调用函数用指针的方法由小到大排序,再用主函数输出。# include # include # include int main (int argc,char *argv) char a4255,t255; int i,j,k,f,n,c; for (i=0;i4;i+) printf(input a%d string
10、:n,i); for(j=0;j0;n-) for(k=0;k0) strcpy(t,ak); strcpy(ak,ak+1); strcpy(ak+1,t); for(c=0;c4;c+) puts(ac); return 0; 5.定义一个结构体类型book,包括成员:bianhao,bookname,price,next,动态地形成一个有5个结点的链表。# include # include # include # include # define N 5typedef struct node int bianhao; char bookname30; float price; stru
11、ct node *next;book;book * creat(int n) book *p,*h,*s; int i; if(h=(book *)malloc(sizeof(book)=NULL) printf(不能分配内存空间!); exit(0); h-bianhao=0; h-bookname0=0; h-price=0; h-next=NULL; p=h; for(i=0;inext=s; printf(请输入第%d个人的班号:,i+1); scanf(%d,&s-bianhao); printf(请输入书名:); scanf(%s,s-bookname); printf(请输入价格
12、:); scanf(%f,&s-price); s-next=NULL; p=s; return (h);void print(book *h) book *p; int i=1; p=h-next; printf(数据信息为:n); while(p!=NULL) printf(第%d个人的班号%dn,i,p-bianhao); printf(书名:%sn,&*(p-bookname); printf(价格:%fn,p-price); p=p-next; i+; int main (void) book *head; head=creat(N); print(head); return 0;7
13、.写一函数,输入一个八进值的字符串,输出相应的十进制数。# include # include # include # include int main (void) char a255; int i,n,s; int b255; s=0; printf(input oct:); for (i=0;i255;i+) scanf(%c,&ai); if (ai=n) break; if (ai7) printf(input error!n); exit (1); bi=ai-48; for (n=0,i=i-1;i=0;n+,i-) s=s+bi*pow(8,n); printf(result:
14、%dn,s);return 0;8.用指针法处理输入的3个字符串,按由小到大的顺序输出。# include # include # include int main (void) char a255=0; char b255=0; char c255=0; char *pa,*pb,*pc; int i; printf(input a string:); for(i=0;i255;i+) scanf(%c,&ai); if(ai=n) ai=0; break; printf(input b string:); for(i=0;i255;i+) scanf(%c,&bi); if(bi=n) b
15、i=0; break; printf(input c string:); for(i=0;i255;i+) scanf(%c,&ci); if(ci=n) ci=0; break; pa=a;pb=b;pc=c; printf(n*order by*:n); for(i=0;i255;i+) if (*(pa+i)(*(pb+i)&(*(pa+i)(*(pc+i) puts(a); if(*(pb+i)(*(pc+i) puts(b);puts(c); else puts(c);puts(b); break; if (*(pb+i)(*(pa+i)&(*(pb+i)(*(pc+i) puts(
16、b); if(*(pa+i)(*(pc+i) puts(a);puts(c); else puts(c);puts(a); break; if (*(pc+i)(*(pa+i)&(*(pc+i)(*(pb+i) puts(c); if(*(pa+i)(*(pb+i) puts(a);puts(b); else puts(b);puts(a); break; return 0;9.用指针法输入一行文字,最长为30,找出字母、空格、数字及其它字符的个数为多少。# include # include # include int main(void) char a30; char *pi; int i
17、,c,n,s,o; i=c=n=s=o=0; pi=a; printf(input string a:n); for(i=0;i=a&(*(pi+i)=A&(*(pi+i)=0)&(*(pi+i)=9) n+; else if(*(pi+i)= ) s+; else o+; printf(character:%dn,c); printf(number:%dn,n); printf(space:%dn,s); printf(other:%dn,o); return 0;10.从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“aa.txt”中保存。输入的串以“$”结
18、束。# include # include # include int main (void) char ch; FILE *pi; if (pi=fopen(/tmp/aa.txt,wb+)=NULL) printf(open file error!n); exit (1); printf(input string:n); ch=getchar(); while(ch!=$) if (ch=a&ch=z) ch=ch-32; fputc(ch,pi); ch=getchar(); fclose(pi); return 0;15.建立文件“FT.DAT”, 存放一串字符,长度不超过80个字符,
19、然后再读出后统计显示字符的个数。# include # include # include int main (int argc,char *argv) FILE *fp; int i,k=0; char ch; if(fp=fopen(FT.DAT,wt+)=NULL) printf(nerror open FT.DAT!n); exit(EXIT_FAILURE); printf(input a string:n); ch=getchar(); while(ch!=n) if (+k)=81) break; fputc(ch,fp); ch=getchar(); rewind(fp); c
20、h=fgetc(fp); while(ch!=EOF) putchar(ch); ch=fgetc(fp); printf(n); fclose(fp); exit(EXIT_SUCCESS); 16.用数组和循环实现:统计输入的一个字符串中的数字、大写字母、小写字母、空格及其它字符的个数。# include # include # include int main (int argc,char *argv) int i,j,n=0,c1=0,c2=0,s=0,o=0; char a255; for(i=0;i255;i+) scanf(%c,&ai); if(ai=n) break; for
21、(j=0;j=0&aj=a&aj=A&aj=Z) c2+; else if(aj= ) s+; else o+; printf(number count:%dn,n); printf(a-z)s count:%dn,c1); printf(A-Z)s count:%dn,c2); printf(space count:%dn,s); printf(other count:%dn,o); 17.用函数实现:输出2100间的素数,每行5个。# include # include # include int main (int argc,char *argv) int i,j,f,t=0; for(i=2;i=100;i+) f=0; for(j=2;ji;j+) if(i%j=0) f=1; break; if(f=0) if(t=4) printf(n); t=0; t+; printf(%d ,i); printf(n); return 0;18.用指针数组实现:在主函数中输入10个不等长的字符串,用另一函数对它们递增排序,然后在主函数中输出已排好序的字符串。# include # include # include void sort(char a10255)
限制150内