#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct LNode
{
long no;
char name[10];
int math;
int chinese;
int English;
int total;
struct LNode
next;
}LNode;
void ma1in()
{
int i,j,a=1,b=1,c,d;
struct LNode s[100];
FILE
fp=NULL;
char ch;
fp=fopen(“D:\student.date”,“w”);/
以写的方式打开文件
/
if(fp
NULL)/
判断是否打开成功
/
{
printf(“Failure to open student.txt!\n”);
exit(0);
}
for(i=0;b
1;i++)/
数据的输入
/
{
printf(“please input stduent number ID:”);
scanf(“%ld”,&s[i].no);
printf(“plese input name:”);
//测试代码printf(“111111”);
scanf(“%s”,s[i].name);
printf(“math:”);
scanf(“%d”,&s[i].math);
printf(“chinese”);
scanf(“%d”,&s[i].chinese);
printf(“Engkish:”);
scanf(“%d”,&s[i].English);
printf(“if thinking continue Please input 1,else other number”);
scanf(“%d”,&b);
s[i].total=s[i].math+s[i].English+s[i].chinese;
}/
致辞位置进行数据输入
/
c=i;
printf(” no name[10] math chinese English total\n”);/
基本框架打印好
/
for(i=0;i<c;i++)/
文件的写入与屏幕显示此程序可以进行优化一下,化为一个单独的小块在五个大模块中多次用到
/
{
fprintf(fp,”%10ld %3s %3d %3d %3d %3d”,s[i].no,s[i].name,s[i].math,s[i].chinese,s[i].English,s[i].total);
fprintf(fp,”\n”);
printf(“%10ld %3s %3d %3d %3d %3d”,s[i].no,s[i].name,s[i].math,s[i].chinese,s[i].English,s[i].total);
putchar(’\n’);
}
fclose(fp);
}
void ma2in()
{
int n,i=0,j,w;
struct LNode a[100],q;
FILE
fp=NULL;
fp=fopen(“D:\student.date”,“r”);/
只单单以读的方式打开文件
/
if(fp
NULL)
{
printf(“Failure to open student.txt!\n”);
exit(0);
}
while(1)/
从文件中读取数据到结构体中
/
{
fscanf(fp,”%10ld%s%3d%3d%3d%3d\n”,&a[i].no,&a[i].name,&a[i].math,&a[i].chinese,&a[i].English,&a[i].total);
i++;
if(feof(fp))
break;
}
w=i;
for(i=0;i<w;i++)/
利用选择排序法,通过改变结构体的内存地址改变,其他想法可以通过定义指针在结构体中用指针联系
/
{
for(j=1+i;j<w;j++)
{
if(a[i].no>a[j].no)
{
q=a[i];
a[i]=a[j];
a[j]=q;
}
}
}
fclose(fp);
fp=fopen(“D:\s_no.date”,“w”);
if(fp
NULL)
{
printf(“Failure to open student.txt!\n”);
exit(0);
}
printf(” no name[10] math chinese English total\n”);/
基本框架打印好
/
for(j=0;j<w;j++)
{
fprintf(fp,”%10ld %3s %3d %3d %3d %3d\n”,a[j].no,a[j].name,a[j].math,a[j].chinese,a[j].English,a[j].total);
printf(“%ld %s %d %d %d %d\n”,a[j].no,a[j].name ,a[j].math ,a[j].chinese,a[j].English,a[j].total);
}
fclose(fp);
}
void ma3in()
{
int i=0, j,w;
LNode a[100],q;
FILE
fp;
fp=fopen(“D:\student.date”,“r”);
if(fp==NULL)
{
printf(“Failure to open student.txt!\n”);
exit(0);
}
while(1)
{
fscanf(fp,”%10ld%s%3d%3d%3d%3d\n”,&a[i].no,&a[i].name,&a[i].math,&a[i].chinese,&a[i].English,&a[i].total);
i++;
if(feof(fp))
break;
}
w=i;
for (j = 0; j < w; j++)
for (i = 1+j; i < w; i++)
if (strcmp((char
)a[j].name, (char
)a[i].name) > 0)
{
q=a[i];
a[i]=a[j];
a[j]=q;
}
fclose(fp);
fp=fopen(“D:\s_name.date”,“w”);
if(fp
NULL)
{
printf(“Failure to open s_name.txt!\n”);
exit(0);
}
printf(” no name[10] math chinese English total\n”);/
基本框架打印好
/
for(j=0;j<w;j++)
{
fprintf(fp,”%10ld %3s %3d %3d %3d %3d\n”,a[j].no,a[j].name,a[j].math,a[j].chinese,a[j].English,a[j].total);
printf(“%ld %s %d %d %d %d\n”,a[j].no,a[j].name ,a[j].math ,a[j].chinese,a[j].English,a[j].total);
}
fclose(fp);
}
void ma4in()
{
int i=0,b=1,w,j;
struct LNode s[100],q;
FILE *fp=NULL;
fp=fopen(“D:\s_no.date”,“r”);
if(fp
NULL)
{
printf(“Failure to open student.txt!\n”);
exit(0);
}
while(1)
{
fscanf(fp,”%10ld%s%3d%3d%3d%3d\n”,&s[i].no,&s[i].name,&s[i].math,&s[i].chinese,&s[i].English,&s[i].total);
i++;
if(feof(fp))
break;
}
fclose(fp);
//测试代码printf(“7777777777777777777777\n”);
for(;b
1;i++)
{
printf(“please input stduent number ID:”);
scanf(“%ld”,&s[i].no);
printf(“plese input name:”);
//测试代码printf(“111111”);
scanf(“%s”,s[i].name);
printf(“math:”);
scanf(“%d”,&s[i].math);
printf(“chinese”);
scanf(“%d”,&s[i].chinese);
printf(“Engkish:”);
scanf(“%d”,&s[i].English);
printf(“if thinking continue Please input 1,else other number”);/
这两行进行了小小的优化可以一次插入多组数据
/
scanf(“%d”,&b);
s[i].total=s[i].math+s[i].English+s[i].chinese;
}/
致辞位置进行数据输入
/
w=i;
//测试代码printf(“33333333333333333333\n”);
for(i=0;i<w;i++)
{
for(j=1+i;j<w;j++)
{
if(s[i].no>s[j].no)
{
q=s[i];
s[i]=s[j];
s[j]=q;
}
}
}
//测试代码printf(“8888888888888888888888888888\n”);
fp=fopen(“D:\s_no.date”,“w”);
if(fp
NULL)
{
printf(“Failure to open student.txt!\n”);
exit(0);
}
printf(” no name[10] math chinese English total\n”);/
基本框架打印好
/
for(i=0;i<w;i++)/
文件的写入
/
{
fprintf(fp,”%10ld %3s %3d %3d %3d %3d”,s[i].no,s[i].name,s[i].math,s[i].chinese,s[i].English,s[i].total);
fprintf(fp,”\n”);
printf(“%ld %s %d %d %d %d\n”,s[i].no,s[i].name ,s[i].math ,s[i].chinese,s[i].English,s[i].total);
}
fclose(fp);
}
void ma5in()
{
long z;
int i=0,b=1,w,j;
struct LNode s[100];
FILE
fp=NULL;
fp=fopen(“D:\s_no.date”,“r”);
if(fp
NULL)
{
printf(“Failure to open student.txt!\n”);
exit(0);
}
while(1)
{
fscanf(fp,”%10ld%s%3d%3d%3d%3d\n”,&s[i].no,&s[i].name,&s[i].math,&s[i].chinese,&s[i].English,&s[i].total);
i++;
if(feof(fp))
break;
}
fclose(fp);
printf(“please input delate stduent number ID:”);
scanf(“%ld”,&z);
w=i;
for(i=0;i<w;i++)/
由题意得不可能存在两个相同的学号故而可以考虑将两个相同需要删除连在一骑的情况
/
{
if(s[i].no
z)
{
printf(“处于第:%d位数据为[%ld %s %d %d %d %d]\n”,i,s[i].no,s[i].name ,s[i].math ,s[i].chinese,s[i].English,s[i].total);
for(j=i;j<w;j++)
{
s[j]=s[j+1];
}
–i;/
考虑前后代号一样
/
–w;
}
}
fp=fopen(“D:\s_no.date”,“w”);
if(fp==NULL)
{
printf(“Failure to open student.txt!\n”);
exit(0);
}
printf(” no name[10] math chinese English total\n”);/
基本框架打印好
/
for(i=0;i<w;i++)/
文件的写入
/
{
fprintf(fp,”%10ld %3s %3d %3d %3d %3d”,s[i].no,s[i].name,s[i].math,s[i].chinese,s[i].English,s[i].total);
fprintf(fp,”\n”);
printf(“%ld %s %d %d %d %d\n”,s[i].no,s[i].name ,s[i].math ,s[i].chinese,s[i].English,s[i].total);
}
fclose(fp);
}
void main()
{
int a=1,i,b;
printf(”
\n
菜单一:创建源数据文件
\n
菜单二:按学号由大到小排序
\n
菜单三:按姓名排序
\n
菜单四:插入成绩
\n
菜单五:删除特定学号
\n
*******************\n”);
for(i=0;a==1;i++)
{
printf(“input your selecte:”);
scanf(“%d”,&b);
getchar();
switch(b)
{
case 1:
ma1in();
break;
case 2:
ma2in();
break;
case 3:
ma3in();
break;
case 4:
ma4in();
break;
case 5:
ma5in();
break;
default:
printf(“miss miss miss chaochufanwei”);
}
printf(“If your think continue Please input 1 else other number:”);
scanf(“%d”,&a);
}
}