c语言课本例题(ExamplesofClanguagetextbooks)

上传人:无*** 文档编号:168649200 上传时间:2022-11-11 格式:DOCX 页数:57 大小:30.83KB
收藏 版权申诉 举报 下载
c语言课本例题(ExamplesofClanguagetextbooks)_第1页
第1页 / 共57页
c语言课本例题(ExamplesofClanguagetextbooks)_第2页
第2页 / 共57页
c语言课本例题(ExamplesofClanguagetextbooks)_第3页
第3页 / 共57页
资源描述:

《c语言课本例题(ExamplesofClanguagetextbooks)》由会员分享,可在线阅读,更多相关《c语言课本例题(ExamplesofClanguagetextbooks)(57页珍藏版)》请在装配图网上搜索。

1、c 语言课本例题(Examples of C language textbooks)Advanced programming 2012-01-05 9:00-11:00 1-203 Lu Yuhong Zhou Jinfeng Lu to diagnose ischemic cerebrovascular diseaseThe third chapter is sequential structure programming.1. input 3 characters, and reverse output 3 characters and their ASCII code.ttinclude

2、 stdio. h”Void main ()char chi, CH2, ch3;Scanf (,%c%c%c,/, &chl, &ch2, &ch3);Printf (祝,dn”, CH3, CH3);Printf (%c, %dn, CH2, CH2);Printf (%c, %dn, chi, chi);2. finding the real roots of equation ax*x+bx+c=0.ttinclude stdio. hVoid main () float a, B, C, disc, xl, x2;Printf input a, B, c:);Scanf C%f, %

3、f,&a, &b, &c);Disc=b*b-4ac;Xl= (-b+sqrt (disc) / (2*a);X2= (-b-sqrt (disc) / (2*a);Printf (nxl=%6. 2fnx2=%6. 2fn”, xl, X2);3. seeking predecessor characters and subsequent characters. Enter a character to find its predecessor and subsequent characters. Output in order from large to small.#include st

4、dio”Void main ()char chi;Printf (enter a character n);Scanf (%c, &chl);Printf (precursor characters:%c, meta characters:%c, successor characters:%c, their ASCII values are:%d, %d, %dn, chl+1, chi, chl-1, chl+1, chi, chl-l);4. input the length of the triangle, calculate the area of the triangle (area

5、 =sqrt (s (S-A) (S-B) (S-A), s= (a+b+c) /2)The fourth chapter chooses structural programmingFiveAccording to the input student achievement judgment, when the score is =90, for A and so on,ttinclude stdio.h”Void main ()int score;Scanf (“刎,&score);Switch (score/10)case 10:Case 9:printf (%d:An, score);

6、 break;Case 8:Case 7:printf (%d:Bn, score); break;Case 6:printf (%d:Cn, score); break;Default:printf (%d:Dn, score); break;6. solving the real solution of equation ax*x+bx+c=0.#include stdio. hVoid main ()float a, B, C, xl, X2, disc;Printf (,zinput a B c:);Scanf (,%f%f%f/,, &a, &b, &c);If (Fabs (a)

7、le-6)Printf (the equation is not a quadratic);Elsedisc=b*b-4*a*c;If (disc0)Printf (the equation has not real root);ElseIf (fac (disc) #包括数学。”无效main()无符号整型年份、月份、跳跃;scanf ( %d %d”,年、月);如果(年% = 400 = 0)其他的(如果(% 4 = 0 和年% 100)! = 0)跳跃=1;其他的跳跃=0;)开关(月)案例 1: printf (月 31”);休息;案例2:如果(跳跃=1)printf (月 29 n);p

8、rintf (“月 28”);休息;案例 3: printf (“月 31”);休息;案例 4: printf (“月 30”);休息;案例 5: printf (“月 31”);休息; 案例 6: printf (“月 30”);休息; 案例 7: printf (“月 31”);休息;案例 8: printf (“月 31”);休息; 案例9 printf (“月30”);休息;案例 10: printf (“月 31”);休息; 案例 11: printf (“月 30”);休息; 案例 12: printf (“月 31”);休息;)break;Case 1: printf (% d,

9、 y = x + 1);第五章循环结构程序设计1.写个程序,输入一个班学生的成绩,求全班的平均成绩.# include STDIO. HVoid main ()float Score, average = 0;Int n = 0;Each (% F & score);While (score = 0)+ = average score;N + +;Each (% F & score);)If (n! Average average = = 0) / N;Prinf (% 6.2f“ average);2 .计算 s = l + 2 + 3 + 4+. + 100.# include STDIO

10、. HVoid main ()Int s = 0, R;For (i = 0; I = 0)switch (SCORE / 10)caselO:Case9: printf (% d: n Score); break;Case8:Case7: printf (% d: B score n); break;Case6: printf (% d score C; n); break;Default: printf (% d score D: n);Each (% d & score)5. input n integers, find the average number of even number

11、s in these n numbers, and output.(main)(Int n, sum, I, J, count=0;Int sum=0;Scanf (%d”, &n)1=0While (i+n)scanf (%d, &j);If (. J%2)Sum+=j;Count+;)Sum=sum/count;Printfsum);)6. input a positive integer from the keyboard, calculate and output the number of the sum of the numbersttinclude stdio. h”(main)

12、(Int a, s=0, u, i;Printf Void main ()int i=0;Char a100, c;Printf (enter the string content: t);Doscanf (%c, &ai);C=ai;1+;While (c! = n);Ai=(r ;1=0;Printf input string is t);While (ai! =、) !Printf (%c, ai); i+;)Printf (the content of the converted string is: t); 1=0;While (ai! = 0) c i ;如果(C = A & C

13、= Z)我=32; 如果(C = A & C = Z)我=32;printf ( %c”, 个我);+;)8。输出所有的水仙花数。”水仙花数”指个三位数,其个位数字 立方和等于该数本身。#包括 “stdio. h”主 i, j, k, n;对于(n= 100; n 1000; n + +) i=n/ 100;J = N / 10-1 * 10;k = n 10;如果(i)printf (水仙花数是:%d,n);printf ( n);)9。如何编写程序”输出1999中能被3整除,而且至少有一位数 字是5的所有数字”?国际main()int i;对于(i = 1; i 999; i + +) 如

14、果(我 3 = = 0 & (我% 10 = = 5) I I (I / 10) % 10 = = 5 I I (I / 100) % 10 = = 5) printf ( %d ”,我);)10o计算斐波那契分数序列前N项之和#包括stdio. h” int, n, i;浮点数和,a, b;printf ( n 输入 N: ”);scanf ( “%d” ,和 N);总和=0;a = 2; b = l;对于(i = 1; i = n; i + +)和/ +A = A + B;B = A-B;)printf (“金额=%f n”,总和);11。个数如果恰好等于它的因子之和。即6 = 1 + 2

15、 + 3, 1000内 满足条件的数求。#包括 “stdio. h” m, n, i;对于(m=2; m le-b) = a;a + 1. 0/f;printf ( %d , n);氮+;printf ( % 10. 8f n” , A);)printf ( % 10. 6f n” , A);)第六章。函数与编译预处理lo编写计算X的N次乘方的程序。#包括 “stdio. h”主 浮点数X, y;int n;浮动功率(浮点数x, int n);scanf ( %、%、& x 和 n);y =功率(x, n);printf ( 8. 2f”,Y);)浮动功率(浮点数x, int n); int

16、I;浮点T=l;对于(i = l; i = n; i + +)T=x;返回T;)2 求组米! / (n! (M-N)!)长因子(k)长 f=l;int i ;对于(i = 1, i = k; i +)f=f i;返回F;)长梳(整数n整数m)长C;C = FAC (M) / FAC (N) XFAC (M-N);返回C;)主要()整数n, m;长C;scanf ( “%d, %d”,与 N 和 M);丙(m, n);PrintfC);3. input two numbers, output the larger number.ttinclude stdio. h”Void main ()int

17、 a, B, m;Int max (int int);Scanf (%d, %d, &a, &b);M=max (a, B);Printf (max=%d”, m);)Int max (int x, int y);int t;If (xy) t=x;Else t=y;Return t;4. for the common denominator, ttinclude stdio. h”Int divisor (int a, int b) int r;Do r=a%b;A=b;B=r;while (r! =0);Return a;)Void main ()int a, B, d;Scanf (肌|

18、,%d”, &a, &b);If (ab)D=divisor (a, B);ElseD=divisor (B, a);Printf (a=%d, b=%dn”, a, B);Printf (d=%d”, D);)The common denominator 5. using recursive method for M and n.Int GCD (int m, int n)int g;If (n=0)G=m;ElseG=gcd (n, m%n)Return (g);Void main () int m, n;Scanf (%d, %d, &m, &n);Printf (gcd=%d”, GC

19、D (m, n);)6. seeking the least common multiple.Int divisor (int a, int b)int r;While (r=a%b)! =0)a=b; b=r;Return b;)Int multiple (int a, int b)int d;D=divisor (a, B);Return a*b/d;Void main ()int a, B, c;Printf (,zinput a b:);Scanf (“紀d, &a, &b);C=multiple (a, B);Printf (c=%d, C);)7. enter a string t

20、hat represents a positive integer, and convert the string to the corresponding number.Int cton ()char ch;Int n=0, f=0, nI=0;While (ch=getchar () =O | |ch=9) nl+;If (nl=10) printf (data is wrong!) ; return 0;)Don=n*10+ch- ;While (ch=getchar () - 0 &chn) temp=m; m=n; n=temp;For (i=m; i0; i-)If (m%i=0&

21、n%i=0) printf (%dn, I);Break;)For (i=l; i=m; i+)If (n*i%m=0)printf (%dn, n*i);Break;)9. input a line of characters, statistics of the English letters, spaces, numbers and other characters.ttinclude stdio. h”Void main () int a, B, K, d;Z=k=s=q=O;Char c;While (c=getchar () ! =n)if (c= a &c= z I I c= A

22、 &c= Z)A+;Else if (c=)B+;Else if (c=O&c=9)K+;ElseD+;)Printf alphabet”:%3d, space:%3d, number:%3d,Other:%3dn , Z, K, Q);Seventh chapters, array1. input 5 numbers from the keyboard, output the maximum, minimum element value and their subscript.ttinclude stdio. h”Void main ()int I, J, K, Max, min;Stati

23、c int a5;For (i=0; i5; i+)Scanf (%d”, &a i);Max=min=a0;J=k=O;For (i=0; iai) min=ai; k=i;Printf (max:a%d=%d, min:a%d=%d,/, J, Max, K, min);2. input 16 integers from the keyboard, save in the two-dimensional array of 4x4, output even line and even column.Include stdio.h”Void main ()int I, j;Int a44;Fo

24、r (i=0; i4; i+)For (j=0; j4; j+)Scantf (%d, &aij);For (i=0; i4; i+)for (j=0; j4; j+)If (i%2=0|Ij%2=0) printf (%-4d”, aij);Else printf (%-4c,);Printf (n);3. initialize the array a44 with the 4X4 matrix, and findits transpose matrix and output.#define M 4ftdefine N 4Include stdio. hVoid main ()int I,

25、k;Int aMN = l,2,3,4, 5, 6, 7,8, 9, 10, 11,12, 13, 14,15, 16;Int bM N;For (i=0; iM; i+)For (k=0; kN; K+) bi k=ak i;For (i=0; iM; i+)(k = 0; k n; k + +) printf (% 5d, b i k);printf ( n);4.求一个3x3的整型矩阵对角线元素之和.# include stdio. hvoid main ()int a 3 3, sum = 0;int i, j;for(i=0;i3;i+)for(j = 0,j3;j+)scanf C

26、% d & a i j);for(i=0;i3;i+)for(j = 0,j3;j+)if (i = = j)sum = sum + a i j;printf (sum =% d n , sum);)5 .用选择法对!0个数进行排序.# include stdio. hvoid main ()int i, j, my, temp, a 11;for (i = 0; i = 10; 1 + +)scanf (% d & a i);for (i = 0; i = 10; i + +)i = i;for (j = i + 1; j = 10; j + +)if (a j = a i)my = j;t

27、emp = a i, a i = a i, a i = temp;)for (i = 0; i =0&n 100)sum + = x;score n + + = x;scanf (% f, & x);ave = sum / n;printf (average =% f n ”, ave);for (count = 0; i = 0; i ave) (printf (% f n, score in)count + +;如果(计数% 5 = = 0) printf ( n);)printf (数=%dn,计数);)7。假设在数组一中的数据按由小到大的顺序排列,从键盘上输入 个数,判定该数是否在数组

28、中,若在,输出所在序列号;若不在,输 出相应信息。#定义M 4#定义N 4包括stdio. h主海峡 int M = - 12,0, 6, 16, 23, 56, 80100110115 ;整数n,低,中,高,找到;低=0;咼=m-1; 发现=0; printf (“输入一个数字,要搜索:“);scanf ( “d”,和 N);同时(低 =高)=(低+高)/ 2;如果(n = a)- = 1;断裂;否则如果(=中)低=中+ 1;其他高=中叶1;)如果(发现=1) printf (索引%d %d” , N, MID);else printf (没有%d , n);8o用冒泡法将8个数按由小到大排

29、序。#包括stdio. h # Deine N 80 主() INTA 的口;整数,m;空排序(int b , int k);空打印(int ,int k);printf ( niput m (80):); scanf (“机!”,和一个我);排序(a, m);打印(A, M);)空排序(int b , int k) i, j, t,标志;为(i = 0; J = a & & str i = z) c str i - a + ;if (str i = a & & str i = z) c str i - a + 26+ +;i + +;for (i = 0; i 26; i + +)if (c

30、 i) if (m% 8 = = 0) putchar ( n) ; m + +;printf (% c:% d ”, i + a c i);)for (i = 0; i 26; i + +)if (c i + 26) if (m% 8 = = 0) putchar ( n) ; m + +;printf (% c:% d ”, i + a , c i + 26);12.约瑟夫环问题.# include (stdio. h # define n 100void main () int a n;int i, j, m, n, k = 0;printf input n, m:);scanf (%

31、d% d”, & n & m); printf (enter code array:); 为(i=0; i ;; + (+) scanf ( %d,和一个我); printf ( Noutput: n); 为(i = 0; i ; ; + ( + ) 二 1;同时(JM) (a = k = 0) k= (k + 1) % n;+;k = (k + 1) % n;)当(a = = 0) k = (k + 1) % n;printf ( % 6d”,K);a; k0;)第八章指针1。指针交换比较。#包括stdio. h主 a, b, *, *, PB;空交换(int * Pl, int * P2)

32、;和一个;铅与硼;scanf ( d, %d”,&, & B)交换(PA, PB);printf ( n main() : = %d, B = %dn”,A, B);printf ( n 小说:* PA = %d, * PD = %dn” , * * PA, PB);)空交换(int * Pl, int * P2) int * p;P=P1;Pl = P2;P2 = p;printf (“美国国家社会主义白人党:* Pl = %d, * P2=刎n, * * Pl, P2);2。函数max ()用来求一位数组中元素的最大值,在主调用函数中用 函数名调用该函数与函数指针调用该函数实现。#包括 “

33、stdio. h”主浮sumf,水池;浮动M = 11,2, - 3, 4, 5, 5, 69, 7, 80 );浮点数(P) ;浮点(浮点,int, n);最大值;污水池=(* P) (A, M);sumf = max (a, m);printf (仓=% 2f n,水池);printf (仓=% 2f n”,水池);)浮点(浮点),int n)( k;漂浮的;s= 0 ;对于(k = 0; k; n; k +)如果( );返回的;)3用不同的方法输出整型数组的各元素。包括stdio. h主 static int 5 = 第 3 ;int, p;对于(i = 0; i 5; i +)prin

34、tf ( 4D, 个我);putchar ( n);对于(i = 0; i 5; i +) printf ( “4D”,* (+我); putchar ( n);对于(p = a; p+ 5; p+)printf ( “4D” , * P);putchar ( n);=a;对于(i = 0; i 5; i +)printf ( “4D”,P 我);)4。用数组名常量指针输出维数组,并将数组中的最大元素及所在 行列号输出。#包括 “stdio. h主() i, j, m, n, max;a 3 4 = 1,2,3,4,5,6,7,8,9,10,11,12 );m=0; n=0;最大值;对于(i

35、= 0; i 3, + +) printf ( n);对于(j=0; jV4; j + +) printf ( % 5D”,* (* (+我)+ J);如果(最大* * (* + i) + j); max *(*(a+i) + j);m=i; n = j; printf ( n 麦克斯:一%2D % 2D = % d, m, n, ” 麦克斯);)用指向数组元素的指针输出维数组,并将数组中的最大元素及所在 行列号输出。#包括stdio. h i, j, m, n, max;a 3 4 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ;整数P;P=a 0 ;max

36、 = P;m=0; n=0;对于(i = 0; i 3; i +) printf ( n);对于(j=0; jV4; j + +) printf ( n);如果(max p) max=P;m=i; n = j;+; printf ( n 麦克斯:一%2D % 2D=% d, m, n, ” 麦克斯);用指向维数组的行指针输出维数组,并将数组中的最大元素及所 在行列号输出。#包括 “stdio. h”主 i, j, m, n, max;a 3 4 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ;int (* p) 4 ;=a;max=P;对于(i = 0; i

37、3; i +)printf ( n);对于(j = 0; j4; j + +);printf ( “ 5D”,* (* P + J);如果(最大 (*);m=i; n = j;)Hr )printf ( “n 麦克斯:一%2D % 2D = % d, m, n, ” 麦克斯);5。有若干本书,将其书名按字典顺序排列。printf ( narray b:);for (k = 0; k = 0; i )printf (% c * (a + i);puts ( n);void trans 108 _ 16 (char * p, long m, int base);int r;while (m 0)r

38、 = m% base;if (r 10) * p = r + 48;else * p = r + 55;m = m / base;P + +;)* p 二 、;)8.从键盘上输入个字符串与一个指定字符,将字符串中出现的指 定字符全部删除.# include stdio. hvoid prochar (char * str, char c) (char * p;for (p = str; * p! = 、0; p + +)if (* p! = c)* str + + = * p;* str = 、0;void main ()char str 80, char _ c;gets (str);scanf (% c”Char _ (c);Prochar (STR, char _ (c);Puts (STR);

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

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

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


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