| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 566 人关注过本帖
标题:请大家帮忙看看这个C++写的学生成绩单管理系统???
只看楼主 加入收藏
林浩
Rank: 2
等 级:论坛游民
帖 子:47
专家分:23
注 册:2011-1-12
结帖率:90.91%
收藏
 问题点数:0 回复次数:0 
请大家帮忙看看这个C++写的学生成绩单管理系统???
不知道哪里出错了, 感觉是文件读和写那一块出了问题, 调试了很久都没找出来;
主要问题是当"学生成绩单.txt"还没有建立的时候(就是第一次运行该程序), 调试过程中数据显示,修改这块都还是正常, 但是当关闭程序,再次打开程序,运行测试时,在GetList()函数处,从文件读数据时, 学生的名字就开始乱码了,当然显示,修改这些函数就没法做了,请明白的人说一说阿 。。感激不敬。

student.h///#include<iostream>
#include<string>
using namespace std;

class Student
{
    public:
            Student(){}
            void InputData();            //学生数据输入;
            void LinkData(Student *s);  //链接学生数据;
            void ShowData();            //显示学生数据;
            string Getname();            //取姓名;
            long Getnumber();            //取学号;
            float Getscores();            //取成绩;
            Student &AlterName();        //修改姓名;
            Student &AlterNumber();        //修改学号;
            Student &AlterScores();        //修改成绩;
            Student *next;

    private:
            string name;
            long number;
            float scores;
};


void Student::ShowData(void)
{
    cout<<"姓名\t学号\t成绩:"<<endl;
    cout<<name<<"\t"<<number<<"\t"<<scores<<endl;
}

void Student::InputData()
{
    cout<<"请输入学生数据:"<<endl;
    cout<<"姓名:"<<endl;
    cin>>name;
    cout<<"学号:"<<endl;
    cin>>number;
    cout<<"成绩:"<<endl;
    cin>>scores;
}

void Student::LinkData(Student *s)
{
    next = s;
}

string Student::Getname()
{
    return name;
}            

long Student::Getnumber()
{
    return number;
}
            
float Student::Getscores()
{
    return scores;
}

Student &Student::AlterName()
{
    string new_name;

    cout<<"请输入新名字:"<<endl;
    cin>>new_name;

    name = new_name;

    return *this;
}
            
Student &Student::AlterNumber()
{
    long new_number;

    cout<<"请输入新学号:"<<endl;
    cin>>new_number;

    number = new_number;

    return *this;
}
            
Student &Student::AlterScores()
{
    float new_scores;

    cout<<"请输入新成绩:"<<endl;
    cin>>new_scores;

    scores = new_scores;

    return *this;
}
//学生成绩单.cpp
#include<fstream>
#include"student.h"

int Menu(void);                    //主菜单函数;
int FindofName(void);            //以姓名方式查询函数;
int FindofNumber(void);            //以学号方式查询;
int Find(void);                    // 查询函数;
Student *GetList(void);            // 读文件,建立链表;
void save(Student *head);        //写文件,存数据;
int Change(void);                //修改学生数据;
int Del(void);                    //删除学生数据;
int Print(void);                //打印学生成绩单;
int Add(void);                    //添加学生数据;
void Sort(void);                //排序学生成绩;

int FindofName(void)
{
    string findname;
   
    cout<<"请输入要查找学生的姓名:"<<endl;
    cin>>findname;

    Student *head, *p;
    p = head = GetList();
   
    while(head)
    {
        if (head->Getname() == findname)
        {
            head->ShowData();
            break;
        }
        head = head->next;
    }
    if (!head)
    {
        cout<<"对不起,没有该学生!"<<endl;
    }

    free(p);
    return 0;
}

int FindofNumber(void)
{
    long findnumber;
   
    cout<<"请输入要查找学生的学号:"<<endl;
    cin>>findnumber;

    Student *head, *p;
    p = head = GetList();
   
    while(head)
    {
        if (head->Getnumber() == findnumber)
        {
            head->ShowData();
            break;
        }
        head = head->next;
    }
    if (!head)
    {
        cout<<"对不起,没有该学生!"<<endl;
    }
   
    free(p);

    return 0;
}

int Find(void)
{   
    cout<<"1.按姓名查找"<<endl;
    cout<<"2.按学号查找"<<endl;
    cout<<"0.退出"<<endl;

    int choice;

    while(1)
    {
        cout<<"请选择查询方式:"<<endl;
        cin>>choice;
        switch(choice)
        {
            case 1:
                    FindofName();
                    break;
            case 2:
                    FindofNumber();
                    break;
            case 0:
                    return 0;
            default:
                    cout<<"选择错误!"<<endl;
        }
    }

    return 0;
}

Student *GetList(void)
{
    Student *head = NULL, *p1 = NULL ,*p2 = NULL;

    ifstream fromfile("学生成绩单.txt", ios_base::in|ios_base::binary);

    p1 = new Student;
    fromfile.read((char *)p1, sizeof(Student));
    p2 = p1;
    while(!fromfile.eof())
    {   
        if (NULL == head)
        {
            head = p2 = p1;
        }   
        else
        {
            p2->next = p1;
            p2 = p1;
        }   
        p1 = new Student;
        fromfile.read((char *)p1, sizeof(Student));   
    }
    p2->next = NULL;
    free(p1);
    fromfile.close();
    return head;
}

void save(Student *head)
{
    ofstream tofile("学生成绩单.txt", ios_base::app|ios_base::binary);

    Student *p1 = head;
    while(p1)
    {
        tofile.write((char*)(p1), sizeof(Student));
        p1 = p1->next;
    }

    tofile.close();
    free(head);
}

int Change(void)
{
    Student *head, *p;

    head = p = GetList();

    string name;

    cout<<"请输入姓名:"<<endl;
    cin>>name;

    while(p)
    {
        if (p->Getname() == name)
        {
            cout<<"请问要修改学生什么数据:"<<endl;
            cout<<"1.姓名"<<endl;
            cout<<"2.学号"<<endl;
            cout<<"3.成绩"<<endl;
            cout<<"0.修改完毕!"<<endl;
            
            int choice;
            while(1)
            {
                cout<<"请选择:"<<endl;
                cin>>choice;
                switch(choice)
                {
                    case 1:
                            p->AlterName();break;                    
                    case 2:
                            p->AlterNumber();break;
                    case 3:
                            p->AlterScores();break;
                    case 0:
                            save(head);
                            return 0;
                    default:
                            cout<<"选择错误!"<<endl;
                }
            }
            break;
        }
        p = p->next;
    }
    if (!p)
    {
        cout<<"对不起,没有该学生!"<<endl;
    }

    save(head);

   
    return 0;   
}

int Del(void)
{
     Student *head, *p1 = NULL , *p2 = NULL;
     head = GetList();

     string name;

     cout<<"请输入需删除学生的姓名:"<<endl;
     cin>>name;

     p1 = head;

     while(p1)
     {
        if (head->Getname() == name) //删除为头结点;
        {
            head = head->next;
            break;
        }
        else
        {
            if (p1->Getname() == name)
            {
                p2->next = p1->next;
                break;
            }
        }
        p2 = p1;
        p1 = p1->next;
     }

     if (!p1)
     {
        cout<<"对不起,没有该学生!"<<endl;
     }

     save(head);
     free(p1);

     return 0;
}

void Sort(void)
{
    float max;
    Student *head = NULL, *p1 = NULL, *p2 = NULL, *newhead = NULL, *p3 = NULL, *p4 = NULL;
   
    head = GetList();

    while(1)
    {
        p1 = head;
        if(NULL == head)
        {
            break;
        }
        p1 = head;
        max = head->Getscores();
        while(1)
        {
            if(p1->next != NULL)
            {
                p1 = p1->next;
            }
            if(max < p1->Getscores())
            {
                max = p1->Getscores();
            }
            if(p1->next == NULL)
            {
                break;
            }
        }
        p1 = head;
        while(max != p1->Getscores())
        {
            p2=p1;
            p1=p1->next;        
        }

        if(p1 == head)
        {
            head = head->next;
        }
        else
        {
            p2->next = p1->next;
        }

        if(newhead == NULL)
        {
            p3 = newhead = p1;
        }
        else
        {
            p4 = p3;
            p3 = p1;
            p4->next = p3;
        }
    }
    cout<<"成绩排列如下:"<<endl;
    p1 = newhead;

    while(newhead)
    {
        newhead->ShowData();
        newhead = newhead->next;
    }

    free(p1);
}

int Add(void)
{
    Student *head = NULL, *p1 = NULL, *p2 = NULL;
    int N;

    cout<<"请问要输入几个学生数据:"<<endl;
    cin>>N;
        
    for (int i = 0; i < N; i ++)
    {
        p1 = new Student;
        p1->InputData();
        if (NULL == head)
        {
            head = p2 = p1;
        }   
        else
        {
            p2->next = p1;
            p2 = p1;
        }
    }
    p2->next = NULL;
    save(head);
    free(p1);

    return 0;
}
   
int Print(void)
{
    Student *head, *p;

    head = p = GetList();
    if(!p)
    {
        cout<<"文件为空!"<<endl;
    }

    while(p)
    {
        p->ShowData();
        p = p->next;
    }

    free(head);
   
    return 0;
}

int Menu(void)
{
    cout<<"*****************"<<"欢迎进入学生成绩管理系统!"<<"*****************"<<endl;

    int n;

    cout<<"1.查询某学生成绩"<<endl;
    cout<<"2.修改某学生成绩"<<endl;
    cout<<"3.删除某学生成绩"<<endl;
    cout<<"4.增加某学生成绩"<<endl;
    cout<<"5.打印学生成绩单"<<endl;
    cout<<"6.排序学生成绩单"<<endl;
    cout<<"7.退出"<<endl;

    while (1)
    {
        cout<<"请从主菜单中选择:"<<endl;
        cin>>n;

        switch(n)
        {
            case 1:
                    Find();
                    break;
            case 2:
                    Change();
                    break;
            case 3:
                    Del();
                    break;
            case 4:
                    Add();
                    break;
            case 5:
                    Print();
                    break;
            case 6:
                    Sort();
                    break;
            case 7:
                    return 0;
                    break;
            default:
                    cout<<"选择错误!"<<endl;               
        }

    }

    return 0;
}

int main(void)
{
    ofstream tofile("学生成绩单.txt", ios_base::app|ios_base::binary); // 若没有文件,建立文件;
    tofile.close();

    Menu();
   
    return 0;
}

搜索更多相关主题的帖子: 管理系统 测试 include 数据显示 
2011-09-27 17:15
快速回复:请大家帮忙看看这个C++写的学生成绩单管理系统???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033457 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved