C语言实验报告参考答案(原)

来源:工作范文网 时间:2020-10-19 08:50:45

C语言实验报告参考答案

实验一熟悉c语言程序开发环境及数据描述

四、程序清单

1编写程序实现在屏幕上显示以下结果:

The dress is long

The shoes are big

The trousers are black

答案:

#in clude<stdio.h>

main ()

{

prin tf("The dress is long\n ”);

prin tf("The shoes are big\n");

prin tf("The trousers are black\ n");

}

编写程序:

a=150,b=20,c=45, 编写求 a/b、a/c(商)和 a%b a%c(余数)的程序。

 ⑵a=160,b=46,c=18,d=170,编写求(a+b)/(b-c)*(c-d)的程序。

答案:

(1)

#in clude<stdio.h>

main ()

{

int a,b,c,x,y;

a=150;

b=20;

c=45;

x=a/b;

y=a/c;

printf("a/b 的商=%d\in",x);

printf("a/c 的商=%d\in",y);

x=a%b;

y=a%c;

printf("a/b 的余数=%d\n",x);

printf("a/c 的余数=%d\in",y);

(2)

#in clude<stdio.h>

main ()

{

int a,b,c,d;

float x;

a=160;

b=46;

c=18;

d=170;

x=(a+b)/(b_c)*(c-d);

prin tf("(a+b)/(b-c)*(c-d)=%f\n",x);

}

设变量a的值为0, b的值为-10,编写程序:当a>b时,将b赋给c;当a<=b时,将0 赋给co (提示:用条件运算符)

答案:

#in clude<stdio.h>

main ()

{

int a,b,c;

a=0;

b=-10;

c= (a>b) ? b:a;

printf("c = %d\n" ,c);

}

五、调试和测试结果

编译、连接无错,运行后屏幕上显示以下结果:

The dress is long

The shoes are big

The trousers are black

2、 (1)编译、连接无错,运行后屏幕上显示以下结果:

a/b的商=7

a/c的商=3

a/b的余数=10

a/c的余数=15

编译、连接无错,运行后屏幕上显示以下结果:

(a+b)/(b-c)*(c-d)=-1064.0000

编译、连接无错,运行后屏幕上显示以下结果:

c =-10

实验二顺序结构程序设计

四、程序清单

1 ?键盘输入与屏幕输出练习

问题1 D 。

问题 2 改 printf("%c,%c,%d\n",a,b,c); 这条语句

改成:printf("%c %c %d\n",a,b,c);

问题 3 改 scanf("%c%c%d",&a,&b,&c); 这条语句

改为:scanf("%c , %c , %d",&a,&b,&c);

问题 4 改 printf("%c,%c,%d\n",a,b,c); 这条语句

改成: '%c' ' ' %d\n",a,b,c);

问题 5 把 scanf("%c%c%d",&a,&b,&c);和 printf("%c,%c,%d\n",a,b,c);

改成 scan f("%c%*c%c%*c%d",&a,& b, &c);

prin tf("\'%c\',\'%c\',%d\n",a,b,c);

2(1)从键盘输入两个八进制数,计算两数之和并分别用十进制和十六进制数形式输出。

#i nclude <stdio.h>

int mai n()

{

int a,b,c;

scan f("%d%d",&a,&b);

c = a + b;

prin tf("%d\n",c);

prin tf("%x\ n",c);

return 0;

}

2(2)编写程序:从键盘输入两个实数 a和x,按公式计算并输出 y的值:

y a5 sin (ax) ln(a x) eax

#in clude<stdio.h>

#in clude<math.h>

int mai n()

{

float a,x,y;

y = pow(a,5) + sin( a*x) + exp(a*x) + log(a+x); prin tf("y=%f\n",y);

return 0;

五、调试和测试结果

2(1)输入:12 14

输出:26

1a

2(2)输入:1 0

输出:2.000000

实验三选择结构程序设计

四、 设计流程(算法描述)

(请写出上机内容2(3)题的算法描述)

主要是两两比较,然后得出最大的数

五、 程序清单

(1)输入一个整数,若大于等于0,输出提示信息“is positive ”否则输出

#in clude<stdio.h>

#in clude<math.h> main ()

{

int a;

scan f("%d",&a);

if(a>=0)

prin tf("the nu mber is positve\n");

else

prin tf("the nu mber is n egetive\n");

return 0;

}

(2)输入两个整数a和b,若a>=b时,求其积c并显示;若a<b时,求其商

#in clude<stdio.h>

main ()

{

is negative 。c

is negative 。

c并显示。

scan f("%d%d",&a,&b); if(a>=b)

prin tf("c=%d\n",a*b);

else

prin tf("c=%d\n",a/b);

return 0;

}

输入a、b、c三个整数,输出最大数。

#in clude<stdio.h>

main ()

{

int a,b,c, x;

scan f("%d%d%d", &a,&b,&c);

if(a>=b)

x=a;

else

x=b;

if (x<c)

x=c;

prin tf("the max nu mber is:%d\n",x);

return 0;

}

六、调试和测试结果

2(1)输入:2

输出:the number is positve

输入:0

输出:the number is positve

输入:-2

输出:the number is negetive

2(2)

输入:3

2

输出:

c=6

输入:2

3

输出:

c=0

2(3)

输入:3

2

1

输出:

the max nu mber is:3

输入:2

3

1

输出:

the max nu mber is:3

输入:1

2

3

输出:

the max nu mber is:3

实验四循环结构程序设计

四、设计流程(算法描述)

(请写出上机内容2的算法描述)

首先求出每一个给定数的所有因子和,然后从 2到5000循环,那一个数

x与因子之和相等,就是完数。

五、程序清单

. . 2 2 3 2

1 ?编写程序:求 1+2+3+…+100和1+2+3+…+ 100。

#in clude<stdio.h>

#in clude<math.h>

int mai n()

{

int i,j,sum;

sum = 0;

for (i=1;i<=100;i++)

sum += i;

prin tf("the sum is:%d\n",sum);

sum =0;

for(i=1;i<=100;i++)

{

j=pow(i,2);

sum +=j;

}

prin tf("the square sum is:%d\n",sum);

return 0;

2 ?

2 ?5000

2 ?一个数如果恰好等于它的因子之和,这个数就称为“完数” ,编写程序找出

中的所有完数。

#in clude<stdio.h>

#in clude<math.h>

main ()

{

int i,j,sum=0;

for(i=2;i<=5000;i++) //{sum = 0;for (j=1;j<=i/2;j++) //

for(i=2;i<=5000;i++) //

{

sum = 0;

for (j=1;j<=i/2;j++) //

{

if(i%j == 0)

遍历从2到5000的所有数

找出给定整数X的所有因子和

sum +=j;if(i == sum)//sum为因子和,如果和i等,则输出printf("%d ”,i);return 0;}

sum +=j;

if(i == sum)

//sum

为因子和,如果和i

等,则输出

printf("%d ”,i);

return 0;

}

3.编写程序:计算sinx的近似值,精确到10-6。

sin x x

3 x

3!

5 x

5!

7 x

7!

x(2n1)

其实 sin x(n 1)

其实 sin x

1) 所以程序

(2n 1)!

#i nclude <stdio.h>

#in clude <math.h>

mai n()

{

float x,s in x,i,t;

printf(" 请输入一个x值(弧度值):");

sca nf("%f", &x);

sin x=0; t=x;i=1;

while(fabs(t)>=1e-6)

{ sin x=s in x+t;

t=t*(-x*x/(2*i*(2*i+1)));

i++;

}

prin tf("s in (%.2f)=%.6f\n",x,si nx);

}

六、调试和测试结果

1:结果:the sum is: 5050

the square sum is 338350

2:结果:6 28 496

3、输入 0,输出 sin(0.00)=0.000000

输入 1.57,输出 sin(1.57)=1.000000

输入 0.5,输出 sin(0.50)=0.479426

实验五函数和编译预处理

四、设计流程(算法描述)

(请写出上机内容2的算法描述)

求素数的方法就是:给定一个大于 3的数x,从2到X的平方根遍历,只要有数 可以被x整除,就不是素数

五、程序清单

1编写自定义函数long power(int m,int n) ,计算mn的值。禾U用此函数编程序实现:从

键盘输入两个整数 m和n,计算出mn的值。

#in clude<stdio.h>

long power(int m,int n)〃 要返回的是 long 型

{

int i;

long s;〃 因为是要返回的数,所以这里也定义为 long型

s=1;

for(i=1;i<=n ;i++) {

s *=m;

}

return s;

} int main( void)

{

int m,n;

scan f("%d%d",&m,&n);

prin tf("s=%ld\n",power ( m,n));

return 0;

}

2?编写自定义函数 prime(int x),判断x是否为素数。利用此函数编写程序找出 3?5000

中的所有素数,并输出素数的个数。

#in clude<stdio.h>

#in clude<math.h>

int prime(i nt m)

{

int i,k;

k=sqrt(m);

for(i=2;i<=k;i++)

if(m%i==0)break;

if(i>k)return 1;

return 0;

main ()

{

int i,k;

k=0;

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

if(prime(i)==1){k++;pri ntf("%d is a prime muber \n ",i);} printf(" 共有 %d个素数 \n",k);

}

,计算x的因子个数。利用此函数找出并输出1 ?

,计算x的因子个数。利用此函数找出并输出

1 ?1000

中有奇数个不同因子的整数。

#in clude<stdio.h> #in clude<math.h> int coun t(i nt x)

{

int sum,i;

sum =0;//记住因子的个数

for(i=1;i<=x/2;i++)

if(x%i == 0)

sum +=1;

retur n sum+1;

} int main( void)

{

int i,y;

for(i=1;i<=100;i++)

{ y=co un t(i);

if(y%2==1)pri ntf("%d\t",i); }

return 0;

}六、调试和测试结果

输入:2 3

输出:s=8

输出:共有668个素数

71壬K4 -a J 7 13 -V b 1 ± 11 1^111呼空于3141址S£M符 a^)Hv2 JH 1 口疇电乎2吕电于 a?p4^3?< J ava^ "ICI-I 审JU M9 :1SCH3 32^^t^n,91WS1

71

壬K

4 -a J 7 1

3 -V b 1 ± 1

1 1^11

1呼空于

3141

址S£M符 a^)Hv

2 JH 1 口疇电乎

2吕电于 a?p4^

3?< J ava^ "ICI-I 审

JU M9 :1SCH3 32^^

t^n,9

1WS1

ZUN J ato

2111

2出七抽

v:7vt9f. ?-.■£*,雪 r*R3 炉 -l-ul&^yHvtv z 2 a a .3 a 3 1

Tfl 3

J*

叩址J

El 31

naif j r#

3457 UH址* m丘吁/ 3<73

:JM-t^ 3V1V ■li-ii'1 d?4V i 11 “ ?!b 3 g?于 4-441

4c>H J -W7T

y h*

ULI呼 鼻gift裁

J LI<11 L

94i£^

Hi-J .i

3^07

17W? 3&^±

3V3 9

401.3

斗叶:1

fl-1-IV

1I b>: i

■ 31 I < I ■ I I

1 &^r7 1 w?

Bi&7 1 z-mt -I 2UHV

3i?a 盘出岂勺 ±-u.v ^SSi? >&ty 3-^1 i 着丹站 IRK?

Z #!S 3 t WIV J 1 !r^1 331V

1391 □:4&3

t时埸

事*”

4EH.S

■述嗨*

■m. i

V 哩.h 1 ?耳3吕

? i■胡叩 电"吕 <?|-3 ■? W:l

i

■曰乃!

1V<^

2WL7

3

3ri e?i 工證“1

a 3*3

Z.丹

2:E7*"9

3 4n&3!

S?1.3

29fil

3 V &V

mjl i

HL 3T?

3231

3:9 ±3

合4>57 鼻如令Jl

工"站

1 7K

3AI&3

IV ^11 吨E" 总H

4Z'Z1 ■4 3&3 电y *£旳 -4 t> J V -*

diVPW

1 Vg?

■1F3 “kH 笄 ft*1? mvvv a i g

&J4i

3 W S

廿"直 制R匚廉 M VtfV

5?.*?

mdL n;2x 脅

JFL* Il JHV

3 4^9

71 I

3.E :iT<n 理 DICh

d21 7 gR -iikM: -44U J> 4S.-&9 ■4?么」

49BA

W -1TB?

1丹 “吩

1413.1 肯左價洼 1£1£-?3

124X1. 雷 bins 5E5¥3: W"

&73U 5fl

3P ■

s?乐v

7l:iH召 IL

31 t'Z 曲霄五■

33 2 3 3? 1¥ L 日*于1

y, 3&3L 3曲* 氓?nw

Bl 和嘴7 40^4? di i 3tf

43 fl J

4合于L

诵电?L

4fr49 -?

4IB ±3

733

HiiL fl W V? ■沙* K 口岂帮 Zll J 工加爭 工乂占丄

2:41 *?

壬春*¥ ^6? 3 373 1

3-H^"? a9 Vi -■*? ■JlErM g"

_V44 3499 J!j*>¥

JN?】 37JLV ^H01

-iW^l V nn ?2N¥

4台¥7 吩电?M 4jE£,7 4&*>i. -*?33 -iB I s?1 如工1

L733

IL BB? 19WV KWF 3 Zlid 9 £花壬鼻

缶加斤即 31

A&31

5ffe? V

37-H1 苗W ^9iR3 古呼督囈 Plfl" M-日丄 nat;?

3F33X J41 3 35^ x

J!?V1 事£<4 3 sai*sjr iitai

tVH V -tee?

4C£31. flIMVV ppp于 々"了 4Hfi 3 £> *?环 4B3± 押*訐¥

1.曲詁H tT?l W Z Wk3 2131 a^at 企±93 a i^i 2 43? RG门号 2&21 莒生■于 51=峠母

RESH

J1H7 斤爭 3序43 341J3

J!?N1 ?3&吕号 sras 1 ft STS

JV± JL 4HPIH <HF73 4f D3 gg 4 442 s 4!?M7 4K?Z 4f?£>3 嶋护斤唱 49G1L 切*IF

共有&辭亍素址

2.

3、输出结果为:

实验六数组

四、 设计流程(算法描述)

(请写出上机内容1的算法描述)

设置两个变量分别指示头和尾。第一个和最后一个元素值互换,然后头和尾变量 向里移动,最终到两变量相遇为止。

五、 程序清单

1.编写程序:从键盘输入一串整数保存到数组中,调用函数 an tit on e() 将数组反序输出。

自定义函数void antitone(int a[],int n) 实现将数组中的 n个数据按逆序存放。

void an tit on e(i nt a[],i nt n)

{

int i,j;

int k;

i=0;

j=n-1;

while(i<j)

{

k=a[i]; a[i]=a[j];

a[j]=k;

i +=1;

j -=1;

}

}

2 ?已知某数列的前两项为 2和3,其后每一项为其前两项之积。编程实现:从

键盘输入一个整数 x,判断并输出x最接近数列的第几项?

#in clude<stdio.h>

#in clude<math.h>

void Mad(int a[],int n)

{

int i;

a[0]=2;

a[1]=3;

for(i=2;i< n;i++)

{

a[i] = a[i-1] * a[i-2];

}

}

int mai n(void)

{

int a[100],x,k1,k2;

int i;

Mad(a,100);〃 产生序列

prin tf("i nput x:");

scan f("%d",& x);

i=0;

for(;x>a[i];i++);

k1 = abs(x-a[i-1]);

k2 = abs(x-a[i]);

if(k1>k2)

prin tf("the most similar x nu mber is:%d\n",a[i]);

else

prin tf("the most similar x nu mber is:%d\n",a[i-1]);

return 0;

}

3.编程实现:输入10个学生5门课的成绩并完成如下功能

求每个学生的平均分;

求每门课程的平均分。

#in clude<stdio.h>

#in clude<math.h>

#defi ne num 10

typedef struct stude nt

{

char n ame[20];

float math;

float en glis;

float computer;

float Chin ese;

float history;

}STUDENT;

int mai n(void)

{

STUDENT stu[ nu m];

int i;

float score,sum,average;

char s[10];

float scoreMath,scoreE ng,scoreCom,scoreChi,scoreHis;

for(i=0;i< nu m;i++)

{

prin tf("Name:");

gets(stu[i]. name);

prin tf("math score:");

scan f("%f", &score);

stu[i].math = score;

prin tf("e nglis score:");

scan f("%f", &score);

stu[i].e nglis = score;

prin tf("computer score:");

scan f("%f", &score);

stu[i].computer = score;

prin tf("Ch in ese score:");

scan f("%f", &score);

stu[i].Chi nese = score;

prin tf("history score: ”); scan f("%f", &score); stu[i].history = score;

才能起gets(s);// 功能是接受最后一个回车符,然后下一次 gets(stu[i]. name);

才能起

到作用

}

//求每个学生的平均分数

for(i=0;i< nu m;i++)

{

sum=0;

sum +=stu[i].math;

sum +=stu[i].e nglis;

sum +=stu[i].computer;

sum +=stu[i].Ch in ese;

sum +=stu[i].history;

average = sum/5;

prin tf("%s's average score is:%f\n",stu[i]. name,average);

}

//求每门课的平均成绩 scoreMath=0;

scoreE ng=0;

scoreCom=0;

scoreChi=0;

scoreHis=0;

for(i=0;i< nu m;i++)

{

scoreMath += stu[i].math; scoreE ng += stu[i].e nglis; scoreCom += stu[i].computer; scoreChi += stu[i].Ch in ese; scoreHis += stu[i].history;

}

prin tf("math's average score is:%f\n",scoreMath/nu m);

printf("englis's average score is:%f\n",scoreEng/num);

prin tf("computer's average score is:%f\n",scoreCo m/nu m);

printf("Chinese's average score is:%f\n",scoreChi/num); prin tf("history's average score is:%f\n",scoreHis/num);

return 0;

}

实验七数组和函数

四、程序清单

(请写出上机内容 2中函数的源代码)

void fun(int tt[M][N],int pp[N])

{ int i,j,max;

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

{ max=tt[0][j];

for(i=1;i<M;i++) if(tt[i][j]>max)max=tt[i][j]; pp[j]=max;

}

}

五、调试和测试结果

(写出上机内容1中填空的内容)

(1) ( 1)

(1) ( 1) sum=0 ( 2)

Oil

(3) 1

(2) ( 1)

(2) i

(3) a[p+i]

实验八指针(1)

四、程序清单

(请写出上机内容2中的函数)

求出每个位上的数字,然后放在千位上的数字乘以 1000,放在百位上的数字乘

以100,放在10位上的数字乘以10,然后相加。

void fun (i nt a,i nt b,lo ng *c) {

int a10,a1,b10,b1;

a10=a/10;

a1=a%10;

b10=b/10;

b1=b%10;

*c = a10 * 1000 + b1 * 100 + a1 *10 + b10;

}

五、调试和测试结果(请写出上机内容1的输出结果)

1(1)输出结果为:8,7,7,8

⑵6

⑶(1)x=10 y=20

⑵ x=20 y=10

⑷ 【1】int *p 【2】 &a[i] 【3】 p[i]

输入:1 2 3 4 5 6

输入:1 2 3 4 5 6

输出:

实验九指针(2)

设计流程(算法描述)

(请写出上机内容2中的算法描述)

i=0

当 *(x+i)!= '\0'

return 1

i=i+1

return 0

五、程序清单

?已知一个整型数组 a[5],其各元素值为4,6,8,10,12 。使用指针编程求数组元素之积。

#in elude <stdio.h> int mai n(void)

int a[]={4,6,8,10,12},sum;

int *p;

sum=1;

for(p=a;p<a+5;p++) {

sum *= *p;

}

prin tf("%d\n",sum); return 0;

}

判断x所指的字符串中是否包含字符 y,若是则函.定义函数 int f(char *x, char y) 数返回

判断x所指的字符串中是否包含字符 y,若是则函

int f(char *x, char y)

{

char *p;

for(p=x;*p!='\0';p++)

if(*p == y)

{

prin tf("%c\n",*p);

return 1;

}

return 0;

}

将x的整数部分存于y所指的存储单3.定义函数 void f(float x, int *y, float *z) 元,x

将x的整数部分存于

y所指的存储单

void f(float x, i nt *y, float *z)

{

*y=(i nt)x;

*z=x - *y;

}

实验十结构体

四、 程序清单

(请写出上机内容2中的函数的源代码)

void fun (struct STREC *a)

{ int i;

a_>ave=0;

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

a->ave+=a->s[i];

a->ave/=N;

}

五、 调试和测试结果(请写出上机内容1的填空结果) 上机内容1的填空结果

(1) ->s no (2) ->n ame (3) &t

实验十一共用体与枚举 文件

四、程序清单

(请写出上机内容2中的程序源代码)

#in elude <stdio.h>

#in elude <stri ng.h>

#in elude <stdlib.h> int mai n(void)

{

int i,sum;

FILE *fd;

char s[10],*p,ch;

if( (fd=fopen("D:\\shi.txt","wt"))==NULL)

{

prin tf("creat the \n");

exit(0);

}

else

{

for(i=1;i<100;i++)

{

if( (i%3 ==0) && (i%5 == 0))

{

prin tf("%d, ",i);

itoa(i,s,10); //转换成字符串

fputs(s,fd);

fputc(' ',fd);

}

}

prin tf("\n ”);

fclose(fd);

}

//提取字符转换成数字输入

if( (fd=fope n( "D:\\shi.txt","rt"))==NULL)

{

prin tf("ope n the \n ”);

exit(0);

}

else

{

p=s;

sum=0;

do

{

ch=fgetc(fd);

if(ch =='')

{

i=atoi(s);

sum +=i;

prin tf("%d ",i);

strset(s,'\0');

p=s;

}

else

{

*p=ch;

P++;

}

}while(ch != EOF);

printf(” 数的和是:%d\n",sum);

fclose(fd);

}

return 0;

}

实验十二参考答案

实验十二参考答案:(可根据情况,弄清楚一个模块即可 )

题目:设某班有 n位同学,每位同学的数据包括以下内容:学号(长整型) 、姓名(字

符串)、数学成绩(整型)、程序设计成绩(整型)。设计程序完成以下五项功能:新建数据 档案、添加数据、删除数据、对输入的数据进行排序和查询。

注:输入数据时,要求学号不能相同,姓名可以相同。

设计思路:

)?程序运行时,首先显示主菜单(模块)如下:

?程序运行时,首先显示主菜单如下:

?新建数据

?添加数据

?删除数据

.排序

.查询

.退出

用户输入序号后,程序进行相应操作。

)?在主菜单中选择序号 4,弹出子菜单选择排序方式,子菜单如下:

?数学成绩排序

?程序设计成绩排序

?总分排序。

?返回主菜单

选择子菜单的序号后,程序能正确运行并在屏幕上显示按要求排序后的相关信息。

.在主菜单中选择序号 5,弹出子菜单选择查询方式,子菜单如下:

1 .学号查询

2.姓名查询

?数学成绩查询

?程序设计成绩查询

.总分查询

?返回主菜单

请按序号选择相应操作

在子菜单中选择序号后,程序按以下方式工作。

(1 )学号查询:输入学号后,若该学号存在则显示与其相关的所有信息,否则显示找 不到的提示信息;(提示:查询到满足条件的结果后,查询即可结束)

(2 )姓名查询:输入姓名后,若该姓名存在则显示与其相关的所有信息,否则显示找 不到的提示信息;(提示:使用字符串比较函数进行比较)

按科目查询:输入指定分数,程序运行后显示该科目中考试成绩大于等于指定分 数的同学的学号、姓名以及该科成绩并统计满足条件的人数;

总分查询:输入指定分数,程序运行后显示总分成绩大于等于指定分数的同学的 学号、姓名以及各科成绩并统计满足条件的人数。

C源程序清单如下:

#i nclude "stdio.h"

#in elude "stdlib.h"

#include "string.h"

#i nclude "con io.h"

#i nclude "mem.h"

#in clude "ctype.h"

#i nclude "alloc.h"

#defi ne N 2

typedef struct z1

{

char no[11];

char n ame[15];

int score[N];

float sum;

float average;

int order;

struct z1 *n ext; }STUDENT;

/*Fu nctio ns*/

STUDENT *init(); /*initialize*/

STUDENT *create();

STUDENT *delete(STUDENT *h);

STUDENT *search no (STUDENT *h);

void prin t(STUDENT *h);

void search(STUDENT *h);

void save(STUDENT *h);

STUDENT *load();

STUDENT *in sert(STUDENT *h);

STUDENT *sort(STUDENT *h); STUDENT *i ndex(STUDENT *h);

int menu _select(); /*me nu*/

****ma in *****

mai n()

{ int i;

STUDENT *head;

head=in it();

clrscr();

for(;;)

{

switch(me nu _select())

{

case 1:head=in it();break;

case 2:head=create();break;

case 3:head=delete(head);break;

case 4:pri nt(head);break;

case 5:search(head);break;

case 6:head=search no (head);break;

case 7:save(head);break;

case 8:head=load(); break;

case 9:head=in sert(head); break;

case 10:head=sort(head);break;

case 11:

case 12:

case 13:head=in dex(head);break;

case 0:exit(0);

}

}

} menu _select()

{ char *menu[]={"***************MENU***************" "1. I nit list", "2. En ter list",

"3. Delete a record from list", "4. print list ",

"5. Search record by n ame",

"6. Search record by Number",

"7. Save the file",

"8. Load the file",

"9. in sert record to list ",

"10. sort by total scores",

"11. sort by maths scores",

"12. sort by program scores",

"13. in dex on nu mber",

"0. Quit"};

char s[3];

int c,i;

gotoxy(1,25);

printf("press any key continue \n“);

getch();

clrscr();

gotoxy(1,1);

textcolor(YELLOW);

textbackgro un d(BLACK);

gotoxy(10,2);

putch(0xc9);

for(i=1;i<44;i++)

putch(0xcd);

putch(0xbb);

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

{

gotoxy(10,i);putch(0xba);

gotoxy(54,i);putch(0xba);

}

gotoxy(10,20);putch(0xc8);

for(i=1;i<44;i++)

putch(0xcd);

putch(0xbc);

wi ndow(11,3,53,19);

clrscr();

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

{

gotoxy(10,i+1);

cprin tf("%s",me nu[i]);

}

textbackgro un d(BLACK);

wi ndow(1,1,80,25);

gotoxy(10,21);

do{

prin tf("\n Enter you choice(0~13):"); scan f("%s",s);

c=atoi(s);

}while(c<0||c>14);

return c;

}

STUDENT *ini t()

{

return NULL;

}

STUDENT *create()

{

int i; int s;

STUDENT *h=NULL,*i nfo;

for(;;)

{

in fo=(STUDENT *)malloc(sizeof(STUDENT));

if(!i nfo)

{

prin tf("\nout of memory");

return NULL;

}

in puts("e nter n o:(10 digitals .en ter 0 to exit)",i nfo->n o,11);

if(info->no[0]=='0') break; /*whe n the first nu mber is 0,break*/

in puts("e nter n ame:(<15 letters)",i nfo->n ame,15);

prin tf("please in put scores \n ”);

s=0; /*s is sum,begi ns with 0*/

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

{

do{

if(i==0)

prin tf("Please in put Maths scores:");

if(i==1)

programprin tf("Please in put Program scores:");

program

scanf("%d",&info->score[i]); /* socre[0] stores maths scores,socore[1] stores scores*/

if(i nfo->score[i]>100||i nfo->score[i]<0)

prin tf("bad data,repeat in put\n");

}while(i nfo->score[i]>100||i nfo->score[i]<0);

s=s+i nfo_>score[i];

}

in fo->sum=s;

info_> order=0;

info->n ext=h;

h=i nfo;

}

return(h);

}

in puts(char *prompt, char *s, int count)

{

char p[255];

do{

prin tf(prompt);

sca nf("%s",p);

if(strlen(p)>count)printf("\n too long! \n“);

}while(strle n( p)>co un t);

strcpy(s,p);

}

/*Print in for*/ void prin t(STUDENT *h) {

int i=0;

STUDENT *p;

clrscr();

p=h;

prin tf("\n\n\n ****************************** *STUDENT ********************* prin tf("|rec| NO. | n ame | maths | program | sum |order|\n");

printf("|---| 1 1-------1 1------1-----|\n");

while(p!=NULL)

{

i++;

%3dprin tf("|%3d|%-10s|%-15s|%7d|%9d|%4.2f|

%3d

|\n “,i,p->no ,p->n ame,p->score[0],p->score[1],p->sum,p->order);

p=p->n ext; }

printf( "*********************************

*************************

STUDENT *delete(STUDENT *h)

{

STUDENT *p,*q;

char s[11];

clrscr();

prin tf("please en ter the nu mber you want to delete \n"); scan f("%s",s);

q=p=h;

while(strcmp(p-> no,s)&&p!=NULL)

{

q=p;

p=p->n ext;

}

if(p==NULL)

printf("\nlist no %s student\n",s);

else

prin tf("\n\n\n ***************************** *STUDENT ******************** prin tf("| NO. | n ame | maths | program | sum |order|\n");

printf("| 1 卜------1 卜-----|-----|\n");

%3dprin tf("|%-10s|%-15s|%7d|%9d|%4.2f|

%3d

|\n “,p->no ,p->n ame,p->score[0],p->score[1],p->sum,p->order);

printf(*end*****************************\ n");

printf(

*end

***************************

**\ n");

getch(); if(p==h) h=p->n ext;

else q->n ext=p->n ext;

free(p);

prin tf("\n have deleted No %s stude nt\n",s); }

return(h);

}

STUDENT *search no (STUDENT *h)

{

STUDENT *p,*q;

char s[11];

clrscr();

prin tf("please en ter the nu mber you want to search \n"); scan f("%s",s);

q=p=h;

while(strcmp(p-> no,s)&&p!=NULL)

{

q=p;

p=p->n ext;

}

if(p==NULL)

prin tf("\n %s No Fou nd!\in",s);

else {

prin tf("\n %s Fou nd!\in",s);

prin tf("\n\n\n****************************STUDENT************************\n");

prin tf("\n\n\n

***************************

*STUDENT

**********************

**\n");

prin tf("| NO. | n ame | maths | program | sum |order|\n"); printf("| 1 1-------1 1------|-----|\n");

%3dprin tf("|%-10s|%-15s|%7d|%9d|%4.2f|

%3d

|\n “,p->no ,p->n ame,p->score[0],p->score[1],p->sum,p->order);

printf(*end*****************************\ n");

printf(

*end

***************************

**\ n");

getch();

}

return(h);

}

void search(STUDENT *h)

{

STUDENT *p;

char s[15];

clrscr();

prin tf("please en ter n ame for search\n"); scan f("%s",s);

p=h;

while(strcmp(p-> name’s )&&p!=NULL) p=p->n ext;

if(p==NULL)

prin tf("\n %s No Fou nd!\n",s);

else {

prin tf("\n %s Fou nd!\in",s);

prin tf("\n\n\n *************************** *STUDENT ********************* prin tf("| NO. | n ame | maths | program | sum |order|\n");

printf("| 1 卜------1 卜-----|-----|\n");

%3dprin tf("|%-10s|%-15s|%7d|%9d|%4.2f|

%3d

|\n “,p->no ,p->n ame,p->score[0],p->score[1],p->sum,p->order);

printf(*end*****************************\ n");

printf(

*end

***************************

**\ n");

STUDENT *in sert(STUDENT *h) {

STUDENT *p,*q,*i nfo;

char s[11];

int s1,i;

prin tf("please en ter the No.which this record will be located before \n"); scan f("%s",s);

prin tf("\nplease new record\n");

in fo=(STUDENT *)malloc(sizeof(STUDENT));

if(!i nfo)

{

prin tf("\nout of memory");

return NULL;

} inputs("enter no:(10 digitals)",info->no,11); in puts("e nter n ame:(<15 letters)"」nfo->n ame,15);

prin tf("please in put scores \n ”);

s1=0;

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

{

do{

if(i==0)

prin tf("Please in put Maths scores:");

if(i==1)

prin tf("Please in put Program scores:");

sca nf("%d",&in fo->score[i]);

if(i nfo->score[i]>100||i nfo->score[i]<0)

prin tf("bad data,repeat in put\n");

}while(i nfo->score[i]>100||i nfo->score[i]<0);

s1=s1+i nfo->score[i];

}

in fo->sum=s1;

info_> order=0;

in fo-> next=NULL;

p=h;

q=h;

while(strcmp(p-> no,s)&&p!=NULL)

{

q=p;

p=p->n ext;

}

if(p==NULL)

if(p==h)

h=i nfo;

else

q-> next=i nfo;

else

if(p==h)

{

info_>n ext=p;

h=i nfo;

}

else

{

info_>n ext=p;

q-> next=i nfo;

}

printf("\n ----have inserted %s student----\n",info->name); return(h);

}

/* SAVE*/

void save(STUDENT *h)

{

FILE *fp;

STUDENT *p;

char out];

prin tf("E nter out example c:\\c\\stude nt.txt:\n");

sca nf("%s",outfile);

if((fp=fope n( outfile,"wb"))==NULL)

{

prin tf("ca n not ope n file\n");

exit(1);

}

printf("\nSaving file......\n");

p=h;

while(p!=NULL)

{

fwrite(p,sizeof(STUDENT),1,fp);

p=p->n ext;

}

fclose(fp);

printf(”——save success!!——\n ”);

}

STUDENT *load()

{

STUDENT *p,*q,*h=NULL;

FILE *fp;

char in];

prin tf("E nter in example c:\\c\\stude nt.txt:\n"); sca nf("%s",i nfile);

if((fp=fope n(in file,"rb"))==NULL)

{

prin tf("ca n not ope n file\n");