最佳文档
本文由作者推荐
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(0
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
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
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; i
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
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
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->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
if(m1!=m2) return 0;
for(i=0; i
row=colum= 0;
for(j=0; j
/**********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; i
");
for(i=0; i
");
}
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->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->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->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
#define M 5
#define N 20
void fun(char (*ss)[N])
{ int i, j, n, len=0;
for(i=0; i
if(i==0) n=len;
if(len>n)n=len;
}
for(i=0; i
n=strlen(ss[i]);
for(j=0; j
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
printf("
");
fun(ss);
printf("The result is :
");
for(i=0; 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; i
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
/**********found**********/
t[j]=s[i]; j++;}
else
{ p[k]=s[i]; k++; }
}
/**********found**********/
for(i=0; i
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; i
y[j]=x[i]; x[i]=-1; j++;}
i=0;
while(i
/**********found**********/
i++;
}
for(i=0; i
}
main()
{ int i; double x[N];
for(i=0; i
");
printf("
The average is: %f
",fun(x));
printf("
The result :
",fun(x));
for(i=0; i
");
}
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
av=s/N;
for(i=j=0; i
/**********found**********/
y[j++]=x[i]; x[i]=-1;}
for(i=0; i
if( x[i]!= -1) y[j++]=x[i];
for(i=0; i
}
main()
{ int i; double x[N];
for(i=0; i
");
printf("
The average is: %f
",fun(x));
printf("
The result :
",fun(x));
for(i=0; i
");
}
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
b[i]= a[0][i];
for(j=1; j
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
");
}
/**********found**********/
fun(x,y);
printf("
The result is:");
for(i=0; i
");
}
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
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->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
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
t=i;
/**********found**********/
for(j=i+2 j
if(t!=i)
{ x=s[i]; s[i]=s[t]; s[t]=x; }
}
for(i=1,j=0; i
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
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
for(j= i j
for(k=i+1; k
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
");
}
}
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->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
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; i
");
n = fun(b, N);
printf("
The number of odd : %d
", n);
printf("
The odd number :
");
for (i=0; i
");
}
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
/**********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->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
*av=s/N;
d=32767;
for(i=0; i
/**********found**********/
d=*av-x[i]; j=i;}
/**********found**********/
return x[j];
}
main()
{ int i; double x[N],av,m;
for(i=0; i
");
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
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
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; i
");
n = fun(b, N);
printf("
The number of even :%d
", n);
printf("
The even :
");
for (i=0; i
");
}
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; i
k= i
for(j=i+1; j
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
/**********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
");
}
fun(t);
printf("
The result is:
");
for(i=0; i
");
}
}
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
");
}
fun(t);
printf("
The result is:
");
for(i=0; i
");
}
}
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->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