20xxC语言实验报告参考答案

                                   2010C语言实验报告参考答案

实验一  熟悉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.改错题(将正确程序写在指定位置)

正确的程序为:

#include <stdio.h>

main()

{

  printf("商品名称    价格\n");

  printf("TCL电视机   ¥7600\n");

  printf("美的空调    ¥2000\n");

  printf("SunRose键盘  ¥50.5\n");

}

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

答案:

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

}

4. 设变量a的值为0,b的值为-10,编写程序:当a>b时,将b赋给c;当a<=b时,将a赋给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

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

a/b的商=7

       a/c的商=3

       a/b的余数=10

       a/c的余数=15

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

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

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

#include  <stdio.h>

int main()

{

   int a,b,c;            

   printf("Enter a and b:");

   scanf("%o%o",&a,&b);

   c = a + b;

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

   printf("x:%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;

}

3.改错题

正确的程序为:

#include <stdio.h>

main()

{

       int a,b,c,s;

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

       s=a+b+c;

       printf("%d=%d+%d+%d\n",s,a,b,c);   /*输出s=a+b+c*/

       printf("%d+%d+%d=%d\n",a,b,c,s);         /*输出a+b+c=s*/

}

五、调试和测试结果

2(1) 输入: 12  14

    输出:26

          1a

2(2) 输入:1  0

输出:2.000000

实验三  选择结构程序设计

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

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

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

五、程序清单

2(1) 输入整数x和a,计算并输出下列分段函数f(x)的值(保留2位小数),请调用log函数求自然对数,调用fabs函数求绝对值。

程序为:

#include <stdio.h>

#include <math.h>

main()

{

   int x,a;

   double y;

   printf("Enter a and x:");

   scanf("%d%d",&a,&x);

   if(fabs(x)!=a)

       y=log(fabs((a+x)/(a-x)))/(2*a);

   else

       y=0;

   printf("a=%d,f(%d)=%.2f\n",a,x,y);

}

 (2)输入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;

}

3.改错题

正确程序为:

#include <stdio.h>

main()

{

       double n;

       printf("Enter n:");

       scanf("%lf",&n);

       if(n<0)

              printf("n is less than 0\n");

       else if(n==0)

                     printf("n is equal to 0\n");

       else

              printf("n is greater 0\n");

}

六、调试和测试结果

2(1) Enter a and x:5  6

a=5,f(6)=0.24

Enter a and x:5  5

a=5,f(5)=0.00

2(2) 输入: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. 改错题

正确的程序为:

#include <stdio.h>

main()

{

   int n=1;

   int find=0;

   while(!find)

   {

       if(n%5==1 && n%6==5 && n%7==4 && n%11==10)

       {

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

           find =1;

       }

       n++;

   }

}

六、调试和测试结果

1:结果:the sum is:5050

         the square sum is:338350

2:结果:6   28   496

实验五  函数和预处理命令

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

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

利用循环将m乘n次

五、程序清单

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、写出两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,两个整数由键盘输入。

1.  源程序如下:

int zdgys(int n1,int n2)

{int y,i;

 for(i=n2;i<=1;i--)

   if(n1%i==0&&n2%i==0)

     {y=i;break;}

  return y;

}

int zxgbs(int n1,int n2)

{int y,i;

 for(i=n1;i<=n1*n2;i++)

   if(i%n1==0&&i%n2==0)

     {y=i;break;}

  return y;

}

main()

{int n1,n2,t;

 scanf("n1=%d n2=%d",&n1,&n2);

 if(n1<n2)

   {t=n1;n1=n2;n2=t;}

 printf("zdgys=%d  zxgbs=%d",zdgys(n1,n2),zxgbs(n1,n2));

}

3、改错题

正确程序如下:

#include <stdio.h>

int fact(int n);

int multi(int n);

main()

{int i;

 double sum,item,eps;

 eps=1E-6;

 sum=1;

 item=1;

 for(i=1;item>=eps;i++)

{item=fact(i)/multi(2*i+1);

     sum=sum+item;

    }

printf(“PI=%0.5lf\n”,sum*2);

return 0;

}

int fact(int n)

{int i;

 int res=1;

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

  res=res*i;

return  res;

}

int multi(int n)

{int  i;

 int  res=1;

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

   res=res*i;

return  res;

}

  六、调试和测试结果

1、输入:2  3

 输出:s=8

2、 输入:n1=24  n2=16

输出:zdgys=8  zxgbs=48

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(" x 最接近第%d项\n",i+1);

else

         printf("x 最接近第%d项\n",i);

return 0;

3、源程序如下:

#include <stdio.h>

main()

{

       char c[15];

       int i,word=0,num=0,space=0;

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

              scanf("%c",&c[i]);

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

       {     if(c[i]==' ') space++;

           if(c[i]>='0'&&c[i]<='9') num++;

              if(c[i]>'a'&&c[i]<'z') word++;

}

       printf("字符:%d 数字:%d 空格:%d\n",word,num,space);

}

六、调试和测试结果

1、输入:1 2 3 4 5

输出:5 4 3 2 1

2、输入:110

输出:x 最接近第6项

3、输入:sd234kj64jk mjk

输出:字符:9  数字:5         空格:1

实验七  数组和函数综合程序设计

四、程序清单

(请写出上机内容2、3中源程序)

2、  原程序求得的是下三角形,经改进调试之后的程序为:

 #include <stdio.h>

#define N 6

main()

{

int i,j,sum=0;

int a[N][N]={0};

printf("input 5×5 data:\n");

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

{

printf("Input the %d line data:\n",i);

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

scanf("%d",&a[i][j]);

}

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

{

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

printf("%5d",a[i][j]);

printf("\n");

}

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

for(j=N-1;j>=i;j--)

sum=sum+a[i][j];

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

}

3

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中的函数)

求出每个位上的数字,然后放在千位上的数字乘以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中的算法描述)

 

五、程序清单

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;

}

六、调试和测试结果

测试结果正确

实验十  结构体

四、程序清单

(请写出上机内容1的源程序和上机内容2中的函数)

1、上机内容1的源程序

#include<stdio.h>

#include <string.h>

#define FORMAT "%0d\t%s\t%d\t%c\n"

struct student

{

    int num;

    char name[20];

int age;

    char sex;

   

};

main()

{  void input(struct student stu[] );

   void stat(struct student  stu[]);

struct student stu[4];

int i;

    input(stu);

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

    { printf(FORMAT,stu[i].num,stu[i].name,stu[i].age,stu[i].sex);  

    }

   stat(stu);

}

void input(struct student  stu[])

{int i;

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

{scanf("%d",&stu[i].num);

getchar();

scanf("%s",&stu[i].name);

getchar();

scanf("%d",&stu[i].age);

getchar();

scanf("%c",&stu[i].sex);

getchar();}

}

void stat(struct student  stu[])

{int i,c=0,boy=0,girl=0;

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

    {     

      if(stu[i].age<18) c+=1;

  if(stu[i].sex=='m') boy++;

      else  girl++;

           }

printf("boy\tgirl\tage<18\n");

printf("%d\t%d\t%d\n",boy,girl,c);

}

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;

}

3改错题

正确的程序为:

将    printf("%5d %-20s %2c %4d\n",*p.num, *p.name, p.sex, p.age);

改为:printf("%5d %-20s %2c %4d\n", p->.num, p->name, p->sex, p->age);

实验十一  共用体、位运算和文件

四、程序清单

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

(1)   求100以内能同时被3和5整除的自然数,分别将它们输出到显示器屏幕和x.txt文件中。

(2)用程序读出上述x.txt文件中的数据,将它们输出到屏幕,并求它们的和。

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

}

相关推荐