C语言实验报告参考答案
实验一 熟悉C语言程序开发环境及数据描述
四、程序清单
1.编写程序实现在屏幕上显示以下结果:
The dress is long
The shoes are big
The trousers are black
答案:
#include<stdio.h>
main()
{
printf("The dress is long\n");
printf("The shoes are big\n");
printf("The trousers are black\n");
}
2.编写程序:
(1) a=150,b=20,c=45,编写求a/b、a/c(商)和a%b、a%c(余数)的程序。
(2)a=160,b=46,c=18,d=170, 编写求(a+b)/(b-c)*(c-d)的程序。
答案:
(1)
#include<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\n",x);
printf("a/c的商=%d\n",y);
x=a%b;
y=a%c;
printf("a/b的余数=%d\n",x);
printf("a/c的余数=%d\n",y);
}
(2)
#include<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);
printf("(a+b)/(b-c)*(c-d)=%f\n",x);
}
3. 设变量a的值为0,b的值为-10,编写程序:当a>b时,将b赋给c;当a<=b时,将0赋给c。(提示:用条件运算符)
答案:
#include<stdio.h>
main()
{
int a,b,c;
a=0;
b=-10;
c= (a>b) ? b:a;
printf("c = %d\n",c);
}
五、调试和测试结果
1.编译、连接无错,运行后屏幕上显示以下结果:
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
(2) 编译、连接无错,运行后屏幕上显示以下结果:
(a+b)/(b-c)*(c-d)=-1064.0000
3. 编译、连接无错,运行后屏幕上显示以下结果:
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);这条语句
改成:printf("\’%c\’ \’ %c\’ %d\n",a,b,c);
问题5 把scanf("%c%c%d",&a,&b,&c);和printf("%c,%c,%d\n",a,b,c);
改成scanf("%c%*c%c%*c%d",&a,&b,&c);
printf("\'%c\',\'%c\',%d\n",a,b,c);
2(1)从键盘输入两个八进制数,计算两数之和并分别用十进制和十六进制数形式输出。
#include <stdio.h>
int main()
{
int a,b,c;
scanf("%d%d",&a,&b);
c = a + b;
printf("%d\n",c);
printf("%x\n",c);
return 0;
}
2(2)编写程序:从键盘输入两个实数a和x,按公式计算并输出y的值:
#include<stdio.h>
#include<math.h>
int main()
{
float a,x,y;
scanf("%f%f",&a,&x);
y = pow(a,5) + sin(a*x) + exp(a*x) + log(a+x);
printf("y=%f\n",y);
return 0;
}
五、调试和测试结果
2(1) 输入: 12 14
输出:26
1a
2(2) 输入:1 0
输出:2.000000
实验三 选择结构程序设计
四、设计流程(算法描述)
(请写出上机内容2(3)题的算法描述)
主要是两两比较,然后得出最大的数
五、程序清单
(1)输入一个整数,若大于等于0,输出提示信息“is positive”,否则输出“is negative”。
#include<stdio.h>
#include<math.h>
main()
{
int a;
scanf("%d",&a);
if(a>=0)
printf("the number is positve\n");
else
printf("the number is negetive\n");
return 0;
}
(2)输入两个整数a和b,若a>=b时,求其积c并显示;若a<b时,求其商c并显示。
#include<stdio.h>
main()
{
int a,b,c;
scanf("%d%d",&a,&b);
if(a>=b)
printf("c=%d\n",a*b);
else
printf("c=%d\n",a/b);
return 0;
}
(3)输入a、b、c三个整数,输出最大数。
#include<stdio.h>
main()
{
int a,b,c,x;
scanf("%d%d%d",&a,&b,&c);
if(a>=b)
x=a;
else
x=b;
if (x<c)
x=c;
printf("the max number 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 number is:3
输入:2 3 1 输出:the max number is:3
输入:1 2 3 输出:the max number is:3
实验四 循环结构程序设计
四、设计流程(算法描述)
(请写出上机内容2的算法描述)
首先求出每一个给定数的所有因子和,然后从2到5000循环,那一个数x与因子之和相等,就是完数。
五、程序清单
1.编写程序:求1+2+3+…+100和12+22+33+…+1002。
#include<stdio.h>
#include<math.h>
int main()
{
int i,j,sum;
sum = 0;
for (i=1;i<=100;i++)
sum += i;
printf("the sum is:%d\n",sum);
sum =0;
for(i=1;i<=100;i++)
{
j=pow(i,2);
sum +=j;
}
printf("the square sum is:%d\n",sum);
return 0;
}
2.一个数如果恰好等于它的因子之和,这个数就称为“完数”,编写程序找出2~5000中的所有完数。
#include<stdio.h>
#include<math.h>
main()
{
int i,j,sum=0;
for(i=2;i<=5000;i++) //遍历从2到5000的所有数
{
sum = 0;
for (j=1;j<=i/2;j++) //找出给定整数X的所有因子和
{
if(i%j == 0)
sum +=j;
}
if(i == sum) //sum为因子和,如果和i相等,则输出
printf("%d ",i);
}
return 0;
}
3.编写程序:计算sinx的近似值,精确到10-6。
其实 所以程序
#include <stdio.h>
#include <math.h>
main()
{
float x,sinx,i,t;
printf("请输入一个x值(弧度值):");
scanf("%f",&x);
sinx=0; t=x;i=1;
while(fabs(t)>=1e-6)
{ sinx=sinx+t;
t=t*(-x*x/(2*i*(2*i+1)));
i++;
}
printf("sin(%.2f)=%.6f\n",x,sinx);
}
六、调试和测试结果
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),计算的值。利用此函数编程序实现:从键盘输入两个整数m和n,计算出的值。
#include<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;
scanf("%d%d",&m,&n);
printf("s=%ld\n",power ( m,n));
return 0;
}
2.编写自定义函数prime(int x),判断x是否为素数。利用此函数编写程序找出3~5000中的所有素数,并输出素数的个数。
#include<stdio.h>
#include<math.h>
int prime(int 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++;printf("%d is a prime muber \n",i);}
printf("共有%d个素数\n",k);
}
3. 编写自定义函数count(int x),计算x的因子个数。利用此函数找出并输出1~1000中有奇数个不同因子的整数。
#include<stdio.h>
#include<math.h>
int count(int x)
{
int sum,i;
sum =0;//记住因子的个数
for(i=1;i<=x/2;i++)
if(x%i == 0)
sum +=1;
return sum+1;
}
int main(void)
{
int i,y;
for(i=1;i<=100;i++)
{ y=count(i);
if(y%2==1)printf("%d\t",i);
}
return 0;
} 六、调试和测试结果
1.输入:2 3
输出:s=8
2.输出:共有668个素数
2.
3、输出结果为:
实验六 数组
四、设计流程(算法描述)
(请写出上机内容1的算法描述)
设置两个变量分别指示头和尾。第一个和最后一个元素值互换,然后头和尾变量向里移动,最终到两变量相遇为止。
五、程序清单
1.编写程序:从键盘输入一串整数保存到数组中,调用函数antitone()将数组反序输出。自定义函数void antitone(int a[],int n)实现将数组中的n个数据按逆序存放。
void antitone(int a[],int 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最接近数列的第几项?
#include<stdio.h>
#include<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 main(void)
{
int a[100],x,k1,k2;
int i;
Mad(a,100);//产生序列
printf("input x:");
scanf("%d",&x);
i=0;
for(;x>a[i];i++);
k1 = abs(x-a[i-1]);
k2 = abs(x-a[i]);
if(k1>k2)
printf("the most similar x number is:%d\n",a[i]);
else
printf("the most similar x number is:%d\n",a[i-1]);
return 0;
}
3.编程实现:输入10个学生5门课的成绩并完成如下功能
(1)求每个学生的平均分;
(2)求每门课程的平均分。
#include<stdio.h>
#include<math.h>
#define num 10
typedef struct student
{
char name[20];
float math;
float englis;
float computer;
float Chinese;
float history;
}STUDENT;
int main(void)
{
STUDENT stu[num];
int i;
float score,sum,average;
char s[10];
float scoreMath,scoreEng,scoreCom,scoreChi,scoreHis;
for(i=0;i<num;i++)
{
printf("Name: ");
gets(stu[i].name);
printf("math score: ");
scanf("%f",&score);
stu[i].math = score;
printf("englis score: ");
scanf("%f",&score);
stu[i].englis = score;
printf("computer score: ");
scanf("%f",&score);
stu[i].computer = score;
printf("Chinese score: ");
scanf("%f",&score);
stu[i].Chinese = score;
printf("history score: ");
scanf("%f",&score);
stu[i].history = score;
gets(s);//功能是接受最后一个回车符,然后下一次gets(stu[i].name);才能起到作用
}
//求每个学生的平均分数
for(i=0;i<num;i++)
{
sum=0;
sum +=stu[i].math;
sum +=stu[i].englis;
sum +=stu[i].computer;
sum +=stu[i].Chinese;
sum +=stu[i].history;
average = sum/5;
printf("%s's average score is:%f\n",stu[i].name,average);
}
//求每门课的平均成绩
scoreMath=0;
scoreEng=0;
scoreCom=0;
scoreChi=0;
scoreHis=0;
for(i=0;i<num;i++)
{
scoreMath += stu[i].math;
scoreEng += stu[i].englis;
scoreCom += stu[i].computer;
scoreChi += stu[i].Chinese;
scoreHis += stu[i].history;
}
printf("math's average score is:%f\n",scoreMath/num);
printf("englis's average score is:%f\n",scoreEng/num);
printf("computer's average score is:%f\n",scoreCom/num);
printf("Chinese's average score is:%f\n",scoreChi/num);
printf("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) sum=0 (2) t[i][i] (3) 1
(2) (1) 1 (2) i (3) a[p+i]
实验八 指针(1)
四、程序清单
(请写出上机内容2中的函数)
求出每个位上的数字,然后放在千位上的数字乘以1000,放在百位上的数字乘以100,放在10位上的数字乘以10,然后相加。
void fun(int a,int b,long *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
(2) 6
(3) (1)x=10 y=20
(2)x=20 y=10
(4) 【1】 int *p 【2】 &a[i] 【3】 p[i]
输入:1 2 3 4 5 6 输出: 1 2 3 4 5 6
实验九 指针(2)
设计流程(算法描述)
(请写出上机内容2中的算法描述)
五、程序清单
1.已知一个整型数组a[5],其各元素值为4,6,8,10,12。使用指针编程求数组元素之积。
#include <stdio.h>
int main(void)
{
int a[]={4,6,8,10,12},sum;
int *p;
sum=1;
for(p=a;p<a+5;p++)
{
sum *= *p;
}
printf("%d\n",sum);
return 0;
}
2.定义函数int f(char *x, char y)判断x所指的字符串中是否包含字符y,若是则函数返回1,否则返回1。
int f(char *x, char y)
{
char *p;
for(p=x;*p!='\0';p++)
if(*p == y)
{
printf("%c\n",*p);
return 1;
}
return 0;
}
3.定义函数void f(float x, int *y, float *z)将x的整数部分存于y所指的存储单元,x的小数部分存于x所指的存储单元。
void f(float x, int *y, float *z)
{
*y=(int)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) ->sno (2) ->name (3) &t
实验十一 共用体与枚举 文件
四、程序清单
(请写出上机内容2中的程序源代码)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int i,sum;
FILE *fd;
char s[10],*p,ch;
if( (fd=fopen("D:\\shi.txt","wt"))==NULL)
{
printf("creat the file failed\n");
exit(0);
}
else
{
for(i=1;i<100;i++)
{
if( (i%3 ==0) && (i%5 == 0) )
{
printf("%d, ",i);
itoa(i,s,10); //转换成字符串
fputs(s,fd);
fputc(' ',fd);
}
}
printf("\n");
fclose(fd);
}
//提取字符转换成数字输入
if( (fd=fopen("D:\\shi.txt","rt"))==NULL)
{
printf("open the file failed\n");
exit(0);
}
else
{
p=s;
sum=0;
do
{
ch=fgetc(fd);
if(ch == ' ')
{
i=atoi(s);
sum +=i;
printf("%d ",i);
strset(s,'\0');
p=s;
}
else
{
*p=ch;
p++;
}
}while(ch != EOF);
printf("数的和是:%d\n",sum);
fclose(fd);
}
return 0;
}
实验十二 参考答案
实验十二参考答案:(可根据情况,弄清楚一个模块即可)
题目:设某班有n位同学,每位同学的数据包括以下内容:学号(长整型)、姓名(字符串)、数学成绩(整型)、程序设计成绩(整型)。设计程序完成以下五项功能:新建数据档案、添加数据、删除数据、对输入的数据进行排序和查询。
注:输入数据时,要求学号不能相同,姓名可以相同。
设计思路:
1).程序运行时,首先显示主菜单(模块)如下:
1.程序运行时,首先显示主菜单如下:
1.新建数据
2.添加数据
3.删除数据
4.排序
5.查询
6.退出
用户输入序号后,程序进行相应操作。
2).在主菜单中选择序号4,弹出子菜单选择排序方式,子菜单如下:
1.数学成绩排序
2.程序设计成绩排序
3.总分排序。
4.返回主菜单
选择子菜单的序号后,程序能正确运行并在屏幕上显示按要求排序后的相关信息。
3.在主菜单中选择序号5,弹出子菜单选择查询方式,子菜单如下:
1.学号查询
2.姓名查询
3.数学成绩查询
4.程序设计成绩查询
5.总分查询
6.返回主菜单
请按序号选择相应操作
在子菜单中选择序号后,程序按以下方式工作。
(1)学号查询:输入学号后,若该学号存在则显示与其相关的所有信息,否则显示找不到的提示信息;(提示:查询到满足条件的结果后,查询即可结束)
(2)姓名查询:输入姓名后,若该姓名存在则显示与其相关的所有信息,否则显示找不到的提示信息;(提示:使用字符串比较函数进行比较)
(3)按科目查询:输入指定分数,程序运行后显示该科目中考试成绩大于等于指定分数的同学的学号、姓名以及该科成绩并统计满足条件的人数;
(4)总分查询:输入指定分数,程序运行后显示总分成绩大于等于指定分数的同学的学号、姓名以及各科成绩并统计满足条件的人数。
C源程序清单如下:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "mem.h"
#include "ctype.h"
#include "alloc.h"
#define N 2
typedef struct z1
{
char no[11];
char name[15];
int score[N];
float sum;
float average;
int order;
struct z1 *next;
}STUDENT;
/*Functions*/
STUDENT *init(); /*initialize*/
STUDENT *create();
STUDENT *delete(STUDENT *h);
STUDENT *searchno(STUDENT *h);
void print(STUDENT *h);
void search(STUDENT *h);
void save(STUDENT *h);
STUDENT *load();
STUDENT *insert(STUDENT *h);
STUDENT *sort(STUDENT *h);
STUDENT *index(STUDENT *h);
int menu_select(); /*menu*/
/******main*******/
main()
{
int i;
STUDENT *head;
head=init();
clrscr();
for(;;)
{
switch(menu_select())
{
case 1:head=init();break;
case 2:head=create();break;
case 3:head=delete(head);break;
case 4:print(head);break;
case 5:search(head);break;
case 6:head=searchno(head);break;
case 7:save(head);break;
case 8:head=load(); break;
case 9:head=insert(head); break;
case 10:head=sort(head);break;
case 11:
case 12:
case 13:head=index(head);break;
case 0:exit(0);
}
}
}
menu_select()
{
char *menu[]={"***************MENU***************",
" 1. Init list",
" 2. Enter list",
" 3. Delete a record from list",
" 4. print list ",
" 5. Search record by name",
" 6. Search record by Number",
" 7. Save the file",
" 8. Load the file",
" 9. insert record to list ",
" 10. sort by total scores",
" 11. sort by maths scores",
" 12. sort by program scores",
" 13. index on number",
" 0. Quit"};
char s[3];
int c,i;
gotoxy(1,25);
printf("press any key continue......\n");
getch();
clrscr();
gotoxy(1,1);
textcolor(YELLOW);
textbackground(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);
window(11,3,53,19);
clrscr();
for(i=0;i<16;i++)
{
gotoxy(10,i+1);
cprintf("%s",menu[i]);
}
textbackground(BLACK);
window(1,1,80,25);
gotoxy(10,21);
do{
printf("\n Enter you choice(0~13):");
scanf("%s",s);
c=atoi(s);
}while(c<0||c>14);
return c;
}
STUDENT *init()
{
return NULL;
}
STUDENT *create()
{
int i; int s;
STUDENT *h=NULL,*info;
for(;;)
{
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\nout of memory");
return NULL;
}
inputs("enter no:(10 digitals .enter 0 to exit)",info->no,11);
if(info->no[0]=='0') break; /*when the first number is 0,break*/
inputs("enter name:(<15 letters)",info->name,15);
printf("please input scores \n");
s=0; /*s is sum,begins with 0*/
for(i=0;i<N;I++)
{
do{
if(i==0)
printf("Please input Maths scores:");
if(i==1)
printf("Please input Program scores:");
scanf("%d",&info->score[i]); /* socre[0] stores maths scores,socore[1] stores program scores*/
if(info->score[i]>100||info->score[i]<0)
printf("bad data,repeat input\n");
}while(info->score[i]>100||info->score[i]<0);
s=s+info->score[i];
}
info->sum=s;
info->order=0;
info->next=h;
h=info;
}
return(h);
}
inputs(char *prompt, char *s, int count)
{
char p[255];
do{
printf(prompt);
scanf("%s",p);
if(strlen(p)>count)printf("\n too long! \n");
}while(strlen(p)>count);
strcpy(s,p);
}
/*Print infor*/
void print(STUDENT *h)
{
int i=0;
STUDENT *p;
clrscr();
p=h;
printf("\n\n\n*******************************STUDENT**********************\n");
printf("|rec| NO. | name | maths | program | sum |order|\n");
printf("|---|----------|---------------|-------|---------|------|-----|\n");
while(p!=NULL)
{
i++;
printf("|%3d|%-10s|%-15s|%7d|%9d|%4.2f| %3d |\n",i,p->no,p->name,p->score[0],p->score[1],p->sum,p->order);
p=p->next;
}
printf("**********************************end***************************\n");
}
STUDENT *delete(STUDENT *h)
{
STUDENT *p,*q;
char s[11];
clrscr();
printf("please enter the number you want to delete \n");
scanf("%s",s);
q=p=h;
while(strcmp(p->no,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
printf("\nlist no %s student\n",s);
else
{
printf("\n\n\n******************************STUDENT**********************\n");
printf("| NO. | name | maths | program | sum |order|\n");
printf("|----------|---------------|-------|---------|------|-----|\n");
printf("|%-10s|%-15s|%7d|%9d|%4.2f| %3d |\n",p->no,p->name,p->score[0],p->score[1],p->sum,p->order);
printf("********************************end*****************************\n");
getch();
if(p==h)
h=p->next;
else
q->next=p->next;
free(p);
printf("\n have deleted No %s student\n",s);
}
return(h);
}
STUDENT *searchno(STUDENT *h)
{
STUDENT *p,*q;
char s[11];
clrscr();
printf("please enter the number you want to search \n");
scanf("%s",s);
q=p=h;
while(strcmp(p->no,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
printf("\n %s No Found!\n",s);
else
{
printf("\n %s Found!\n",s);
printf("\n\n\n****************************STUDENT************************\n");
printf("| NO. | name | maths | program | sum |order|\n");
printf("|----------|---------------|-------|---------|------|-----|\n");
printf("|%-10s|%-15s|%7d|%9d|%4.2f| %3d |\n",p->no,p->name,p->score[0],p->score[1],p->sum,p->order);
printf("********************************end*****************************\n");
getch();
}
return(h);
}
void search(STUDENT *h)
{
STUDENT *p;
char s[15];
clrscr();
printf("please enter name for search\n");
scanf("%s",s);
p=h;
while(strcmp(p->name,s)&&p!=NULL)
p=p->next;
if(p==NULL)
printf("\n %s No Found!\n",s);
else
{
printf("\n %s Found!\n",s);
printf("\n\n\n****************************STUDENT**********************\n");
printf("| NO. | name | maths | program | sum |order|\n");
printf("|----------|---------------|-------|---------|------|-----|\n");
printf("|%-10s|%-15s|%7d|%9d|%4.2f| %3d |\n",p->no,p->name,p->score[0],p->score[1],p->sum,p->order);
printf("********************************end*****************************\n");
}
}
STUDENT *insert(STUDENT *h)
{
STUDENT *p,*q,*info;
char s[11];
int s1,i;
printf("please enter the No.which this record will be located before \n");
scanf("%s",s);
printf("\nplease new record\n");
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\nout of memory");
return NULL;
}
inputs("enter no:(10 digitals)",info->no,11);
inputs("enter name:(<15 letters)",info->name,15);
printf("please input scores \n");
s1=0;
for(i=0;i<N;I++)
{
do{
if(i==0)
printf("Please input Maths scores:");
if(i==1)
printf("Please input Program scores:");
scanf("%d",&info->score[i]);
if(info->score[i]>100||info->score[i]<0)
printf("bad data,repeat input\n");
}while(info->score[i]>100||info->score[i]<0);
s1=s1+info->score[i];
}
info->sum=s1;
info->order=0;
info->next=NULL;
p=h;
q=h;
while(strcmp(p->no,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
if(p==h)
h=info;
else
q->next=info;
else
if(p==h)
{
info->next=p;
h=info;
}
else
{
info->next=p;
q->next=info;
}
printf("\n ----have inserted %s student----\n",info->name);
return(h);
}
/* SAVE*/
void save(STUDENT *h)
{
FILE *fp;
STUDENT *p;
char outfile[10];
printf("Enter outfile name,for example c:\\c\\student.txt:\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
{
printf("can not open file\n");
exit(1);
}
printf("\nSaving file......\n");
p=h;
while(p!=NULL)
{
fwrite(p,sizeof(STUDENT),1,fp);
p=p->next;
}
fclose(fp);
printf("-----save success!!-----\n");
}
STUDENT *load()
{
STUDENT *p,*q,*h=NULL;
FILE *fp;
char infile[10];
printf("Enter infile name,for example c:\\c\\student.txt:\n"); scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)
{
printf("can not open file\n");
exit(1);
}
printf("\n -----Loading file!-----\n");
p=(STUDENT *)malloc(sizeof(STUDENT));
if(!p)
{
printf("out of memory!\n");
return h;
}
h=p;
while(!feof(fp))
{
if(1!=fread(p,sizeof(STUDENT),1,fp))
break;
p->next=(STUDENT *)malloc(sizeof(STUDENT));
if(!p->next)
{
printf("out of memory!\n");
return h;
}
q=p;
p=p->next;
}
q->next=NULL;
fclose(fp);
printf("--- Read data successful !---\n");
return h;
}
/*sort*/
STUDENT *sort(STUDENT *h)
{
int i=0;
STUDENT *p,*q,*t,*h1;
h1=h->next;
h->next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while(t->sumsum&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
p=h;
while(p!=NULL)
{
i++;
p->order=i;
p=p->next;
}
printf("sort sucess!!!\n");
return h;
}
/*index by number*/
STUDENT *index(STUDENT *h)
{
STUDENT *p,*q,*t,*h1;
h1=h->next;
h->next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while(strcmp(t->no,p->no)>0&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
printf("index sucess!!!\n");
return h;
}