我在大二上学期期末时候写的,大概2014年1月5号左右。我写在博客里一是为我以后复习所用,二是希望对需要的人有所帮助。
问题描述:
现有学生成绩文本文件)和文件)。文件数据按学好排序。文件格式如下:
姓名 学号 C语言 英语 高数
张三 001 87 76 65
李四 002 89 90 96
王五 003 66 54 39
写一个程序,实现对两个文件数据的合并,生成新文件3.txt,文件3.txt中的数据按总分降序排序。
#include
#include
#include
#include "iostream"
using namespace std;
#define MAXLIN 40
//一个学生结构体存储结构
typedef struct Stu{
char name[18]; //名字
char num[18]; //学号
char C[18]; //C语言成绩
char E[18]; //英语成绩
char M[18]; //高数成绩
int sum; //成绩总和
int t; //名次
struct Student *next;
}Stu;
int o;
void substring(char *s,int i,int n,char *t) //截取指定位置的 字符串子串
{
int j=0;
for(;j
t[j]=s[i++];
t[j]=0;
}
void Read(Stu stu[40],char str[20]) //从txt 1 2文档中 读取 数据 并进行 处理
{
FILE *fp;
int t=0;
char haha[26]={"NULL"};
char hehe[18];
char hehe2[18];
if((fp=fopen(str,"rt"))==NULL)
{
printf("can not open the filen");
exit(0);
}
fgets(haha,27,fp);
printf("%s",haha);
while(fgets(hehe,18,fp)!=NULL)
{
substring(hehe,0,4,hehe2);
strcpy(stu[o].name,hehe2);
substring(hehe,5,3,stu[o].num);
substring(hehe,9,2,stu[o].C);
substring(hehe,12,2,stu[o].E);
substring(hehe,15,2,stu[o].M);
printf("%st",stu[o].name);
printf("%st",stu[o].num);
printf("%st",stu[o].C);
printf("%st",stu[o].E);
printf("%sn",stu[o].M);
fgets(hehe,18,fp);
o++;
}
fclose(fp);
printf("n");
}
void enen(Stu stu[40]) //对 从文档 1 2 中 取出 的学生 成绩 数据 进行 排序
{
int a,b;
for(int i=0;i
{
a=stu[i].C[0]-'0'; //-'0'是 把 字符 转换为 数字
b=stu[i].C[1]-'0';
stu[i].sum=a*10+b;
a=stu[i].E[0]-'0';
b=stu[i].E[1]-'0';
stu[i].sum+=a*10+b;
a=stu[i].M[0]-'0';
b=stu[i].M[1]-'0';
stu[i].sum+=a*10+b;
}
for(i=0;i
{
int y=1;
for(int j=0;j
{
if(stu[i].sum
y++;
}
for(j=i+1;j
{
if(stu[i].sum
y++;
}
stu[i].t=y;
}
printf("姓名 学号 C语言 英语 高数 总分 名次n");
for(int k=0;k
printf("%s %s %s %s %s %d %dn",stu[k].name,stu[k].num,stu[k].C,stu[k].E,stu[k].M,stu[k].sum,stu[k].t);
printf("排序完毕!nn");
}
//生成文档3.txt
void Create(Stu stu[40]) //生成 3.txt文档
{
char haha[26]={"姓名 学号 C语言 英语 高数"};
FILE *fp1;
char hehe1[5]={"t"};
char hehe2[5]={"n"};
if((fp1=fopen("E:\3.txt","w+"))==NULL)
{
printf("can not open the filen");
exit(0);
}
fputs(haha,fp1);
fputs(hehe2,fp1);
for(int i=1;i<=o;i++) //将学生相关信息 输入到 3.txt文档中
for(int j=0;j
{
if(stu[j].t==i)
{
fputs(stu[j].name,fp1);
fputs(hehe1,fp1);
fputs(stu[j].num,fp1);
fputs(hehe1,fp1);
fputs(stu[j].C,fp1);
fputs(hehe1,fp1);
fputs(stu[j].E,fp1);
fputs(hehe1,fp1);
fputs(stu[j].M,fp1);
fputs(hehe1,fp1);
fputs(hehe2,fp1);
}
}
fclose(fp1);
printf("已经按总成绩降序排好顺序,并存入到了3.txt文件里!nn");
}
//输出文档3数据
void OutPut(Stu stu[]) //把已经存入到 3.txt中的 学生信息 输出来
{
FILE *fp;
int t=0;
char haha[26]={"NULL"};
char hehe[18];
char hehe2[18];
if((fp=fopen("E:\3.txt","rt"))==NULL)
{
printf("can not open the filen");
exit(0);
}
fgets(haha,27,fp);
printf("%s",haha);
while(fgets(hehe,18,fp)!=NULL)
{
substring(hehe,0,4,hehe);
printf("%st",hehe);
substring(hehe,5,3,hehe2);
printf("%st",hehe2);
substring(hehe,9,2,hehe2);
printf("%st",hehe2);
substring(hehe,12,2,hehe2);
printf("%st",hehe2);
substring(hehe,15,2,hehe2);
printf("%st",hehe2);
for(int k=0;k
{
if(strcmp(hehe,stu[k].name)==0)
printf("%dt%dn",stu[k].sum,stu[k].t);
}
fgets(hehe,18,fp);
t++;
}
fclose(fp);
printf("文件3.txt输出完毕!");
printf("nn");
}
//退出
void TuiChu()
{
printf("谢谢使用!nn");
}
//主函数
int main()
{
int choice;
Stu stu[MAXLIN];
char str1[20]={"E:\1.txt"};
char str2[20]={"E:\2.txt"};
while(true)
{
printf("**********欢迎使用学生成绩管理程序**********n");
printf("*****读取学生文档 1.txt 数据 请输入: 1*****n");
printf("*****读取学生文档 2.txt 数据 请输入: 2*****n");
printf("*****对学 生总成绩排序 请输入: 3*****n");
printf("*****生成学生文档 3.txt 请输入: 4*****n");
printf("*****输出学生文档 3.txt 请输入: 5*****n");
printf("*****退出请输入 0*****n");
printf("请输入您的选择:");
scanf("%d",&choice);
if(choice<0||choice>5)
{
printf("请输入0~5之间的数字!请重新输入!nn");
continue;
}
switch(choice)
{
case 1: Read(stu,str1);
break;
case 2: Read(stu,str2);
break;
case 3: enen(stu);
break;
case 4: Create(stu);
break;
case 5: OutPut(stu);
break;
case 0: TuiChu();
exit(0);
}
}
return 0;
}
6.测试结果
6.1读取文件1.txt信息部分用户界面
图6.1读取文件1.txt信息用户界面
6.2读取文件2.txt信息用户界面
图6.2读取文件2.txt信息用户界面
6.3学生成绩排序部分用户界面
图 6.3 学生成绩排序用户界面
6.4往3.txt输入学生成绩用户界面
图 6.4往3.txt输入学生成绩用户界面
6.5读取文件3.txt信息用户界面
图 6.5读取文件3.txt信息用户界
< 里面数据:姓名 学号 C语言 英语 高数
张三 001 76 34 45
李四 002 25 45 67
王二 003 78 34 67
郑秀 007 87 98 89
<里面数据姓名 学号 C语言 英语 高数
黄宏 010 87 76 46
巩俐 011 56 45 87
牛群 012 45 78 45
排序后的3.txt文档数据
姓名 学号 C语言 英语 高数 郑秀 007 87 98 89 黄宏 010 87 76 46 巩俐 011 56 45 87 王二 003 78 34 67 牛群 012 45 78 45 张三 001 76 34 45 李四 002 25 45 67
本文发布于:2024-02-02 09:51:11,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170683867043005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |