2011年3月全国计算机等级考试二级C语言上机题库

巡山小妖精
927次浏览
2020年07月28日 17:20
最佳经验
本文由作者推荐

cat音标-别扭怎么读


一、程序填空题
1.给定程序中,函数fun的功能是:将形参n所指变量中,各位上为偶数的去除,剩余的数按原来从高到低的顺序
组成一个新的数,并通过形参指针n传回所指变量。
例如:输入一个数:27638496,新的数为:739。
#include
void fun(unsigned long *n)
{ unsigned long x=0, i; int t;
i=1;
while(*n)
/**********found**********/
{ t=*n % 10;
/**********found**********/
if(t%2!= 0)
{ x=x+t*i; i=i*10; }
*n =*n /10;
}
/**********found**********/
*n=x;
}
main()
{ unsigned long n=-1;
while(n>99999999||n<0)
{ printf("Please input(0fun(&n);
printf("
The result is: %ld
",n);
}

2.给定程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到文本文件中,再用字符方式从此文本文
件中逐个读入并显示在终端屏幕上。
#include
void fun(char *s, int a, double f)
{
/**********found**********/
FILE * fp;
char ch;
fp = fopen("", "w");
fprintf(fp, "%s %d %f
", s, a, f);
fclose(fp);
fp = fopen("", "r");
printf("
The result :

");
ch = fgetc(fp);
/**********found**********/
while (!feof(fp)) {
/**********found**********/
putchar(ch); ch = fgetc(fp); }
putchar('
');
fclose(fp);
}
main()
{ char a[10]="Hello!"; int b=12345;
double c= 98.76;
fun(a,b,c);
}


3.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出
到文件中。函数fun的功能是重写形参filename所指文件中最后一个学生的数据,即用新的学生数据覆盖该学生
原来的数据,其它学生的数据不变。
#include
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n)
{ FILE *fp;
/**********found**********/
fp = fopen(filename, "rb+");
/**********found**********/
fseek(fp, -1L*sizeof(STU), SEEK_END);
/**********found**********/
fwrite(&n, sizeof(STU), 1, fp);
fclose(fp);
}
main()
{ STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},
{10005,"ZhangSan", 95, 80, 88}};
STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
int i,j; FILE *fp;
fp = fopen("", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
fp = fopen("", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
printf("
The original data :

");
for (j=0; j{ printf("
No: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("
");
}
fun("", n);
printf("
The data after modifing :

");
fp = fope
n("", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j{ printf("
No: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("
");
}
}


4.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出
到文件中。函数fun的功能是从形参filename所指的文件中读入学生数据,并按照学号从小到大排序后,再用二
进制方式把排序后的学生数据输出到filename所指的文件中,覆盖原来的文件内容。
#include
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename)
{ FILE *fp; int i, j;
STU s[N], t;
/**********found**********/
fp = fopen(filename, "rb");
fread(s, sizeof(STU), N, fp);
fclose(fp);
for (i=0; ifor (j=i+1; j/**********found**********/
if (s[i].sno >s[j].sno)
{ t = s[i]; s[i] = s[j]; s[j] = t; }
fp = fopen(filename, "wb");
/**********found**********/
fwrite(s, sizeof(STU), N, fp);
fclose(fp);
}
main()
{ STU t[N]={ {10005,"ZhangSan", 95, 80, 88}, {10003,"LiSi", 85, 70, 78},
{10002,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87},
{10001,"MaChao", 91, 92, 77}}, ss[N];
int i,j; FILE *fp;
fp = fopen("", "wb");
fwrite(t, sizeof(STU), 5, fp);
fclose(fp);
printf("

The original data :

");
for (j=0; j{ printf("
No: %ld Name: %-8s Scores: ",t[j].sno, t[j].name);
for (i=0; i<3; i++) printf("%6.2f ", t[j].score[i]);
printf("
");
}
fun("");
printf("

The data after sorting :

");
fp = fopen("", "rb");
fread(ss, sizeof(STU), 5, fp);
fclose(fp);
for (j=0; j{ printf("
No: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("
");
}
}


5.给定程序中,函数fun的功能是将参数给定的字符串、整数、浮点数写到文本文件中,再用字符串方式从此文
本文件中逐个读入,并调用库函数atoi和atof将字符串转换成相应的整数、浮点数,然后将其显示在屏幕上。
#include
#include
void fun(char *s, int a, double f)
{
/**********found**********/
FILE * fp;
char str[100], str1[100], str2[100];
int a1; double f1;
fp = fopen("", "w");
fprintf(fp, "%s %d %f
", s, a, f);
/**********found**********/
fclose(fp)
fp = fopen("", "r");
/**********found**********/
fscanf(fp,"%s%s%s", str, str1, str2);
fclose(fp);
a1 = atoi(str1);
f1 = atof(str2);
printf("
The result :

%s %d %f
", str, a1, f1);
}
main()
{ char a[10]="Hello!";
int b=12345;
double c= 98.76;
fun(a,b,c);
}
6.给定程序中,函数fun的功能是根据形参i的值返回某个函数的值,当调用正确时,程序输出:
x1=5.000000, x2=3.000000 x1*x1+x1*x2=40.000000
#include
double f1(double x)
{ return x*x; }
double f2(double x, double y)
{ return x*y; }
/**********found**********/
double fun(int i, double x, double y)
{ if (i==1)
/**********found**********/
return f1(x);
else
/**********found**********/
return f2(x, y);
}
main()
{ double x1=5, x2=3, r;
r = fun(1, x1, x2);
r += fun(2, x1, x2);
printf("
x1=%f, x2=%f, x1*x1+x1*x2=%f

",x1, x2, r);
}

7.程序通过定义并赋初值的方式,利用结构体变量存储了一名学生的信息。函数fun的功能是输出这位学生的信
息。
#include
typedef struct
{ int num;
char name[9];
char sex;
struct { int year,month,day } birthday;
float score[3];
}STU;
/**********found**********/
void show(STU tt)
{ int i;
printf("
%d %s %c %d-%d-%d", , , ,
, , );
for(i=0; i<3; i++)
/**********found**********/
printf("%5.1f", [i]);
printf("
");
}
main( )
{ STU std={ 1,"Zhanghua",'Ma = q->data; q->data = t; }
q = q->next;
}
/**********found**********/
p = p->next
}
}
NODE *creatlist(int a[])
{ NODE *h,*p,*q; int i;
h=NULL;
for(i=0; i{ q=(NODE *)malloc(sizeof(NODE));
q->data=a[i];
q->next = NULL;
if (h == NULL) h = p = q;
else { p->next = q; p = q; }
}
return h;
}
void outlist(NODE *h)
{ NODE *p;
p=h;
if (p==NULL) printf("The list is NULL!
");
else
{ printf("
Head ");
do
{ printf("->%d", p->data); p=p->next; }
while(p!=NULL);
printf("->End
");
}
}
10.给定程序中,函数fun的功能是:叛定形参a所指的N×N(规定N为奇数)的矩阵是否是“幻方”,若是,函数
返回值为1;不是,函数返回值为0。“幻方”的叛定条件是:矩阵每行、每列、主对角线及反对角线上元素之和
都相等。例如:以下3×3的矩阵就是一个“幻方”:
4 9 2
3 5 7
8 1 6
#include
#define N 3
int fun(int (*a)[N])
{ int i,j,m1,m2,row,colum;
m1=m2=0;
for(i=0; i{ j=N-i-1; m1+=a[i][i]; m2+=a[i][j]; }
if(m1!=m2) return 0;
for(i=0; i/**********found**********/
row=colum= 0;
for(j=0; j{ row+=a[i][j]; colum+=a[j][i]; }
/**********found**********/
if( (row!=colum) || (row!=m1) ) return 0;
}
/**********found**********/
return 1;
}
main()
{ int x[N][N],i,j;
printf("Enter number for array:
");
for(i=0; ifor(j=0; jprintf("Array:
");
for(i=0; i{ for(j=0; jprintf("
");
}
if(fun(x)) printf("The Array is a magic square.
");
else printf("The Array isn't a magic square.
");
}

11.给定程序中,函数fun的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、
4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2。
#include
#include
#define N 5
typedef struct node {
int data;
struct node *next;
} NODE;
void fun(NODE *h)
{ NODE *p, *q, *r;
/**********found**********/
p = h->next;
/**********found**********/
if (p==0) return;
q = p->next;
p->next = NULL;
while (q)
{ r = q->next; q->next = p;
/**********found**********/
p = q; q = r;
}
h->next = p;
}
NODE *creatlist(int a[])
{ NODE *h,*p,*q; int i;
h = (NODE *)malloc(sizeof(NODE));
h->next = NULL;
for(i=0; i{ q=(NODE *)malloc(sizeof(NODE));
q->data=a[i];
q->next = NULL;
if (h->next == NULL) h->next = p = q;
else { p->next = q; p = q; }
}
return h;
}
void outlist(NODE *h)
{ NODE *p;
p = h->next;
if (p==NULL) printf("The list is NULL!
");
else
{ printf("
Head "
);
do
{ printf("->%d", p->data); p=p->next; }
while(p!=NULL);
printf("->End
");
}
}

12.给定程序中,函数fun的功能是将不带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2
、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2
#include
#include
#define N 5
typedef struct node {
int data;
struct node *next;
} NODE;
/**********found**********/
NODE * fun(NODE *h)
{ NODE *p, *q, *r;
p = h;
if (p == NULL)
return NULL;
q = p->next;
p->next = NULL;
while (q)
{
/**********found**********/
r = q->next;
q->next = p;
p = q;
/**********found**********/
q = r
}
return p;
}
NODE *creatlist(int a[])
{ NODE *h,*p,*q; int i;
h=NULL;
for(i=0; i{ q=(NODE *)malloc(sizeof(NODE));
q->data=a[i];
q->next = NULL;
if (h == NULL) h = p = q;
else { p->next = q; p = q; }
}
return h;
}
void outlist(NODE *h)
{ NODE *p;
p=h;
if (p==NULL) printf("The list is NULL!
");
else
{ printf("
Head ");
do
{ printf("->%d", p->data); p=p->next; }
while(p!=NULL);
printf("->End
");
}
}

13.给定程序中,函数fun的功能是将带头结点的单向链表结点数据域中的数据从小到大排序。即若原链表结点数
据域从头至尾的数据为:10、4、2、8、6,排序后链表结点数据域从头至尾的数据为:2、4、6、8、10。
#include
#include
#define N 6
typedef struct node {
int data;
struct node *next;
} NODE;
void fun(NODE *h)
{ NODE *p, *q; int t;
/**********found**********/
p = h->next
while (p) {
/**********found**********/
q = p->next
while (q) {
/**********found**********/
if (p->data >= q->data)
{ t = p->data; p->data = q->data; q->data = t; }
q = q->next;
}
p = p->next;
}
}
NODE *creatlist(int a[])
{ NODE *h,*p,*q; int i;
h = (NODE *)malloc(sizeof(NODE));
h->next = NULL;
for(i=0; i{ q=(NODE *)malloc(sizeof(NODE));
q->data=a[i];
q->next = NULL;
if (h->next == NULL) h->next = p = q;
else { p->next = q; p = q; }
}
return h;
}
void outlist(NODE *h)
{ NODE *p;
p = h->next;
if (p==NULL) printf("The list is NULL!
");
else
{ printf("
Head ");
do
{ printf("->%d", p->data); p=p->next; }
while(p!=NULL);
printf("->End
");
}
}
14.给定程序中,函数fun的功能是用函数指针指向要调用的函数,并进行调用。规定在___2____处使f指向函数
f1,在___3___处使f指向函数f2。当调用正确时,程序输出:
X1=5.000000, x2=3.000000, x1*x1+x1*x2=40.000000
#include
double f1(double x)
{ return x*x; }
double f2(double x, double y)
{ return x*y; }
double fun(double a, double b)
{
/**********found**********/
double (*f)();
double r1, r2;
/**********found**********/
f = f1 /* point fountion f1 */
r1 = f(a);
/**********found**********/
f = f2 /* point fountion f2 */
r2 = (*f)(a, b);
return r1 + r2;
}
main()
{ double x1=5, x2=3, r;
r = fun(x1, x2);
printf("
x1=%f, x2=%f, x1*x1+x1*x2=%f
",x1, x2, r);
}

15.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出
到中。函数fun 的功能是从指定文件中找出指定学号的学生数据,读入此学生数据,对该生的分数
进行修改,使每门课的分数加3分,修改后重写文件中该学生的数据,即用该学生的新数据覆盖原数据,其它学
生数据不变,若找不到,则什么都不做。
#include
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, long sno)
{ FILE *fp;
STU n; int i;
fp = fopen(filename,"rb+");
/**********found**********/
while (!feof(fp))
{ fread(&n, sizeof(STU), 1, fp);
/**********found**********/
if (==sno) break;
}
if (!feof(fp))
{ for (i=0; i<3; i++) [i] += 3;
/**********found**********/
fseek(fp, -1L*sizeof(STU), SEEK_CUR);
fwrite(&n, sizeof(STU), 1, fp);
}
fclose(fp);
}
16.给定程序中,函数fun的功能是:求出形参ss所指字符串数组中最长字符串的长度,将其余字符串右边用字符
*补齐,使其与最长字符串等长。ss所指字符串数组中共有N个字符串,且串长#include
#include
#define M 5
#define N 20
void fun(char (*ss)[N])
{ int i, j, n, len=0;
for(i=0; i{ len=strlen(ss[i]);
if(i==0) n=len;
if(len>n)n=len;
}
for(i=0; i/**********found**********/
n=strlen(ss[i]);
for(j=0; j/**********found**********/
ss[i][n+j]='*';
/**********found**********/
ss[i][n+j+1]='0';
}
}
main()
{ char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};
int i;
printf("The original strings are :
");
for(i=0; i",ss[i]);
printf("
");
fun(ss);
printf("The result is :
");
for(i=0; i",ss[i]);
}

17.程序通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门课的成绩,函数fun的功能是将存放学
生数据的结构体数组,按照姓名的字典序(从小到大)排序。
#include
#include
struct student {
long sno;
char name[10];
float score[3];
};
void fun(struct student a[], int n)
{
/**********found**********/
struct student t
;
int i, j;
/**********found**********/
for (i=0; ifor (j=i+1; j/**********found**********/
if (strcmp(a[i].name,a[j].name) > 0)
{ t = a[i]; a[i] = a[j]; a[j] = t; }
}
main()
{ struct student s[4]={{10001,"ZhangSan", 95, 80, 88},{10002,"LiSi", 85, 70, 78},
{10003,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87}};
int i, j;
printf("

The original data :

");
for (j=0; j<4; j++)
{ printf("
No: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);
printf("
");
}
fun(s, 4);
printf("

The data after sorting :

");
for (j=0; j<4; j++)
{ printf("
No: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);
printf("
");
}
}
18.给定程序中,函数fun的功能是:将形参s所指字符中中的所有字母字符顺序前移,其他字符顺序后移,处理
后新字符串的首地址作为函数值返回。
例如,s所指字符串为:asd123fgh543df,处理后新字符串为asdfghdf123543。
#include
#include
#include
char *fun(char *s)
{ int i, j, k, n; char *p, *t;
n=strlen(s)+1;
t=(char*)malloc(n*sizeof(char));
p=(char*)malloc(n*sizeof(char));
j=0; k=0;
for(i=0; i{ if(((s[i]>='a')&&(s[i]<='z'))||((s[i]>='A')&&(s[i]<='Z'))) {
/**********found**********/
t[j]=s[i]; j++;}
else
{ p[k]=s[i]; k++; }
}
/**********found**********/
for(i=0; i/**********found**********/
t[j+k]= 0;
return t;
}
main()
{ char s[80];
printf("Please input: "); scanf("%s",s);
printf("
The result is: %s
",fun(s));
}

19.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a所指结构
体变量s中的数据进行修改,并把a中地址作为函数值返回主函数,在主函数中输出修改后的数据。
例如:a所指变量中的学号、姓名、和三门课的成绩依次是:10001、”ZhangSan”、95、80、88,修改后输出t
中的数据应为:10002、”LiSi”、96、81、89。
#include
#include
struct student {
long sno;
char name[10];
float score[3];
};
/**********found**********/
struct student * fun(struct student *a)
{ int i;
a->sno = 10002;
strcpy(a->name, "LiSi");
/**********found**********/
for (i=0; i<3; i++) a->score[i] += 1;
/**********found**********/
return a
}
main()
{ struct student s={10001,"ZhangSan", 95, 80, 88}, *t;
int i;
printf("

The original data :
");
printf("
No: %ld Name: %s
Scores: ",, );
for (i=0; i<3; i++) printf("%6.2f ", [i]);
printf("
");
t = fun(&s);
printf("
The data after modified :
");
printf("
No: %ld Name: %s
nScores: ",t->sno, t->name);
for (i=0; i<3; i++) printf("%6.2f ", t->score[i]);
printf("
");
}
20.给定程序中,函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),将所指数组
中小于平均值的数据移至数组的前部,大于等于平均值的数据移至x所指数据的后部,平均值作为函数值返回,
在主函数中输出平均值和移动后的数据。
例如:有10个正数:46 30 32 40 6 17 45 15 48 26,平均值为:30.5000000
移动后的输出为:30 6 17 15 26 46 32 40 45 48
#include
#include
#define N 10
double fun(double *x)
{ int i, j; double av, y[N];
av=0;
/**********found**********/
for(i=0; ifor(i=j=0; iif( x[i]/**********found**********/
y[j]=x[i]; x[i]=-1; j++;}
i=0;
while(i{ if( x[i]!= -1 ) y[j++]=x[i];
/**********found**********/
i++;
}
for(i=0; ireturn av;
}
main()
{ int i; double x[N];
for(i=0; iprintf("
");
printf("
The average is: %f
",fun(x));
printf("
The result :
",fun(x));
for(i=0; iprintf("
");
}

21.给定程序中,函数fun的功能是:计算形参中N个数的平均值(规定所有数均为正数),将所指数组中大于平
均值的数据移至数组的前部,小于等于平均值的数据移至x所指数组的后部,平均值作为函数值返回,在主函数
中输出平均值和移动后的数据。
例如:有10个正数:46 30 32 40 6 17 45 15 48 26,平均值为:30.5000000
移动后的输出为:46 32 40 45 48 30 6 17 15 26
#include
#include
#define N 10
double fun(double *x)
{ int i, j; double s, av, y[N];
s=0;
for(i=0; i/**********found**********/
av=s/N;
for(i=j=0; iif( x[i]>av ){
/**********found**********/
y[j++]=x[i]; x[i]=-1;}
for(i=0; i/**********found**********/
if( x[i]!= -1) y[j++]=x[i];
for(i=0; ireturn av;
}
main()
{ int i; double x[N];
for(i=0; iprintf("
");
printf("
The average is: %f
",fun(x));
printf("
The result :
",fun(x));
for(i=0; iprintf("
");
}
22.给定程序中,函数fun的功能是:将自然数1~10以及它们的平方根写到名为的文本文件中,然后
再顺序读出显示在屏幕上。
#include
#include

int fun(char *fname )
{ FILE *fp; int i,n; float x;
if((fp=fopen(fname, "w"))==NULL) return 0;
for(i=1;i<=10;i++)
/**********found**********/
fprintf(fp,"%d %f
",i,sqrt((double)i));
printf("
Succeed!!
");
/**********found**********/

fclose(fp);
printf("
The data in file :
");
/**********found**********/
if((fp=fopen(fname,"r"))==NULL)
return 0;
fscanf(fp,"%d%f",&n,&x);
while(!feof(fp))
{ printf("%d %f
",n,x); fscanf(fp,"%d%f",&n,&x); }
fclose(fp);
return 1;
}
main()
{ char fname[]="";
fun(fname);
}

23.给定程序中,函数fun的功能是:找出N×N矩阵中每列元素中的最大值,并按顺序依次存放于形参b所指的一
维数组中。
#include
#define N 4
void fun(int (*a)[N], int *b)
{ int i,j;
for(i=0; i/**********found**********/
b[i]= a[0][i];
for(j=1; j/**********found**********/
if(b[i] < a[j][i]) b[i]=a[j][i];
}
}
main()
{ int x[N][N]={ {12,5,8,7},{6,1,9,3},{1,2,3,4},{2,8,4,3} },y[N],i,j;
printf("
The matrix :
");
for(i=0;i{ for(j=0;jprintf("
");
}
/**********found**********/
fun(x,y);
printf("
The result is:");
for(i=0; iprintf("
");
}
24.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a中的数据
进行修改,把修改后的数据作为函数值返回主函数进行输出。
例如:传给形参a的数据中,学号、姓名、和3门课的成绩依次是:10001、”ZhangSan”、95、80、88,修改后
的数据应为:10002、”LiSi”、96、81、89。
#include
#include
struct student {
long sno;
char name[10];
float score[3];
};
/**********found**********/
struct student fun(struct student a)
{ int i;
= 10002;
/**********found**********/
strcpy(, "LiSi");
/**********found**********/
for (i=0; i<3; i++) [i]+= 1;
return a;
}
main()
{ struct student s={10001,"ZhangSan", 95, 80, 88}, t;
int i;
printf("

The original data :
");
printf("
No: %ld Name: %s
Scores: ",, );
for (i=0; i<3; i++) printf("%6.2f ", [i]);
printf("
");
t = fun(s);
printf("
The data after modified :
");
printf("
No: %ld Name: %s
Scores: ",, );
for (i=0; i<3; i++) printf("%6.2f ", [i]);
printf("
");
}

25.人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号唯一
。函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数输出,若指定编号不存在,返回数
据中的编号为空串。
#include
#include
#define N 8
typedef struct
{ char num[10];
int year,month,day
}STU;

/**********found**********/
STU fun(STU *std, char *num)
{ int i; STU a={"",9999,99,99};
for (i=0; i/**********found**********/
if( strcmp(std[i].num,num)==0 )
/**********found*******
***/
return (std[i]);
return a;
}
main()
{ STU std[N]={ {"111111",1984,2,15},{"222222",1983,9,21},{"333333",1984,9,1},
{"444444",1983,7,15},{"555555",1984,9,28},{"666666",1983,11,15},
{"777777",1983,6,22},{"888888",1984,8,19}};
STU p; char n[10]="666666";
p=fun(std,n);
if([0]==0)
printf("
Not found !
");
else
{ printf("
Succeed !
");
printf("%s %d-%d-%d
",,,,);
}
}

26.给定程序中已建立一个带有头结点的单向链表,链表中的各结点按数据域递增有序链接。函数fun的功能是:
删除链表中数据域值相同的结点,使之只保留一个。
#include
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
void fun( SLIST *h)
{ SLIST *p, *q;
p=h->next;
if (p!=NULL)
{ q=p->next;
while(q!=NULL)
{ if (p->data==q->data)
{ p->next=q->next;
/**********found**********/
free(q);
/**********found**********/
q=p->next;
}
else
{ p=q;
/**********found**********/
q=q->next;
}
}
}
}
SLIST *creatlist(int *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("
The list is NULL!
");
else
{ printf("
Head");
do { printf("->%d",p->data); p=p->next; } while(p!=NULL);
printf("->End
");
}
}
main( )
{ SLIST *head; int a[N]={1,2,2,3,4,4,4,5};
head=creatlist(a);
printf("
The list before deleting :
"); outlist(head);
fun(head);
printf("
The list after deleting :
"); outlist(head);
}
27.给定程序中,函数fun的功能是:计算下载前n项和作为函数值返回。

例如:当形参n的值为10时,函数返回:9.612558.
#include
double fun(int n)
{ int i; double s, t;
/**********found**********/
s=0;
/**********found**********/
for(i=1; i<=n; i++)
{ t=2.0*i;
/**********found**********/
s=s+(2.0*i-1)*(2.0*i+1)/(t*t);
}
return s;
}
main()
{ int n=-1;
while(n<0)
{ printf("Please input(n>0): "); scanf("%d",&n); }
printf("
The result is: %f
",fun(n));
}

28.给定程序中,函数fun的功能是:统计形参s所指字符串中数字字符出现的次数,并存放在形参t所指的变量中
,最后在主函数中输出。例如:形参s所指的字符串为:abcdef35adgh3kjsdf7。输出结果为:4。
#include
void fun(char *s, int *t)
{ int i, n;
n=0;
/**********found**********/
for(i=0; s[i] !=NULL; i++)
/**********found**********/
if(s[i]>='0'&&s[i]
<= '9') n++;
/**********found**********/
*t=n;
}
main()
{ char s[80]="abcdef35adgh3kjsdf7";
int t;
printf("
The original string is : %s
",s);
fun(s,&t);
printf("
The result is : %d
",t);
}
29.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是对形参b所指结构
体变量中的数据进行修改,最后在主函数中输出修改后的数据。
例如:b所指变量t中的学号、姓名、和三门课的成绩依次是:10002、”ZhangQi”、93、85、87,修改后输出t
中的数据应为:10004、”LiJie”、93、85、87。
#include
#include
struct student {
long sno;
char name[10];
float score[3];
};
void fun( struct student *b)
{ int i;
/**********found**********/
b->sno = 10004;
/**********found**********/
strcpy(b->name, "LiJie");
}
main()
{ struct student t={10002,"ZhangQi", 93, 85, 87};
int i;
printf("

The original data :
");
printf("
No: %ld Name: %s
Scores: ",, );
for (i=0; i<3; i++) printf("%6.2f ", [i]);
printf("
");
/**********found**********/
fun(&t);
printf("
The data after modified :
");
printf("
No: %ld Name: %s
Scores: ",, );
for (i=0; i<3; i++) printf("%6.2f ", [i]);
printf("
");
}
30.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a所指结构
体变量中的数据赋给函数中的结构体变量b,并修改b中的学号和姓名,最后输出修改后的数据。
例如:a所指变量中的学号、姓名和三门课的成绩依次是:10001、”ZhangSan”、95、80、88,则修改后输出b
中的数据应为:10002、”LiSi”、95、80、88。
#include
#include
struct student {
long sno;
char name[10];
float score[3];
};
void fun(struct student a)
{ struct student b; int i;
/**********found**********/
b = a;
= 10002;
/**********found**********/
strcpy(, "LiSi");
printf("
The data after modified :
");
printf("
No: %ld Name: %s
Scores: ",, );
/**********found**********/
for (i=0; i<3; i++) printf("%6.2f ", [i]);
printf("
");
}
main()
{ struct student s={10001,"ZhangSan", 95, 80, 88};
int i;
printf("

The original data :
");
printf("
No: %ld Name: %s
Scores: ",, );
for (i=0; i<3; i++) printf("%6.2f ", [i]);
printf("
");
fun(s);
}

31.给定程序中,函数fun的功能是:对形参s所指字符串中下标为奇数的字符按ASCII码大小递增排序,并将排序
后下标为奇数的字符取出,存入形参p所指字符数组中,形成一个新串。
例如:形参s所指的字符串为:baawrskjghzlicda,执行后p所指字符数组中的字符串应为:aachilsw。
#include io.h>
void fun(char *s, char *p)
{ int i, j, n, x, t;
n=0;
for(i=0; s[i]!='0'; i++) n++;
for(i=1; i/**********found**********/
t=i;
/**********found**********/
for(j=i+2 jif(s[t]>s[j]) t=j;
if(t!=i)
{ x=s[i]; s[i]=s[t]; s[t]=x; }
}
for(i=1,j=0; i/**********found**********/
p[j]=0;
}
main()
{ char s[80]="baawrskjghzlicda", p[50];
printf("
The original string is : %s
",s);
fun(s,p);
printf("
The result is : %s
",p);
}
32. .给定程序中,函数fun的功能是:在形参ss所指字符串数组中,将所有串长超过k的字符串中右边的字符删
除,只保留左边的k个字符。Ss所指字符串数组中共有N个字符串,且串长小于M。
#include
#include
#define N 5
#define M 10
/**********found**********/
void fun(char (*ss) [M], int k)
{ int i=0
/**********found**********/
while(i< N) {
/**********found**********/
ss[i][k]=0; i++; }
}
main()
{ char x[N][M]={"Create","Modify","Sort","skip","Delete"};
int i;
printf("
The original string

");
for(i=0;i");
fun(x,4);
printf("
The string after deleted :

");
for(i=0; i");
}
33.给定程序的功能是:调用函数fun将指定源文件中的内容复制到指定的目标文件中,复制成功时函数返回值为
1,失败时返回值为0。在复制的过程中,把复制的内容输出到终端屏幕。主函数中源文件名放在变量sfname中,
目标文件名放在变量tfname中。
#include
#include
int fun(char *source, char *target)
{ FILE *fs,*ft; char ch;
/**********found**********/
if((fs=fopen(source, "r"))==NULL)
return 0;
if((ft=fopen(target, "w"))==NULL)
return 0;
printf("
The data in file :
");
ch=fgetc(fs);
/**********found**********/
while(!feof(fs))
{ putchar( ch );
/**********found**********/
fputc(ch,ft);
ch=fgetc(fs);
}
fclose(fs); fclose(ft);
printf("

");
return 1;
}
main()
{ char sfname[20] ="myfile1",tfname[20]="myfile2";
FILE *myf; int i; char c;
myf=fopen(sfname,"w");
printf("
The original data :
");
for(i=1; i<30; i++){ c='A'+rand()%25;fprintf(myf,"%c",c); printf("%c",c); }
fclose(myf);printf("

");
if (fun(sfname, tfname)) printf("Succeed!");
else printf("Fail!");
}

34.用筛选法可得到2~n(n<10000)之间的所有素数,方法是:首先从素数2开始,将所有2的倍数从数表中删去
(把数表中相应位置的值置成0);接着从数表中找下一个非0数,并从数表中删去该数的所有倍数;依此类推,
直到所找的下一个数等于n为止。这样会得到一个序列:
2,3,5,7,11,13,17,19,23,……
函数fu
n用筛选法找出所有小于等于n的素数,并统计素数的个数作为函数值返回。
#include
int fun(int n)
{ int a[10000], i,j, count=0;
for (i=2; i<=n; i++) a[i] = i;
i = 2;
while (i/**********found**********/
for (j=a[i]*2; j<=n; j+=a[i])
a[j] = 0;
i++;
/**********found**********/
while (a[i]==0)
i++;
}
printf("
The prime number between 2 to %d
", n);
for (i=2; i<=n; i++)
/**********found**********/
if (a[i]!=0)
{ count++; printf( count%15?"%5d":"
%5d",a[i]); }
return count;
}
main()
{ int n=20, r;
r = fun(n);
printf("
The number of prime is : %d
", r);
}
35.给定程序中,函数fun 的功能是建立一个N×N的矩阵。矩阵元素的构成规律是:最外层元素的值全部为1;从
外向内第2层元素的值全部为2;第3层元素的值全部为3,…依次类推。例如:若N=5,生成的矩阵为:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
#include
#define N 7
/**********found**********/
void fun(int (*a) [N])
{ int i,j,k,m;
if(N%2==0) m=N/2
else m=N/2+1;
for(i=0; i/**********found**********/
for(j= i ja[i][j]=a[N-i-1][j]=i+1;
for(k=i+1; k/**********found**********/
a[k][i]=a[k][N-i-1]= i+1;
}
}
main()
{ int x[N][N]={0},i,j;
fun(x);
printf("
The result is:
");
for(i=0; i{ for(j=0; jprintf("
");
}
}

36.给定程序中,函数fun的功能是:统计出带有头结点的单向链表中结点的个数,存放在形参n所指的存储单元
中。
#include
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(int *a);
void outlist(SLIST *);
void fun( SLIST *h, int *n)
{ SLIST *p;
/**********found**********/
*n=0;
p=h->next;
while(p)
{ (*n)++;
/**********found**********/
p=p->next;
}
}
main()
{ SLIST *head;
int a[N]={12,87,45,32,91,16,20,48}, num;
head=creatlist(a); outlist(head);
/**********found**********/
fun(head, &num);
printf("
number=%d
",num);
}
SLIST *creatlist(int a[])
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("The list is NULL!
");
else
{ printf("
Head ");
do
{ printf("->%d",p->data); p=p->next; }
while(p!=NULL);
printf("->End
");
}
}
37.给定程序中,函数fun的功能是:在形参ss所指字符串数组中,查找含有形参substr所指子串的所有字符串并

输出,若没找到则输出相应信息。ss所指字符串数组中共有N个字符串,且串长小于M。程序中库函数strstr
(s1,s2)的功能是在s1串中查找s2子串,若没有,函数值为0,若有,为非0。
#include
#include
#define N 5
#define M 15
void fun(char (*ss)[M], char *substr)
{ int i,find=0;
/**********found**********/
for(i=0; i< N i++)
/**********found**********/
if( strstr(ss[i], substr) != NULL )
{ find=1; puts(ss[i]); printf("
"); }
/**********found**********/
if (find==0) printf("
Don't found!
");
}
main()
{ char x[N][M]={"BASIC","C langwage","Java","QBASIC","Access"},str[M];
int i;
printf("
The original string

");
for(i=0;i");
printf("
Enter a string for search : "); gets(str);
fun(x,str);
}

38.函数fun的功能是:把形参a所指数组中的奇数按原顺序依次存放到a[0]、a[1]、a[2]、……中,把偶数从数
组中删除,奇数个数通过函数值返回。例如:若a所指数组中的数据最初排列为:9、1、4、2、3、6、5、8、7,
删除偶数后a所指数组中的数据为:9、1、3、5、7,返回值为5。
#include
#define N 9
int fun(int a[], int n)
{ int i,j;
j = 0;
for (i=0; i/**********found**********/
if (a[i]%2==1)
{
/**********found**********/
a[j] = a[i]; j++;
}
/**********found**********/
return j;
}
main()
{ int b[N]={9,1,4,2,3,6,5,8,7}, i, n;
printf("
The original data :
");
for (i=0; iprintf("
");
n = fun(b, N);
printf("
The number of odd : %d
", n);
printf("
The odd number :
");
for (i=0; iprintf("
");
}
39. 给定程序中,函数fun的功能是:在形参ss所指字符串数组中,删除所有串长超过k的字符串,函数返回所剩
字符串的个数。ss所指字符串数组中共有N个字符串,且串长小于M。
#include
#include
#define N 5
#define M 10
int fun(char (*ss)[M], int k)
{ int i,j=0,len;
/**********found**********/
for(i=0; i{ len=strlen(ss[i]);
/**********found**********/
if(len<= k)
/**********found**********/
strcpy(ss[j++],ss[i]);
}
return j;
}
main()
{ char x[N][M]={"Beijing","Shanghai","Tianjing","Nanjing","Wuhan"};
int i,f;
printf("
The original string

");
for(i=0;i");
f=fun(x,7);
printf("The string witch length is less than or equal to 7 :
");
for(i=0; i");
}

40.给定程序中已建立一个带有头结点的单向链表,链表中的各结点按结点数据域中的数据递增有序链接。函数
fun的功能是:把形参x的值放入一个新结点并插入到链表中,插入后各结点数
据域的值仍保持递增有序。
#include
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
void fun( SLIST *h, int x)
{ SLIST *p, *q, *s;
s=(SLIST *)malloc(sizeof(SLIST));
/**********found**********/
s->data=x;
q=h;
p=h->next;
while(p!=NULL && x>p->data) {
/**********found**********/
q=p;
p=p->next;
}
s->next=p;
/**********found**********/
q->next=s;
}
SLIST *creatlist(int *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("
The list is NULL!
");
else
{ printf("
Head");
do { printf("->%d",p->data); p=p->next; } while(p!=NULL);
printf("->End
");
}
}
main()
{ SLIST *head; int x;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a);
printf("
The list before inserting:
"); outlist(head);
printf("
Enter a number : "); scanf("%d",&x);
fun(head,x);
printf("
The list after inserting:
"); outlist(head);
}
41.给定程序中,函数fun的功能是:计算x所指数组中N个数的平均值(规定所有数均为正数),平均值通过形参
返回主函数,将小于平均值且最接近于平均值的数作为函数值返回,在主函数中输出。
例如:有10个正数:46 30 32 40 6 17 45 15 48 26,平均值为:30.5000000
主函数中输出:m=30.0
#include
#define N 10
double fun(double x[],double *av)
{ int i,j; double d,s;
s=0;
for(i=0; i/**********found**********/
*av=s/N;
d=32767;
for(i=0; iif(x[i]<*av && *av - x[i]<=d){
/**********found**********/
d=*av-x[i]; j=i;}
/**********found**********/
return x[j];
}
main()
{ int i; double x[N],av,m;
for(i=0; iprintf("
");
m=fun(x,&av);
printf("
The average is: %f
",av);
printf("m=%5.1f ",m);
printf("
");
}

42.给定程序中,函数fun的功能是:将s所指字符串中的所有数字字符移到所有非数字字符之后,并保持数字字
符串和非数字字符串原有的先后次序。例如,形参s所指的字符串为:def35adh3kjsdf7。执行结果为:
defadhkjsdf3537。
#include
void fun(char *s)
{ int i, j=0, k=0; char t1[80], t2[80];
for(i=0; s[i]!='0'; i++)
if(s[i]>='0' && s[i]<='9')
{
/**********found**********/
t2[j]=s[i]; j++;
}
else t1[k++]=s[i];
t2[j]=0; t1[k]=0;
/**********found**********/
for(i=0; i/**********found**********/
for(i=0; i}

main()
{ char s[80]="ba3a54j7sd567sdffs";
pri
ntf("
The original string is : %s
",s);
fun(s);
printf("
The result is : %s
",s);
}
43.给定程序中,函数fun的功能是:在形参ss所指字符串数组中查找与形参t所指字符串相同的串,找到后返回
该串在字符串数组中的位置(下标值),未找到则返回-1。ss所指字符串数组中共有N个内容不同的字符串,且
串长小于M。
#include
#include
#define N 5
#define M 8
int fun(char (*ss)[M],char *t)
{ int i;
/**********found**********/
for(i=0; i< N i++)
/**********found**********/
if(strcmp(ss[i],t)==0 ) return i
return -1;
}
main()
{ char ch[N][M]={"if","while","switch","int","for"},t[M];
int n,i;
printf("
The original string

");
for(i=0;i");
printf("
Enter a string for search: "); gets(t);
n=fun(ch,t);
/**********found**********/
if(n== -1) printf("
Don't found!
");
else printf("
The position is %d .
",n);
}
44.函数fun的功能是进行数字字符转换。若形参ch中是数字字符’0’~’9’,则’0’转换成’9’,’1’t转
换成’8’,’2’转换成’7’,……,’9’转换成’0’;若是其它字符则保持不变;并将转换后的结果作为
函数值返回。
#include
/**********found**********/
char fun(char ch)
{
/**********found**********/
if (ch>='0' && ch<='9')
/**********found**********/
return '9'- (ch-'0');
return ch
}
main()
{ char c1, c2;
printf("
The result :
");
c1='2'; c2 = fun(c1);
printf("c1=%c c2=%c
", c1, c2);
c1='8'; c2 = fun(c1);
printf("c1=%c c2=%c
", c1, c2);
c1='a'; c2 = fun(c1);
printf("c1=%c c2=%c
", c1, c2);
}

45.函数fun的功能是:把形参a所指数组中的偶数按原顺序依次存放到a[0]、a[1]、a[2]、……中,把奇数从数
组中删除,偶数个数通过函数值返回。例如:若a所指数组中的数据最初排列为:9、1、4、2、3、6、5、8、7,
删除奇数后a所指数组中的数据为:4、2、6、8,返回值为4。
#include
#define N 9
int fun(int a[], int n)
{ int i,j;
j = 0;
for (i=0; i/**********found**********/
if (a[i]%2== 0) {
/**********found**********/
a[j] = a[i]; j++;
}
/**********found**********/
return j;
}
main()
{ int b[N]={9,1,4,2,3,6,5,8,7}, i, n;
printf("
The original data :
");
for (i=0; iprintf("
");
n = fun(b, N);
printf("
The number of even :%d
", n);
printf("
The even :
");
for (i=0; iprintf("
");
}
46.给定程序中,函数fun的功能是:利用指针数组对形参ss所指字符串数组中的字符串按由长到短的顺序排序,
并输出排序结果。ss所指字符串数组中共有N个
字符串,且串长小于M。
#include
#include
#define N 5
#define M 8
void fun(char (*ss)[M])
{ char *ps[N],*tp; int i,j,k;
for(i=0; ifor(i=0; i/**********found**********/
k= i
for(j=i+1; j/**********found**********/
if(strlen(ps[k]) < strlen(ps[j]) ) k=j;
/**********found**********/
tp=ps[i]; ps[i]=ps[k]; ps[k]= tp
}
printf("
The string after sorting by length:

");
for(i=0; i}
main()
{ char ch[N][M]={"red","green","blue","yellow","black"};
int i;
printf("
The original string

");
for(i=0;i");
fun(ch);
}
47.给定程序中,函数fun的功能是:找出形参s所指字符串中出现频率最高的字母(不区分大小写),并统计出
其出现的次数。
例如:形参s所指的字符串为:abcAbsmaxless,程序执行后的输出结果为:
Letter ‘a’: 3 times
Letter ‘s’: 3 times
#include
#include
#include
void fun(char *s)
{ int k[26]={0},n,i,max=0; char ch;
while(*s)
{ if( isalpha(*s) ) {
/**********found**********/
ch=tolower(*s);
n=ch-'a';
/**********found**********/
k[n]+= 1
}
s++;
/**********found**********/
if(max}
printf("
After count :
");
for(i=0; i<26;i++)
if (k[i]==max) printf("
letter '%c' : %d times
",i+'a);
n=fun(s,t);
printf("
There are %d letter which ASCII code is less than 97: %s
",n,t);
}
50.给定程序中,函数fun的功能是:有N×N矩阵,以主对角线为对称线,对称元素相加并将结果存放在左下三角
元素中,右上三角元素置0。例如,若N=3,有下列矩阵:
1 2 3
4 5 6
7 8 9
计算结果为:
1 0 0
6 5 0
10 14 9
#include
#define N 4
/**********found**********/
void fun(int (*t)[N])
{ int i, j;
for(i=1; i{ for(j=0; j{
/**********found**********/
t[i][j] =t[i][j]+t[j][i];
/**********found**********/
t[j][i]=0;
}
}
}
main()
{ int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;
printf("
The original array:
");
for(i=0; i{ for(j=0; jprintf("
");
}
fun(t);
printf("
The result is:
");
for(i=0; i{ for(j=0; jprintf("
");
}
}

51.给定程序中,函数fun的功能是:计算出形参s所指字符串中包含的单词个数,作为函数值返回。为便于统计
,规定各单词之间用空格隔开。
例如,形参s所指的字符串为:This is a C language program.。函数的返回值为6。
#include
int fun(char *s)
{ int n=0, flag=0;
while(*s!='0')
{ if(*s!=' ' && flag==0) {
/**********found**********/
n++ flag=1;}
/**********found**********/
if (*s==' ') flag= 0
/**********found**********/
s++
}
return n;
}
main()
{ char str[81]; int n;
printf("
Enter a line text:
"); gets(str);
n=fun(str);
printf("
There are %d words in this text.

",n);
}
52.给定程序中,函数fun的功能是:将N×N矩阵中元素的值按列右移1个位置,右边被移出矩阵的元素绕回右边
。例如,N=3,有下列矩阵:
1 2 3
4 5 6
7 8 9
计算结果为
3 1 2
6 4 5
9 7 8
#include
#define N 4
void fun(int (*t)[N])
{ int i, j, x;
/**********found**********/
for(i=0; i{
/**********found**********/
x=t[i][N-1]
for(j=N-1; j>=1; j--)
t[i][j]=t[i][j-1];
/**********found**********/
t[i][0]=x;
}
}
main()
{ int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;
printf("The original array:
");
for(i=0; i{ for(j=0; jprintf("
");
}
fun(t);
printf("
The result is:
");
for(i=0; i{ for(j=0; jprintf("
");
}
}

53.函数fun的功能是:计算

直到 ,若x=2.5,函数值为:1.917915。
#include
#include
double fun(double x)
{ double f, t; int n;
f = 1.0 + x;
/**********found***
*******/
t = x;
n = 1;
do {
n++;
/**********found**********/
t *= (-1.0)*x/n;
f += t;
}
/**********found**********/
while (fabs(t) >= 1e-6);
return f;
}
main()
{ double x, y;
x=2.5;
y = fun(x);
printf("
The result is :
");
printf("x=%-12.6f y=%-12.6f
", x, y);
}
54 给定程序中,函数fun的功能是:计算出带有头结点的单向链表中各结点数据域中值之和作为函数值返回。
#include
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(int *);
void outlist(SLIST *);
int fun( SLIST *h)
{ SLIST *p; int s=0;
p=h->next;
while(p)
{
/**********found**********/
s+= p->data;
/**********found**********/
p=p->next;
}
return s;
}
main()
{ SLIST *head;
int a[N]={12,87,45,32,91,16,20,48};
head=creatlist(a); outlist(head);
/**********found**********/
printf("
sum=%d
", fun(head));
}

SLIST *creatlist(int a[])
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("The list is NULL!
");
else
{ printf("
Head ");
do
{ printf("->%d", p->data); p=p->next; }
while(p!=NULL);
printf("->End
");
}
}

55.给定程序中,函数fun的功能是:判断形参s所指字符串是否是“回文”,若是,函数返回值为1;不是,函数
返回值为0。“回文”是正读和反读都一样的字符串(不区分大小写字母)。
例如:LEVEL和Level是“回文”,而LEVLEV不是“回文”。
#include
#include
#include
int fun(char *s)
{ char *lp,*rp;
/**********found**********/
lp= s
rp=s+strlen(s)-1;
while((toupper(*lp)==toupper(*rp)) && (lp/**********found**********/
lp++; rp -- }
/**********found**********/
if(lpelse return 1;
}
main()
{ char s[81];
printf("Enter a string: "); scanf("%s",s);
if(fun(s)) printf("
"%s" is a Palindrome.

",s);
else printf("
"%s" isn't a Palindrome.

",s);
}
56.给定程序的功能是:从键盘输入若干行文本(每行不超过80个字符),写到文件中,用-1作为字
符串输入结束的标志。然后将文件的内容读出显示在屏幕上。文件的读写分别由自定义函数ReadText和
WriteText实现。
#include
#include
#include
void WriteText(FILE *);
void ReadText(FILE *);
main()
{ FILE *fp;
if((fp=fopen("","w"))==NULL)
{ printf(" open fail!!
"); exit(0); }
WriteText(fp);
fclose(fp);
if((fp=fopen(""
,"r"))==NULL)
{ printf(" open fail!!
"); exit(0); }
ReadText(fp);
fclose(fp);
}
/**********found**********/
void WriteText(FILE *fw)
{ char str[81];
printf("
Enter string with -1 to end :
");
gets(str);
while(strcmp(str,"-1")!=0) {
/**********found**********/
fputs(str,fw); fputs("
",fw);
gets(str);
}
}
void ReadText(FILE *fr)
{ char str[81];
printf("
Read file and output to screen :
");
fgets(str,81,fr);
while( !feof(fr) ) {
/**********found**********/
printf("%s",str);
fgets(str,81,fr);
}
}

57.函数fun的功能是:把形参a所指数组中的最小值放在元素a[0]中,接着把形参a所指数组中的最大值放在a[1]
元素中;再把a所指数组元素中的次小值放在a[2]中,把a所指数组元素中的次大值放在a[3];其余以此类推。例
如:若a所指数组中的数据最初排列为:9、`、4、2、3、6、5、8、7;则按规则移动后,数据排列为:1、9、2
、8、3、7、4、6、5。形参n中存放a所指数组中数据的个数。
注意:规定fun函数中的max存放当前所找的最大值,px存放当前所找最大值的下标。
# include
#define N 9
void fun(int a[], int n)
{ int i,j, max, min, px, pn, t;
for (i=0; i{
/**********found**********/
max = min = a[i];
px = pn = i;
for (j=i+1; j/**********found**********/
if (max{ max = a[j]; px = j; }
/**********found**********/
if (min>a[j])
{ min = a[j]; pn = j; }
}
if (pn != i)
{ t = a[i]; a[i] = min; a[pn] = t;
if (px == i) px =pn;
}
if (px != i+1)
{ t = a[i+1]; a[i+1] = max; a[px] = t; }
}
}
main()
{ int b[N]={9,1,4,2,3,6,5,8,7}, i;
printf("
The original data :
");
for (i=0; iprintf("
");
fun(b, N);
printf("
The data after moving :
");
for (i=0; iprintf("
");
}
58.给定程序中,函数fun的功能是:把形参s所指字符串中最右边的n个字符复制到形参t所指字符数组中,形成
一个新串。若s所指字符串的长度小于n,则将整个字符串复制到形参t所指字符数组中。
例如,形参s所指的字符串为:abcdefgh,n的值为5,程序执行后t所指字符数组中的字符串应为:defgh。
#include
#include
#define N 80
void fun(char *s, int n, char *t)
{ int len,i,j=0;
len=strlen(s);
/**********found**********/
if(n>=len) strcpy(t,s);
else {
/**********found**********/
for(i=len-n; i<=len-1; i++) t[j++]= s[i]
/**********found**********/
t[j]= 0
}
}
main()
{ char s[N],t[N]; int n;
printf("Enter a string: ");gets(s);
printf( "Enter n:"); scanf("%d",&n);
fun(s,n,t);
printf("The string t : ");
puts(t);
}

59.给定程序中,函数fun的功能是:在3×4的矩阵中找出在行上最大、在列上最小的那个元素,若没有符合条件
的元素则输出相应信息。
例如,有下列矩阵:
1 2 13 4
7 8 10 6
3 5 9 7
程序执行结果为:find:a[2][2]=9
#include
#define M 3
#define N 4
void fun(int (*a)[N])
{ int i=0,j,find=0,rmax,c,k;
while( (i{ rmax=a[i][0]; c=0;
for(j=1; jif(rmax/**********found**********/
rmax=a[i][j]; c= j }
find=1; k=0;
while(k/**********found**********/
if (k!=i && a[k][c]<=rmax) find= 0
k++;
}
if(find) printf("find: a[%d][%d]=%d
",i,c,a[i][c]);
/**********found**********/
i++
}
if(!find) printf("not found!
");
}
main()
{ int x[M][N],i,j;
printf("Enter number for array:
");
for(i=0; ifor(j=0; jprintf("The array:
");
for(i=0; i{ for(j=0; jprintf("

");
}
fun(x);
}
60.给定程序中,函数fun的功能是:将形参所指结构体数组中的三个元素按num成员进行升序排列。
#include
typedef struct
{ int num;
char name[10];
}PERSON;
/**********found**********/
void fun(PERSON *std)
{
/**********found**********/
PERSON temp;
if(std[0].num>std[1].num)
{ temp=std[0]; std[0]=std[1]; std[1]=temp; }
if(std[0].num>std[2].num)
{ temp=std[0]; std[0]=std[2]; std[2]=temp; }
if(std[1].num>std[2].num)
{ temp=std[1]; std[1]=std[2]; std[2]=temp; }
}
main()
{ PERSON std[ ]={ 5,"Zhanghu",2,"WangLi",6,"LinMin" };
int i;
/**********found**********/
fun(std);
printf("
The result is :
");
for(i=0; i<3; i++)
printf("%d,%s
",std[i].num,std[i].name);
}

61.函数fun的功能是进行字母转换。若形参ch中是小写英文字母,则转换成对应的大写英文字母;若ch中是大写
英文字母,则转换成对应的小写英文字母;若是其它字符则保持不变;并将转换后的结果作为函数值返回。
#include
#include
char fun(char ch)
{
/**********found**********/
if ((ch>='a')&&(ch<='z'))
return ch -'a' + 'A';
if ( isupper(ch) )
/**********found**********/
return ch +'a'-'A'
/**********found**********/
return ch;
}
main()
{ char c1, c2;
printf("
The result :
");
c1='w'; c2 = fun(c1);
printf("c1=%c c2=%c
", c1, c2);
c1='W'; c2 = fun(c1);
printf("c1=%c c2=%c
", c1, c2);
c1='8'; c2 = fun(c1);
printf("c1=%c c2=%c
", c1, c2);
}
62.给定程序中,函数fun的功能是:把形参s所指字符串中下标为奇数的字符右移到下一个奇数位置,最右边被
移出字符串的字符绕回到第一个奇数位置,下
标为偶数的字符不动(注:字符串的长度大于等于2)。例如,形
参s所指的字符串为:abcdefgh,执行结果为:ahcbedgf。
#include
void fun(char *s)
{ int i, n, k; char c;
n=0;
for(i=0; s[i]!='0'; i++) n++;
/**********found**********/
if(n%2==0) k=n-1;
else k=n-2;
/**********found**********/
c=s[k];
for(i=k-2; i>=1; i=i-2) s[i+2]=s[i];
/**********found**********/
s[1]=c;
}
main()
{ char s[80]="abcdefgh";
printf("
The original string is : %s
",s);
fun(s);
printf("
The result is : %s
",s);
}

63.给定程序中,函数fun的功能是:有N×N的矩阵,根据给定的m(m<=N)值,将每行元素中的值均右移m个位置
,左边置为0。例如:N=3,m=2,有下列矩阵
1 2 3
4 5 6
7 8 9
程序执行结果为
0 0 1
0 0 4
0 0 7
#include
#define N 4
void fun(int (*t)[N], int m)
{ int i, j;
/**********found**********/
for(i=0; i{ for(j=N-1-m; j>=0; j--)
/**********found**********/
t[i][j+m]=t[i][j];
/**********found**********/
for(j=0; jt[i][j]=0;
}
}
main()
{ int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j, m;
printf("
The original array:
");
for(i=0; i{ for(j=0; jprintf("%2d ",t[i][j]);
printf("
");
}
printf("Input m (m<=%d): ",N);scanf("%d",&m);
fun(t,m);
printf("
The result is:
");
for(i=0; i{ for(j=0; jprintf("%2d ",t[i][j]);
printf("
");
}
}
64.给定程序中,函数fun的功能是:将a所指3×5矩阵中第k列的元素左移到第0列。第k列以后的每列元素行依次
左移,原来左边的各列依次绕到右边。
例如,有下列矩阵
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
若k为2,程序执行结果为
3 4 5 1 2
3 4 5 1 2
3 4 5 1 2
#include
#define M 3
#define N 5
void fun(int (*a)[N],int k)
{ int i,j,p,temp;
/**********found**********/
for(p=1; p<= k; p++)
for(i=0; i{ temp=a[i][0];
/**********found**********/
for(j=0; j< N-1 j++) a[i][j]=a[i][j+1];
/**********found**********/
a[i][N-1]= temp;
}
}
main( )
{ int x[M][N]={ {1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5} },i,j;
printf("The array before moving:

");
for(i=0; i{ for(j=0; jprintf("
");
}
fun(x,2);
printf("The array after moving:

");
for(i=0; i{ for(j=0; jprintf("
");
}
}

65.给定程序中,函数fun的功能是:将a所指4×3矩阵中第k行的元素与第0行元素交换。
例如,有下列矩阵:
1 2 3
4 5 6
7 8 9
10 11 12
若k为2,程序执行结果为:
7 8
9
4 5 6
1 2 3
10 11 12
#include
#define N 3
#define M 4
/**********found**********/
void fun(int (*a)[N], int k)
{ int i,j,temp
/**********found**********/
for(i = 0 i < N i++)
{ temp=a[0][i]
/**********found**********/
a[0][i] = a[k][i]
a[k][i] = temp
}
}
main()
{ int x[M][N]={ {1,2,3},{4,5,6},{7,8,9},{10,11,12} },i,j;
printf("The array before moving:

");
for(i=0; i{ for(j=0; jprintf("

");
}
fun(x,2);
printf("The array after moving:

");
for(i=0; i{ for(j=0; jprintf("

");
}
}
66.给定程序中,函数fun的功能是:将形参std所指结构体数组中年龄最大者的数据作为函数值返回,并在main
函数中输出。
#include
typedef struct
{ char name[10];
int age;
}STD;
STD fun(STD std[], int n)
{ STD max; int i;
/**********found**********/
max= *std;
for(i=1; i/**********found**********/
if(return max;
}
main( )
{ STD std[5]={"aaa",17,"bbb",16,"ccc",18,"ddd",17,"eee",15 };
STD max;
max=fun(std,5);
printf("
The result:
");
/**********found**********/
printf("
Name : %s, Age : %d
", ,);
}

67.给定程序中,函数fun的功能是:调用随机函数产生20个互不相同的整数放在形参a所指数组中(此数组在主
函数中已置0)。
#include
#define N 20
void fun( int *a)
{ int i, x, n=0;
x=rand()%20;
/**********found**********/
while (n{ for(i=0; i/**********found**********/
if( x==a[i] ) break;
/**********found**********/
if( i==n){ a[n]=x; n++; }
x=rand()%20;
}
}
main()
{ int x[N]={0} ,i;
fun( x );
printf("The result :
");
for( i=0; i{ printf("%4d",x[i]);
if((i+1)%5==0)printf("
");
}
printf("

");
}
68.给定程序中,函数fun的功能是:求ss所指字符串数组中长度最长的字符串所在的行下标,作为函数值返回,
并把其吕长放在形参n所指变量中。Ss所指字符串数组中共有M个字符串,且串长#include
#define M 5
#define N 20
/**********found**********/
int fun(char (*ss) [N], int *n)
{ int i, k=0, len=0;
for(i=0; i{ len=strlen(ss[i]);
/**********found**********/
if(i==0) *n=len;
if(len>*n) {
/**********found**********/
*n=len;
k=i;
}
}
return(k);
}
main()
{ char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};
int n,k,i;
printf("
The original strings are :
");
for(i=0;ik=fun(ss,&n);
printf("
The length of longest string is : %d
",n);
printf("
The longest string is : %s
",ss[k]);
}

69.
给定程序中,函数fun的功能是将a和b所指的两个字符串转换成面值相同的整数,并进行相加作为函数值返回
,规定字符串中只含9个以下数字字符。
例如:主函数中输入字符串:32486和12345,在主函数中输出的函数值为:44831。
#include
#include
#include
#define N 9
long ctod( char *s )
{ long d=0;
while(*s)
if(isdigit( *s)) {
/**********found**********/
d=d*10+*s-'0';
/**********found**********/
s++; }
return d;
}
long fun( char *a, char *b )
{
/**********found**********/
return ctod(a)+ctod(b);
}
main()
{ char s1[N],s2[N];
do
{ printf("Input string s1 : "); gets(s1); }
while( strlen(s1)>N );
do
{ printf("Input string s2 : "); gets(s2); }
while( strlen(s2)>N );
printf("The result is: %ld
", fun(s1,s2) );
}
70.给定程序中,函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),作为函数值
返回;并将大于平均值的数放在形参y所指数组中,在主函数中输出。
例如,有10个正数:46 30 32 40 6 17 45 15 48 26,平均值为:30.500000
主函数中输出:46 32 40 45 48
#include
#define N 10
double fun(double x[],double *y)
{ int i,j; double av;
/**********found**********/
av=0;
/**********found**********/
for(i=0; ifor(i=j=0; i/**********found**********/
if(x[i]>av) y[j++]= x[i];
y[j]=-1;
return av;
}
main()
{ int i; double x[N],y[N];
for(i=0; iprintf("
");
printf("
The average is: %f
",fun(x,y));
for(i=0; y[i]>=0; i++) printf("%5.1f ",y[i]);
printf("
");
}

71.给定程序中,函数fun的功能是:将形参s所指字符串中的所有数字字符顺序前移,其他字符顺序后移,处理
后新字符串的首地址作为函数值返回。
例如,s所指字符串为:asd123fgh5##43df,处理后新字符串为:123543asdfgh##df。
#include
#include
#include
#include
char *fun(char *s)
{ int i, j, k, n; char *p, *t;
n=strlen(s)+1;
t=(char*)malloc(n*sizeof(char));
p=(char*)malloc(n*sizeof(char));
j=0; k=0;
for(i=0; i{ if(isdigit(s[i])) {
/**********found**********/
p[j]=s[i]; j++;}
else
{ t[k]=s[i]; k++; }
}
/**********found**********/
for(i=0; ip[j+k]=0;
/**********found**********/
return p;
}
main()
{ char s[80];
printf("Please input: "); scanf("%s",s);
printf("
The result is: %s
",fun(s));
}
72.给定程序中,函数fun的功能是计算下式:
,直到 ,并把计算结果作为函数值返回。
例如,若形参e的值为1e-3,函数的返回值2.735678。
#include
double fun(double e)
{ int i;
double s, x;
/**********found**********/
s=0; i=0;
x=1.0;
while(x>e){
/**********found**********/
i++;
/**********found**********/
x=(2.0*i-1)/((2.0*i)*(2.0*i));
s=s+x;
}
return s;
}
main()
{ double e=1e-3;
printf("
The result is: %f
",fun(e));
}
73.给定程序中,函数fun的功能是计算下式:
,直到 ,并把计算结果作为函数值返回。
例如:若形参e的值为1e-3,函数的返回值为0.551690。
#include
double fun(double e)
{ int i, k; double s, t, x;
s=0; k=1; i=2;
/**********found**********/
x=3./4;
/**********found**********/
while(x > e)
{ s=s+k*x;
k=k* (-1);
t=2*i;
/**********found**********/
x=(2*i+1)/(t*t);
i++;
}
return s;
}
main()
{ double e=1e-3;
printf("
The result is: %f
",fun(e));
}

74.人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中。函数fun的功
能是:找出指定出生年份的人员,将其数据放在形参k所指的数组中,由主函数输出,同时由函数值返回满足指
定条件的人数。
#include
#define N 8
typedef struct
{ int num;
int year,month,day
}STU;
int fun(STU *std, STU *k, int year)
{ int i,n=0;
for (i=0; i/**********found**********/
if(std[i].year==year)
/**********found**********/
k[n++]= std[i];
/**********found**********/
return (n);
}
main()
{ STU std[N]={ {1,1984,2,15},{2,1983,9,21},{3,1984,9,1},{4,1983,7,15},
{5,1985,9,28},{6,1982,11,15},{7,1982,6,22},{8,1984,8,19}};
STU k[N]; int i,n,year;
printf("Enter a year : "); scanf("%d",&year);
n=fun(std,k,year);
if(n==0)
printf("
No person was born in %d
",year);
else
{ printf("
These persons were born in %d
",year);
for(i=0; iprintf("%d %d-%d-%d
",k[i].num,k[i].year,k[i].month,k[i].day);
}
}
75.给定程序中,函数fun的功能是:对形参ss所指字符串数组中的M个字符串按长度由短到长进行排序。ss所指
字符串数组中共有M个字符串,且串长#include
#include
#define M 5
#define N 20
void fun(char (*ss)[N])
{ int i, j, k, n[M]; char t[N];
for(i=0; ifor(i=0; i{ k=i;
/**********found**********/
for(j=i+1; j/**********found**********/
if(n[k]>n[j]) k=j;
if(k!=i)
{ strcpy(t,ss[i]);
strcpy(ss[i],ss[k]);
/**********found**********/
strcpy(ss[k],t);
n[k]=n[i];
}
}
}
main()
{ char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};
int i;
printf("
The original strings are :
");
for(i=0; i",ss[i]);
printf("
");
fun(ss);
printf("
nThe result :
");
for(i=0; i",ss[i]);
}

76.给定程序中,函数fun的功能是:计算下式前n项的和作为函数值返回。

例如:当形参n的值为10时,函数返回-0.204491。
#include
double fun(int n)
{ int i, k; double s, t;
s=0;
/**********found**********/
k=1;
for(i=1; i<=n; i++) {
/**********found**********/
t=2*i;
s=s+k*(2*i-1)*(2*i+1)/(t*t);
/**********found**********/
k=k*(-1);
}
return s;
}
main()
{ int n=-1;
while(n<0)
{ printf("Please input(n>0): "); scanf("%d",&n); }
printf("
The result is: %f
",fun(n));
}
77.给定程序中,函数fun的功能是:将形参n中,各位上为偶数的数取出,并按原来从高位到低位相反的顺序组
成一个新的数,并作为函数值返回。
例如,输入一个整数:27638496,函数返回值为:64862。
#include
unsigned long fun(unsigned long n)
{ unsigned long x=0; int t;
while(n)
{ t=n%10;
/**********found**********/
if(t%2==0)
/**********found**********/
x=10*x+t;
/**********found**********/
n=n/10;
}
return x;
}
main()
{ unsigned long n=-1;
while(n>99999999||n<0)
{ printf("Please input(0printf("
The result is: %ld
",fun(n));
}
78.给定程序中,函数fun的功能是:将N×N矩阵主对角线元素中的值与反向对角线对应位置上元素中的值进行交
换。例如,若N=3,有下列矩阵:
1 2 3
4 5 6
7 8 9
交换后为:
3 2 1
4 5 6
9 8 7
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
#include
#define N 4
/**********found**********/
void fun(int t[][N] , int n)
{ int i,s;
/**********found**********/
for(i=0;i{ s=t[i][i];
t[i][i]=t[i][n-i-1];
/**********found**********/
t[i][n-1-i]=s;
}
}
main()
{ int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;
printf("
The original array:
");
for(i=0; i{ for(j=0; jprintf("
");
}
fun(t,N);
printf("
The result is:
");
for(i=0; i{ for(j=0; jprintf("
");
}
}

79.给定程序中,函数fun的功能是:求ss所指字符串数组中长度最短的字符串所在的行下标,作为函数值返回,
并把其串长放在形参n所指变量中。ss所指字符串数组中共有M个字符串,且串长#include
#include
#define M 5
#define N 20
int fun(char (*ss)[N], int *n)
{ int i, k=0, len= N;
/**********found**********/
for(i=0; i{ len=strlen(ss[i]);
if(i==0) *n=len;
/**********found**********/
if(len < *n)
{ *n=len;
k=i;
}
}
/**********found***
*******/
return(k);
}
main()
{ char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","chongqing"};
int n,k,i;
printf("
The original strings are :
");
for(i=0;ik=fun(ss,&n);
printf("
The length of shortest string is : %d
",n);
printf("
The shortest string is : %s
",ss[k]);
}
80.给定程序中,函数fun的功能是:将形参n中,各位上为偶数的数取出,并按原来从高到低位的顺序组成一个
新的数,并作为函数值返回。
例如,从主函数输入一个整数:27638496,函数返回值为:26846。
#include
unsigned long fun(unsigned long n)
{ unsigned long x=0, s, i; int t;
s=n;
/**********found**********/
i=1;
/**********found**********/
while(s)
{ t=s%10;
if(t%2==0){
/**********found**********/
x=x+t*i; i=i*10;
}
s=s/10;
}
return x;
}
main()
{ unsigned long n=-1;
while(n>99999999||n<0)
{ printf("Please input(0printf("
The result is: %ld
",fun(n));
}
81.给定程序中,函数fun的功能是:在形参s所指字符串中的每个数字字符之后插入一个*号。例如,形参s所指
的字符串为:def35adh3kjsdf7。执行结果为:def3*5*adh3*kjsdf7*。
#include
void fun(char *s)
{ int i, j, n;
for(i=0; s[i]!='0'; i++)
/**********found**********/
if(s[i]>='0' && s[i]<='9')
{ n=0;
/**********found**********/
while(s[i+1+n]!= 0) n++;
for(j=i+n+1; j>i; j--)
/**********found**********/
s[j+1]= s[j];
s[j+1]='*';
i=i+1;
}
}
main()
{ char s[80]="ba3a54cd23a";
printf("
The original string is : %s
",s);
fun(s);
printf("
The result is : %s
",s);
}

82.给定程序中,函数fun的功能是:找出100~999之间(含100和999)所有整数中各位上数字之和为x(x为一正
整数)的整数,然后输出;符合条件的整数个数作为函数值返回。
例如,当x值为5时,100~999之间各位上数字之和为5的整数有:104、113、122、131、140、203、212、221、
230、302、311、320、401、410、500。共有15个。当x值为27时,各位数字之和为27的整数是:999。只有1个。
#include
fun(int x)
{ int n, s1, s2, s3, t;
n=0;
t=100;
/**********found**********/
while(t<=999){
/**********found**********/
s1=t%10; s2=(t/10)%10; s3=t/100;
/**********found**********/
if(s1+s2+s3==x)
{ printf("%d ",t);
n++;
}
t++;
}
return n;
}
main()
{ int x=-1;
while(x<0)
{ printf("Please input(x>0): "); scanf("%d",&x); }
printf("
The result is: %d
",fun(x));
}
83.给定程序中,函数fun的功能是:找出100至x(x≤999)之间各位上的数字之和为15的所有整数,然后输出:
符合条件的整数个数作为函数值返回。

如:当n值为500时,各位数字之和为15的整数有:159、168、177、186、195、249、258、267、276、285、
294、339、348、357、366、375、384、393、429、438、447、456、465、474、483、492。共有26个。
#include
fun(int x)
{ int n, s1, s2, s3, t;
/**********found**********/
n=0;
t=100;
/**********found**********/
while(t<=x)
{ s1=t%10; s2=(t/10)%10; s3=t/100;
if(s1+s2+s3==15)
{ printf("%d ",t);
n++;
}
/**********found**********/
t++;
}
return n;
}
main()
{ int x=-1;
while(x>999||x<0)
{ printf("Please input(0printf("
The result is: %d
",fun(x));
}
84.函数fun的功能是:从三个形参a,b,c中找出中间的那个数,作为函数值返回。
例如:当a=3,b=5,c=4时,中数为4。
#include
int fun(int a, int b, int c)
{
int t;
/**********found**********/
t = (a>b) ? (b>c? b :(a>c?c:a)) : ((a>c)?a : ((b>c)?c:b));
return t;
}
main()
{ int a1=3, a2=5, a3=4, r;
r = fun(a1, a2, a3);
printf("
The middle number is : %d
", r);
}

85.给定程序的功能是调用fun函数建立班组通讯录。通讯录中记录每位学生的编号、姓名和电话号码。班组的人
数和学生的信息从键盘读入,每个人的信息作为一个数据块写到名为的二进制文件中。
#include
#include
#define N 5
typedef struct
{ int num;
char name[10];
char tel[10];
}STYPE;
void check();

/**********found**********/
int fun(STYPE *std)
{
/**********found**********/
FILE *fp; int i;
if((fp=fopen("","wb"))==NULL)
return(0);
printf("
Output data to file !
");
for(i=0; i/**********found**********/
fwrite(&std[i], sizeof(STYPE), 1, fp);
fclose(fp);
return (1);
}
main()
{ STYPE s[10]={ {1,"aaaaa","111111"},{1,"bbbbb","222222"},{1,"ccccc","333333"},
{1,"ddddd","444444"},{1,"eeeee","555555"}};
int k;
k=fun(s);
if (k==1)
{ printf("Succeed!"); check(); }
else
printf("Fail!");
}
void check()
{ FILE *fp; int i;
STYPE s[10];
if((fp=fopen("","rb"))==NULL)
{ printf("Fail !!
"); exit(0); }
printf("
Read file and output to screen :
");
printf("
num name tel
");
for(i=0; i{ fread(&s[i],sizeof(STYPE),1, fp);
printf("%6d %s %s
",s[i].num,s[i].name,s[i].tel);
}
fclose(fp);
}
86.甲乙丙丁四人同时开始放鞭炮,甲每隔t1秒放一次,乙每隔t2秒放一次,丙每隔t3秒放一次,丁每隔t4秒放
一次,每人各放n次。函数fun的功能是根据形参提供的值,求出总共听到多少次鞭炮声作为函数值返回。注意,
当几个鞭炮同时炸响,只算一次响声,第一次响声是在第0秒。
例如,若t1=7, t2=5,
t3=6, t4=4 ,n=10,则总共可听到28次鞭炮声。
#include
/**********found**********/
#define OK(i, t, n) ((i%t==0) && (i/tint fun(int t1, int t2, int t3, int t4, int n)
{ int count, t , maxt=t1;
if (maxt < t2) maxt = t2;
if (maxt < t3) maxt = t3;
if (maxt < t4) maxt = t4;
count=1; /* 给count赋初值 */
/**********found**********/
for(t=1; t< maxt*(n-1); t++)
{
if(OK(t, t1, n) || OK(t, t2, n)|| OK(t, t3, n) || OK(t, t4, n) )
count++;
}
/**********found**********/
return count;
}
main()
{ int t1=7, t2=5, t3=6, t4=4, n=10, r;
r = fun(t1, t2, t3, t4, n);
printf("The sound : %d
", r);
}

87.函数fun的功能是:统计长整数n的各个位上出现数字1、2、3的次数,并通过外部(全局)变量c1,c2,c3返
回主函数。例如:当n=123114350时,结果应该为:c1=3 c2=1 c3=2。
#include
int c1,c2,c3;
void fun(long n)
{ c1 = c2 = c3 = 0;
while (n) {
/**********found**********/
switch(n%10)
{
/**********found**********/
case 1: c1++;break;
/**********found**********/
case 2: c2++;break;
case 3: c3++;
}
n /= 10;
}
}
main()
{ long n=123114350L;
fun(n);
printf("
The result :
");
printf("n=%ld c1=%d c2=%d c3=%d
",n,c1,c2,c3);
}
88.函数fun的功能是:把形参a所指数组中的最大值放在a[0]中,接着求出a所指数组中的最小值放在a[1]中;再
把a所指数组元素中的次大值放在a[2]中,把a数组元素中的次小值放在a[3]中;其余以此类推。例如:若a所指
数组中的数据最初排列为:1、4、2、3、9、6、5、8、7,则按规则移动后,数据排列为:9、1、8、2、7、3、6
、4、5。形参n中存放a所指数组中数据的个数。
#include
#define N 9
/**********found**********/
void fun(int *a, int n)
{ int i, j, max, min, px, pn, t;
/**********found**********/
for (i=0; i{ max = min = a[i];
px = pn = i;
/**********found**********/
for (j=i+1; j{ if (max < a[j])
{ max = a[j]; px = j; }
if (min > a[j])
{ min = a[j]; pn = j; }
}
if (px != i)
{ t = a[i]; a[i] = max; a[px] = t;
if (pn == i) pn= px;
}
if (pn != i+1)
{ t = a[i+1]; a[i+1] = min; a[pn] = t; }
}
}
main()
{ int b[N]={1,4,2,3,9,6,5,8,7}, i;
printf("
The original data :
");
for (i=0; iprintf("
");
fun(b, N);
printf("
The data after moving :
");
for (i=0; iprintf("
");
}

89.给定程序中,函数fun的功能是:求出形参ss所指字符串数组中最长字符串的长度,其余字符串左边用字符*
补齐,使其与最长的字符串等长。字符串数组中共有M个字
符串,且串长#include
#include
#define M 5
#define N 20
void fun(char (*ss)[N])
{ int i, j, k=0, n, m, len;
for(i=0; i{ len=strlen(ss[i]);
if(i==0) n=len;
if(len>n) {
/**********found**********/
n=len; k=i;
}
}
for(i=0; iif (i!=k)
{ m=n;
len=strlen(ss[i]);
/**********found**********/
for(j=len; j>=0; j--)
ss[i][m--]=ss[i][j];
for(j=0; j/**********found**********/
ss[i][j]='*';
}
}
main()
{ char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};
int i;
printf("
The original strings are :
");
for(i=0; i",ss[i]);
printf("
");
fun(ss);
printf("
The result:
");
for(i=0; i",ss[i]);
}
90.函数fun的功能是:统计所有小于等于n(n>2)的素数的个数,素数的个数作为函数值返回。
#include
int fun(int n)
{ int i,j, count=0;
printf("
The prime number between 3 to %d
", n);
for (i=3; i<=n; i++) {
/**********found**********/
for (j=2; j/**********found**********/
if (i%j == 0)
break;
/**********found**********/
if (j>=i)
{ count++; printf( count%15? "%5d":"
%5d",i); }
}
return count;
}
main()
{ int n=20, r;
r = fun(n);
printf("
The number of prime is : %d
", r);
}

91.函数fun的功能是,计算:

直到 。
若x=2.5,函数值为:12.182494。
#include
#include
double fun(double x)
{ double f, t; int n;
/**********found**********/
f = 1.0+x;
t = x;
n = 1;
do {
n++;
/**********found**********/
t *= x/n;
/**********found**********/
f += t;
} while (fabs(t) >= 1e-6);
return f;
}
main()
{ double x, y;
x=2.5;
y = fun(x);
printf("
The result is :
");
printf("x=%-12.6f y=%-12.6f
", x, y);
}
92.函数fun的功能是,计算
的前n项。
若x=2.5,函数值为:12.182340。
#include
double fun(double x, int n)
{ double f, t; int i;
f = 1.0;
/**********found**********/
t = 1;
/**********found**********/
for (i=1; i{
/**********found**********/
t *= x/i;
f += t;
}
return f;
}
main()
{ double x, y;
x=2.5;
y = fun(x, 12);
printf("
The result is :
");
printf("x=%-12.6f y=%-12.6f
", x, y);
}

93.给定程序中已建立一个带有头结点的单向链表,在main函数中将多次调用fun函数,每调用一次fun函数,输
出链表尾部结点中的数据,并释放该结点,使链表缩短。
#include
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;

void fun( SLIST *p)
{ SLIST *t, *s;
t=p->
next; s=p;
while(t->next != NULL)
{ s=t;
/**********found**********/
t=t->next;
}
/**********found**********/
printf(" %d ",t->data);
s->next=NULL;
/**********found**********/
free(t);
}
SLIST *creatlist(int *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("
The list is NULL!
");
else
{ printf("
Head");
do { printf("->%d",p->data); p=p->next; } while(p!=NULL);
printf("->End
");
}
}
main()
{ SLIST *head;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a);
printf("
Output from head:
"); outlist(head);
printf("
Output from tail:
");
while (head->next != NULL){
fun(head);
printf("

");
printf("
Output from head again :
"); outlist(head);
}}
94.函数fun的功能是,计算

的前n 项之和。若x=2.5,n=15时,函数值为:1.917914。
#include
#include
double fun(double x, int n)
{ double f, t; int i;
/**********found**********/
f = 1;
t = -1;
for (i=1; i{
/**********found**********/
t *= (-1)*x/n;
/**********found**********/
f += t;
}
return f;
}
main()
{ double x, y;
x=2.5;
y = fun(x, 15);
printf("
The result is :
");
printf("x=%-12.6f y=%-12.6f
", x, y);
}

95.给定程序中,函数fun的功能是:计算N×N矩阵的主对角线元素和反向对角线元素之和,并作为函数值返回。
注意:要求先累加主对角线元素中的值,然后累加反向对角线元素中的值。例如,若N=3,有下列矩阵:
1 2 3
4 5 6
7 8 9
Fun函数首先累加1、5、9,然后累加3、5、7,函数的返回值为30。
#include
#define N 4
fun(int t[][N], int n)
{ int i, sum;
/**********found**********/
sum=0;
for(i=0; i/**********found**********/
sum+=t[i][i]
for(i=0; i/**********found**********/
sum+= t[i][n-i-1]
return sum;
}
main()
{ int t[][N]={21,2,13,24,25,16,47,38,29,11,32,54,42,21,3,10},i,j;
printf("
The original data:
");
for(i=0; i{ for(j=0; jprintf("
");
}
printf("The result is: %d",fun(t,N));
}
96.给定程序中,函数fun的功能是:有N×N矩阵,将知阵的外围元素顺时针旋转。操作顺序是:首先将第一行元
素的值存入临时数组r,然后使第一列成为第一行,最后一行成为第一列,最后一列成为最后一行,临时数组中
的元素成为最后一列。例如,若N=3,有下列矩阵:
1 2 3
4 5 6
7 8 9
计算结果为:
7 4 1
8 5 2
9 6 3
#include h>
#define N 4
void fun(int (*t)[N])
{ int j ,r[N];
for(j=0; jfor(j=0; j/**********found**********/
t[0][N-j-1]=t[j][0];
for(j=0; jt[j][0]=t[N-1][j];
/**********found**********/
for(j=N-1; j>=0;j--)
t[N-1][N-1-j]=t[j][N-1];
for(j=N-1; j>=0; j--)
/**********found**********/
t[j][N-1]=r[j];
}
main()
{ int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;
printf("
The original array:
");
for(i=0; i{ for(j=0; jprintf("
");
}
fun(t);
printf("
The result is:
");
for(i=0; i{ for(j=0; jprintf("
");
}
}

97.函数fun的功能是:逆置数组元素中的值。例如:若a所指数组中的数据依次为1、2、3、4、5、6、7、8、9,
则逆置后依次为:9、8、7、6、5、4、3、2、1。形参n给出数组中数据的个数。
#include
void fun(int a[], int n)
{ int i,t;
/**********found**********/
for (i=0; i{
t=a[i];
/**********found**********/
a[i] = a[n-1-i];
/**********found**********/
a[n-i-1] = t;
}
}
main()
{ int b[9]={1,2,3,4,5,6,7,8,9}, i;
printf("
The original data :
");
for (i=0; i<9; i++)
printf("%4d ", b[i]);
printf("
");
fun(b, 9);
printf("
The data after invert :
");
for (i=0; i<9; i++)
printf("%4d ", b[i]);
printf("
");
}
98.给定程序中,函数fun的功能是:在带有头结点的单向链表中,查找数据域中值为ch的结点。找到后通过函数
值返回该结点在链表中所处的顺序号;若不存在值为ch的结噗,函数返回0值。
#include
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(char *);
void outlist(SLIST *);
int fun( SLIST *h, char ch)
{ SLIST *p; int n=0;
p=h->next;
/**********found**********/
while(p!=0)
{ n++;
/**********found**********/
if (p->data==ch) return n;
else p=p->next;
}
return 0;
}
main()
{ SLIST *head; int k; char ch;
char a[N]={'mprintf("->%c",p->data); p=p->next; }
while(p!=NULL);
printf("->End
");
}}

99.函数fun的功能是:将形参a所指数组中的前半部分元素中的值和后半部分元素中的值对换。形参n中存放数组
中数据的个数,若n为奇数,则中间的元素不动。例如:若a所指数组中的数据依次为:1、2、3、4、5、6、7、8
、9,则调换后为6、7、8、9、5、1、2、3、4。
#include
#define N 9
void fun(int a[], int n)
{ int i, t, p;
/**********found**********/
p = (n%2==0)?n/2:n/2+1;
for (i=0; i{
t=a[i];
/**********found**********/
a[i] = a[p+i];
/**********found**********/
a[p+i] = t;
}
}
main()
{ int b[N]={1,2,3,4,5,6,7,8,9}, i;
printf("
The original data :
");
for (i=0; iprintf("
");
fun(b, N);
printf("
The data after moving :
");
for (i=0; iprintf("
");
}
100 给定程序中,函数fun的功能是:在形参s所指字符串中寻找与参数c相同的字符,并在其后插入一个与之相
同的字符,若找不到相同的字符则函数不做任何处理。
例如,s所指字符串为:baacda,c中的字符为:a,执行后s所指字符串为:baaaacdaa。
#include
void fun(char *s, char c)
{ int i, j, n;
/**********found**********/
for(i=0; s[i]!=0; i++)
if(s[i]==c)
{
/**********found**********/
n=0;
while(s[i+1+n]!='0') n++;
for(j=i+n+1; j>i; j--) s[j+1]=s[j];
/**********found**********/
s[j+1]=c;
i=i+1;
}
}
main()
{ char s[80]="baacda", c;
printf("
The string: %s
",s);
printf("
Input a character: "); scanf("%c",&c);
fun(s,c);
printf("
The result is: %s
",s);
}






失败的成语-热情洋溢的意思


nh3是什么-江雪的意思


8k是多大-二文


蛹怎么读-妇姑勃溪


墨西哥说什么语言-蓦然拼音


乌金纸-含章


4月4号-龙怎么读


题目的作用-厚的反义词