m0_74749779 2023-06-05 23:41 采纳率: 50%
浏览 103
已结题

结构体数组读取文件信息失败,读取不了

代码读取不了文件结构体数组读不了txt文件里的信息。希望有会的能帮忙改一下,不要用类与对象,指针这些因为还没学看不懂。谢谢!

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <cassert>
#include<vector>
using namespace std;
// 学生结构体
struct Student {
    string id; // 学号
    string name; // 姓名
    double chinese; // 语文成绩
    double math; // 数学成绩
    double english; // 英语成绩
    double average; // 考试成绩
    int rank; // 考试名次
    double peer_score; // 同学互评分
    double morality_score; // 品德成绩
    double teacher_score; // 任课教师评分
    double total_score; // 综合测评总分
    int total_rank; // 综合测评名次
};

const int MAX_STUDENT_NUM = 1000;
Student studentss[MAX_STUDENT_NUM]; // 学生数组
int student_num = 0; // 学生数
const string filePath2 = "information2.txt";

// 按照从大到小的顺序比较学生成绩,用于排序
bool compare_averages(const Student& s1, const Student& s2)
{
    return s1.average > s2.average;
}

// 按照从大到小的顺序比较学生总分,用于排序
bool compare_total_scores(const Student& s1, const Student& s2)
{
    return s1.total_score > s2.total_score;
}

void getline()
{
    int count = 0;
    char line[128];
    fstream information2_file;
    information2_file.open(filePath2, ios::in);
    while (!information2_file.eof())
    {
        information2_file.getline(line, sizeof(line));
        count++;
    }
    information2_file.close();
    student_num = count;
}
// 读入学生信息
void  read_students()
{
    ifstream information2_file; // 打开文件
    //    assert(fin.is_open());
    information2_file.open(filePath2, ios::in);
    int i = 0;
    while (i<student_num)
    {
        information2_file>>studentss[i].id >> studentss[i].name >> studentss[i].chinese >> studentss[i].math >> studentss[i].english >> studentss[i].peer_score >> studentss[i].morality_score >> studentss[i].teacher_score;
        i++;
    }
    information2_file.close();//关闭文件
    sort(studentss, studentss +student_num);
}
// 计算学生考试成绩和名次
void calc_exam_score_and_rank()
{
    for (int i = 0; i < student_num; i++)
    {
        studentss[i].average = (studentss[i].chinese + studentss[i].math + studentss[i].english) / 3.0; // 计算考试成绩
    }

    sort(studentss, studentss + student_num, compare_averages); // 按照考试成绩排序

    // 计算考试名次
    for (int i = 0; i < student_num; i++)
    {
        if (i == 0)
        {
            studentss[i].rank = 1;
        }
        else if (studentss[i].average == studentss[i - 1].average)
        {
            studentss[i].rank = studentss[i - 1].rank;
        }
        else
        {
            studentss[i].rank = i + 1;
        }
    }
}

// 计算学生综合测评总分和名次
void calc_total_score_and_rank() {
    for (int i = 0; i < student_num; i++) {
        studentss[i].total_score = studentss[i].average * 0.6 + studentss[i].peer_score * 0.1
            + studentss[i].morality_score * 0.1 + studentss[i].teacher_score * 0.2; // 计算综合测评总分
    }

    sort(studentss, studentss + student_num, compare_total_scores); // 按照综合测评总分排序

    // 计算综合测评名次
    for (int i = 0; i < student_num; i++) {
        if (i == 0) {
            studentss[i].total_rank = 1;
        }
        else if (studentss[i].total_score == studentss[i - 1].total_score) {
            studentss[i].total_rank = studentss[i - 1].total_rank;
        }
        else {
            studentss[i].total_rank = i + 1;
        }
    }
}

// 查询学生信息
void query_student_info()
{   int count;
    string id;
    cout << "请输入学号:";
    cin >> id;
    bool found = false;
    for (int i = 0; i < count; i++)
    {
        if (studentss[i].id == id)
        {
            cout << "学号:" << studentss[i].id << endl;
            cout << "姓名:" << studentss[i].name << endl;
            cout << "语文成绩:" << studentss[i].chinese << endl;
            cout << "数学成绩:" << studentss[i].math << endl;
            cout << "外语成绩:" << studentss[i].english << endl;
            cout << "考试平均成绩:" << studentss[i].average << endl;
            cout << "考试名次:" << studentss[i].rank << endl;
            cout << "同学互评分:" << studentss[i].peer_score << endl;
            cout << "品德成绩:" << studentss[i].morality_score << endl;
            cout << "任课教师评分:" << studentss[i].teacher_score << endl;
            cout << "综合测评总分:" << studentss[i].total_score << endl;
            cout << "综合测评名次:" << studentss[i].total_rank << endl;

            found = true;
            break;
        }
    }

    if (!found)
    {
        cout << "没有找到对应学生信息!" << endl;
    }
}
//修改学生信息
void modify_student_info(Student studentss[], int count)
{
    for (int i = 0; i < count; i++)
    {
        string id;
        cout << "请输入学号:";
        cin >> id;
        bool found = false;
        if (studentss[i].id == id)
        {
            cout << "请输入新的语文成绩:";
            cin >> studentss[i].chinese;
            cout << "请输入新的数学成绩:";
            cin >> studentss[i].math;
            cout << "请输入新的英语成绩:";
            cin >> studentss[i].english;
            cout << "请输入新的同学互评分:";
            cin >> studentss[i].peer_score;
            cout << "请输入新的品德成绩:";
            cin >> studentss[i].morality_score;
            cout << "请输入新的任课教师评分:";
            cin >> studentss[i].teacher_score;

            // 重新计算该学生的信息
            studentss[i].average = (studentss[i].chinese + studentss[i].math + studentss[i].english) / 3.0;
        }
    }
}
void bSystem()
{
                cout << "进入学生数据信息处理系统" << endl;
                /* step 1 读取学生信息*/
                read_students(); // 初始化全局学生数组 studentss
//                for (int i = 0; i < 10; ++i) {
//                    cout<<studentss[i].id<<studentss[i].name<<endl;
//                }
                int selectA = 0;
                while (selectA != 3)
                {
                    cout << endl;
                    /* step 2 增删改查*/
                    cout << "1. 查询数据 " << endl;
                    cout << "2. 修改数据" << endl;
                    cout << "3. 退出子系统" << endl;
                    cout << endl << endl;// 输入
                    cin >> selectA;
                    cout << "您输入的是 " << selectA << endl;
//                    Student studentss[MAX_STUDENT_NUM]; // 学生数组
                    getline();
                    read_students();
                    if (selectA == 1)
                    {
                        query_student_info();
                    }
                    else if (selectA == 2)
                    {   
                        modify_student_info(studentss, 1);
                    }
                    else if (selectA == 3)
                    {
                        break;
                    }
                    else
                    {
                        cout << "错误的输入,请重新输入!" << endl;
                    }
                }
}
int main()
{
    bSystem();
//1

}

  • 写回答

2条回答 默认 最新

  • 喝茶品人生 2023-06-06 00:56
    关注

    对你的代码修改了很多,原本的txt可以私信给我

    img

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <algorithm>
    
    using namespace std;
    
    const int MAX_STUDENT_NUM = 100;
    
    string file_path; // 存储读入文件的路径
    int student_num = 0; // 记录读入的学生数量
    
    struct Student
    {
        string id;
        string name;
        int chinese;
        int math;
        int english;
        double peer_score;
        double morality_score;
        double teacher_score;
        double average;
        int rank;
        double total_score;
        int total_rank;
    } studentss[MAX_STUDENT_NUM];
    
    bool compare_averages(Student a, Student b)
    {
        if (a.average == b.average)
        {
            return a.id < b.id;
        }
        return a.average > b.average;
    }
    
    bool compare_total_scores(Student a, Student b)
    {
        if (a.total_score == b.total_score)
        {
            return a.id < b.id;
        }
        return a.total_score > b.total_score;
    }
    
    void read_students()
    {
        ifstream input_file(file_path);
        if (!input_file.is_open())
        {
            cout << "无法打开文件!" << endl;
            return;
        }
        string line;
        getline(input_file, line);
        while (getline(input_file, line))
        {
            stringstream ss(line);
            ss >> studentss[student_num].id >> studentss[student_num].name >> studentss[student_num].chinese >> studentss[student_num].math >> studentss[student_num].english >> studentss[student_num].peer_score >> studentss[student_num].morality_score >> studentss[student_num].teacher_score;
            student_num++;
        }
        input_file.close();
        cout << "成功读入 " << student_num << " 名学生的信息!" << endl;
    }
    
    void calc_exam_score_and_rank()
    {
        for (int i = 0; i < student_num; i++)
        {
            studentss[i].average = (studentss[i].chinese + studentss[i].math + studentss[i].english) / 3.0;
        }
        sort(studentss, studentss + student_num, compare_averages);
        for (int i = 0; i < student_num; i++)
        {
            if (i == 0)
            {
                studentss[i].rank = 1;
            }
            else if (studentss[i].average == studentss[i - 1].average)
            {
                studentss[i].rank = studentss[i - 1].rank;
            }
            else
            {
                studentss[i].rank = i + 1;
            }
        }
    }
    
    void calc_total_score_and_rank()
    {
        for (int i = 0; i < student_num; i++)
        {
            studentss[i].total_score = studentss[i].average * 0.6 + studentss[i].peer_score * 0.1 + studentss[i].morality_score * 0.1 + studentss[i].teacher_score * 0.2;
        }
        sort(studentss, studentss + student_num, compare_total_scores);
        for (int i = 0; i < student_num; i++)
        {
            if (i == 0)
            {
                studentss[i].total_rank = 1;
            }
            else if (studentss[i].total_score == studentss[i - 1].total_score)
            {
                studentss[i].total_rank = studentss[i - 1].total_rank;
            }
            else
            {
                studentss[i].total_rank = i + 1;
            }
        }
    }
    
    void query_student_info()
    {
        string id;
        cout << "请输入要查询的学生学号:";
        cin >> id;
        bool found = false;
        for (int i = 0; i < student_num; i++)
        {
            if (studentss[i].id == id)
            {
                cout << "学号:" << studentss[i].id << endl;
                cout << "姓名:" << studentss[i].name << endl;
                cout << "语文成绩:" << studentss[i].chinese << endl;
                cout << "数学成绩:" << studentss[i].math << endl;
                cout << "英语成绩:" << studentss[i].english << endl;
                cout << "平均分:" << studentss[i].average << endl;
                cout << "排名:" << studentss[i].rank << endl;
                cout << "同学互评分:" << studentss[i].peer_score << endl;
                cout << "品德成绩:" << studentss[i].morality_score << endl;
                cout << "任课教师评分:" << studentss[i].teacher_score << endl;
                cout << "综合测评总分:" << studentss[i].total_score << endl;
                cout << "综合测评名次:" << studentss[i].total_rank << endl;
                found = true;
                break;
            }
        }
        if (!found)
        {
            cout << "没有找到对应学生信息!" << endl;
        }
    }
    
    void modify_student_info()
    {
        string id;
        cout << "请输入要修改的学生学号:";
        cin >> id;
        bool found = false;
        for (int i = 0; i < student_num; i++)
        {
            if (studentss[i].id == id)
            {
                cout << "请输入新的语文成绩:";
                cin >> studentss[i].chinese;
                cout << "请输入新的数学成绩:";
                cin >> studentss[i].math;
                cout << "请输入新的英语成绩:";
                cin >> studentss[i].english;
                cout << "请输入新的同学互评分:";
                cin >> studentss[i].peer_score;
                cout << "请输入新的品德成绩:";
                cin >> studentss[i].morality_score;
                cout << "请输入新的任课教师评分:";
                cin >> studentss[i].teacher_score;
    
                studentss[i].average = (studentss[i].chinese + studentss[i].math + studentss[i].english) / 3.0;
    
                calc_exam_score_and_rank();
                calc_total_score_and_rank();
    
                found = true;
                break;
            }
        }
        if (!found)
        {
            cout << "没有找到对应学生信息!" << endl;
        }
    }
    
    void delete_student_info()
    {
        string id;
        cout << "请输入要删除的学生学号:";
        cin >> id;
    
        for (int i = 0; i < student_num; i++)
        {
            if (studentss[i].id == id)
            {
                for (int j = i; j < student_num - 1; j++)
                {
                    studentss[j] = studentss[j + 1];
                }
                student_num--;
                cout << "学生信息删除成功!" << endl;
                return;
            }
        }
        cout << "没有找到要删除的学生信息!" << endl;
    }
    
    void export_student_info()
    {
        string file_path;
        cout << "请输入要导出学生成绩的文件路径:";
        cin >> file_path;
        ofstream output_file(file_path);
        if (!output_file.is_open())
        {
            cout << "无法打开文件!" << endl;
            return;
        }
        output_file << "学号\t姓名\t语文\t数学\t英语\t平均分\t排名\t互评分\t品德\t教师评分\t总分\t总排名\n";
        for (int i = 0; i < student_num; i++)
        {
            output_file << studentss[i].id << "\t" << studentss[i].name << "\t" << studentss[i].chinese << "\t" << studentss[i].math << "\t" << studentss[i].english << "\t" << studentss[i].average << "\t" << studentss[i].rank << "\t" << studentss[i].peer_score << "\t" << studentss[i].morality_score << "\t" << studentss[i].teacher_score << "\t" << studentss[i].total_score << "\t" << studentss[i].total_rank << "\n";
        }
        output_file.close();
        cout << "学生成绩已成功导出到文件 " << file_path << endl;
    }
    
    void bSystem()
    {
        cout << "进入学生数据信息处理系统" << endl;
    
        read_students();
        calc_exam_score_and_rank();
        calc_total_score_and_rank();
    
        int selectA = 0;
        while (selectA != 5)
        {
            cout << endl;
            cout << "1. 查询数据 " << endl;
            cout << "2. 修改数据" << endl;
            cout << "3. 删除数据" << endl;
            cout << "4. 导出学生成绩" << endl;
            cout << "5. 退出子系统" << endl;
            cout << endl << endl;
    
            cin >> selectA;
            cout << "您输入的是 " << selectA << endl;
    
            if (selectA == 1)
            {
                query_student_info();
            }
            else if (selectA == 2)
            {
                modify_student_info();
            }
            else if (selectA == 3)
            {
                delete_student_info();
            }
            else if (selectA == 4)
            {
                export_student_info();
            }
            else if (selectA == 5)
            {
                break;
            }
            else
            {
                cout << "错误的输入,请重新输入!" << endl;
            }
        }
    }
    
    int main()
    {
        cout << "请输入学生信息文件的路径:";
        cin >> file_path;
    
        bSystem();
    
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月14日
  • 已采纳回答 6月6日
  • 修改了问题 6月5日
  • 创建了问题 6月5日

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体