1、1、请编写一个函数 fun,它的功能是:将一个数字字符串转换为一个整数(不得 调用 C语言提供的将字符串转换为整数的函数)。例如,若输入字符串“-1234“,则 函数把它转换为整数值 -1234。函数 fun中给出的语句仅供参考。 注意: 部分源程序存在文件PROG1.C文件中。 请勿改动主函数 main和其它函数中的任何内容, 仅在函数fun的花括号中填 入你编写的若干语句。 给定源程序: #include #include long fun ( char *p) /* 以下代码仅供参考 */ int i, len, t; /* len为串长,t为正负标识 */ long x=0; len=
2、strlen(p); if(p0=-) t=-1; len-; p+; else t=1; /* 以下完成数字字符串转换为一个数字 */ return x*t; main() /* 主函数 */ char s6; long n; printf(“Enter a string:n“) ; gets(s); n = fun(s); printf(“%ldn“,n); NONO ( ); NONO ( ) /* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf ; int i ; char s20 ; long n ; fp = fopen(“c:tes
3、tin.dat“,“r“) ; wf = fopen(“c:testout.dat“,“w“) ; for(i = 0 ; i #include long fun ( char *p) /* 以下代码仅供参考 */ int i, len, t; /* len为串长,t为正负标识 */ long x=0; len=strlen(p); if(p0=-) t=-1; len-; p+; else t=1; /* 以下完成数字字符串转换为一个数字 */ while(*p) x = x*10-48+(*p+); return x*t; main() /* 主函数 */ char s6; long n;
4、 printf(“Enter a string:n“) ; gets(s); n = fun(s); printf(“%ldn“,n); NONO ( ); NONO ( ) /* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf ; int i ; char s20 ; long n ; fp = fopen(“c:testin.dat“,“r“) ; wf = fopen(“c:testout.dat“,“w“) ; for(i = 0 ; i #define M 3 #define N 4 void fun(char sN, char *b)
5、 int i,j,n=0; for(i=0; i #include #define N 80 int fun( char *s) main() char lineN; int num=0; printf(“Enter a string :n“); gets(line); num=fun( line ); printf(“The number of word is : %dnn“,num); NONO(); 解题思路: 本题是统计字符串中的单词数。1. 利用 while循环语句和指针变量,当字符为空格时,则单词数 k加 1。 2. 循环结束返回 k。 参考答案: int fun( char *s
6、) int k = 1 ; while(*s) if(*s = ) k+ ; s+ ; return k ; 4、请编写函数 fun,函数的功能是:统计各年龄段的人数。N 个年龄通过调用随机 函数获得,并放在主函数的 age数组中;要求函数把 0至 9岁年龄段的人数放在 d0 中,把 10至 19岁年龄段的人数放在 d1中,把 20至 29岁年龄段的人数放在 d2中, 其余依此类推, 把 100岁 (含100)以上年龄的人数都放在d10中。结果在主函数中输出。 注意:部分源程序在文件 PROG1.C中。 请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入 你编写的若
7、干语句。 给定源程序: #include #define N 50 #define M 11 void fun( int *a, int *b) double rnd() static t=29,c=217,m=1024,r=0; r=(r*t+c)%m; return(double)r/m); main() int ageN, i, dM; for(i=0; i 10) bM - 1+ ; else bj+ ; double rnd() static t=29,c=217,m=1024,r=0; r=(r*t+c)%m; return(double)r/m); 5、请编写函数 fun, 函数
8、的功能是: 删去一维数组中所有相同的数, 使之只剩一 个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。 例如, 一维数组中的数据是: 2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10。 删除后,数组中的内容应该是: 2 3 4 5 6 7 8 9 10。 注意:部分源程序在文件 PROG1.C中。 请勿改动主函数 main和其它函数中的任何内容, 仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include #define N 80 int fun(int a, int n) main() int aN=2,2,2,3
9、,4,4,5,6,6,6,6,7,7,8,9,9,10,10,10,10,i,n=20; printf(“The original data :n“); for(i=0; i #include #define N 80 void fun1(char *w) /* 本函数的功能是将字符串中字符循环左移一个位置 */ int i; char t; t=w0; for(i=0;i unsigned fun ( unsigned w ) main( ) unsigned x; printf ( “Enter a unsigned integer number : “ ); scanf ( “%u“,
10、printf ( “The original data is : %un“, x ); if ( x 10000) w %= 10000 ; else if(w1000) w %= 1000 ; else if(w100) w %= 100 ; else if(w10) w %=10 ; return w ; 8、程序定义了 NN的二维数组,并在主函数中自动赋值。请编写函数 fun(int aN, int n),函数的功能是:使数组左下三角元素中的值乘以 n 。 例如:若 n的值为 3,a 数组中的值为 | 1 9 7 | | 3 9 7 | a = | 2 3 8 | 则返回主程序后 a数组
11、中的值应为 | 6 9 8 | | 4 5 6 | | 12 15 18| 注意: 部分源程序存在文件PROG1.C中。 请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include #include #define N 5 int fun ( int aN, int n ) main ( ) int aNN, n, i, j; printf(“* The array *n“); for ( i =0; i=3 ); printf(“n = %4dn“,n); fun ( a, n ); printf (“* THE RES
12、ULT *n“); for ( i =0; i #define N 80 void fun(int *w, int p, int n) main() int aN=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; int i,p,n=15; printf(“The original data:n“); for(i=0; i #include int fun(char *str) main() char str81; int n; printf(“Input a string:“) ; gets(str); puts(str); fun(str); printf(“* s
13、tr: %sn“,str); NONO(); 解题思路: 本题是考察考生怎样利用字符串指针来删除字符串的空格字符。在函数开始处把指针 p先指向字符串 str以及位置变量 i置 0,然后采用 while循环和字符串指针 p来处理的,每循环一次来判断指针 p所指的字符是否为空格,如果不是空格,则把该字符写到 str字符串中,位置由i来控制,如果是空格,则不处理,继续取下一字符,直至字符串结束为止,最后再把字符串结束符写到位置为 i的 str上,再通过形参 str返回。 参考答案: int fun(char *str) char *p = str ; int i = 0 ; while(*p) if
14、(*p != ) stri+ = *p ; p+ ; stri = 0 ; 11、请编写函数 fun, 函数的功能是: 将放在字符串数组中的M个字符串(每串的长 度不超过 N), 按顺序合并组成一个新的字符串。函数 fun中给出的语句仅供参考。 例如, 字符串数组中的 M个字符串为 AAAA BBBBBBB CC 则合并后的字符串的内容应是: AAAABBBBBBBCC。 提示:strcat(a,b)的功能是将字符串 b复制到字符串 a的串尾上,成为一个新 串。 注意:部分源程序在文件 PROG1.C中。 请勿改动主函数 main和其它函数中的任何内容, 仅在函数 fun的花括号中填入 你编写
15、的若干语句。 给定源程序: #include #define M 3 #define N 20 void fun(char aMN, char *b) /* 以下代码仅供参考 */ int i; *b=0; main() char wMN=“AAAA“,“BBBBBBB“,“CC“, a100; int i ; printf(“The string:n“); for(i=0; i #define M 3 #define N 20 void fun(char aMN, char *b) /* 以下代码仅供参考 */ int i; *b=0; for(i = 0 ; i void fun(int
16、s10, int b, int *n, int mm, int nn) /* 以下代码仅供参考 */ int i,j,np=0; /* np用作 b数组下标 */ *n=np; main() int w1010=33,33,33,33,44,44,44,44,55,55,55,55,i,j; int a100=0, n=0; printf(“The matrix:n“); for(i=0; i void fun(int (*s)10, int *b, int *n, int mm, int nn) main() int w1010 = 33,33,33,33,44,44,44,44,55,55
17、,55,55,i,j ; int a100 = 0, n = 0 ; printf(“The matrix:n“) ; for(i = 0 ; i #define N 80 int fun(char *str) main() char sN ; printf(“Enter a string: “) ; gets(s) ; printf(“nn“) ; puts(s) ; if(fun(s) printf(“ YESn“) ; else printf(“ NOn“) ; NONO() ; 解题思路: 本题是考察如何判断一个字符串是回文字符串,回文是指顺读和倒读都一样的字符串。可以利用 for循环
18、语句来判断,如果前后不一致,则不是回文字符串。 参考答案: int fun(char *str) int i, j = strlen(str) ; for(i = 0 ; i #include #define N 8 struct slist double s; struct slist *next; ; typedef struct slist STREC; double fun( STREC *h ) STREC * creat( double *s) STREC *h,*p,*q; int i=0; h=p=(STREC*)malloc(sizeof(STREC);p-s=0; whil
19、e(is=si; i+; p-next=q; p=q; p-next=0; return h; outlist( STREC *h) STREC *p; p=h-next; printf(“head“); do printf(“-%2.0f“,p-s);p=p-next; while(p!=0); printf(“nn“); main() double sN=85,76,69,85,91,72,64,87, max; STREC *h; h=creat( s ); outlist(h); max=fun( h ); printf(“max=%6.1fn“,max); NONO(); 解题思路:
20、 本题是考察如何从链表中求出学生的最高分。 我们给出的程序是利用 while循环语句以及临时结构指针 p变量来求出最高分。 1. 将链表中的第 1个值赋给变量 max。 2. 将链表指针 p的初始位置指向 h的 next指针(h-next) 。 3. 判断 p指针是否结束,如果结束,则返回 max,否则做下一步。 4. 判断 max是否小于 p-s,如果小于,则 max取 p-s,否则不替换。 5. 取 p-next赋值给 p(取下一结点位置给 p) ,转 3继续。 参考答案: double fun( STREC *h ) double max=h-s; STREC *p; p=h-next;
21、 while(p) if(p-smax ) max=p-s; p=p-next; return max; STREC * creat( double *s) STREC *h,*p,*q; int i=0; h=p=(STREC*)malloc(sizeof(STREC);p-s=0; while(is=si; i+; p-next=q; p=q; p-next=0; return h; outlist( STREC *h) STREC *p; p=h-next; printf(“head“); do printf(“-%2.0f“,p-s);p=p-next; while(p!=0); pr
22、intf(“nn“); 16、请编写函数 fun, 对长度为7个字符的字符串, 除首、尾字符外,将其余 5个字 符按 ASCII码降序排列。 例如,原来的字符串为 CEAedca,排序后输出为 CedcEAa 。 注意: 部分源程序在文件 PROG1.C中。 请勿改动主函数 main和其它函数中的任何内容, 仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include #include #include int fun(char *s,int num) main() char s10; printf(“输入 7个字符的字符串:“); gets(s); fun(s,7);
23、printf(“n%s“,s); NONO(); 解题思路: 本题是考察考生如何对字符串中的字符按降序进行排序。给出的程序是使用双重 for循环以及冒泡法进行排序的,结果仍存放在原先的字符串上。 参考答案: int fun(char *s,int num) char ch ; int i, j ; for(i = 1 ; i #define N 16 typedef struct char num10; int s; STREC; int fun( STREC *a,STREC *b,int l, int h ) main() STREC sN=“GA005“,85,“GA003“,76,“G
24、A002“,69,“GA004“,85,“GA001“,96,“GA007“,72,“GA008“,64,“GA006“,87, “GA015“,85,“GA013“,94,“GA012“,64,“GA014“,91, “GA011“,90,“GA017“,64,“GA018“,64,“GA016“,72; STREC hN,tt;FILE *out ; int i,j,n,low,heigh,t; printf(“Enter 2 integer number low scanf(“%d%d“, if ( heighhj.s) tt=hi ;hi=hj; hj=tt; for(i=0;i= l
25、 printf(“nPlease enter string S:“); scanf(“%s“, s); fun(s, t); printf(“nThe result is: %sn“, t); NONO(); 解题思路: 本题是从一个字符串按要求生成另一个新的字符串。我们使用 for循环语句来解决这个问题。 参考答案: void fun(char *s, char t) int i, j = 0 ; for(i = 0 ; i void fun(int a, int b, long *c) main() int a,b; long c; printf(“Input a, b:“); scanf
26、(“%d%d“, fun(a, b, printf(“The result is: %ldn“, c); NONO(); 解题思路: 本题是给出两个两位数的正整数分别取出各位上的数字,再按条件组成一个新数。 取 a十位数字的方法:a/10 取 a个位数字的方法:a%10 参考答案: void fun(int a, int b, long *c) *c = (b%10)*1000+(a/10)*100+(b/10)*10+a%10; 20、请编写函数 fun,函数的功能是求出二维数组周边元素之和,作为函数值返 回。二维数组中的值在主函数中赋予。 例如:二维数组中的值为 1 3 5 7 9 2 9
27、 9 9 4 6 9 9 9 8 1 3 5 7 0 则函数值为 61。 注意: 部分源程序存在文件PROG1.C文件中。 请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include #define M 4 #define N 5 int fun ( int aMN ) main( ) int aaMN=1,3,5,7,9, 2,9,9,9,4, 6,9,9,9,8, 1,3,5,7,0; int i, j, y; clrscr(); printf ( “The original data is : n“ ); for (
28、 i=0; i #include #define N 5 double fun ( int wN ) main ( ) int aNN=0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5,9,1,4,1; int i, j; double s ; printf(“* The array *n“); for ( i =0; i void fun(int a, int b, long *c) main() int a,b; long c; printf(“Input a, b:“); scanf(“%d,%d“, fun(a, b, printf(“The resu
29、lt is: %ldn“, c); NONO(); 解题思路: 本题是给出两个两位数的正整数分别取出各位上的数字,再按条件组成一个新数。 取 a十位数字的方法:a/10 取 a个位数字的方法:a%10 参考答案: void fun(int a, int b, long *c) *c=(b%10)*1000+(a%10)*100+(b/10)*10+(a/10); 23、编写函数 fun, 函数的功能是求出小于或等于 lim的所有素数并放在 aa数组中, 函数返回所求出的素数的个数。函数 fun中给出的语句仅供参考。 注意:部分源程序在文件 PROG1.C中。 请勿改动主函数 main和其它函数
30、中的任何内容,仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include #define MAX 100 int fun(int lim, int aaMAX) /* 以下代码仅供参考 */ int i,j,k=0; /* 其中变量 k用于统计素数个数 */ for(i=2;i (i/2) aak+ = i; return k; 24、学生的记录由学号和成绩组成,N 名学生的数据已在主函数中放入结构体数组 s中, 请编写函数 fun,它的功能是:把分数最低的学生数据放在 b所指的数组中, 注意:分数最低的学生可能不止一个,函数返回分数最低的学生的人数。 注意: 部分源程
31、序在文件 PROG1.C文件中。 请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include #define N 16 typedef struct char num10; int s; STREC; int fun( STREC *a, STREC *b ) main() STREC sN=“GA05“,85,“GA03“,76,“GA02“,69,“GA04“,85, TAB TAB “GA01“,91,“GA07“,72,“GA08“,64,“GA06“,87, TAB TAB “GA015“,85,“GA013“,
32、91,“GA012“,64,“GA014“,91, TAB TAB “GA011“,91,“GA017“,64,“GA018“,64,“GA016“,72; STREC hN; int i,n;FILE *out ; n=fun( s,h ); printf(“The %d lowest score :n“,n); for(i=0;i ai.s) j = 0 ; bj+ = ai ; min = ai.s ; else if(min = ai.s) bj+ = ai ; return j ; 25、函数 fun的功能是:将 s所指字符串中 ASCII值为偶数的字符删除,串中剩余字 符形成一个新
33、串放在 t所指的数组中。 例如,若 s所指字符串中的内容为:“ABCDEFG12345“,其中字符 B的 ASCII码值 为偶数、字符 2的 ASCII码值为偶数、 都应当删除,其它依此类推。最后 t所 指的数组中的内容应是:“ACEG135“。 注意: 部分源程序存在文件PROG1.C中。 请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include #include void fun(char *s, char t) main() char s100, t100; printf(“nPlease enter string
34、 S:“); scanf(“%s“, s); fun(s, t); printf(“nThe result is: %sn“, t); NONO(); 解题思路: 本题是从一个字符串按要求生成另一个新的字符串。我们使用 for循环语句来解决这个问题。 参考答案: void fun(char *s, char t) int i, j = 0 ; for(i = 0 ; i #define M 100 void fun ( int m, int *a , int *n ) main( ) int aaM, n, k; fun ( 50, aa, for ( k = 0; k #define M 1
35、00 void fun ( int m, int *a , int *n ) int i ; *n=0 ; for(i=7 ; i #include double fun ( double eps) main( ) double x; printf(“Input eps:“) ; scanf(“%lf“, printf(“neps = %lf, PI=%lfn“, x, fun(x); NONO(); 解题思路: 本题是根据公式进行计算。 参考答案: double fun ( double eps) double s=1,t=1./3; int n=1; while(t=eps) s+=t;
36、n+; t=t*n/(2.0*n+1); return (s*2.0); main( ) double x; printf(“Input eps:“) ; scanf(“%lf“, printf(“neps = %lf, PI=%lfn“, x, fun(x); NONO(); 28、请编写函数 fun,它的功能是: 求出 1 到 1000 之间能被 7 或 11整除、但不 能同时被 7 和 11 整除的所有整数并将它们放在 a所指的数组中,通过 n 返回这 些数的个数。 注意: 部分源程序在文件 PROG1.C中。 请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填
37、入 你编写的若干语句。 给定源程序: #include void fun (int *a, int *n) main( ) int aa1000, n, k ; fun ( aa, for ( k = 0 ; k #define N 12 typedef struct char num10; double s; STREC; double fun( STREC *a, STREC *b, int *n ) main() STREC sN=“GA05“,85,“GA03“,76,“GA02“,69,“GA04“,85, TAB TAB “GA01“,91,“GA07“,72,“GA08“,64,
38、“GA06“,87, TAB TAB “GA09“,60,“GA11“,79,“GA12“,73,“GA10“,90; STREC hN, t;FILE *out ; int i,j,n; double ave; ave=fun( s,h, printf(“The %d student data which is higher than %7.3f:n“,n,ave); for(i=0;i t) b(*n)+ = ai ; return t ; 30、假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字 符串中的前导*号全部移到字符串的尾部。函数 fun中给出的语句仅供参考。
39、 例如,字符串中的内容为:*A*BC*DEF*G*,移动后,字符串中的内 容应当是:A*BC*DEF*G*。在编写函数时, 不得使用 C语言提供的字符 串函数。 注意: 部分源程序在文件 PROG1.C文件中。 请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include void fun( char *a ) /* 以下代码仅供参考 */ char *p,*q; int n=0; p=a; while(*p=*) /* 统计串头*个数 n */ n+; p+; q=a; /* 向前复制字符串,请填写相应的语句完成其功能 *
40、/ for(;n0;n-) /* 在串尾补 n个* */ *q+=*; *q=0; main() char s81,*p; int n=0; printf(“Enter a string:n“);gets(s); fun( s ); printf(“The string after moveing:n“);puts(s); NONO(); 解题思路: 本题是考察字符串的移动。具体操作请看参考源程序。 参考答案: void fun( char *a ) /* 以下代码仅供参考 */ char *p,*q; int n=0; p=a; while(*p=*) /* 统计串头*个数 n */ n+;
41、 p+; q=a; /* 向前复制字符串,请填写相应的语句完成其功能 */ while(*p) *q=*p; p+;q+; for(;n0;n-) /* 在串尾补 n个* */ *q+=*; *q=0; 31、请编写一个函数 void fun (char a,char b,int n),其功能是:删除一 个字符串中指定下标的字符。其中, a指向原字符串, 删除指定字符后的字符串存 放在 b所指的数组中,n 中存放指定的下标。例如,输入一个字符串: World,然后输入 3,则调用该函数后的结果为: Word。 注意: 部分源程序在文件 PROG1.C中。 请勿改动主函数 main和其它函数中的
42、任何内容,仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include #include #define LEN 20 void fun (char a, char b, int n) main( ) char str1LEN, str2LEN ; int n ; printf(“Enter the string:n“) ; gets(str1) ; printf(“Enter the position of the string deleted:“) ; scanf(“%d“, fun(str1, str2, n) ; printf(“The new string is
43、: %sn“, str2) ; NONO() ; 解题思路: 本题是利用字符串拷贝和字符串连接来生成新的字符串。 参考答案: void fun (char a, char b, int n) strncpy(b, a, n) ; bn = 0 ; strcat(b, a + n + 1) ; 32、假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:使字 符串的前导*号不得多于 n个;若多于 n个,则删除多余的*号; 若少于或等于 n个, 则什么也不做,字符串中间和尾部的*号不删除。函数 fun中给出的语句仅供参考。 例如,字符串中的内容为:*A*BC*DEF*G*,若 n的值为
44、 4,删除后, 字符串中的内容应当是:*A*BC*DEF*G*;若 n的值为 8,则字符串中的内容仍 为:*A*BC*DEF*G*。n 的值在主函数中输入。 在编写函数时,不得使用 C语言提供的字符串函数。 注意: 部分源程序在文件 PROG1.C文件中。 请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include void fun( char *a, int n ) /* 以下代码仅供参考 */ int i=0,j,k=0; while(ak=*) k+; /* k为统计*字符个数 */ if(kn) i=n;j=k;
45、/* 以下完成将下标为 k至串尾的字符前移 k-n个位置 */ main() char s81; int n; printf(“Enter a string:n“);gets(s); printf(“Enter n : “);scanf(“%d“, fun( s,n ); printf(“The string after deleted:n“);puts(s); NONO(); 解题思路: 本题是考察字符串的操作。 利用 for循环来判断 aj的字符是否是字符串结束符,如果不是字符串结束符,则把 aj 的字符依次存放到 ai。其中,变量 i是 n的初始值,变量 j是字符串前导的*号数。 参考答
46、案: void fun( char *a, int n ) /* 以下代码仅供参考 */ int i=0,j,k=0; while(ak=*) k+; /* k为统计*字符个数 */ if(kn) i=n;j=k; /* 以下完成将下标为 k至串尾的字符前移 k-n个位置 */ for(; aj !=0 ; j+) ai+=aj; ai = 0; 33、函数 fun的功能是:将两个两位数的正整数 a、b 合并形成一个整数放在 c中。 合并的方式是:将 a数的十位和个位数依次放在 c数的十位和千位上, b 数的十位 和个位数依次放在 c数的百位和个位上。 例如,当 a45,b=12 时,调用该函数后,c=5142。 注意: 部分源程序存在文件PROG1.C中。数据文件 IN.DAT中的数据不得修改。 请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入 你编写的若干语句。 给定源程序: #include void fun(int a, int b, long *c) main() int a,b; long c; printf(“Input a, b:“); scanf(“%d,%d“, fun(a, b, printf(“The result i