标题:程序有点小问题,求大神修改下
只看楼主
想飞
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-5-14
结帖率:50%
已结贴  问题点数:20 回复次数:6 
程序有点小问题,求大神修改下
//学生的成绩管理系统

#include "iostream.h"
#include "malloc.h"
#include <fstream.h>
#include "conio.h"
#include "stdlib.h"
#include "string.h"
#define LEN sizeof(struct student)

struct student
{
    int num;
    int math;
    int chinese;
    int english;
    char name[20];
    float aver;
    float sum;
    struct student *next;
};

void type();
void insert(struct student *stu);
void look(struct student *stu);
void lookall();
void query_by_name();
void query_by_num();
void order_by_ave();
void del();
void save();   /*声明保存函数*/
struct student* open();  /*声明打开函数*/
int swap(struct student *p,struct student *q);





struct student *head=NULL;
int total=0;




void main()
{
    system("color 70");
   int choice;
   int flag;
   while(1)
   {
       cout<<"\n\t\t\t欢迎来到学生管理系统\n"<<endl;
       cout<<"\n\t\t\t--------------------\n"<<endl;
       cout<<"\t\t\t1:键入学生的信息\n"<<endl;
       cout<<"\t\t\t2:查看录入学生的信息\n"<<endl;
       cout<<"\t\t\t3:按姓名查看\n"<<endl;
       cout<<"\t\t\t4:按学号查看\n"<<endl;
       cout<<"\t\t\t5:按平均成绩排序\n"<<endl;
       cout<<"\t\t\t6:删除学生的信息\n"<<endl;
       cout<<"\t\t\t7:保存\n"<<endl;
       cout<<"\t\t\t8:打开文件\n"<<endl;
       cout<<"\t\t\t0:退出\n"<<endl;
       cout<<"\t\t请输入你的选择:"<<endl;
       cin>>choice;
       switch(choice)
       {
       case 1:type();break;
       case 2:lookall();break;
       case 3:query_by_name();break;
       case 4:query_by_num();break;
       case 5:order_by_ave();break;
       case 6:del();break;
       case 7:save();break;
       case 8:open();break;
       case 0:flag=0;break;
default :cout<<"错误!"<<endl;
       }
       if(flag) break;
   }
}


void type()
{
    system("CLS");
   struct student *p;
   p=(struct student *)malloc(LEN);
   cout<<"姓名:"<<endl;
   cin>>p->name;
   cout<<"学号:"<<endl;
   cin>>p->num;
   cout<<"数学成绩:"<<endl;
   cin>>p->math;
   cout<<"外语成绩:"<<endl;
   cin>>p->english;
   cout<<"语文成绩:"<<endl;
   cin>>p->chinese;
p->sum=float(p->chinese+p->english+p->math);
p->aver=p->sum/3;
insert(p);
cout<<"按任意键返回主菜单"<<endl;
getch();
save();

}

void look(struct student *stu)
{
    struct student *p;
    p=stu;
    cout<<"姓名:"<<p->name<<"  "<<"学号:"<<p->num<<"  "<<
        "数学:"<<p->math<<"  "<<"外语:"<<p->english<<"  "
        <<"语文:"<<p->chinese<<"  "<<"总分:"<<p->sum<<"  "
        <<"平均分:"<<p->aver<<endl;

}

void lookall()
{    system("CLS");
    struct student *p;
    p=head;
    if(p==NULL)cout<<"无记录"<<endl;
    else
    while(p)
    {
        look(p);
        p=p->next;
    }
cout<<"按任意键返回主菜单"<<endl;
getch();
}


void insert(struct student *stu)
{
    struct student *p,*q1;
    p=stu;
    q1=head;
    if(q1=NULL)
    {
        head=p;p->next=NULL;
    }
    else
    {
        q1=p;p->next=NULL;
        p=q1;  
    }
total++;
}


void query_by_name()
{system("CLS");
    struct student *p;
    char na[20];
    p=head;
    cout<<"请输入查找的姓名:"<<endl;
    cin>>na;
    if(p==NULL)cout<<"无记录"<<endl;
    else
        while(p)
        {   if(!strcmp(p->name,na))look(p);
            else p=p->next;

    }cout<<"按任意键返回主菜单"<<endl;
getch();

}

void query_by_num()
{system("CLS");
   struct student *p;
   int i;
   p=head;
   cout<<"请输入查找的学号:"<<endl;
   cin>>i;
   if(p==NULL)cout<<"无记录"<<endl;
   else
       while(p);
       {
           if(p->num==i)look(p);
           else p=p->next;

       }cout<<"按任意键返回主菜单"<<endl;
getch();

}


void order_by_ave()
{
    system("CLS");
    struct student *p,*q;
    p=head;
    if(p==NULL)cout<<"无记录"<<endl;
    else
    {
        if(total=1)lookall();
        if(p->next!=NULL)
        {
            q=p->next;
            if(q->aver<p->aver)swap(p,q);
            p=p->next;
        }
        else lookall();

    }
    getch();
    int i;
    cout<<"是否保存?(1:yes or 2:no)"<<endl;
    cin>>i;
    if(i==1) save();
    else cout<<"退出"<<endl;
    getch();

}


int swap(struct student *p,struct student *q)
{
    struct student *m;
    m=p;
    p=q;
    q=m;
    return 0;
}

void del()
{system("CLS");
    struct student *p,*q;
    int i;
    p=head;
    cout<<"删除的学号是:"<<endl;
    cin>>i;
    if(p->next==NULL)cout<<"无记录"<<endl;
    else
        while((p->num!=i)&&(p->next!=NULL))
        {q=p;
          p=p->next;
        }
        if(p->num==i)
        {
            q->next=p->next;
            cout<<"删除的学生是:"<<endl;
            look(p);
            total--;
        }
        else cout<<"没有找到该学生!"<<endl;
cout<<"按任意键返回主菜单"<<endl;
getch();

}



void save()  /*建立保存文件函数*/
{    struct student *p;
    p=head;
    system("CLS");
    ofstream out("data.txt",ios::out);
    cout<<total<<endl;
    while(p!=NULL)
    {
        out<<p->aver<<"\t"<<p->chinese<<"\t"<<p->english<<"\t"<<p->math<<
            "\t"<<p->name<<"\n"<<p->num<<"\n"<<p->sum<<endl;
        p=p->next;
    }
cout<<"按任意键返回主菜单"<<endl;
getch();

}



struct student* open()  /*定义打开文件函数*/
{   
    system("CLS");
    int i=0;
    struct student *p1,*p2,*st;
    p1=p2=(struct student *)malloc(LEN);
    head=NULL;
    ifstream in("data.txt",ios::out);
    in>>i;
        if(i==0){cout<<"data.txt文件的数据为空请先输入数据"<<endl;return 0;}
        else
        {
            for(;i>0;i--)
            {p1=(struct student *)malloc(LEN);
            in>>st->aver>>st->chinese>>st->english>>
                st->math>>st->name>>st->num>>st->sum;
                strcpy(p1->name,st->name);
                p1->aver=st->aver;
                p1->chinese=st->chinese;
                p1->english=st->english;
                p1->math=st->math;
                p1->num=st->num;
                p1->sum=st->sum;
                if(total==0)head=p1;
                else p2->next=p1;
                p2=p1;
                total++;
                look(p1);
            }
                cout<<"数据成功读取完毕"<<endl;
                p2->next=NULL;
                return (head);   
            }
    }
搜索更多相关主题的帖子: english include 管理系统 insert 
2014-05-16 09:40
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
得分:0 
小问题是什么?

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2014-05-16 09:48
想飞
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-5-14
得分:0 
回复 2 楼 peach5460
问题多多,,我是新手,还望大神指教
2014-05-16 16:59
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
得分:20 
报什么错呢,最好不要这样把代码粘上去,传文档,方便下载查看。

我不是砖家,要努力成为砖家。
2014-05-16 17:45
yxq211046
Rank: 2
等 级:论坛游民
帖 子:22
专家分:36
注 册:2014-4-17
得分:0 
我也新手一枚,通过修改,程序勉强能在VS2013上运行,但还有几个问题:
1、输入1,在输入信息后好像没有保存,data.txt文件中没有数据。
2、可能因为data.txt文件中没有数据,查询的几项测试不出来。
程序中的一些语句还没弄懂,所以没有修改,如有改错的地方敬请原谅。
程序代码:
//学生的成绩管理系统

#include <iostream>
#include "malloc.h"
#include <fstream>
#include "conio.h"
#include "stdlib.h"
#include <string>
#define LEN sizeof(struct student)
using namespace std;

struct student //学生信息结构体
{
    int num; //学号    
    int math; //数学成绩
    int chinese; //语文成绩
    int english; //英语成绩
    char name[20]; //姓名
    float aver;  //平均成绩
    float sum; //总成绩
    struct student *next;//结构体指针,表示该结构体的起始位置?
};

void type();
void insert(struct student *stu);
void look(struct student *stu);
void lookall();
void query_by_name();
void query_by_num();
void order_by_ave();
void del();
void save();   /*声明保存函数*/
struct student* open();  /*声明打开函数*/
int swap(struct student *p, struct student *q);





struct student *head = NULL;
//定义结构体指针head并将Head指向空,是为了避免发生内存错误
int total = 0;




void main()
{
    system("color 70");//设置默认控制台前景和背景颜色
    int choice;
    int flag=0;
    while (1)
    {
        cout << "\n\t\t\t欢迎来到学生管理系统\n" << endl;
        cout << "\n\t\t\t--------------------\n" << endl;
        cout << "\t\t\t1:键入学生的信息\n" << endl;
        cout << "\t\t\t2:查看录入学生的信息\n" << endl;
        cout << "\t\t\t3:按姓名查看\n" << endl;
        cout << "\t\t\t4:按学号查看\n" << endl;
        cout << "\t\t\t5:按平均成绩排序\n" << endl;
        cout << "\t\t\t6:删除学生的信息\n" << endl;
        cout << "\t\t\t7:保存\n" << endl;
        cout << "\t\t\t8:打开文件\n" << endl;
        cout << "\t\t\t0:退出\n" << endl;
        cout << "\t\t请输入你的选择:" << endl;
        cin >> choice;
        switch (choice)
        {
        case 1:type(); break;
        case 2:lookall(); break;
        case 3:query_by_name(); break;
        case 4:query_by_num(); break;
        case 5:order_by_ave(); break;
        case 6:del(); break;
        case 7:save(); break;
        case 8:open(); break;
        case 0:flag = 1; break;
        default:cout << "错误!" << endl;
        }
        if (flag) break;
    }
}


void type()
//将学生信息录入
{
    system("CLS");// 清除屏幕
    struct student *p;
    p = (struct student *)malloc(LEN);
    //动态申请一块大小为  LEN 的内存,并将它转化为struct student 类型
    //然后把这个内存的首地址赋值给 p
    cout << "姓名:" << endl;
    cin >> p->name;
    //P是某一结构体变量的指针,name则是该结构体的成员。p->name 和*p.name一样
    cout << "学号:" << endl;
    cin >> p->num;
    cout << "数学成绩:" << endl;
    cin >> p->math;
    cout << "外语成绩:" << endl;
    cin >> p->english;
    cout << "语文成绩:" << endl;
    cin >> p->chinese;
    p->sum = float(p->chinese + p->english + p->math);
    p->aver = p->sum / 3;
    insert(p);
    //
    cout << "按任意键返回主菜单" << endl;
    _getch();//暂停屏幕,按任意键跳转
    save();

}

void look(struct student *stu)//输出学生所有成绩信息
{
    struct student *p;
    p = stu;
    cout << "姓名:" << p->name << "  " << "学号:" << p->num << "  " <<
        "数学:" << p->math << "  " << "外语:" << p->english << "  "
        << "语文:" << p->chinese << "  " << "总分:" << p->sum << "  "
        << "平均分:" << p->aver << endl;

}

void lookall()
{
    system("CLS");//清除屏幕
    struct student *p;
    p = head;
    if (p == NULL)cout << "无记录" << endl;
    else
    while (p)
    {
        look(p);
        p = p->next;
    }
    cout << "按任意键返回主菜单" << endl;
    _getch();
}


void insert(struct student *stu)
{
    struct student *p, *q1;
    p = stu;
    q1 = head;
    if (q1 = NULL)
    {
        head = p; p->next = NULL;
    }
    else
    {
        q1 = p; p->next = NULL;
        p = q1;
    }
    total++;
}


void query_by_name()
{
    system("CLS");
    struct student *p;
    char na[20];
    p = head;
    cout << "请输入查找的姓名:" << endl;
    cin >> na;
    if (p == NULL)cout << "无记录" << endl;
    else
    while (p)
    {
        if (!strcmp(p->name, na))look(p);
        else p = p->next;

    }cout << "按任意键返回主菜单" << endl;
    _getch();

}

void query_by_num()
{
    system("CLS");
    struct student *p;
    int i;
    p = head;
    cout << "请输入查找的学号:" << endl;
    cin >> i;
    if (p == NULL)cout << "无记录" << endl;
    else
    while (p);
    {
        if (p->num == i)look(p);
        else p = p->next;

    }cout << "按任意键返回主菜单" << endl;
    _getch();

}


void order_by_ave()
{
    system("CLS");
    struct student *p, *q;
    p = head;
    if (p == NULL)cout << "无记录" << endl;
    else
    {
        if (total = 1)lookall();
        if (p->next != NULL)
        {
            q = p->next;
            if (q->aver<p->aver)swap(p, q);
            p = p->next;
        }
        else lookall();

    }
    _getch();
    int i;
    cout << "是否保存?(1:yes or 2:no)" << endl;
    cin >> i;
    if (i == 1) save();
    else cout << "退出" << endl;
    _getch();

}


int swap(struct student *p, struct student *q)
{
    struct student *m;
    m = p;
    p = q;
    q = m;
    return 0;
}

void del()
{
    system("CLS");
    struct student *p, *q;
    int i;
    p = head;
    q = head;
    cout << "删除的学号是:" << endl;
    cin >> i;
    if (p->next == NULL)cout << "无记录" << endl;
    else
    while ((p->num != i) && (p->next != NULL))
    {
        q = p;
        p = p->next;
    }
    if (p->num == i)
    {
        q->next = p->next;
        cout << "删除的学生是:" << endl;
        look(p);
        total--;
    }
    else cout << "没有找到该学生!" << endl;
    cout << "按任意键返回主菜单" << endl;
    _getch();

}



void save()  /*建立保存文件函数*/
{
    struct student *p;
    p = head;
    system("CLS");
    ofstream out("data.txt", ios::out);
    cout << total << endl;
    while (p != NULL)
    {
        out << p->aver << "\t" << p->chinese << "\t" << p->english << "\t" << p->math <<
            "\t" << p->name << "\n" << p->num << "\n" << p->sum << endl;
        p = p->next;
    }
    cout << "按任意键返回主菜单" << endl;
    _getch();

}



struct student* open()  /*定义打开文件函数*/
{
    system("CLS");
    int i = 0;
    struct student *p1, *p2, *st;
    st = head;
    p1 = p2 = (struct student *)malloc(LEN);
    head = NULL;
    ifstream in("data.txt", ios::out);
    in >> i;
    if (i == 0){ cout << "data.txt文件的数据为空请先输入数据" << endl; return 0; }
    else
    {
        for (; i>0; i--)
        {
            p1 = (struct student *)malloc(LEN);
            in >> st->aver >> st->chinese >> st->english >>
                st->math >> st->name >> st->num >> st->sum;
            p1->name, st->name;
            p1->aver = st->aver;
            p1->chinese = st->chinese;
            p1->english = st->english;
            p1->math = st->math;
            p1->num = st->num;
            p1->sum = st->sum;
            if (total == 0)head = p1;
            else p2->next = p1;
            p2 = p1;
            total++;
            look(p1);
        }
        cout << "数据成功读取完毕" << endl;
        p2->next = NULL;
        return (head);
    }
}
2014-05-17 11:21
想飞
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-5-14
得分:0 
回复 5 楼 yxq211046
谢了,不过好像还是有问题
2014-05-17 16:32
想飞
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-5-14
得分:0 
回复 4 楼 砖家的谎言
你知道这种情况是咋回事么,怎么解决
2014-05-17 16:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-431614-1-1.html




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

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