2014南开大学C语言上机100套

更新时间:2023-05-28 05:39:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

2014南开大学C语言上机100套

2014年3月计算机二级C语言上机100套

一、填空题

1.给定程序中,函数fun的功能是:将形参n所指变量中,各位上为偶数的去除,剩余的数按原来从高到低的顺序组成一个新的数,并通过形参指针n传回所指变量。 例如:输入一个数:27638496,新的数为:739。

#include <stdio.h>

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<n<100000000): "); scanf("%ld",&n); }

fun(&n);

printf("\nThe result is: %ld\n",n);

}

2.给定程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到文本文件中,再用字符方式从此文本文件中逐个读入并显示在终端屏幕上。

#include <stdio.h>

void fun(char *s, int a, double f)

{

/**********found**********/

FILE * fp;

char ch;

fp = fopen("file1.txt", "w");

fprintf(fp, "%s %d %f\n", s, a, f);

fclose(fp);

fp = fopen("file1.txt", "r");

printf("\nThe result :\n\n");

ch = fgetc(fp);

/**********found**********/

2014南开大学C语言上机100套

while (!feof(fp)) {

/**********found**********/

putchar(ch); ch = fgetc(fp); }

putchar('\n');

fclose(fp);

}

main()

{ char a[10]="Hello!"; int b=12345;

double c= 98.76;

fun(a,b,c);

}

3.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出到文件中。函数fun的功能是重写形参filename所指文件中最后一个学生的数据,即用新的学生数据覆盖该学生原来的数据,其它学生的数据不变。 #include <stdio.h>

#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("student.dat", "wb");

fwrite(t, sizeof(STU), N, fp);

fclose(fp);

fp = fopen("student.dat", "rb");

fread(ss, sizeof(STU), N, fp);

fclose(fp);

2014南开大学C语言上机100套

printf("\nThe original data :\n\n");

for (j=0; j<N; j++)

{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);

for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);

printf("\n");

}

fun("student.dat", n);

printf("\nThe data after modifing :\n\n");

fp = fopen("student.dat", "rb");

fread(ss, sizeof(STU), N, fp);

fclose(fp);

for (j=0; j<N; j++)

{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);

for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);

printf("\n");

}

}

4.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出到文件中。函数fun的功能是从形参filename所指的文件中读入学生数据,并按照学号从小到大排序后,再用二进制方式把排序后的学生数据输出到filename所指的文件中,覆盖原来的文件内容。

#include <stdio.h>

#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<N-1; i++)

for (j=i+1; j<N; 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);

2014南开大学C语言上机100套

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("student.dat", "wb");

fwrite(t, sizeof(STU), 5, fp);

fclose(fp);

printf("\n\nThe original data :\n\n");

for (j=0; j<N; j++)

{ printf("\nNo: %ld Name: %-8s Scores: ",t[j].sno, t[j].name);

for (i=0; i<3; i++) printf("%6.2f ", t[j].score[i]);

printf("\n");

}

fun("student.dat");

printf("\n\nThe data after sorting :\n\n");

fp = fopen("student.dat", "rb");

fread(ss, sizeof(STU), 5, fp);

fclose(fp);

for (j=0; j<N; j++)

{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);

for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);

printf("\n");

}

}

5.给定程序中,函数fun的功能是将参数给定的字符串、整数、浮点数写到文本文件中,再用字符串方式从此文本文件中逐个读入,并调用库函数atoi和atof将字符串转换成相应的整数、浮点数,然后将其显示在屏幕上。

#include <stdio.h>

#include <stdlib.h>

void fun(char *s, int a, double f)

{

/**********found**********/

FILE * fp;

char str[100], str1[100], str2[100];

int a1; double f1;

fp = fopen("file1.txt", "w");

fprintf(fp, "%s %d %f\n", s, a, f);

/**********found**********/

fclose(fp) ;

fp = fopen("file1.txt", "r");

2014南开大学C语言上机100套

/**********found**********/

fscanf(fp,"%s%s%s", str, str1, str2);

fclose(fp);

a1 = atoi(str1);

f1 = atof(str2);

printf("\nThe result :\n\n%s %d %f\n", 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 <stdio.h>

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("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n\n",x1, x2, r);

}

7.程序通过定义并赋初值的方式,利用结构体变量存储了一名学生的信息。函数fun的功能是输出这位学生的信息。

#include <stdio.h>

typedef struct

{ int num;

char name[9];

char sex;

struct { int year,month,day ;} birthday;

2014南开大学C语言上机100套

float score[3];

}STU;

/**********found**********/

void show(STU tt)

{ int i;

printf("\n%d %s %c %d-%d-%d", tt.num, , tt.sex,

tt.birthday.year, tt.birthday.month, tt.birthday.day);

for(i=0; i<3; i++)

/**********found**********/

printf("%5.1f", tt.score[i]);

printf("\n");

}

main( )

{ STU std={ 1,"Zhanghua",'M',1961,10,8,76.5,78.0,82.0 };

printf("\nA student data:\n");

/**********found**********/

show(std);

}

8.给定程序通过赋初值的方式,利用结构体变量存储了一名学生的学号、姓名和3门课的成绩。函数fun的功能是将该学生的各科成绩者乘以一个系数a。

#include <stdio.h>

typedef struct

{ int num;

char name[9];

float score[3];

}STU;

void show(STU tt)

{ int i;

printf("%d %s : ",tt.num,);

for(i=0; i<3; i++)

printf("%5.1f",tt.score[i]);

printf("\n");

}

/**********found**********/

void modify(STU *ss,float a)

{ int i;

for(i=0; i<3; i++)

/**********found**********/

ss->score[i] *=a;

}

main( )

{ STU std={ 1,"Zhanghua",76.5,78.0,82.0 };

float a;

printf("\nThe original number and name and scores :\n");

2014南开大学C语言上机100套

show(std);

printf("\nInput a number : "); scanf("%f",&a);

/**********found**********/

modify(&std,a);

printf("\nA result of modifying :\n");

show(std);

}

9.给定程序中,涵数fun的功能是将不带头结点的单向链表结点数据域中的数据从小到大排序。即若原链表结点数据域从头至尾的数据为:10、4、2、8、6,排序后链表结点数据域从头至尾为:2、4、6、8、10。

#include <stdio.h>

#include <stdlib.h>

#define N 6

typedef struct node {

int data;

struct node *next;

} NODE;

void fun(NODE *h)

{ NODE *p, *q; int t;

p = h;

while (p) {

/**********found**********/

q = p->next ;

/**********found**********/

while (q)

{ if (p->data > q->data)

{ t = p->data; p->data = 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<N; 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; }

}

2014南开大学C语言上机100套

return h;

}

void outlist(NODE *h)

{ NODE *p;

p=h;

if (p==NULL) printf("The list is NULL!\n");

else

{ printf("\nHead ");

do

{ printf("->%d", p->data); p=p->next; }

while(p!=NULL);

printf("->End\n");

}

}

10.给定程序中,函数fun的功能是:叛定形参a所指的N×N(规定N为奇数)的矩阵是否是“幻方”,若是,函数返回值为1;不是,函数返回值为0。“幻方”的叛定条件是:矩阵每行、每列、主对角线及反对角线上元素之和都相等。例如:以下3×3的矩阵就是一个“幻方”:

4 9 2

3 5 7

8 1 6

#include <stdio.h>

#define N 3

int fun(int (*a)[N])

{ int i,j,m1,m2,row,colum;

m1=m2=0;

for(i=0; i<N; i++)

{ j=N-i-1; m1+=a[i][i]; m2+=a[i][j]; }

if(m1!=m2) return 0;

for(i=0; i<N; i++) {

/**********found**********/

row=colum= 0;

for(j=0; j<N; 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:\n");

for(i=0; i<N; i++)

2014南开大学C语言上机100套

for(j=0; j<N; j++) scanf("%d",&x[i][j]);

printf("Array:\n");

for(i=0; i<N; i++)

{ for(j=0; j<N; j++) printf("%3d",x[i][j]);

printf("\n");

}

if(fun(x)) printf("The Array is a magic square.\n");

else printf("The Array isn't a magic square.\n");

}

11.给定程序中,函数fun的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2。 #include <stdio.h>

#include <stdlib.h>

#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<N; 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; }

2014南开大学C语言上机100套

return h;

}

void outlist(NODE *h)

{ NODE *p;

p = h->next;

if (p==NULL) printf("The list is NULL!\n");

else

{ printf("\nHead ");

do

{ printf("->%d", p->data); p=p->next; }

while(p!=NULL);

printf("->End\n");

}

}

12.给定程序中,函数fun的功能是将不带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2

#include <stdio.h>

#include <stdlib.h>

#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;

2014南开大学C语言上机100套

NODE *creatlist(int a[])

{ NODE *h,*p,*q; int i;

h=NULL;

for(i=0; i<N; 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!\n");

else

{ printf("\nHead ");

do

{ printf("->%d", p->data); p=p->next; }

while(p!=NULL);

printf("->End\n");

}

}

13.给定程序中,函数fun的功能是将带头结点的单向链表结点数据域中的数据从小到大排序。即若原链表结点数据域从头至尾的数据为:10、4、2、8、6,排序后链表结点数据域从头至尾的数据为:2、4、6、8、10。

#include <stdio.h>

#include <stdlib.h>

#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) {

2014南开大学C语言上机100套

/**********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<N; 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!\n");

else

{ printf("\nHead ");

do

{ printf("->%d", p->data); p=p->next; }

while(p!=NULL);

printf("->End\n");

}

}

14.给定程序中,函数fun的功能是用函数指针指向要调用的函数,并进行调用。规定在___2____处使f指向函数f1,在___3___处使f指向函数f2。当调用正确时,程序输出: X1=5.000000, x2=3.000000, x1*x1+x1*x2=40.000000

#include <stdio.h>

double f1(double x)

{ return x*x; }

double f2(double x, double y)

{ return x*y; }

double fun(double a, double b)

{

/**********found**********/

2014南开大学C语言上机100套

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("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n",x1, x2, r);

}

15.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出到student.dat中。函数fun 的功能是从指定文件中找出指定学号的学生数据,读入此学生数据,对该生的分数进行修改,使每门课的分数加3分,修改后重写文件中该学生的数据,即用该学生的新数据覆盖原数据,其它学生数据不变,若找不到,则什么都不做。

#include <stdio.h>

#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 (n.sno==sno) break;

}

if (!feof(fp))

{ for (i=0; i<3; i++) n.score[i] += 3;

/**********found**********/

fseek(fp, -1L*sizeof(STU), SEEK_CUR);

fwrite(&n, sizeof(STU), 1, fp);

}

2014南开大学C语言上机100套

}

16.给定程序中,函数fun的功能是:求出形参ss所指字符串数组中最长字符串的长度,将其余字符串右边用字符*补齐,使其与最长字符串等长。ss所指字符串数组中共有N个字符串,且串长<N。

#include <stdio.h>

#include <string.h>

#define M 5

#define N 20

void fun(char (*ss)[N])

{ int i, j, n, len=0;

for(i=0; i<M; i++)

{ len=strlen(ss[i]);

if(i==0) n=len;

if(len>n)n=len;

}

for(i=0; i<M; i++) {

/**********found**********/

n=strlen(ss[i]);

for(j=0; j<len-n; 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 :\n");

for(i=0; i<M; i++) printf("%s\n",ss[i]);

printf("\n");

fun(ss);

printf("The result is :\n");

for(i=0; i<M; i++) printf("%s\n",ss[i]);

}

17.程序通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门课的成绩,函数fun的功能是将存放学生数据的结构体数组,按照姓名的字典序(从小到大)排序。 #include <stdio.h>

#include <string.h>

struct student {

long sno;

char name[10];

2014南开大学C语言上机100套

float score[3];

};

void fun(struct student a[], int n)

{

/**********found**********/

struct student t;

int i, j;

/**********found**********/

for (i=0; i<n-1; i++)

for (j=i+1; j<n; 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("\n\nThe original data :\n\n");

for (j=0; j<4; j++)

{ printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);

for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);

printf("\n");

}

fun(s, 4);

printf("\n\nThe data after sorting :\n\n");

for (j=0; j<4; j++)

{ printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);

for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);

printf("\n");

}

}

18.给定程序中,函数fun的功能是:将形参s所指字符中中的所有字母字符顺序前移,其他字符顺序后移,处理后新字符串的首地址作为函数值返回。

例如,s所指字符串为:asd123fgh543df,处理后新字符串为asdfghdf123543。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

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;

2014南开大学C语言上机100套

for(i=0; i<n; 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<k; i++) t[j+i]=p[i];

/**********found**********/

t[j+k]= 0;

return t;

}

main()

{ char s[80];

printf("Please input: "); scanf("%s",s);

printf("\nThe result is: %s\n",fun(s));

}

19.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a所指结构体变量s中的数据进行修改,并把a中地址作为函数值返回主函数,在主函数中输出修改后的数据。

例如:a所指变量中的学号、姓名、和三门课的成绩依次是:10001、”ZhangSan”、95、80、88,修改后输出t中的数据应为:10002、”LiSi”、96、81、89。

#include <stdio.h>

#include <string.h>

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;

2014南开大学C语言上机100套

printf("\n\nThe original data :\n");

printf("\nNo: %ld Name: %s\nScores: ",s.sno, );

for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);

printf("\n");

t = fun(&s);

printf("\nThe data after modified :\n");

printf("\nNo: %ld Name: %s\nScores: ",t->sno, t->name);

for (i=0; i<3; i++) printf("%6.2f ", t->score[i]);

printf("\n");

}

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 <stdlib.h>

#include <stdio.h>

#define N 10

double fun(double *x)

{ int i, j; double av, y[N];

av=0;

/**********found**********/

for(i=0; i<N; i++) av +=x[i]/N;

for(i=j=0; i<N; i++)

if( x[i]<av ){

/**********found**********/

y[j]=x[i]; x[i]=-1; j++;}

i=0;

while(i<N)

{ if( x[i]!= -1 ) y[j++]=x[i];

/**********found**********/

i++;

}

for(i=0; i<N; i++)x[i] = y[i];

return av;

}

main()

{ int i; double x[N];

for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);}

printf("\n");

printf("\nThe average is: %f\n",fun(x));

printf("\nThe result :\n",fun(x));

for(i=0; i<N; i++) printf("%5.0f ",x[i]);

printf("\n");

2014南开大学C语言上机100套

}

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 <stdlib.h>

#include <stdio.h>

#define N 10

double fun(double *x)

{ int i, j; double s, av, y[N];

s=0;

for(i=0; i<N; i++) s=s+x[i];

/**********found**********/

av=s/N;

for(i=j=0; i<N; i++)

if( x[i]>av ){

/**********found**********/

y[j++]=x[i]; x[i]=-1;}

for(i=0; i<N; i++)

/**********found**********/

if( x[i]!= -1) y[j++]=x[i];

for(i=0; i<N; i++)x[i] = y[i];

return av;

}

main()

{ int i; double x[N];

for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);}

printf("\n");

printf("\nThe average is: %f\n",fun(x));

printf("\nThe result :\n",fun(x));

for(i=0; i<N; i++) printf("%5.0f ",x[i]);

printf("\n");

}

22.给定程序中,函数fun的功能是:将自然数1~10以及它们的平方根写到名为myfile3.txt的文本文件中,然后再顺序读出显示在屏幕上。

#include <math.h>

#include <stdio.h>

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++)

2014南开大学C语言上机100套

/**********found**********/

fprintf(fp,"%d %f\n",i,sqrt((double)i));

printf("\nSucceed!!\n");

/**********found**********/

fclose(fp);

printf("\nThe data in file :\n");

/**********found**********/

if((fp=fopen(fname,"r"))==NULL)

return 0;

fscanf(fp,"%d%f",&n,&x);

while(!feof(fp))

{ printf("%d %f\n",n,x); fscanf(fp,"%d%f",&n,&x); }

fclose(fp);

return 1;

}

main()

{ char fname[]="myfile3.txt";

fun(fname);

}

23.给定程序中,函数fun的功能是:找出N×N矩阵中每列元素中的最大值,并按顺序依次存放于形参b所指的一维数组中。

#include <stdio.h>

#define N 4

void fun(int (*a)[N], int *b)

{ int i,j;

for(i=0; i<N; i++) {

/**********found**********/

b[i]= a[0][i];

for(j=1; j<N; 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("\nThe matrix :\n");

for(i=0;i<N; i++)

{ for(j=0;j<N; j++) printf("%4d",x[i][j]);

printf("\n");

}

/**********found**********/

fun(x,y);

printf("\nThe result is:");

2014南开大学C语言上机100套

for(i=0; i<N; i++) printf("%3d",y[i]);

printf("\n");

}

24.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a中的数据进行修改,把修改后的数据作为函数值返回主函数进行输出。 例如:传给形参a的数据中,学号、姓名、和3门课的成绩依次是:10001、”ZhangSan”、95、80、88,修改后的数据应为:10002、”LiSi”、96、81、89。

#include <stdio.h>

#include <string.h>

struct student {

long sno;

char name[10];

float score[3];

};

/**********found**********/

struct student fun(struct student a)

{ int i;

a.sno = 10002;

/**********found**********/

strcpy(, "LiSi");

/**********found**********/

for (i=0; i<3; i++) a.score[i]+= 1;

return a;

}

main()

{ struct student s={10001,"ZhangSan", 95, 80, 88}, t;

int i;

printf("\n\nThe original data :\n");

printf("\nNo: %ld Name: %s\nScores: ",s.sno, );

for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);

printf("\n");

t = fun(s);

printf("\nThe data after modified :\n");

printf("\nNo: %ld Name: %s\nScores: ",t.sno, );

for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);

printf("\n");

}

25.人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号唯一。函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数输出,若指定编号不存在,返回数据中的编号为空串。

#include <stdio.h>

#include <string.h>

#define N 8

2014南开大学C语言上机100套

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<N; 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(p.num[0]==0)

printf("\nNot found !\n");

else

{ printf("\nSucceed !\n ");

printf("%s %d-%d-%d\n",p.num,p.year,p.month,p.day);

}

}

26.给定程序中已建立一个带有头结点的单向链表,链表中的各结点按数据域递增有序链接。函数fun的功能是:删除链表中数据域值相同的结点,使之只保留一个。

#include <stdio.h>

#include <stdlib.h>

#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)

本文来源:https://www.bwwdw.com/article/xz84.html

Top