回 帖 发 新 帖 刷新版面

主题:[原创]急急急 修改程序  大侠救救能满足下程序要求吗

#include <iostream>

using namespace std;
#ifndef HEAD_H
#define HEAD_H  
#include <string>

using namespace std;

//成绩类
class achievement
{
public:
    achievement();//默认构造
    float get_math();//获得数学成绩
    float get_clangue();//获得C语言的月份
    float get_english();//获得英语成绩
    void set_sum();//输出总成绩
private:
    float math;
    float clangue;
    float english;
};

//学生类
class student
{
public:
    student();//默认构造函数
    char face();//首页
    void inster_new();//新学生登记
    void show_all();//显示所有学生信息
    void select();//查询学生信息
    void taxis();//成绩排序
    void fail();//不及格人数
    void alter();//修改学生信息
    void deletes();//删除学生信息
    void delete_sigle();//删除一个学生记录
    void delete_all();//删除所有学生记录
private:
    string name;
    string number;//学号
    char sex;//姓别 m/f
    int age;
    achievement record;//成绩
};

#endif
#include <fstream>
#include <string>
#include <iomanip>
#include <cassert>
#include <conio.h>

//成绩类构造函数
achievement::achievement()
{
    math=0;
    clangue=0;
    english=0;
}

//得到数学成绩 
float achievement::get_math()
{
    return math;
}

//得到C语言成绩
float achievement::get_clangue()
{
    return clangue;
}

//得到英语成绩
float achievent::get_english()
{
    return english;
}

//输入成绩
float achievement::set_sum()
{
    cin >>math >>clangue >>english;
}

//学生类默认构造函数
student::student():birth()
{
    name = "\0";
    number = "\0";
    sex = '\0';
    age = 0;
}
// __page_break__
//首页
char student::face()
{
    char choose;
    
    system("cls");
    cout <<endl;
    cout <<"\t     ◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇             学 生 成 绩 管 理 系 统              ◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇              1. 新学生登记处                     ◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇              2. 查看所有学生信息                 ◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇              3. 查询信息(学号或名字)             ◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇              4. 修改信息                         ◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇              5. 删除信息                         ◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇              6. 成绩排序                         ◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇              7. 不及格人数统计                   ◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇              8. 关闭系统                         ◇" <<endl
        <<"\t     ||                                                  ||" <<endl
        <<"\t     ◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇" <<endl <<"\t\t";
    
    choose = getch();
    fflush(stdin);
    return choose;
}

//新学生登记
void student::inster_new()
{
    system("cls");
    cout <<endl <<"\t新学生登记处欢迎使用" <<endl <<endl;
    cout <<"\t     姓名 : ";
    cin >>name;
    cout <<endl <<"\t     学号 : ";
    cin >>number;
    cout <<endl <<"\t性别(m/f) : ";
    cin >>sex;
    cout <<endl <<"\t     年龄 : ";
    cin >>age;
    cout <<endl <<"\t 成绩记录 : ";
    record.set_sum();
    
    ofstream stuFile("students\\stu.txt", ios::app);
    assert(stuFile);
    stuFile <<number <<endl;
    stuFile.close();
    
    string stuFileName = "students\\"+number+".txt";
    ofstream stuFile1(stuFileName.c_str());
    assert(stuFile1);

    stuFile1 <<"姓名 : " <<name <<endl <<endl
        <<"学号 : "<<number <<endl <<endl
        <<"性别 : "<<sex <<endl <<endl
        <<"年龄 : "<<age <<endl <<endl
        <<"成绩记录 : " <<数学<<record.get_math() <<" C语言"<<record.get_clangue() <<" 英语"<<record.get_day() <<endl <<endl
    stuFile1.close();
    
    cout <<endl <<endl <<"\t新生登记完成!!!!" <<endl<<endl <<"\t";
    system("pause");
}

//显示所有学生信息
void student::show_all()
{
    system("cls");
    cout <<endl <<"所有学生信息如下 : " <<endl <<endl;
    
    ifstream stuFile("students\\stu.txt");
    assert(stuFile);
    string str1;
    string str2;
    string str3;
    /*    cout <<setiosflags(ios::left) <<setw(15) <<"姓名 "
    <<setw(15) <<"学号 " <<setw(5) <<"性别 "
    <<setw(5) <<"年龄 " <<setw(15) <<"学生成绩 " */
    while (stuFile >>number)
    {
        number = "students\\"+number+".txt";
        ifstream stuFile1(number.c_str());
        assert(stuFile1);
        cout <<stuFile1.rdbuf() <<endl <<endl;
        stuFile1.close();
    }
    stuFile.close();
    
    cout <<endl <<endl <<"所有学生信息显示完成!!!!" <<endl <<endl;
    system("pause");
}

//查询学生信息
void student::select()
{
    system("cls");
    cout <<endl <<"\t学生信息查询" <<endl <<endl;   // __page_break__
    cout <<"\t请输入查询方式(1)按姓名(2)按学号 : ";
        cout <<"\t请输学号 : ";
         string no;
         cin >>no;
         ifstream stuFile("students\\stu.txt");
         assert(stuFile);
         while (stuFile >>number)
         {
              if (no == number)
              {
                  cout <<endl <<number <<"的信息如下 : " <<endl;
                  number = "students\\"+number+".txt";
                  ifstream stuFile1(number.c_str());
                  assert(stuFile1);
                  cout <<endl <<stuFile1.rdbuf() <<endl <<endl;
                  cout <<"查询完毕!!!!" <<endl <<endl;
                  system("pause");
                  return;
              }
              else cout<<"查无此人"<<endl;
         }

   
}

//修改学生信息
void student::alter()
{
    system("cls");
    cout <<endl <<"\t信息修改" <<endl <<endl;
    cout <<"\t请输入学号 : ";
    string no;
    cin >>no;
    
    ifstream stuFile("students\\stu.txt");
    while (stuFile >>number)
    {
        if (no == number)
        {
            cout <<endl <<number <<"以前的信息如下 : " <<endl <<endl;
            number = "students\\"+number+".txt";
            ifstream stuFile1(number.c_str());
            assert(stuFile1);
            cout <<stuFile1.rdbuf() <<endl <<endl;
            cout <<endl <<"请根据提示输入新的信息 : " <<endl <<endl;
            cout <<"\t     姓名 : ";
            cin >>name;
            cout <<endl <<"\t性别(m/w) : ";
            cin >>sex;
            cout <<endl <<"\t     年龄 : ";
            cin >>age;
            cout <<endl <<"\t 成绩记录 : ";
            record.set_sum();
            
            string stuFileName = "students\\"+no+".txt";
            ofstream stuFile2(stuFileName.c_str());
            assert(stuFile2);

            stuFile2 <<"姓名 : " <<name <<endl <<endl
                <<"学号 : "<<no <<endl <<endl
                <<"性别 : "<<sex <<endl <<endl
                <<"年龄 : "<<age <<endl <<endl
                <<"成绩记录 : " <<数学<<record.get_math() <<" C语言"<<record.get_clangue() <<" 英语"<<record.get_english() <<endl <<endl
            stuFile2.close();
            
            cout <<endl <<endl <<"\t信息修改完成!!!!" <<endl<<endl <<"\t";
            system("pause");
            return;
        }
    }
}

//删除学生信息
void student::deletes()
{
    char choose;
    
    while (1)
    {
        system("cls");
        cout <<endl <<"\t删除学生信息" <<endl <<endl;
        cout <<endl <<"\t●\t1.  删除除一个学生记录" <<endl
            <<endl <<"\t●\t2.  删除所有学生记录" <<endl 
            <<endl <<"\t●\t3.  返回首页" <<endl <<endl <<"\t\t";
        
        choose = getch();     
        switch (choose)
        {
        case '1':
            delete_sigle();//删除一个学生记录
            break;
        case '2':
            delete_all();//删除所有学生信息
            break;
        case '3':
            return;
        default:
            break;
        }
    }
}

//删除一个学生记录
void student::delete_sigle()
{
    system("cls");
    cout <<endl <<"\t删除学生信息" <<endl <<endl;
    cout <<"\t请输入学号 : ";
    string no;
    cin >>no;
    fflush(stdin);

    ifstream stuFile("students\\stu.txt");
    assert(stuFile);
    while (stuFile >>number)
    {
        if (no == number)
        {
            number = "students\\"+number+".txt";
            ifstream stuFile1(number.c_str());
            assert(stuFile1);
            cout <<endl <<"你想删除的学生信息如下 : " <<endl <<endl;
            cout <<stuFile1.rdbuf() <<endl;
            stuFile1.close();

            cout <<endl <<"你确定要删除吗?(y/n) : ";
            if (getch() == 'y')
            {
                ifstream stuFile2("students\\stu.txt");
                assert(stuFile2);
                ofstream tempFile("students\\temp.txt");
                assert(tempFile);

                while (stuFile2 >>number)
                {
                    if (no != number)
                    {
                        tempFile <<number <<endl;
                    }
                }
                stuFile2.close();
                tempFile.close();

回复列表 (共1个回复)

沙发

ifstream tempFile1("students\\temp.txt");
                assert(tempFile1);
                ofstream stuFile3("students\\stu.txt");
                assert(stuFile3);
                stuFile3 <<tempFile1.rdbuf();
                tempFile1.close();
                stuFile3.close();             // __page_break__

                number = "students\\"+no+".txt";
                ofstream stuFile4(number.c_str());
                stuFile4.close();
                cout <<endl <<endl <<"该生信息已经删除!!" <<endl <<endl <<"\t";
                system("pause");
            }
            break;
        }
    }
}

//删除所有学生记录
void student::delete_all()
{
    cout <<endl <<endl <<"\t你确定在删除所有学生信息吗? 此操作不可恢复.(y/n) : ";
    if (getch() == 'y')
    {
        ifstream stuFile("students\\stu.txt");
        assert(stuFile);
        while (stuFile >>number)
        {
            number = "students\\"+number+".txt";
            ofstream stuFile1(number.c_str());
            assert(stuFile1);
            stuFile1.close();
        }
        stuFile.close();

        ofstream stuFile2("students\\stu.txt");
        assert(stuFile2);
        cout <<endl <<endl <<"\t所用学生信息已经删除!!!" <<endl <<endl;
        system("pause");
    }
}

//主函数,选择功能
int main()
{
    char choose;
    student astu;

    while (choose = astu.face())
    {
        switch (choose)
        {
        case '1':
            astu.inster_new();//新生登记
            break;
        case '2':
            astu.show_all();//显示所有学生信息
            break;
        case '3':
            astu.select();//信息查询
            break;
        case '4':
            astu.alter();//修改学生信息
            break;
        case '5':
            astu.deletes();//删除学生信息
            break;
        case '6':
            astu.taxis();//成绩排序
            break;
        case '7':
            astu.fail();//不及格人数
            break;
        case '8':
            cout <<endl <<"\t\t系统关闭中.........." <<endl <<endl <<"\t\t";
            cout <<"\t\t谢谢使用!!!!!"<<endl;
            exit(0);
            break;
        default:
            break;
        }
    }

    return 0;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册