模拟程序设计题(附参答案).docVIP

  • 4
  • 0
  • 约4.13千字
  • 约 5页
  • 2016-11-28 发布于贵州
  • 举报
模拟程序设计题(附参答案)

1、请编一个函数fun(char *s),函数的功能是把字符串中的内容逆置。例如:字符串中原有的内容为:abcdefg,则调用该函数后,串中的内容为:gfedcba。 注意: 部分源程序存在考生文件夹下的文件prog.c中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 #include string.h #include conio.h #include stdio.h #define N 81 fun(char *s) { int i, j; char t; for (i=0,j=strlen(s)-1; ij; i++,j--) { t=s[i]; s[i]=s[j]; s[j]=t; } } main() { char a[N]; printf(Enter a string: );gets(a); printf(The original string is: );puts(a); fun(a); printf(\n); printf(The string after modified: ); puts(a); } 2、请编一个函数fun(char *s),函数的功能是把字符串中所有的字符前移一个位置,串中的第一个字符移到最后。 例如:原有的字符串为:Mn.123xyZ,则调用该函数后,串中的内容为:n.123xyZM。 注意:部分源程序存在考生文件夹下的文件prog16.c中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 #include conio.h #include stdio.h #include stdlib.h #define N 81 fun(char *s) { int i; char t=s[0]; for (i=0; s[i]!=\0; i++) s[i]=s[i+1]; s[--i]=t; } main() { char a[N]; system(cls); printf(Enter a string: );gets(a); fun(a); printf(The string after modified: ); puts(a); } 3、请编写函数fun,它的功能是:求出ss所指字符串中指定字符的个数,并返回此值。 例如,若输入字符串:123412132,输入字符为:1,则输出:3。 注意:部分源程序存在考生文件夹下的文件prog21.c中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 #include conio.h #include stdio.h #include stdlib.h #define M 81 int fun(char *ss,char c) { int i,j=0; for(i=0;ss[i]!=\0;i++) if(ss[i]==c) j++; return j; } main() { char a[M],ch; system(cls); printf(\nPlease enter a string:);gets(a); printf(\nPlease enter a char:);ch=getchar(); printf(\nThe number of the char is:%d\n,fun(a,ch)); } 4、编写函数fun,函数的功能是:根据以下公式计算s,计算结果作为函数值返回;n通过形参传入。 1 1 1 S = 1 + ── + ─── + …… + ────── 1+2   1+2+3   1+2+3+…+n 例如:若n的值为11时,则函数值为:1.833333 注意:部分源程序存在考生文件夹下的文件prog42.c中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 #include conio.h #include stdio.h #include string.h #include stdlib.h float fun(int n) { int i; float s=0,item=0;

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档