标准C语言程序设计及应用周纯杰每章课后编程题.doc

上传人:wux****ua 文档编号:9420014 上传时间:2020-04-05 格式:DOC 页数:40 大小:85.50KB
返回 下载 相关 举报
标准C语言程序设计及应用周纯杰每章课后编程题.doc_第1页
第1页 / 共40页
标准C语言程序设计及应用周纯杰每章课后编程题.doc_第2页
第2页 / 共40页
标准C语言程序设计及应用周纯杰每章课后编程题.doc_第3页
第3页 / 共40页
点击查看更多>>
资源描述
P71#include #include void main() clrscr(); int i; int sum; sum=0; printf(Enter i:); scanf(%d,&i); while (i=100) sum=sum+i; i+; printf(sum=%dn,sum); getch();P96(6)#include #include void main() int a, b, sum, i; a=8; b=1; sum=8; for(i=1; i=9;i+); b=b+3; a=a+b; sum+=a; printf(sum=%dn,sum); getch();P97(18)#include #include void main() float x, a, b, expx; while(1) char i; a=1; expx=1; b=1; printf(please input x:); scanf(%f,&x); if(x1e-6) break; for(i=1;i+) a*=x; b*=i; expx+=a/b; if(a/b1e-6) break; else continue; printf(expx=%fn,expx); getch(); clrscr(); P90#include #include void main() unsigned long int a, i; for(a=10;a=100;a+) for(i=2;i=a) printf(%dt,a); getch();P97(9)#include #include #include void main() unsigned int a, b, i; randomize(); a=random(65535); for(i=1;i1) printf(Right! You have guessed %d times.n,i); printf(Enter any singel number to break.n); scanf(%d,&b); if(1) break; else if(ab) printf(Wrong! Your number is small.n); continue; else printf(Wrong! Your number is large.n); continue; P97(19)#include #include #include void main() float x, a; char i; x=1.0; printf(Please input a(ji suan a de ping fang gen):); scanf(%f,&a); for(i=1;i+) if(fabs(x-(x+a/x)/2)1e-5) x=(x+a/x)/2; break; else x=(x+a/x)/2; printf(x=%fn,x); getch(); clrscr();P97(16)#include #include void main() long int x, S; int a, n, i; printf(Please input a, n:); scanf(%d,%d,&a,&n); S=0; x=0; for(i=1;i=n;i+) x=x*10+a; S=S+x; printf(S=%ldn,S); getch();P97(11)#include #include void main() int i; int a, b; printf(Please input a, b:); scanf(%d,%d,&a,&b); for(i=a0;i-) if(a%i=0&b%i=0) printf(The max is %d.n,i); break; for(i=ab?a:b;i+) if(i%a=0&i%b=0) printf(The min is %d.n,i); break; getch();P139(2)#include#includevoid main() int n, x; int i, j; int a100; printf(nInput the number of the group:n); scanf(%d,&n); printf(Input the group of numbers:n); for(i=0;i=an-1) an=x; printf(Now the group is:n); for(i=0;in+1;i+) printf(%d ,ai); else for(i=0;in;i+) if(xi;j-) aj=aj-1; ai=x; break; printf(Now the group is:n); for(i=0;in+1;i+) printf(%d ,ai); getch(); P139(6)#include#include#includevoid main() int grade100, tg; char name10010, tn100; int n; int i, j; printf(Input the number:n); scanf(%d,&n); printf(Input the grade and name:); for(i=0;in;i+) printf(n); scanf(%d,&gradei); scanf(%s,&namei); for(i=0;in-1;i+) for(j=0;jn-1-i;j+) if(gradejgradej+1) tg=gradej; gradej=gradej+1; gradej+1=tg; strcpy(tn,namej); strcpy(namej,namej+1); strcpy(namej+1,tn); printf(From large to small by grade:); for(i=0;in;i+) printf(n); printf(%d ,gradei); puts(namei); P139(7)#include#include#define SIZE 4void main() int aSIZESIZE; int i, j, t, max, n=0; int row=0, column=0; printf(Input a two-dimensional array:); for(i=0;iSIZE;i+) printf(n); for(j=0;jSIZE;j+) scanf(%d,&aij); for(i=0;iSIZE;i+) max=ai0; for(j=0;jSIZE;j+) if(maxaij) max=aij; row=i; column=j; for(t=0;tSIZE;t+) if(atcolumnmax) break; if(t=SIZE) printf(One saddle point is %d(%d, %d).n,max, row, column); n+; if(i=SIZE&n=0) printf(There is no saddle point.n); P139(5)#include#include#includevoid main() char str1100, str2100; int i, j; printf(nInput string1 and string2:n); gets(str1); gets(str2); for(i=strlen(str1),j=0;jstrlen(str2);i+,j+) str1i=str2j; str1strlen(str1)+strlen(str2)=0; printf(Now the string is:n); puts(str1); getch(); clrscr();P170(4)#include #include #include void main()char str100, *pstr=str;long int a50=0, *pa=a;int time, flag, n, i;clrscr();printf(Please input a string:n);gets(str);for(time=1, flag=0; *pstr!=0; pstr+)if(isdigit(*pstr)flag=1;*pa*=time;*pa+=(*pstr-0);time=10;n=pa-a;elseif(flag=1)pa+;time=1;flag=0;pa=a;printf(nNumber:%dn,n);for(i=0; i=n; i+)printf(a%d=%ldn,i,pai);getch();P170(6)#include #include #include #define N 6void main()char strN20, *pN, *temp;char *pp=p;int i;clrscr();printf(Please %d strings:n,N);for(i=0; iN; i+)gets(stri);pi=stri;for(i=0; iN-1; i+)pp=p;while(*pp-p0)temp=*pp;*pp=*(pp+1);*(pp+1)=temp;printf(nIn order:n);for(i=0; iN; i+)puts(*(p+i);getch();P170(1)#include #include #include void main()char str1100, str2100;char *p1=str1, *p2=str2;int n=0;clrscr();printf(Please input string1 and string2:n);gets(str1);gets(str2);while(*p1!=0&*p2!=0)if(*p1+=*p2+)n+;printf(Number of the same char:%d,n);getch();P170(3)#include #include #include #define N 20void main()char str1N, str2N;char *p1=str1, *p2=str2;int m;clrscr();printf(Please input a string:n);gets(str1);printf(nPlease input m:n);scanf(%d,&m);while(mstrlen(str1)|m1)printf(nError!nPlease input m:);scanf(%d,&m);p1+=m-1;while(*p2+=*p1+)!=0);*p2=0;puts(str2);getch();P170(7)#include #include #include void main()char str100, *p=str, temp;int i, n;clrscr();printf(Please input a string:n);gets(str);n=strlen(str);printf(nThe length of the string is %d.,n);for(i=0; in/2; i+)temp=*(p+i);*(p+i)=*(p+n-i-1);*(p+n-i-1)=temp;printf(nNow the string is:n);puts(str);getch();P170(8)#include #include #include #define N 5void main()char text500, *p;int i;char sN20=from,in,at,an,on;clrscr();printf(Please input the text:n);gets(text);for(i=0; iN; i+)while(1)p=strstr(text,si);if(p!=NULL)strcpy(p,p+strlen(si)+1);elsebreak;printf(nAfter deleting frominatanon,the text is:n);puts(text);getch();P170(8)#include #include #include #define N 500#define IsnotLetter(x) (!(x)=A&(x)=a&(x)=z)/IsnotLetter(x) stands for that x is not a letter.void delete_word(char *pstr, char *pword);int one_word(char *pstr, char *p, int n);/* one_word is used to judge whether the son string p(n is its length) in the string pstr is a single word. */void main()char textN; clrscr();printf(Please input a text:n);gets(text);delete_word(text, from);delete_word(text, in);delete_word(text, at);delete_word(text, an);delete_word(text, on);printf(nAfter deleting frominatanon, the text is:n);puts(text);getch();void delete_word(char *pstr, char *pword)char *p, *pflag=pstr;int word_length;word_length=strlen(pword);while(1)p=strstr(pflag,pword);if(p=pflag)pflag+;if(p!=NULL&one_word(pstr, p, word_length)strcpy(p, p+word_length);if(*p= )strcpy(p, p+1);else if(p=NULL)break; pflag+;int one_word(char *pstr, char *p, int n)if(p=pstr&IsnotLetter(*(p+n)return 1;else if(p!=pstr&IsnotLetter(*(p-1)&IsnotLetter(*(p+n)return 1;else return 0;P230(4)#include #include #include #define N 20void delete_c(char *p, char c);void main()char strN, c;clrscr();printf(Please input a string:n);gets(str);printf(nPlease input the char you want to delete:n);scanf(%c,&c);delete_c(str, c);printf(nNow the string is:n);puts(str);getch();void delete_c(char *p, char c)for( ;*p!=0; p+)if(*p=c)strcpy(p, p+1);p-;P230(1)#include #include long int SquSum(int x, int y);void main()int a, b;long int c;clrscr();printf(Please input a,b:n);scanf(%d,%d,&a,&b);c=SquSum(a, b);printf(na2+b2=%ld,c);getch();long int SquSum(int x,int y)return(x*x+y*y);P230(2)#include #include long int min(int x, int y);int max(int x, int y);void main()int a, b, max1;long int min1;clrscr();printf(Please input a,b:n);scanf(%d,%d,&a,&b);max1=max(a, b);min1=min(a, b);printf(nmax common denominator:%d,max1);printf(nmin common multiple:%ld,min1);getch();int max(int x, int y)int temp;if(xy)temp=x;x=y;y=temp;while(y!=0)temp=x%y;x=y;y=temp;return x;long int min(int x, int y) return (x*y)/max(x, y);P230(5)#include #include int judge_sushu(long int x);void main()long int a;clrscr();printf(Please input a int number:n);scanf(%ld,&a);if(judge_sushu(a)printf(It is a sushu.);elseprintf(It isnt a sushu.);getch();int judge_sushu(long int x)int i;for(i=2; i=x/2; i+)if(x%i=0)return 0; return 1;P231(6)#include #include #define N 20void my_strcat(char *p, char *p1, char *p2);void main()char s1N, s2N, sN*2;clrscr();printf(Please input string1 and string2:n);gets(s1);gets(s2);my_strcat(s, s1, s2);printf(nAdd string2 to the end of string1:n);puts(s);getch();void my_strcat(char *p, char *p1, char *p2)for( ;*p1!=0; p+,p1+)*p=*p1;for( ;*p2!=0; p+,p2+)*p=*p2;*p=0;P231(7)#include #include #define N 50void str_tongji(char *pstr, int *pnum);void main()char strN;int num4=0,0,0,0;clrscr();printf(Please input a string:n);gets(str);str_tongji(str, num);printf(nLetter:%dn,num0);printf(Figure:%dn,num1);printf(Space:%dn,num2);printf(Other:%dn,num3);getch();void str_tongji(char *pstr, int *pnum)for( ;*pstr!=0; pstr+)if(*pstr=A&*pstr=a&*pstr=0&*pstr=9)pnum1+;else if(*pstr= )pnum2+;elsepnum3+;P231(9)#include #include #define N 10void sort(int *pnum, int n, int flag);void main()int numN=0, n, flag, i;clrscr();printf(How many ints will yo input:n);scanf(%d,&n);printf(nNow, input the ints one by one:n);for(i=0; in; i+)scanf(%d,&numi);printf(How to sort them?n);printf(From large to small: input 1.n);printf(from small to large: input any other figure.n);scanf(%d,&flag);sort(num, n, flag);printf(nNow, they are:n);for(i=0; in; i+)printf(%dt,numi);getch();void sort(int *pnum, int n, int flag)int i, j, temp;if(flag=1)for(i=0; in-1; i+)for(j=0; jn-i-1; j+)if(pnumjpnumj+1)temp=pnumj;pnumj=pnumj+1;pnumj+1=temp;elsefor(i=0; in-1; i+)for(j=0; jpnumj+1)temp=pnumj;pnumj=pnumj+1;pnumj+1=temp;P231(10)#include #include #include #define LINE 10#define SIZE 100int input(char (*ptext)SIZE, int n);int find(char (*ptext)SIZE, int n, int flag);void main()char textLINESIZE;int n, line_num, flag;clrscr();printf(Please input a text less than %d lines, end with a space in a single line:n,LINE);n=input(text, LINE);printf(nInput 1 for the max line,others for the min line:);scanf(%d,&flag);line_num=find(text, n, flag);printf(nThe line is:n);puts(textline_num);getch();int input(char (*ptext)SIZE, int n)int i;for(i=0; in; i+)gets(ptexti);if(strcmp(ptexti, )=0)break;return i;int find(char (*ptext)SIZE, int n, int flag)int i, line_num=0;if(flag=1)for(i=1; i0)line_num=i;elsefor(i=1; in; i+)if(strcmp(ptexti, ptextline_num)0)line_num=i;return line_num;P231(14)#include #include #include #define MAX 10#define SIZE 20int input(char (*p)SIZE, int max);void sort(char (*p)SIZE, int n);void output(char (*p)SIZE, int n);void main()char strMAXSIZE;int n;clrscr();printf(Please input less than %d strings, end with string :n,MAX);n=input(str, MAX);sort(str, n);printf(nAfter sorted:n);output(str, n);getch();int input(char (*p)SIZE, int max)int i;for(i=0; imax; i+)gets(pi);if(strcmp(pi, )=0)break;return i;void sort(char (*p)SIZE, int n)int i, j;char tempSIZE;for(i=0; in-1; i+)for(j=0; j0)strcpy(temp, pj);strcpy(pj, pj+1);strcpy(pj+1, temp);void output(char (*p)SIZE, int n)int i;for(i=0; in; i+)puts(pi);P266(4)#include #include #include #include #define MAX 20 /The largest number of studentstypedef structint year;int month;int day;DATE;typedef structchar number20;int stunum;DATE sheet;CLASS;typedef structchar number20;char name20;int grade4;float aver;STUDENT;void input_class(CLASS *p);void input_student(STUDENT *p, int n);void average(STUDENT *p, int n);float remain_2float(float x); /Remain 2 figures after the point.void sort(STUDENT *p, int n);void output(CLASS *pc, STUDENT *ps, int n);void main()CLASS cla;STUDENT stuMAX;clrscr();input_class(&cla);input_student(stu, cla.stunum);average(stu, cla.stunum);sort(stu, cla.stunum);output(&cla, stu, cla.stunum);getch();void input_class(CLASS *p)char s20;printf(Please input the class information:);printf(nClass number:);gets(p-number);printf(Number of students:);gets(s);p-stunum=atoi(s);printf(Date:n);printf(year:);gets(s);p-sheet.year=atoi(s);printf(month:);gets(s);p-sheet.month=atoi(s);printf(day:);gets(s);p-sheet.day=atoi(s);printf(n);void input_student(STUDENT *p, int n)int i, j, s3;printf(Now, please input the students information:n);for(i=0; in; i+)printf(Number:);gets(pi.number);printf(Name:);gets(pi.name);for(j=0; j4; j+)printf(Grade of course %d:,j+1);gets(s);pi.gradej=atoi(s);void average(STUDENT *p, int n)int i, j;for(i=0; in; i+)pi.aver=0;for(j=0; j=1e-6)x+;x=(float)(int)x)/100;return x;void sort(STUDENT *p, int n)STUDENT temp;int i, j;for(i=0; in-1; i+)for(j=0; jn-i-1; j+)if(pj.avernumber);printf(tnumber of students:%d,pc-stunum);printf(tDate:%d.%d.%d,pc-sheet.year, pc-sheet.month, pc-sheet.day);printf(nNumber Name Average grade rank);for(i=0; i0&(psi.aver)!=(psi-1.aver)rank+;printf(n%-10s %-10s %.2f %d,psi.number, psi.name, psi.aver, rank);P266(5)#include #include #include #include #define MAX 20 /The largest number of studentstypedef structint year;int month;int day;DATE;typedef structchar number20;int stunum;DATE sheet;CLASS;typedef structchar number20;char name20;int grade4;float aver;STUDENT;void input_class(CLASS *p);void input_student(STUDENT *p, int n);void average(STUDENT *p, int n);float remain_2float(float x); /Remain 2 figures after the point.void sort(STUDENT *p, int n);void output(CLASS *pc, STUDENT *ps, int n);void output_highest_average(STUDENT *p, int n);void find_fail(STUDENT *p, int n);void main()CLASS cla;STUDENT stuMAX;clrscr();input_class(&cla);input_student(stu, cla.stunum);average(stu, cla.stunum);sort(stu, cla.stunum);output(&cla, stu, cla.stunum);output
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!