[关闭]
@abnor 2017-03-22T11:34:17.000000Z 字数 4316 阅读 1898

计算机二级 —— C语言

梁策


前言: 应试

  1. 默认大家都学过C语言
  2. 局限于考试框架
    3.以下代码不是我写的!

操作题

对象\操作 运算 查找 排列
数字
字符
结构

数据可来源于主函数,也可来源于文件

题目(数值运算)

请编写函数fun(),它的功能是求Fibonacci数列中小于t的最大的一个
数,结果由函数返回。其中Fibonacci 数列F(n)的定义为 F(0)=0,F(1)=1,
F(n)=F(n-1)+F(n-2) 例如:t=1000 时 ,函数值为987。 注意:部分源程序给
出如下。 请勿改动主函数main 和其他函数中的任何内容,仅在函数fun的花括号
中填入所编写的若干语句。 试题程序:

  1. #include < conio.h >
  2. #include < math.h >
  3. #include < stdio.h >
  4. int fun(int t) //编写函数
  5. {…… }
  6. main()
  7. {
  8. int n;
  9. clrscr();
  10. n=1000;
  11. printf("n=%d, f=%d\n",n, fun(n));
  12. }
  13. /* 解析 */
  14. int fun(int t)
  15. {
  16. int a=1,b=1,c=0,i;
  17. do {
  18. c=a+b;
  19. a=b;
  20. b=c;
  21. }
  22. while (c< t);
  23. c=a;
  24. return c;}

Note:

  1. 根据题目阅读代码,main函数里的代码很多可以忽略
  2. 函数实现尽量不用递归
  3. 类型转换,传值传指

题目(字符运算)

请编写一个函数fun,它的功能是:将一个数字字符串转换为一个整数(不得调用C 语言提供的将字符串转换为整数的函数)。
例如,若输入字符串"-1234",则函数把它转换为整数值 -1234。
注意:部分源程序存在文件prog.c中。

  1. #include < stdio.h >
  2. #include < string.h >
  3. long fun(char *p)
  4. {……}
  5. main() /* 主函数 */
  6. { char s[6];
  7. long n;
  8. printf("Enter a string:\n") ;
  9. gets(s);
  10. n = fun(s);
  11. printf("%ld\n", n);
  12. }
  13. /*解析*/
  14. long fun(char *p)
  15. {
  16. long n=0;
  17. int flag=0;
  18. while(*p!='\0')
  19. {
  20. if(*p=='-')
  21. flag=1;
  22. else if (*p=='+')
  23. flag=0;
  24. else
  25. n=n*10+(*p-'0');
  26. p++;
  27. }
  28. if (flag==1) n=-n;
  29. return n;
  30. }

字符运算使用指针

题目(字符查找)

给定程序的功能是分别统计字符串中大写字母和小写字母的个数。
例如,给字符串ss输入:AaaaBBb123CCccccd,
则输出结果应为:upper = 5,lower = 9

  1. #include < stdio.h >
  2. void fun ( char *s, int *a, int *b )
  3. {
  4. while ( *s )
  5. { if ( *s >= 'A' && *s < = 'Z' )
  6. /**********found**********/
  7. ___1___ ;
  8. if ( *s >= 'a' && *s < = 'z' )
  9. /**********found**********/
  10. ___2___ ;
  11. s++;
  12. }
  13. }
  14. main( )
  15. {
  16. char s[100]; int upper = 0, lower = 0 ;
  17. printf( "\nPlease a string : " ); gets ( s );
  18. fun ( s, & upper, &lower );
  19. /**********found**********/
  20. printf( "\n upper = %d lower = %d\n", ___3___ );
  21. }

Note: 遍历+判断

题目(数值排序)

请编写函数fun,函数的功能是:移动一维数组中的内容;若数组中有n个整数,要求把下标从0到p(含p,p小于等于n-1)的数组元素平移到数组的最后。
例如,一维数组中的原始内容为:1,2,3,4,5,6,7,8,9,10;p的值为3。移动后,一维数组中的内容应为: 5,6,7,8,9,10,1,2,3,4。

  1. #include < stdio.h >#define N 80
  2. void fun(int *w, int p, int n)
  3. {……}
  4. main()
  5. {
  6. int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  7. int i,p,n=15;
  8. printf("The original data:\n");
  9. for(i=0; i< n; i++)printf("%3d",a[i]);
  10. printf("\n\nEnter p: ");scanf("%d",&p);
  11. fun(a,p,n);
  12. printf("\nThe data after moving:\n");
  13. for(i=0; i< n; i++)printf("%3d",a[i]);
  14. printf("\n\n");
  15. }
  16. /*解析*/
  17. void fun(int *w, int p, int n)
  18. {
  19. int x,j,ch;
  20. for(x=0;x<=p;x++)
  21. {
  22. ch=w[0];
  23. for(j=1;j<n;j++)
  24. {
  25. w[j-1]=w[j];
  26. }
  27. w[n-1]=ch; /*将0到p个数组元素逐一赋给数组w[n-1]*/
  28. }
  29. }

题目(字符排序)

下列给定程序中函数fun()的功能是:在字符串的最前端加入n 个*号,
形成新串,并且覆盖原串。
注意:字符串的长度最长允许79。
请改正函数fun()中的错误,使它能得出正确的结果。
注意:不要改动main 函数,不得增行或删行,也不得更改程序的结构。
试题程序:

  1. #include < stdio.h >
  2. #include < string.h >
  3. #include < conio.h>
  4. /**********************found***********************/
  5. void fun(char s[],int n)
  6. {
  7. char a[80],*p;
  8. int i;
  9. /**********************found***********************/
  10. s=p;
  11. for(i=0;i< n;i++) a[i]= '*';
  12. do
  13. {a[i]=*p;
  14. /**********************found***********************/
  15. i++;
  16. }while(*p);
  17. a[i]=0;
  18. strcpy(s,a);
  19. }
  20. main()
  21. {int n;char s[80];
  22. clrscr();
  23. printf("\nEnter a string: ");gets(s);
  24. printf("\nThe string\%s\n",s);
  25. printf("\nEnter n (number of*):"); scanf("%d",&n);
  26. fun(s,n);
  27. printf("\nThe string after inster:\%s\n",s);
  28. }

给定程序功能是用冒泡法对6个字符串进行排序。

  1. #include < stdio.h >
  2. #define MAXLINE 20
  3. fun ( char *pstr[6])
  4. {
  5. int i, j ;
  6. char *p ;
  7. for (i = 0 ; i < 5 ; i++ )
  8. {
  9. for (j = i + 1; j < 6; j++)
  10. {
  11. /**************found**************/
  12. if(strcmp(*(pstr+i),___1___) >0)
  13. {
  14. p = *(pstr + i) ;
  15. /**************found**************/
  16. pstr[i] = ___2___ ;
  17. /**************found**************/
  18. *(pstr + j) = ___3___ ;
  19. }
  20. }
  21. }
  22. }
  23. main( )
  24. {
  25. int i ;
  26. char *pstr[6], str[6][MAXLINE] ;
  27. for(i = 0; i < 6 ; i++) pstr[i] = str[i] ;
  28. printf( "\nEnter 6 string(1 string at each line): \n" ) ;
  29. for(i = 0 ; i < 6 ; i++) scanf("%s", pstr[i]) ;
  30. fun(pstr) ;
  31. printf("The strings after sorting:\n") ;
  32. for(i = 0 ; i < 6 ; i++) printf("%s\n", pstr[i]) ;
  33. }
  34. /*解析*/
  35. 1】*(pstr+j) 2pstr[j] 3p

题目(结构体+文件)

给定程序的功能是调用fun函数建立班级通讯录。通讯录中记录每位学生的编号、姓名和电话号码。
班级的人数和学生的信息从键盘读入,每个人的信息作为一个数据块写到名为myfile5.dat 的二进制文件中。

  1. #include < stdio.h >
  2. #include < stdlib.h >
  3. #define N 5
  4. typedef struct
  5. {
  6. int num;
  7. char name[10];
  8. char tel[10];
  9. }STYPE;
  10. void check();
  11. /**********found**********/
  12. int fun(___1___ *std)
  13. {
  14. /**********found**********/
  15. ___2___ *fp; int i;
  16. if((fp=fopen("myfile5.dat","wb"))==NULL)
  17. return(0);
  18. printf("\nOutput data to file !\n");
  19. for(i=0; i<N; i++)
  20. /**********found**********/
  21. fwrite(&std[i], sizeof(STYPE), 1, ___3___);
  22. fclose(fp);
  23. return (1);
  24. }
  25. main()
  26. {
  27. STYPE s[10]={{1,"aaaaa","111111"},
  28. {2,"bbbbb","222222"},
  29. {3,"ccccc","333333"},
  30. {4,"ddddd","444444"},
  31. {5,"eeeee","555555"}};
  32. int k;
  33. k=fun(s);
  34. if (k==1)
  35. {
  36. printf("Succeed!"); check();
  37. }
  38. else
  39. printf("Fail!");
  40. }
  41. void check()
  42. {
  43. FILE *fp; int i;
  44. STYPE s[10];
  45. if((fp=fopen("myfile5.dat","rb"))==NULL)
  46. {printf("Fail !!\n"); exit(0);}
  47. printf("\nRead file and output to screen :\n");
  48. printf("\n num name tel\n");
  49. for(i=0; i<N; i++)
  50. { fread(&s[i],sizeof(STYPE), 1, fp);
  51. printf("%6d %s %s\n", s[i].num, s[i].name,
  52. s[i].tel);
  53. }
  54. fclose(fp);
  55. }

链表简介

链表

链表

链表运算

创建

链表创建

插入

链表插入

删除

链表插入

结语

有代码的地方就有玄学

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注