| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 526 人关注过本帖
标题:这个程序怎么在C语言环境下运行不了,请告诉帮忙改改。。。。
只看楼主 加入收藏
颜建林
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2010-10-24
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:8 
这个程序怎么在C语言环境下运行不了,请告诉帮忙改改。。。。
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
struct student   
{
    long int num;
    string name;
    int age;
    string sex;
    string classes;
    string E_mail;
    double math;
    double english;
    double computer;
    double sum;
    struct student *next;
};

int TOTAL_NUM = 0;
student *head = NULL;

void Welcome();
void Mainmenu();
void Record();
void Insert(student *stu);
void Display(student *stu);
void DisplayAll();
void Query();
void Query_by_num();
void Query_by_name();
void Paixu();
void Desc_by_num(int s) ;
void Desc_by_name(int s);
void Desc_by_average(int s) ;
void ReadData();
void WriteData();
void FreeAll();
void Del();
void Change();
void Transfer(student *p);

int main()
{
    string userName;
    string userPWD;
    int i;
    Welcome();
    for(i = 0; i < 3; i++)
    {
        cout<<"\n管理员初始用户名和密码均为admin\n";
        cout<<"请输入您的用户名:";
        getline(cin,userName);

        cout<<"\n请输入您的密码:";
        getline(cin,userPWD);

        if ((userName=="admin") && (userPWD=="admin"))
        {
            //用户名和密码正确,显示主菜单
            Mainmenu();
            break;
        }
        else
        {
            if (i < 2)
            {
                //用户名或密码错误,提示用户重新输入
                cout<<"用户名或密码错误,请重新输入!";
            }
            else
            {
                //连续次输错用户名或密码,退出系统。
                cout<<"您已连续次将用户名或密码输错,系统将退出!";
            }
        }
    }
    return 0;
}
//显示欢迎信息
void Welcome()
{
    time_t t;
    time(&t);
    cout<< "\t--------------------------欢迎进入学生成绩管理系统--------------------------"<<endl;
    cout<<" \t\t\t\t\t\t-----计算机科学与工程学院\n\n";
    cout<< "\t\t\t时间:" << ctime(&t);
    cout<<"----------------------------------------------------------------------------------------\n" ;
}

//系统主菜单
void Mainmenu()
{
    int choice;
    choice = -1;
    ReadData();
    cout<<"\n\n温馨提示:为保证您的操作得到保存,请按0 键正常顺序退出系统^_^\n";
    do
    {
        cout<<"\n\n\n";
        cout<<"-------------------------------------------------------------------------------------\n";
        cout<<"\t\t\t+        学生信息管理系统     |\n";  
        cout<<"\t\t\t--------------------------------\n";               
        cout<<"\t\t\t+    [1]----录入学生信息      |\n";
        cout<<"\t\t\t+    [2]----浏览学生信息      |\n";
        cout<<"\t\t\t+    [3]----查询学生信息      |\n";
        cout<<"\t\t\t+    [4]----删除学生信息      |\n";
        cout<<"\t\t\t+    [5]----修改学生信息      |\n";
        cout<<"\t\t\t+    [6]----排序学生信息      |\n";
        cout<<"\t\t\t+    [0]----退出系统          |\n";
        cout<<"\t\t\t+*?*?*?*?*?*?*?*?*?*?|\n";
        cout<<"\t\t\t--------------------------------\n";
        cout<<"请输入您的选择:";
        cin>>choice;
        switch(choice)
        {
        case 0:
            WriteData();
            cout<<"\n\n------------------------------------------------------------------------------\n"
                <<"----------------------------------------------------------------------------------\n"
                <<"谢谢您使用!\n\n";
            FreeAll();
            exit(0);
        case 1:
            Record();
            break;
        case 2:
            DisplayAll();
            break;
        case 3:
            Query();
            break;  
        case 4:
            Del();
            break;
        case 5:
            Change();
            break;
        case 6:
            Paixu();

        default:
            cout<<"\n无效选项!";
            break;
        }
    }
    while(choice != 0);
}
//录入学生信息
void Record()
{
    student *p0;   
    p0 = new (struct student);
    cout<<"请输入学生的学号:";
    cin>>p0->num;
    getchar();
    cout<<"请输入学生的姓名:";
    getline(cin,p0->name);
    getchar();
    cout<<"请输入学生的年龄:";
    cin>>p0->age;
    getchar();
    cout<<"请输入学生的性别:";
    getline(cin,p0->sex);
    getchar();
    cout<<"请输入学生的班级:";
    getline(cin,p0->classes);
    getchar();
    cout<<"请输入学生的E-mail:";
    getline(cin,p0->E_mail);
    getchar();
    cout<<"请输入学生的英语成绩:";
    cin>>p0->english;
    getchar();
    cout<<"请输入学生的数学成绩:";
    cin>>p0->math;
    getchar();
    cout<<"请输入学生的计算机成绩:";
    cin>>p0->computer;
    getchar();
    Insert(p0);
    cout<<"该学生的信息为:\n";
    cout<<"-----------------------------------------------------------------------------------------------\n\n";
    cout<<"学号\t姓名\t年龄\t性别\t班级\tE-mail\t\t英语\t数学\t计算机\n";
    Display(p0);
}

void Insert(student *stu)
{
    student *p0, *p1, *p2;
    p1 = head;
    p0 = stu;
    if(head == NULL)
    {
        head = p0;
        p0->next = NULL;
    }
    else
    {
        while((p0->num > p1->num)&&(p1->next != NULL))
        {
            p2 = p1;
            p1 = p1->next;
        }
        if(p0->num <= p1->num)
        {
            if(head == p1)
                head = p0;
            else
                p2->next = p0;
            p0->next = p1;
        }
        else
        {
            p1->next = p0;
            p0->next = NULL;
        }  
    }
    TOTAL_NUM++;
}

void Display( student *p)
{
    cout<<p->num<<"\t"<<p->name<<"\t"<<p->age<<"\t"<< p->sex<<"\t"<<p->classes<<"\t"<< p->E_mail<<"\t"<<p->english<<"\t"<<p->math<<"\t"<<p->computer<<endl;

}
//浏览学生信息
void DisplayAll()
{
    student *p;

    cout<<"学生总数"<<TOTAL_NUM<<"\n";
    p = head;
    if(head != NULL)
    {
        cout<<"-----------------------------------------------------------------------------------------------\n\n";
        cout<<"学号\t姓名\t年龄\t性别\t班级\tE-mail\t\t英语\t数学\t计算机\n";
        do
        {
            Display(p);
            cout<<endl;
            p = p->next;
        }
        while(p != NULL);
    }
    cout<<"\n";
}
//查询学生信息
void Query()
{
    int choice;
    choice = -1;
    do
    {
        cout<<"\n";
        cout<<"\t\t\t+--------------------+\n";
        cout<<"\t\t\t| 按学号查询 请按1 |\n";
        cout<<"\t\t\t| 按姓名查询 请按2 |\n";        
        cout<<"\t\t\t| 取消       请按0 |\n";
        cout<<"\t\t\t+--------------------+\n";

        cout<<"请输入您的选择:";
        cin>>choice;

        switch(choice)
        {
        case 0:
            return;
        case 1:
            Query_by_num();
            break;
        case 2:
            Query_by_name();
            break;           
        default:
            cout<<"\n无效选项!";
            break;
        }
    }
    while(choice != 0);
}
//按学号查询学生信息
void Query_by_num()
{
    int num;
    student *p1;

    cout<<"请输入学生的学号:";
    cin>>num;
    if(head==NULL)
    {
        cout<<"无学生记录!\n";
        return;
    }
    p1=head;
    while(num!=p1->num && p1->next!=NULL)
        p1=p1->next;
    if(num == p1->num)
    {
        cout<<"-----------------------------------------------------------------------------------------------\n\n";
        cout<<"学号\t姓名\t年龄\t性别\t班级\tE-mail\t\t英语\t数学\t计算机\n";
        Display(p1);
    }
    else
        cout<<"没有该学生记录,请核对!";
}
//按姓名查询学生信息
void Query_by_name()
{
    string name;
    student *p1;
    getchar();
    cout<<"请输入学生的姓名:";
    getline(cin,name);
    if(head==NULL)
    {
        cout<<"无学生记录!\n";
        return;
    }   
    p1=head;
    while((name!=p1->name) && (p1->next!=NULL))
        p1=p1->next;
    if(name==p1->name)
    {

        cout<<"-----------------------------------------------------------------------------------------------\n\n";
        cout<<"学号\t姓名\t年龄\t性别\t班级\tE-mail\t\t英语\t数学\t计算机\n";
        Display(p1);
    }
    else
        cout<<"没有该学生记录,请核对!";
}

//删除学生信息
void Del()
{
    struct student *p1, *p2;
    long int num;

    if(head == NULL)
    {
        cout<<"无学生记录!\n";
        return;
    }

    cout<<"请输入您要删除的学生的学号:";
    cin>>num;

    p1 = head;

    while(num != p1->num && p1->next != NULL)
    {
        p2 = p1;
        p1 = p1->next;  
    }
    if(num == p1->num)
    {
        if(p1 == head)
            head = p1->next;
        else
            p2->next = p1->next;
        delete p1;


        TOTAL_NUM--;
    }
    else
        cout<<"没有该学生记录,请核对!\n";
}
//修改学生信息
void Change()
{
    student *p1, *p2;
    long int num;

    if(head == NULL)
    {
        cout<<"无学生记录!\n";
        return;
    }

    cout<<"请输入您要修改的学生的学号:";
    cin>>num;

    p1 = head;

    while(num != p1->num && p1->next != NULL)
    {
        p2 = p1;
        p1 = p1->next;  
    }
    if(num == p1->num)
        Transfer(p1);
    else
        cout<<"没有该学生记录,请核对!\n";

}

void Transfer(student *p)
{
    int choice;
    choice = -1;
    do
    {
        cout<<"请选择您要修改的学生的信息内容:\n";
        cout<<"\t\t\t+----------------------+\n";        


        cout<<"\t\t\t| 姓名         请按1 |\n";
        cout<<"\t\t\t| 年龄         请按2 |\n";
        cout<<"\t\t\t| 性别         请按3 |\n";
        cout<<"\t\t\t| 班级         请按4 |\n";
        cout<<"\t\t\t| E-mail        请按5 |\n";
        cout<<"\t\t\t| 英语         请按6 |\n";
        cout<<"\t\t\t| 数学         请按7 |\n";
        cout<<"\t\t\t| 计算机       请按8 |\n";
        cout<<"\t\t\t| 取消         请按0 |\n";
        cout<<"\t\t\t+----------------------+\n";

        cout<<"请输入您的选择:";
        cin>>choice;

        switch(choice)
        {
        case 0:
            return;
        case 1:
            cout<<"请输入新姓名:";
            getchar();
            getline(cin, p->name);
            break;
        case 2:
            cout<<"请输入新年龄:";
            cin>>p->age;
            break;
        case 3:
            cout<<"请输入新性别:";
            getchar();
            getline(cin, p->sex);
            break;
        case 4:
            cout<<"请输入新班级:";
            getchar();
            getline(cin,p->classes);
            break;
        case 5:
            cout<<"请输入新E-mail:";
            getchar();
            getline(cin,p->E_mail);
            break;


        case 6:
            cout<<"请输入新的英语成绩:";
            cin>>p->english;
            break;
        case 7:
            cout<<"请输入新的数学成绩:";
            cin>>p->math;
            break;
        case 8:
            cout<<"请输入新的计算机成绩:";
            cin>>p->computer;
            break;

        default:
            cout<<"\n无效选项!";
            break;
        }
    }
    while(choice != 0);
}
void Paixu()
{
    int choice;
    choice = -1;
    do
    {
        cout<<"\n";
        cout<<"\t\t\t+------------------------+\n";
        cout<<"\t\t\t| 按学号排序     请按1 |\n";
        cout<<"\t\t\t| 按姓名排序     请按2 |\n";        
        cout<<"\t\t\t| 按平均成绩排序 请按3 |\n";
        cout<<"\t\t\t| 取消           请按0 |\n";
        cout<<"\t\t\t+------------------------+\n";

        cout<<"请输入您的选择:";
        cin>>choice;

        switch(choice)
        {
        case 0:
            return;
        case 1:


            Desc_by_num(TOTAL_NUM);
            break;
        case 2:
            Desc_by_name(TOTAL_NUM);
            break;
        case 3:
            Desc_by_average(TOTAL_NUM);
            break;
        default:
            cout<<"\n无效选项!";
            break;
        }
    }
    while(choice != 0);
}

void Desc_by_num(int s)
{
    long int temp;
    long int sum[40];
    student* p1,*p2;
    p1=p2=head;
    while (s==0)
    {
        cout << "没有记录,请先输入记录!" <<endl;
        break;
    }
    if (s!=0)
    {
        cout<<"按学号排序,学生的信息为:\n";
        cout<<"学号\t姓名\t年龄\t性别\t班级\tE-mail\t\t英语\t数学\t计算机\n";
        for (int k=1;(p1!=NULL)&&(k<=s);k++)
        {
            sum[k]=p1->num;
            p1=p1->next;
        }
        for (int i=1;i<=s;i++)    // 利用冒泡法排序
            for (int j=1;j<=s-i;j++)
                if (sum[j]>sum[j+1])
                {
                    temp=sum[j];


                    sum[j]=sum[j+1];
                    sum[j+1]=temp;
                }
                for (int x=1;x<=s;x++)
                {
                    p2=head;
                    while(p2)
                    {        
                        if (sum[x]==p2->num)
                            Display(p2);
                        p2=p2->next;
                    }
                }

    }
    cout<<"\n";

}
void Desc_by_name(int s)
{
    string sum[40];
    string temp;
    student* p1,*p2;
    p1=p2=head;
    while (s==0)
    {
        cout << "没有记录,请先输入记录!" <<endl;
        break;
    }
    if (s!=0)
    {
        cout<<"按姓名排序,学生的信息为:\n";
        cout<<"学号\t姓名\t年龄\t性别\t班级\tE-mail\t\t英语\t数学\t计算机\n";
        for (int k=1;(p1!=NULL)&&(k<=s);k++)
        {
            sum[k]=p1->name;
            p1=p1->next;
        }   
        for (int i=1;i<=s;i++)
            for (int j=1;j<=s-i;j++)
                if (sum[j]>sum[j+1])


                {
                    temp=sum[j];
                    sum[j]=sum[j+1];
                    sum[j+1]=temp;
                }
                for (int x=1;x<=s;x++)
                {
                    p2=head;
                    while(p2!=NULL)
                    {        
                        if (sum[x]==p2->name)
                            Display(p2);
                        p2=p2->next;
                    }
                }
    }
    cout<<"\n";
}
void Desc_by_average(int s)
{
    double temp;
    double sum[40];
    student* p1,*p2;
    p1=head;
    while (s==0)
    {
        cout << "没有记录,请先输入记录!" <<endl;
        break;
    }
    if (s!=0)
    {
        cout<<"按平均成绩排序,学生的信息为:\n";
        cout<<"学号\t姓名\t年龄\t性别\t班级\tE-mail\t\t英语\t数学\t计算机\t平均成绩\n";
        for (int k=1;(p1!=NULL)&&(k<=s);k++)
        {
            sum[k]=(p1->math+p1->english+p1->computer)/3;
            p1->sum=(p1->math+p1->english+p1->computer)/3;
            p1=p1->next;
        }
        for (int i=1;i<=s;i++)
            for (int j=1;j<=s-i;j++)


                if (sum[j]<sum[j+1])
                {
                    temp=sum[j];
                    sum[j]=sum[j+1];
                    sum[j+1]=temp;
                }
                for (int x=1;x<=s;x++)
                {
                    p2=head;
                    while(p2!=NULL)
                    {        
                        if (sum[x]==p2->sum)
                            cout<<p2->num<<"\t"<<p2->name<<"\t"<<p2->age<<"\t"<< p2->sex<<"\t"<<p2->classes<<"\t"<<p2->E_mail<<"\t"<<p2->english<<"\t"<<p2->math<<"\t"<<p2->computer<<"\t"<<p2->sum<<endl;
                        p2=p2->next;
                    }
                }
                cout<<"\n";
    }
}

//写入文件
ostream& operator <<(ostream& os,const student& rhs)
{
    os<<rhs.num<<"\t"<<rhs.name<<"\t"<<rhs.age<<"\t"<<rhs.sex<<"\t"<<rhs.classes<<"\t"<<rhs.E_mail<<"\t"<<rhs.english<<"\t"<<rhs.math<<"\t"<<
    return os;
}
void WriteData()
{
    const char* filename("Student.txt");
    ofstream ofs(filename);
    student *p;
    if (!ofs)
    {
        cout<<"文件打开错误!";
        return;
    }
    ofs<<TOTAL_NUM;


        ofs<<endl;
    for(p = head; p!= NULL; p= p->next)
        ofs<<*p<<endl;
    ofs.close();
}

//读取文件
istream& operator >> (istream& is,student& rhs)
{
    is>>rhs.num>>ws;
    is>>rhs.name>>ws;
    is>>rhs.age>>ws;
    is>>rhs.sex>>ws;
    is>>rhs.classes>>ws;
    is>>rhs.E_mail>>ws;
   

is>>rhs.english>>ws;
    is>>rhs.math>>ws;
    is>>
   
    return is;
}

void ReadData()
{
    const char* filename("Student.txt");
    ifstream ifs(filename);
    student *p1, *p2;
        ifs>>TOTAL_NUM;
    if (!ifs)
    {
        cout<<"没有文件,不能导入数据!";
        return ;
    }
    head = p1 = p2 = new (struct student);
    ifs>>*p1;
    int s=1;
    while(ifs&&s!=TOTAL_NUM)   
    {
        p2->next=new (struct student);
        p1=p2->next;


        ifs>>*p1;
        p2 = p1;
        s++;
    }
    p2->next = NULL;
    ifs.close();
}

// 释放链表内存
void FreeAll()
{
    student *p1, *p2;

    p1=p2=head;
    while(p1)
    {
        p2=p1->next;
        delete(p1);
        p1=p2;
    }
}




搜索更多相关主题的帖子: C语言 环境 
2011-01-09 12:19
hust_sj
Rank: 3Rank: 3
来 自:湖北
等 级:论坛游侠
帖 子:94
专家分:116
注 册:2009-8-26
收藏
得分:2 
兄台,哪拷来的代码。。。这显然是cpp源文件怎么能在C环境下编译运行?

每当我回完帖时,我都会默默的点一根烟,然后离开
2011-01-09 12:36
颜建林
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2010-10-24
收藏
得分:0 
帮我改改啊 。谢谢
2011-01-09 12:43
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:2 
没有全部看完,但是找到了以下的几个错误:
1.c++语言中没有string这个类型的,要定义字符串的话,要用如char ch[100],那么ch就是字符串;
2.没有getline()的用法,改为cin.getline();
3.字符比较要用strcmp(),好像不能像这样的:userName=="admin";
4.case 6: Paixu(); break;//别忘记了break



[ 本帖最后由 qq1023569223 于 2011-1-9 12:50 编辑 ]

   唯实惟新 至诚致志
2011-01-09 12:43
颜建林
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2010-10-24
收藏
得分:0 
谢谢啊指教
2011-01-09 12:50
瓦药墙
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:218
专家分:556
注 册:2009-9-16
收藏
得分:2 
看别人的代码就是辛苦
2011-01-09 12:52
颜建林
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2010-10-24
收藏
得分:0 
呵呵呵, 是啊 这也是一个网友发给我的 ,但我是学C语言的,我实在是改不了了
2011-01-09 12:54
huangapple
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:545
专家分:1790
注 册:2010-12-30
收藏
得分:2 
不活了,

勤能补拙,熟能生巧!
2011-01-10 00:42
url
Rank: 1
等 级:新手上路
帖 子:21
专家分:5
注 册:2011-1-9
收藏
得分:2 
高手!
2011-01-10 01:13
快速回复:这个程序怎么在C语言环境下运行不了,请告诉帮忙改改。。。。
数据加载中...
 
   



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

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