1
0

程序设计题1-20.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. 程序设计题(一)
  2. 请编写函数fun,它的功能是计算下列级数和,和值由函数值返回。
  3. S=1+x+x^2/2!+x^3/3!+…+x^n/n!
  4. 例如,当n=10,x=0.3时,函数值为1.349859。
  5. 注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  6. #include<conio.h>
  7. #include<stdio.h>
  8. #include<math.h>
  9. #include<stdlib.h>
  10. double fun(double x, int n)
  11. {
  12. }
  13. void main()
  14. {
  15. FILE *wf;
  16. system("CLS");
  17. printf("%f ",fun(0.3,10));
  18. /******************************/
  19. wf=fopen("out.dat","w");
  20. fprintf(wf,"%f",fun(0.3,10));
  21. fclose(wf);
  22. /*****************************/
  23. }
  24. 程序设计题(二)
  25. 编写函数fun,其功能是:求出1~1000之间能被7或11整除,但不能同时被7和11整除的所有整数,并将其放在a所指的数组中,通过n返回这些数的个数。
  26. 注意:请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  27. #include <stdio.h>
  28. void fun (int *a, int *n)
  29. {
  30. }
  31. main( )
  32. { int aa[1000], n, k ;
  33. void NONO ( );
  34. fun ( aa, &n ) ;
  35. for ( k = 0 ; k < n ; k++ )
  36. if((k + 1) % 10 == 0) printf("\n") ;
  37. else printf("%5d", aa[k]) ;
  38. NONO( );
  39. }
  40. void NONO ( )
  41. {/* 本函数用于打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/
  42. int aa[1000], n, k ;
  43. FILE *fp ;
  44. fp = fopen("out.dat","w") ;
  45. fun ( aa, &n ) ;
  46. for ( k = 0 ; k < n ; k++ )
  47. if((k + 1) % 10 == 0) fprintf(fp, "\n") ;
  48. else fprintf(fp, "%5d", aa[k]) ;
  49. fclose(fp) ;
  50. }
  51. 程序设计题(三)
  52. 请编写函数fun,该函数的功能是:将s所指字符串中ASCII码值为偶数的字符删除,串中剩余字符形成一个新串放在t所指的数组中。
  53. 例如,若s所指字符串中的内容为"ABCDEFG12345",其中字符B的ASCII码值为偶数,字符2的ASCII码值为偶数,都应当删除,其他依此类推。最后t所指的数组中的内容应是"ACEG135"。
  54. 注意:请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  55. #include<stdio.h>
  56. #include<string.h>
  57. #include<conio.h>
  58. #include<stdlib.h>
  59. void fun(char *s, char t[])
  60. {
  61. }
  62. void main()
  63. {
  64. FILE *wf;
  65. char s[100],t[100];
  66. system("CLS");
  67. printf("\nPlease enter string S: ");
  68. scanf("%s",s);
  69. fun(s,t);
  70. printf("\nThe result is :%s\n",t);
  71. /******************************/
  72. wf=fopen("out.dat","w");
  73. fun("ABCDEFG12345",t);
  74. fprintf(wf,"%s",t);
  75. fclose(wf);
  76. /*****************************/
  77. }
  78. 程序设计题(四)
  79. 请编写函数void fun (int x, int pp[], int *n),它的功能是:求出能整除x且不是偶数的各整数,并按从小到大的顺序放在pp所指的数组中,这些除数的个数通过形参n返回。
  80. 例如,若x中的值为30,则有4个数符合要求,它们是1、3、5、15。
  81. 注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  82. #include <conio.h>
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. void fun (int x, int pp[], int *n)
  86. {
  87. }
  88. void main ()
  89. {
  90. FILE *wf;
  91. int x,aa[1000], n, i ;
  92. system("CLS");
  93. printf("\nPlease enter an integer number : \n ") ;
  94. scanf ("%d", &x) ;
  95. fun (x, aa, &n) ;
  96. for (i=0 ; i<n ; i++)
  97. printf ("%d ", aa [i]);
  98. printf ("\n ") ;
  99. /******************************/
  100. wf=fopen("out.dat","w");
  101. fun (30, aa, &n) ;
  102. for (i=0 ; i<n ; i++)
  103. fprintf (wf,"%d ", aa [i]);
  104. fclose(wf);
  105. /*****************************/
  106. }
  107. 程序设计题(五)
  108. 请编函数fun,其功能是将一个数字字符串转换成与其面值相同的长整型整数。可调用strlen函数求字符串的长度。例如:在键盘输入字符串2345210,函数返回长整型数2345210。
  109. 注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun指定的部位填入所编写的若干语句。
  110. #include <stdio.h>
  111. #include <string.h>
  112. void NONO();
  113. long fun( char *s )
  114. {
  115. }
  116. main()
  117. { char s[10]; long r;
  118. printf("请输入一个长度不超过9个字符的数字字符串 : "); gets(s);
  119. r = fun( s );
  120. printf(" r = %ld\n" , r );
  121. NONO();
  122. }
  123. void NONO()
  124. {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  125. FILE *fp, *wf ;
  126. int i; long r;
  127. char s[10], *p;
  128. fp = fopen("in.dat","r") ;
  129. wf = fopen("out.dat","w") ;
  130. for(i = 0 ; i < 10 ; i++) {
  131. fgets(s, 10, fp);
  132. p = strchr(s, '\n');
  133. if(p) *p = 0;
  134. r = fun(s);
  135. fprintf(wf, "%ld\n", r);
  136. }
  137. fclose(fp) ;
  138. fclose(wf) ;
  139. }
  140. 程序设计题(六)
  141. 假定输入的字符串中只包含字母和*号。请编写函数fun,其功能是:除了尾部的*号之外,将字符中的其它的*号全部删除。形参p已指向字符串中最后的一个字母。在编写函数时,不得使用C语言提供的字符串函数。
  142. 例如,字符串中的内容为****A*BC*DEF*G*******,删除后,字符串中的内容应当是ABCDEFG*******。
  143. 注意:请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  144. #include <stdio.h>
  145. void fun( char *a, char *p )
  146. {
  147. }
  148. main()
  149. { char s[81],*t;
  150. void NONO ( );
  151. printf("Enter a string:\n");gets(s);
  152. t=s;
  153. while(*t)t++;
  154. t--;
  155. while(*t=='*')t--;
  156. fun( s , t );
  157. printf("The string after deleted:\n");puts(s);
  158. NONO();
  159. }
  160. void NONO()
  161. {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  162. FILE *in, *out ;
  163. int i ; char s[81],*t ;
  164. in = fopen("in.dat","r") ;
  165. out = fopen("out.dat","w") ;
  166. for(i = 0 ; i < 10 ; i++) {
  167. fscanf(in, "%s", s) ;
  168. t=s;
  169. while(*t)t++;
  170. t--;
  171. while(*t=='*')t--;
  172. fun(s,t) ;
  173. fprintf(out, "%s\n", s) ;
  174. }
  175. fclose(in) ;
  176. fclose(out) ;
  177. }
  178. 程序设计题(七)
  179. 编写函数void fun(char *tt,int pp[]),统计在tt所指的字符串中'a'到'z'26个小写字母各自出现的次数,并依次放在pp所指的数组中。
  180. 例如,当输入字符串abcdefgabcdeabc后,程序的输出结果应该是:
  181. 3 3 3 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  182. 注意:请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  183. #include <stdio.h>
  184. #include <string.h>
  185. void fun(char *tt, int pp[])
  186. {
  187. }
  188. main( )
  189. { char aa[1000] ;
  190. int bb[26], k ;
  191. void NONO ( );
  192. printf( "\nPlease enter a char string:" ) ; scanf("%s", aa) ;
  193. fun(aa, bb ) ;
  194. for ( k = 0 ; k < 26 ; k++ ) printf ("%d ", bb[k]) ;
  195. printf( "\n" ) ;
  196. NONO ( ) ;
  197. }
  198. void NONO ( )
  199. {/* 本函数用于打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/
  200. char aa[1000] ;
  201. int bb[26], k, i ;
  202. FILE *rf, *wf ;
  203. rf = fopen("in.dat","r") ;
  204. wf = fopen("out.dat","w") ;
  205. for(i = 0 ; i < 10 ; i++) {
  206. fscanf(rf, "%s", aa) ;
  207. fun(aa, bb) ;
  208. for ( k = 0 ; k < 26 ; k++ ) fprintf (wf, "%d ", bb[k]) ;
  209. fprintf(wf, "\n" ) ;
  210. }
  211. fclose(rf) ;
  212. fclose(wf) ;
  213. }
  214. 程序设计题(八)
  215. 下列程序定义了N×N的二维数组,并在主函数中自动赋值。请编写函数fun(int a[][N]),该函数的功能是:将数组左下半三角元素中的值全部置成0。例如a数组中的值为:
  216. 1 9 7
  217. 2 3 8
  218. 4 5 6
  219. 则返回主程序后a数组中的值应为:
  220. 0 9 7
  221. 0 0 8
  222. 0 0 0
  223. 注意:请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  224. #include <conio.h>
  225. #include <stdio.h>
  226. #include <stdlib.h>
  227. #define N 5
  228. void fun (int a[][N])
  229. {
  230. }
  231. void main()
  232. {
  233. FILE *wf;
  234. int a[N][N],i,j;
  235. int b[N][N]={1,9,7,2,4,2,3,8,1,2,4,5,6,7,5,4,0,6,8,0,2,7,1,6,4};
  236. system("CLS");
  237. printf("*****The array*****\n");
  238. for(i=0;i<N;i++) /*产生一个随机的5*5矩阵*/
  239. { for(j=0;j<N;j++)
  240. {a[i][j]=rand()%10;
  241. printf("%4d", a[i][j]);
  242. }
  243. printf("\n");
  244. }
  245. fun(a);
  246. printf("THE RESULT\n");
  247. for(i=0;i<N;i++)
  248. { for(j=0;j<N;j++)
  249. printf("%4d",a[i][j]);
  250. printf("\n");
  251. }
  252. /******************************/
  253. wf=fopen("out.dat","w");
  254. fun(b);
  255. for(i=0;i<N;i++)
  256. { for(j=0;j<N;j++)
  257. fprintf(wf,"%4d",b[i][j]);
  258. fprintf(wf,"\n");
  259. }
  260. fclose(wf);
  261. /*****************************/
  262. }
  263. 程序设计题(九)
  264. 请编写函数fun,该函数的功能是:删除一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。
  265. 例如,若一维数组中的数据是:
  266. 2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10
  267. 删除后,数组中的内容应该是:
  268. 2 3 4 5 6 7 8 9 10。
  269. 注意:请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  270. #include <stdio.h>
  271. #define N 80
  272. int fun(int a[], int n)
  273. {
  274. }
  275. void main()
  276. {
  277. FILE *wf;
  278. int a[N]={ 2,2,2,3,4,4,5,6,6,6,6,7,7,8,9,9,10,10,10,10}, i, n=20;
  279. printf("The original data :\n");
  280. for(i=0; i<n; i++)
  281. printf("%3d",a[i]);
  282. n=fun(a,n);
  283. printf("\nThe data after deleted :\n");
  284. for(i=0; i<n; i++)
  285. printf("%3d",a[i]);
  286. printf("\n");
  287. /******************************/
  288. wf=fopen("out.dat","w");
  289. for(i=0; i<n; i++)
  290. fprintf(wf,"%3d",a[i]);
  291. fclose(wf);
  292. /*****************************/
  293. }
  294. 程序设计题(十)
  295. 函数fun的功能是:将s所指字符串中除下标为偶数同时ASCII码值也为偶数的字符外,其余的全部删除;字符串中剩余字符所形成的新串放在t所指的数组中。
  296. 例如,若s所指字符串中的内容为"ABCDEFG123456",其中字符A的ASCII码值为奇数,因此应当删除;字符B的ASCII码值为偶数,但在数组中的下标为奇数,因此也应当删除;字符2的ASCII码值为偶数,在数组中的下标也为偶数,因此不应当删除,其他依此类推。最后t所指的数组中的内容应是"246"。
  297. 注意:请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  298. #include <stdio.h>
  299. #include <string.h>
  300. void fun(char *s, char t[])
  301. {
  302. }
  303. main()
  304. {
  305. char s[100], t[100];
  306. void NONO ( );
  307. printf("\nPlease enter string S:"); scanf("%s", s);
  308. fun(s, t);
  309. printf("\nThe result is: %s\n", t);
  310. NONO();
  311. }
  312. void NONO ( )
  313. {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  314. char s[100], t[100] ;
  315. FILE *rf, *wf ;
  316. int i ;
  317. rf = fopen("in.dat","r") ;
  318. wf = fopen("out.dat","w") ;
  319. for(i = 0 ; i < 10 ; i++) {
  320. fscanf(rf, "%s", s) ;
  321. fun(s, t) ;
  322. fprintf(wf, "%s\n", t) ;
  323. }
  324. fclose(rf) ;
  325. fclose(wf) ;
  326. }
  327. 程序设计题(十一)
  328. 请编写函数fun,其功能是:找出2×M整型二维数组中最大元素的值,并将此值返回调用函数。
  329. 注意:请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  330. #include <stdio.h>
  331. #define M 4
  332. int fun (int a[][M])
  333. {
  334. }
  335. main( )
  336. { int arr[2][M]={5,8,3,45,76,-4,12,82} ;void NONO ();
  337. printf("max =%d\n", fun(arr)) ;
  338. NONO( ) ;
  339. }
  340. void NONO ()
  341. {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  342. FILE *wf ;
  343. int arr[][M]={5,8,3,90,76,-4,12,82} ;
  344. wf = fopen("out.dat","w") ;
  345. fprintf(wf, "max=%d\n", fun(arr)) ;
  346. fclose(wf) ;
  347. }
  348. 程序设计题(十二)
  349. 请编写一个函数fun,它的功能是:将ss所指字符串中所有下标为奇数位置的字母转换为大写(若该位置上不是字母,则不转换)。
  350. 例如,若输入"abc4Efg",则应输出"aBc4EFg"。
  351. 注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  352. #include<conio.h>
  353. #include<stdio.h>
  354. #include<string.h>
  355. #include<stdlib.h>
  356. void fun(char *ss)
  357. {
  358. }
  359. void main()
  360. {
  361. FILE *wf;
  362. char tt[81],s[81]="abc4Efg";
  363. system("CLS");
  364. printf("\nPlease enter an string within 80 characters:\n");
  365. gets(tt);
  366. printf("\nAfter changing, the string\n %s",tt);
  367. fun(tt);
  368. printf("\nbecomes\n %s\n",tt);
  369. /******************************/
  370. wf=fopen("out.dat","w");
  371. fun(s);
  372. fprintf (wf,"%s",s);
  373. fclose(wf);
  374. /*****************************/
  375. }
  376. 程序设计题(十三)
  377. 请编写函数fun,该函数的功能是:将M行N列的二维数组中的字符数据,按列的顺序依次放到一个字符串中。
  378. 例如,若二维数组中的数据为
  379. W W W W
  380. S S S S
  381. H H H H
  382. 则字符串中的内容应是:WSHWSHWSHWSH。
  383. 注意:请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  384. #include<stdio.h>
  385. #define M 3
  386. #define N 4
  387. void fun(char (*s)[N],char *b)
  388. {
  389. }
  390. void main()
  391. {
  392. FILE *wf;
  393. char a[100],w[M][N]={{ 'W', 'W', 'W', 'W'},{'S', 'S', 'S', 'S'},{'H', 'H', 'H', 'H'}};
  394. int i,j;
  395. printf("The matrix:\n");
  396. for(i=0;i<M;i++)
  397. { for(j=0;j<N;j++)
  398. printf("%3c",w[i][j]);
  399. printf("\n");
  400. }
  401. fun(w,a);
  402. printf("The A string:\n");
  403. puts(a);
  404. printf("\n ");
  405. /******************************/
  406. wf=fopen("out.dat","w");
  407. fprintf(wf,"%s",a);
  408. fclose(wf);
  409. /*****************************/
  410. }
  411. 程序设计题(十四)
  412. 已知学生的记录由学号和学习成绩构成,N名学生的数据已存入a结构体数组中。请编写函数fun,该函数的功能是:找出成绩最高的学生记录,通过形参返回主函数(规定只有一个最高分)。已给出函数的首部,请完成该函数。
  413. 注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  414. #include<stdio.h>
  415. #include<string.h>
  416. #include<conio.h>
  417. #include<stdlib.h>
  418. #define N 10
  419. typedef struct ss /*定义结构体*/
  420. { char num[10];
  421. int s;
  422. } STU;
  423. fun(STU a[], STU *s)
  424. {
  425. }
  426. void main()
  427. {
  428. FILE *wf;
  429. STU a[N]={{ "A01",81},{ "A02",89},{ "A03",66},{ "A04",87},{ "A05",77},
  430. { "A06",90},{ "A07",79},{ "A08",61},{ "A09",80},{ "A10",71}},m;
  431. int i;
  432. system("CLS");
  433. printf("*****The original data*****");
  434. for(i=0;i<N;i++)
  435. printf("No=%s Mark=%d\n", a[i].num,a[i].s);
  436. fun(a,&m);
  437. printf("*****THE RESULT*****\n");
  438. printf("The top :%s, %d\n",m.num,m.s);
  439. /******************************/
  440. wf=fopen("out.dat","w");
  441. fprintf(wf,"%s,%d",m.num,m.s);
  442. fclose(wf);
  443. /*****************************/
  444. }
  445. 程序设计题(十五)
  446. 请编写函数fun,其功能是:判断形参n中的正整数是几位数(输入数据的位数不超过4位),并将结果通过函数值返回。例如:若输入的数据为 123,则输出结果为:输入的数字是3位。
  447. 注意:请勿改动主函数main和其他函数中的任何
  448. 内容,仅在函数fun的花括号中填入所编写的若干语句。
  449. #include <stdio.h>
  450. void NONO();
  451. int fun(int n)
  452. {
  453. }
  454. main()
  455. { int n, place ;
  456. do{
  457. printf("请输入一个4位以内的正整数: ");
  458. scanf("%d", &n);
  459. } while (n<0 || n>9999);
  460. place = fun(n);
  461. printf( "输入的数字是%d位\n", place );
  462. NONO();
  463. }
  464. void NONO()
  465. {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  466. FILE *fp, *wf ;
  467. int i, n, place ;
  468. fp = fopen("in.dat","r") ;
  469. wf = fopen("out.dat","w") ;
  470. for( i=0; i<10; i++ )
  471. {
  472. fscanf(fp,"%d ", &n);
  473. place = fun(n);
  474. fprintf(wf, "%d\n", place);
  475. }
  476. fclose(fp) ;
  477. fclose(wf) ;
  478. }
  479. 程序设计题(十六)
  480. 请编写函数fun,其功能是:计算并输出3~n之间所有素数的平方根之和。
  481. 例如,若主函数从键盘给n输入100后,则输出为sum=148.874270。
  482. 注意:n的值要大于2但不大于100。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  483. 试题程序:
  484. #include <stdio.h>
  485. #include <math.h>
  486. double fun(int n)
  487. {
  488. }
  489. main()
  490. {
  491. int n;
  492. double sum;
  493. FILE *out;
  494. printf("Input n=");
  495. scanf("%d",&n);
  496. sum=fun(n);
  497. printf("\nsum=%f\n",sum);
  498. /******************************/
  499. out=fopen("out.dat","w");
  500. fprintf(out,"%f\n",fun(180));
  501. fclose(out);
  502. /******************************/
  503. }
  504. 程序设计题(十七)
  505. 编写函数fun,其功能是:将两个两位数的正整数a、b合并成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的十位和千位上,b数的十位和个位数依次放在c数的百位和个位上。
  506. 例如,当a=45,b=12时,调用该函数后,c=5142。
  507. 注意:数据文件IN.DAT中的数据不得修改。
  508. 试题程序:
  509. #include <stdio.h>
  510. void fun(int a,int b,long *c)
  511. {
  512. }
  513. main()
  514. { int a,b; long c;
  515. void NONO( );
  516. printf("Input a, b:");
  517. scanf("%d%d", &a, &b);
  518. fun(a,b,&c);
  519. printf("The result is: %d\n", c);
  520. NONO();
  521. }
  522. void NONO( )
  523. {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。*/
  524. FILE *rf, *wf ;
  525. int i, a,b ; long c ;
  526. rf=fopen("in.dat", "r");
  527. wf=fopen("out.dat","w");
  528. for(i=0;i<10;i++)
  529. {
  530. fscanf(rf, "%d,%d",&a,&b);
  531. fun(a, b, &c) ;
  532. fprintf(wf,"a=%d,b=%d,c=%ld\n",a,b,c);
  533. }
  534. fclose(rf);
  535. fclose(wf);
  536. }
  537. 程序设计题(十八)
  538. 请编写函数fun,该函数的功能是:判断字符串是否为回文,若是,则函数返回1,主函数中输出"YES",否则返回0,主函数中输出"NO"。回文是指顺读和倒读都一样的字符串。
  539. 例如,字符串LEVEL是回文,而字符串123312就不是回文。
  540. 注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  541. 试题程序:
  542. #include <stdio.h>
  543. #define N 80
  544. int fun(char *str)
  545. {
  546. }
  547. main()
  548. {
  549. char s[N];
  550. FILE *out;
  551. char *test[]={"1234321","123421","123321","abcdCBA"};
  552. int i;
  553. printf("Enter a string : ");
  554. gets(s);
  555. printf("\n");
  556. puts(s);
  557. if(fun(s))
  558. printf("YES\n");
  559. else
  560. printf("NO\n");
  561. /************************************/
  562. out=fopen("out.dat","w");
  563. for(i=0;i<4;i++)
  564. if(fun(test[i]))
  565. fprintf(out,"YES\n");
  566. else
  567. fprintf(out,"NO\n");
  568. fclose(out);
  569. /************************************/
  570. }
  571. 程序设计题(十九)
  572. 请编写函数fun,对长度为7个字符的字符串,除首、尾字符外,将其余5个字符按ASCII码降序排列。例如,原来的字符串为"CEAedca",排序后输出为"CedcEAa"。
  573. 注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
  574. 试题程序:
  575. #include <stdio.h>
  576. #include <ctype.h>
  577. #include <string.h>
  578. void fun(char *s,int num)
  579. {
  580. }
  581. main()
  582. {
  583. void NONO( );
  584. char s[10];
  585. printf("输入7个字符的字符串:");
  586. gets(s);
  587. fun(s,7);
  588. printf("\n%s",s);
  589. NONO();
  590. }
  591. void NONO()
  592. {
  593. /* 请在此函数内打开文件,输入测试数据,调用 fun 函数,
  594. 输出数据,关闭文件。 */
  595. char s[10] ;
  596. FILE *rf, *wf ;
  597. int i = 0 ;
  598. rf=fopen("in.dat","r") ;
  599. wf=fopen("out.dat","w") ;
  600. while(i<10)
  601. {
  602. fgets(s,10,rf) ;
  603. s[7] = 0 ;
  604. fun(s,7);
  605. fprintf(wf, "%s\n", s) ;
  606. i++ ;
  607. }
  608. fclose(rf) ;
  609. fclose(wf) ;
  610. }
  611. 程序设计题(二十)
  612. 请编写函数fun,其功能是:将一组得分中,去掉一个最高分和一个最低分,然后求平均值,并通过函数返回。函数形参a指向存放得分的数组,形参n中存放得分个数(n>2)。
  613. 例如,若输入9.9 8.5 7.6 8.5 9.3 9.5 8.9 7.8 8.6 8.4十个得分,则输出结果为:8.687500。
  614. 注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  615. 试题程序:
  616. #include <stdio.h>
  617. void NONO();
  618. double fun(double a[ ],int n)
  619. {
  620. }
  621. main()
  622. { double b[10], r; int i;
  623. printf("输入10个数放入b数组中 : ");
  624. for (i=0; i<10; i++)
  625. scanf("%lf",&b[i]);
  626. printf("输入的10个数是 : ");
  627. for (i=0; i<10; i++)
  628. printf("%4.1lf ",b[i]);
  629. printf("\n");
  630. r=fun(b,10);
  631. printf("去掉最高分和最低分后的平均分 : %f\n", r );
  632. NONO();
  633. }
  634. void NONO()
  635. {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  636. FILE *fp, *wf;
  637. int i, j ;
  638. double b[10], r;
  639. fp = fopen("in.dat","r");
  640. wf = fopen("out.dat","w");
  641. for(i = 0 ; i < 10 ; i++)
  642. {
  643. for(j = 0 ; j < 10 ; j++)
  644. {
  645. fscanf(fp,"%lf ",&b[j]);
  646. }
  647. r = fun(b, 10);
  648. fprintf(wf, "%f\n", r);
  649. }
  650. fclose(fp);
  651. fclose(wf);
  652. }