数据结构实验一:基于线性表的学生信息管理(顺序结构)

#include <iostream>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
#define N 100


#define SIZE 500

typedef struct {
    char no[15];   
    char name[50];
    char gender[10];
    char nationality[20];
    char biogenic[20];
    int  age;   
}Student; //学生信息结构定义


typedef struct{
    Student *ST;
    int length;//学生表的学生记录个数
}SqList;

void Input(SqList &L){//从StudentsInfo.txt中读取学生数据
    int i=0;
    char 
        ST_head1[10],   // 学号
        ST_head2[10],   // 姓名
        ST_head3[10],   // 性别
        ST_head4[10],   // 民族
        ST_head5[10],   // 生源
        ST_head6[10];   // 年龄

    L.ST=new Student[SIZE];
    
    ifstream inFile("StudentsInfo.txt");

    if(!inFile)    {
        cerr<<"Cannot open this file!"<<endl;
        exit(1);
    }
    
    inFile>>ST_head1>>ST_head2>>ST_head3>>ST_head4>>ST_head5>>ST_head6;//读取文件中的标题
    L.length=0;
    while(!inFile.eof()){  //逐行依次读取所有学生数据
        inFile
            >>L.ST[i].no >>L.ST[i].name>>L.ST[i].gender
            >>L.ST[i].nationality >>L.ST[i].biogenic>>L.ST[i].age;
        i++;  //记录学生个数
    }
    L.length=i;
    inFile.close();
    cout<<"\n读取 StudentsInfo.txt 信息完毕,可以通过选项(2)查看学生信息\n"<<endl;
}

void Output(SqList L){
    cout
        <<left<<setw(15)<<"学号" <<"\t"<<left<<setw(10)<<"姓名"<<"\t"<<left<<setw(6)<<"性别"
        <<left<<setw(10)<<"民族" <<"\t"<<left<<setw(10)<<"生源"<<"\t"<<left<<setw(5)<<"年龄"<<endl;
    for(int i=0;i<L.length;i++)
        cout
            <<left<<setw(15)<<L.ST[i].no <<"\t"<<left<<setw(10)<<L.ST[i].name<<"\t"<<left<<setw(6)<<L.ST[i].gender
            <<left<<setw(10)<<L.ST[i].nationality <<"\t"<<left<<setw(10)<<L.ST[i].biogenic<<"\t"<<left<<setw(5)<<L.ST[i].age<<endl;
    cout<<"\n信息显示完毕\n"<<endl;
}

void Count_Len(SqList L){
    cout<<"当前学生总数为:"<<L.length<<endl<<endl;
}


int t;
void Maxage(SqList p)
{
    //链表包含头结点。其中查找的最大值存在于MAX中。
    int MAX;if(p.length>0) MAX=p.ST[0].age;int num=0;
    for(int i=0;i<p.length;i++)
        if(p.ST[i].age>MAX) {
            MAX=p.ST[i].age;num++;
        }
    if(num){
        cout<<"输出年龄最大的学生的信息:\n"<<endl;
        cout
            <<left<<setw(15)<<"学号" <<"\t"<<left<<setw(10)<<"姓名"<<"\t"<<left<<setw(6)<<"性别"
            <<left<<setw(10)<<"民族" <<"\t"<<left<<setw(10)<<"生源"<<"\t"<<left<<setw(5)<<"年龄"<<endl;
        for(int i=0;i<p.length;i++)
            if(p.ST[i].age==MAX)
                cout
                    <<left<<setw(15)<<p.ST[i].no <<"\t"<<left<<setw(10)<<p.ST[i].name<<"\t"<<left<<setw(6)<<p.ST[i].gender
                    <<left<<setw(10)<<p.ST[i].nationality <<"\t"<<left<<setw(10)<<p.ST[i].biogenic<<"\t"<<left<<setw(5)<<p.ST[i].age<<endl;
    
        cout<<"*********************************************************************输出完毕!"<<endl;
    }
}
void Average(SqList p)
{
    //计算所有学生的平均年龄,并输出;
    double num=0.0,t=0.0;
    for(int i=0;i<p.length;i++)
        num=num+(double)p.ST[i].age;
    
    cout<<endl<<endl<<"所有学生的平均年龄为:   "<<setw(4)<<num/p.length<<endl<<endl;
}
void Name(SqList p,char name[])
{
    //根据学生姓名进行查找,返回相应的学生信息(考虑重名的情况)
    int num=0;
    for(int i=0;i<p.length;i++)
    {
        if(strcmp(p.ST[i].name,name)==0) 
        {
            if(num==0)
                cout
                    <<left<<setw(15)<<"学号" <<"\t"<<left<<setw(10)<<"姓名"<<"\t"<<left<<setw(6)<<"性别"
                    <<left<<setw(10)<<"民族" <<"\t"<<left<<setw(10)<<"生源"<<"\t"<<left<<setw(5)<<"年龄"<<endl;
            num++;
            cout
                <<left<<setw(15)<<p.ST[i].no <<"\t"<<left<<setw(10)<<p.ST[i].name<<"\t"<<left<<setw(6)<<p.ST[i].gender
                <<left<<setw(10)<<p.ST[i].nationality <<"\t"<<left<<setw(10)<<p.ST[i].biogenic<<"\t"<<left<<setw(5)<<p.ST[i].age<<endl;
        }
    }
    if(num==0) cout<<"没有姓名为:"<<name<<"的学生信息!!!请重新输入!"<<endl;
    else cout<<endl<<"姓名为"<<name<<"学生的信息输出完毕!"<<endl<<"一共有"<<num<<"位!"<<endl;
}
void Location(SqList L,int location)
{
    //按照位置查找学生信息,并输出;
    if(location>L.length || location<=0) cout<<"输入位置有错!"<<endl;
    else {
        cout
            <<left<<setw(15)<<L.ST[location-1].no <<"\t"<<left<<setw(10)<<L.ST[location-1].name<<"\t"<<left<<setw(6)<<L.ST[location-1].gender
            <<left<<setw(10)<<L.ST[location-1].nationality <<"\t"<<left<<setw(10)<<L.ST[location-1].biogenic<<"\t"<<left<<setw(5)<<L.ST[location-1].age<<endl;
        cout<<"输出完毕!"<<endl;
    }
}
void Insert(SqList &L)
{
    //插入新生信息到指定位置,并将插入后的结果重新写入文件StudentsInfo.txt
    int i;cout<<"请输入想要将学生插入的位置:"<<endl;cin>>i;
    if(i<1 || i>L.length) cout<<"输入位置有误!请重新输入!"<<endl;
    else
    {
        cout<<"请输入学生的信息:"<<endl;
        cout
            <<left<<setw(15)<<"学号" <<"\t"<<left<<setw(10)<<"姓名"<<"\t"<<left<<setw(6)<<"性别"
            <<left<<setw(10)<<"民族" <<"\t"<<left<<setw(10)<<"生源"<<"\t"<<left<<setw(5)<<"年龄"<<endl;
        for(int j=L.length;j>=i;j--)
        {
            L.ST[j]=L.ST[j-1];
        }
        i--;
        cin
            >>L.ST[i].no >>L.ST[i].name>>L.ST[i].gender
            >>L.ST[i].nationality >>L.ST[i].biogenic>>L.ST[i].age;
        L.length++;//总学生个数加1;
        //将插入后的结果重新写入文件StudentsInfo.txt---未完成-----回去复习C++文件操作章节;
        cout<<"插入操作完毕!"<<"\n"<<"插入后的结果为:"<<endl;
        Output(L);
    }
}

void Delete(SqList &L)
{
    //总学生个数减1;
    //删除表中位置为location的学生的信息,并重新写入文件中;
    int location;
    cout<<"请输入需要删除学生信息的编号:"<<endl;cin>>location;
    if(location<1 || location>L.length) cout<<"输入有误!请重新输入!"<<endl;
    else
    {
        L.length--;
        for(int i=location-1;i<L.length;i++)
        {
            L.ST[i]=L.ST[i+1];
        }
        cout<<"删除操作完毕!"<<"\n"<<"删除后的结果为:"<<endl;
        Output(L);
        //将删除后的结果重新写入文件--------------------未完成-----回去复习C++文件操作章节;
    }
}

void Rorder(SqList &L)
{
    //将学生的信息逆序之后保存入文件中;
    int len=L.length/2;Student student;
    for(int i=0;i<len;i++)
    {
        student=L.ST[i];
        L.ST[i]=L.ST[L.length-1-i];
        L.ST[L.length-1-i]=student;
    }
    cout<<"逆序之后的学生信息为:"<<endl;
    Output(L);
}
void main()
{
    int no;
    SqList L;
    while(1)
    {
        cout<<"**************************** 欢学使用学生信息管理系统!*************************"<<endl;//菜单选项
        cout<<"***************************** 学号:   姓名:   班级:*************************"<<"\n";//补全你自己的个人信息
        cout<<"******************************************************************************"<<"\n";
        cout<<"(1)输入学生信息"<<"\n";
        cout<<"(2)逐个显示学生信息"<<"\n";
        cout<<"(3)统计表中学生个数"<<"\n";
        cout<<"(4)输出年龄最大的学生信息"<<"\n";
        cout<<"(5)计算所有学生的平均年龄"<<"\n";
        cout<<"(6)根据学生姓名进行查找,返回相应的学生信息(考虑重名的情况)"<<"\n";
        cout<<"(7)根据指定位置进行查找并输出学生信息"<<"\n";
        cout<<"(8)插入新生信息到指定位置,并将插入后的结果重新写入文件StudentsInfo.txt"<<"\n";
        cout<<"(9)删除指定位置的学生记录,并将删除之后的结果重新写入文件StudentsInfo.txt"<<"\n";
        cout<<"(10)将学生表信息逆序存储,将逆序后的学生信息存入文件StuInfo_Inserver.txt"<<"\n";
        cout<<"(0)退出系统"<<"\n";
        cout<<"\n";
        cout<<"请选择您需要的服务:";
        cin>>no;
        switch(no){
        case 1:
            Input(L);break;//从文件StudentInfo.txt获取学生信息;
        case 2:
            Output(L);break;//输出所有学生信息;
        case 3:
            Count_Len(L);break;//统计学生总数;
        case 4:
            Maxage(L);break;//统计最大年龄学生的信息;
        case 5:
            Average(L);break;//计算学生年龄的平均值;
        case 6:
            char name[50];
            cout<<"请输入你想要查找学生的姓名:"<<endl;
            cin>>name;Name(L,name);    break;
        case 7:
            int location;
            cout<<"请输入需要查找学生的位置:"<<endl;    cin>>location;
            Location(L,location);break;
        case 8:
            Insert(L);break;
        case 9:
            Delete(L);break;
        case 10:
            Rorder(L);break;
        case 0:
            cout<<"欢迎再次使用 <*_*>bye<*_*>"<<endl;exit(0);
        default :
            cout<<"请选择正确的操作!!"<<endl;break;
        }
    }
}

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值