数据结构课设-学生管理系统C语言

我在大二上学期期末时候写的,大概2014年1月5号左右。我写在博客里一是为我以后复习所用,二是希望对需要的人有所帮助。

问题描述:

现有学生成绩文本文件1(1.txt)和文件2(2.txt)。文件数据按学好排序。文件格式如下:

姓名     学号      C语言  英语      高数

张三     001        87          76          65

李四     002        89          90          96

王五     003        66          54          39

写一个程序,实现对两个文件数据的合并,生成新文件3.txt,文件3.txt中的数据按总分降序排序。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#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<n;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 file\n");
        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("%s\t",stu[o].name);
        printf("%s\t",stu[o].num);
        printf("%s\t",stu[o].C);
        printf("%s\t",stu[o].E);
        printf("%s\n",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<o;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<o;i++)            //这两个for循环 是 对学生的 总成绩 进行排序 排第几
    {
        int y=1;
        for(int j=0;j<i;j++)
        {
            if(stu[i].sum<stu[j].sum)
                y++;
        }
        for(j=i+1;j<o;j++)
        {
            if(stu[i].sum<stu[j].sum)
                y++;
        }
        stu[i].t=y;
    }
    printf("姓名    学号    C语言    英语    高数    总分    名次\n");
    for(int k=0;k<o;k++)
        printf("%s    %s    %s    %s    %s    %d    %d\n",stu[k].name,stu[k].num,stu[k].C,stu[k].E,stu[k].M,stu[k].sum,stu[k].t);
    printf("排序完毕!\n\n");
}
//生成文档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 file\n");
        exit(0);
    }
    fputs(haha,fp1);
    fputs(hehe2,fp1);
    for(int i=1;i<=o;i++)                //将学生相关信息 输入到 3.txt文档中
        for(int j=0;j<o;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文件里!\n\n");
}
//输出文档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 file\n");
        exit(0);
    }
    fgets(haha,27,fp);
    printf("%s",haha);
    while(fgets(hehe,18,fp)!=NULL)
    {
        substring(hehe,0,4,hehe);
        printf("%s\t",hehe);
        substring(hehe,5,3,hehe2);
        printf("%s\t",hehe2);
        substring(hehe,9,2,hehe2);
        printf("%s\t",hehe2);
        substring(hehe,12,2,hehe2);
        printf("%s\t",hehe2);
        substring(hehe,15,2,hehe2);
        printf("%s\t",hehe2);
        for(int k=0;k<o;k++)
        {
            if(strcmp(hehe,stu[k].name)==0)
              printf("%d\t%d\n",stu[k].sum,stu[k].t);
        }
        fgets(hehe,18,fp);
        t++;
    }
    fclose(fp);
    printf("文件3.txt输出完毕!");
    printf("\n\n");
}
//退出
void TuiChu()
{
    printf("谢谢使用!\n\n");
}
//主函数
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之间的数字!请重新输入!\n\n");
            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信息用户界



1.txt 里面数据:

姓名    学号    C语言    英语    高数
张三    001    76    34    45
李四    002    25    45    67
王二    003    78    34    67
郑秀    007    87    98    89


2.txt里面数据

姓名    学号    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    


  • 14
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值