码迷,mamicode.com
首页 > 其他好文 > 详细

基于vector与CFile的学生信息管理系统

时间:2014-05-07 01:33:36      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   color   

之前课堂上学了vector后一直没有用过,就写了下练了练手

//student.h

bubuko.com,布布扣
class Student        //学生类声明
{
private:
    char m_name[9];
    char m_specialty[14];
    int       m_id;
    float m_math;
    float m_history;
    float m_english;

public:    
    Student(){};
    ~Student(){};
    void ShowAllInfo();            //显示全部信息
    void ShowIndexInfo();        //按编号显示信息
    void AddInfo();                //用于添加自带信息
    void AddInfo(char name[9],char specialty[14],int id,float math,float history,float english);//添加信息至尾部
    void DeleIndexInfo();        //按编号删除信息
    void DeleAllInfo();            //删除全部信息
    void SaveInfo();            //保存信息
    void LoadInfo();            //载入信息
};
bubuko.com,布布扣

 

//student.cpp

bubuko.com,布布扣
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <afx.h>
#include "student.h"
using namespace std;

vector <Student>student;        //vector对象数组,存放信息数据

void Student::AddInfo(){
    char name[9],specialty[14];
    int id;
    float math,history,english;
    cout<<"请输入学生信息(姓名/班级/学号):"<<endl;
    cin>>name>>specialty>>id;
    strcpy(m_name,name);
    strcpy(m_specialty,specialty);
    m_id=id;
    cout<<"请输入该生成绩(数学/历史/英语):"<<endl;
    cin>>math>>history>>english;
    m_math=math;m_history=history;m_english=english;
    student.push_back(*this);
    cout<<"信息添加完毕"<<endl;
    
}

void Student::AddInfo(char name[9],char specialty[14],int id,float math,float history,float english){
    strcpy(m_name,name);
    strcpy(m_specialty,specialty);
    m_id=id;
    m_math=math;m_history=history;m_english=english;    
    student.push_back(*this);
}

void Student::ShowAllInfo(){
    if(student.empty())
        cout<<"没有学生信息!"<<endl;
    else
    {
        cout<<setiosflags(ios::left);
        for(int i=0;i<student.size();i++)
        {
            cout<<i+1<<" "<<setw(9)<<student[i].m_name<<setw(8)<<student[i].m_specialty<<"\t"
                <<setw(10)<<student[i].m_id<<"\t"<<student[i].m_math<<"\t"
                <<student[i].m_history<<"\t"<<student[i].m_english<<endl;
        }
    }
}
void Student::DeleAllInfo(){
    student.clear();
}
void Student::DeleIndexInfo(){
    cout<<"请输入你要删除的学生编号:";
    int  pos;
    cin>>pos;
    student.erase(student.begin()+pos-1);
}
void Student::ShowIndexInfo(){
    cout<<"请输入你要显示的学生编号:";
    int  pos;
    cin>>pos;
    cout<<setiosflags(ios::left);
    cout<<pos<<" "<<setw(9)<<student[pos-1].m_name<<setw(8)<<student[pos-1].m_specialty<<"\t"
        <<setw(10)<<student[pos-1].m_id<<"\t"<<student[pos-1].m_math<<"\t"
        <<student[pos-1].m_history<<"\t"<<student[pos-1].m_english<<endl;
    cout<<setiosflags(ios::right);
}
void Student::SaveInfo(){
    CFile f;
    f.Open("tmp",CFile::modeCreate | CFile::modeWrite|CFile::modeNoTruncate);
    for (int i=0;i<student.size();i++)
    {
        strcpy(this->m_name,student[i].m_name);
        strcpy(this->m_specialty,student[i].m_specialty);
        this->m_id=student.at(i).m_id;
        this->m_math=student.at(i).m_math;
        this->m_history=student.at(i).m_history;
        this->m_english=student.at(i).m_english;
        f.Write(this,sizeof(Student));
    }
    f.Close();
}

void Student::LoadInfo(){
    CFile f;
    if (f.Open("tmp",CFile::modeCreate | CFile::modeRead|CFile::modeNoTruncate))//文件存与读的打开方式要一样
    {
        f.SeekToEnd();
        DWORD dwPosition=f.GetPosition();
        f.SeekToBegin();
        for (int i=0;i<student.size();i++)
        {
            f.Read(this,sizeof(Student));    
            strcpy(student.at(i).m_name,this->m_name);
            strcpy(student[i].m_specialty,this->m_specialty);
            student.at(i).m_id=this->m_id;
            student.at(i).m_math=this->m_math;
            student.at(i).m_history=this->m_history;
            student.at(i).m_english=this->m_english;
            
        }
         DWORD dwpos=f.GetPosition();
        while(dwpos<dwPosition)
        {
            f.Read(this,sizeof(Student));    
            student.push_back(*this);
            dwpos=f.GetPosition();
        }    
    }
    else
    {
        cout<<"文件读取失败!"<<endl;
        return;
    }
    f.Close();
}
bubuko.com,布布扣

 

//main.cpp

bubuko.com,布布扣
#include "student.h"
#include <iostream>
using namespace std;
        
void main()
{
    
    Student obj;//用于调用Student的成员函数,与vector作用隔离

    obj.AddInfo("张三","信息安全",56634541,86,79,87);
    obj.AddInfo("呵呵呵","统计",242541,87,59,67);
    obj.AddInfo("呃呃呃","国际经济贸易",5456434,66,99,96);
    obj.AddInfo("李妈蛋","人力资源",242365,76,34,97);
    obj.AddInfo("老外","人力资源",5646546,76,34,97);
    while(1)
    {
        cout<<"\t ---------------------------------------"<<endl;
        cout<<"\t|         1.显示全部信息             |"<<endl;
        cout<<"\t|         2.按编号显示信息           |"<<endl;
        cout<<"\t|         3.添加信息至尾部           |"<<endl;
        cout<<"\t|         4.按编号删除信息           |"<<endl;
        cout<<"\t|         5.删除全部信息             |"<<endl;
        cout<<"\t|         6.保存信息                 |"<<endl;
        cout<<"\t|         7.载入信息                 |"<<endl;
        cout<<"\t|         8.退出                     |"<<endl;
        cout<<" \t---------------------------------------"<<endl;
        
        cout<<"请输入你的选择:";
        int n;
        cin>>n;
        switch (n)
        {
        case 1:
            {
                obj.ShowAllInfo();
                break;
            }
        case 2:
            {
                obj.ShowIndexInfo();
                break;
            }
        case 3:
            {
                obj.AddInfo();
                break;
            }
        case 4:
            {
                obj.DeleIndexInfo();
                break;
            }
        case 5:
            {
                obj.DeleAllInfo();
                break;
            }
        case 6:
            {
                obj.SaveInfo();
                break;
            }
        case 7:
            obj.LoadInfo();
            break;
        case 8:
            exit(0);
        default:
            cout<<"输入错误,请重新输入!"<<endl;
            break;
        }
    }
    
}
bubuko.com,布布扣

编写过程中主要遇到的问题与总结:

1.string类型不熟悉的话,会导致很多显示的乱码,换用char数组后马上顺畅很多

2.char数组与char*的区别

3.CFile文件的读写,开方式应该保持基本一致,如保存时是 f.Open("tmp",CFile::modeCreate | CFile::modeRead|CFile::modeNoTruncate)),

 则在读取时不能 f.Open("tmp",CFile::modeCreate | CFile::modeRead))

4.CFile是个很强大的读写接口,可以直接将一个类的数据信息保存到到硬盘中,也可以直接读取出来

基于vector与CFile的学生信息管理系统,布布扣,bubuko.com

基于vector与CFile的学生信息管理系统

标签:style   blog   class   code   java   color   

原文地址:http://www.cnblogs.com/zoffy/p/3712294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!