[理学]标准C语言程序设计及应用周纯杰每章课后编程题_第1页
[理学]标准C语言程序设计及应用周纯杰每章课后编程题_第2页
[理学]标准C语言程序设计及应用周纯杰每章课后编程题_第3页
[理学]标准C语言程序设计及应用周纯杰每章课后编程题_第4页
[理学]标准C语言程序设计及应用周纯杰每章课后编程题_第5页
已阅读5页,还剩35页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、P71#include <stdio.h>#include <conio.h>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 <stdio.h>#include <conio.h>void main() int

2、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 <stdio.h>#include <conio.h>void main() float x, a, b, expx; while(1) char i; a=1; expx=1; b=1; printf("please input x:"); scanf("%f",&am

3、p;x); if(x<1e-6) break; for(i=1;i+) a*=x; b*=i; expx+=a/b; if(a/b<1e-6) break; else continue; printf("expx=%fn",expx); getch(); clrscr(); P90#include <stdio.h>#include <conio.h>void main() unsigned long int a, i; for(a=10;a<=100;a+) for(i=2;i<a;i+) if(a%i=0) break;

4、if(i>=a) printf("%dt",a); getch();P97(9)#include <stdio.h>#include <stdlib.h>#include <time.h>void main() unsigned int a, b, i; randomize(); a=random(65535); for(i=1;i<=20;i+) printf("Please guess a(065535):"); scanf("%d",&b); if(a=b&&

5、;i=1) printf("Right! You have guessed 1 time.n"); printf("Enter any singel number to break.n"); scanf("%d",&b); if(1) break; else if(a=b&&i>1) printf("Right! You have guessed %d times.n",i); printf("Enter any singel number to break.n")

6、; scanf("%d",&b); if(1) break; else if(a>b) printf("Wrong! Your number is small.n"); continue; else printf("Wrong! Your number is large.n"); continue; P97(19)#include <stdio.h>#include <conio.h>#include <math.h>void main() float x, a; char i; x=

7、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 <stdio.h>#include <conio.h>void main() long i

8、nt 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 <stdio.h>#include <conio.h>void main() int i; int a, b; printf("Please input a,

9、b:"); scanf("%d,%d",&a,&b); for(i=a<b?a:b;i>0;i-) if(a%i=0&&b%i=0) printf("The max is %d.n",i); break; for(i=a>b?a:b;i+) if(i%a=0&&i%b=0) printf("The min is %d.n",i); break; getch();P139(2)#include<stdio.h>#include<conio.h&

10、gt;void 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<n;i+) scanf("%d",&ai); printf("Input x:n"); scanf("%d",&x); if(x>=a

11、n-1) an=x; printf("Now the group is:n"); for(i=0;i<n+1;i+) printf("%d ",ai); else for(i=0;i<n;i+) if(x<=ai) for(j=n;j>i;j-) aj=aj-1; ai=x; break; printf("Now the group is:n"); for(i=0;i<n+1;i+) printf("%d ",ai); getch(); P139(6)#include<stdio

12、.h>#include<string.h>#include<conio.h>void 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;i<n;i+) printf("n"); scanf("%d&quo

13、t;,&gradei); scanf("%s",&namei); for(i=0;i<n-1;i+) for(j=0;j<n-1-i;j+) if(gradej<gradej+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;i<n;i+) printf(&qu

14、ot;n"); printf("%d ",gradei); puts(namei); P139(7)#include<stdio.h>#include<string.h>#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;i<SIZE;i+) printf("n"); for(j=

15、0;j<SIZE;j+) scanf("%d",&aij); for(i=0;i<SIZE;i+) max=ai0; for(j=0;j<SIZE;j+) if(max<aij) max=aij; row=i; column=j; for(t=0;t<SIZE;t+) if(atcolumn<max) break; if(t=SIZE) printf("One saddle point is %d(%d, %d).n",max, row, column); n+; if(i=SIZE&&n=0)

16、 printf("There is no saddle point.n"); P139(5)#include<stdio.h>#include<string.h>#include<conio.h>void main() char str1100, str2100; int i, j; printf("nInput string1 and string2:n"); gets(str1); gets(str2); for(i=strlen(str1),j=0;j<strlen(str2);i+,j+) str1i=s

17、tr2j; str1strlen(str1)+strlen(str2)='0' printf("Now the string is:n"); puts(str1); getch(); clrscr();P170(4)#include <stdio.h>#include <conio.h>#include <ctype.h>void main()char str100, *pstr=str;long int a50=0, *pa=a;int time, flag, n, i;clrscr();printf("Ple

18、ase 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)#

19、include <stdio.h>#include <conio.h>#include <string.h>#define N 6void main()char strN20, *pN, *temp;char *pp=p;int i;clrscr();printf("Please %d strings:n",N);for(i=0; i<N; i+)gets(stri);pi=stri;for(i=0; i<N-1; i+)pp=p;while(*pp-p<N-i)if(strcmp(*pp+,*(pp+1)>0)t

20、emp=*pp;*pp=*(pp+1);*(pp+1)=temp;printf("nIn order:n");for(i=0; i<N; i+)puts(*(p+i);getch();P170(1)#include <stdio.h>#include <conio.h>#include <string.h>void main()char str1100, str2100;char *p1=str1, *p2=str2;int n=0;clrscr();printf("Please input string1 and str

21、ing2: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 <stdio.h>#include <conio.h>#include <string.h>#define N 20void main()char str1N, str2N;char *p1=str1, *p2=st

22、r2;int m;clrscr();printf("Please input a string:n");gets(str1);printf("nPlease input m:n");scanf("%d",&m);while(m>strlen(str1)|m<1)printf("nError!nPlease input m:");scanf("%d",&m);p1+=m-1;while(*p2+=*p1+)!='0');*p2='0'pu

23、ts(str2);getch();P170(7)#include <stdio.h>#include <conio.h>#include <string.h>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; i<n/2; i+)tem

24、p=*(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 <stdio.h>#include <conio.h>#include <string.h>#define N 5void main()char text500, *p;int i;char sN20="from","in","at","an",&

25、quot;on"clrscr();printf("Please input the text:n");gets(text);for(i=0; i<N; i+)while(1)p=strstr(text,si);if(p!=NULL)strcpy(p,p+strlen(si)+1);elsebreak;printf("nAfter deleting "from""in""at""an""on",the text is:n");puts(tex

26、t);getch();P170(8)#include <stdio.h>#include <conio.h>#include <string.h>#define N 500#define IsnotLetter(x) (!(x)>='A'&&(x)<='Z')&&!(x)>='a'&&(x)<='z')/IsnotLetter(x) stands for that 'x' is not a letter.vo

27、id 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, "

28、from");delete_word(text, "in");delete_word(text, "at");delete_word(text, "an");delete_word(text, "on");printf("nAfter deleting "from""in""at""an""on", the text is:n");puts(text);getch();void dele

29、te_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 *p

30、str, 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 <stdio.h>#include <conio.h>#include <string.h>#define N 20void delete_c(char *p, char c);void main()char s

31、trN, 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=

32、c)strcpy(p, p+1);p-;P230(1)#include <stdio.h>#include <conio.h>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

33、,int y)return(x*x+y*y);P230(2)#include <stdio.h>#include <conio.h>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("nma

34、x common denominator:%d",max1);printf("nmin common multiple:%ld",min1);getch();int max(int x, int y)int temp;if(x<y)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 <stdio.h>#include <conio.h>int

35、 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 isn't a sushu.");getch();int judge_sushu(long int x)int i;for(i=2; i<=x/2; i+)if(x

36、%i=0)return 0; return 1;P231(6)#include <stdio.h>#include <conio.h>#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 th

37、e 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 <stdio.h>#include <conio.h>#define N 50void str_tongji(char *pstr, int *pnum);void main()char strN;

38、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( ;*pst

39、r!='0' pstr+)if(*pstr>='A'&&*pstr<='Z')|(*pstr>='a'&&*pstr<='z')pnum0+;else if(*pstr>='0'&&*pstr<='9')pnum1+;else if(*pstr=' ')pnum2+;elsepnum3+;P231(9)#include <stdio.h>#include <conio

40、.h>#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; i<n; i+)scanf("%d",&numi);printf("How

41、 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; i<n; i+)printf("%dt",numi);getch();voi

42、d sort(int *pnum, int n, int flag)int i, j, temp;if(flag=1)for(i=0; i<n-1; i+)for(j=0; j<n-i-1; j+)if(pnumj<pnumj+1)temp=pnumj;pnumj=pnumj+1;pnumj+1=temp;elsefor(i=0; i<n-1; i+)for(j=0; j<n-i-1; j+)if(pnumj>pnumj+1)temp=pnumj;pnumj=pnumj+1;pnumj+1=temp;P231(10)#include <stdio.h&

43、gt;#include <conio.h>#include <string.h>#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 s

44、ingle 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;

45、 i<n; 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; i<n; i+)if(strcmp(ptexti, ptextline_num)>0)line_num=i;elsefor(i=1; i<n; i+)if(strcmp(ptexti, ptextline_num)<0)line_num=i;return line

46、_num;P231(14)#include <stdio.h>#include <conio.h>#include <string.h>#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 stri

47、ngs, 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; i<max; i+)gets(pi);if(strcmp(pi, " ")=0)break;return i;void sort(char (*p)SIZE, int n)int i, j;char t

48、empSIZE;for(i=0; i<n-1; i+)for(j=0; j<n-i-1; j+)if(strcmp(pj, pj+1)>0)strcpy(temp, pj);strcpy(pj, pj+1);strcpy(pj+1, temp);void output(char (*p)SIZE, int n)int i;for(i=0; i<n; i+)puts(pi);P266(4)#include <stdio.h>#include <conio.h>#include <string.h>#include <stdlib.

49、h>#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

50、(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)

51、;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("yea

52、r:");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; i<n; i+)printf("Number:");gets(pi.number);printf("Name:");gets();for(j=0; j<4; j+)printf("Grade of c

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论