forver1314 2014-12-28 12:16 采纳率: 0%
浏览 1748

关于函数调用,求大神指导

#include

#include
#include
#include
#include
#include
#include
using namespace std;
#include "student.h"
#include "teacher.h"
#include "people.h"
#include "operate.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

operate::operate()
{
stu=0;//将对象指针赋值为0
}

operate::~operate()
{
student *p; //对象指针
p=stu; //对象指针的赋值
while(p)
{
p=p->next; //指针指向下一个地址
delete stu; //删除前一个指针地址所指的内容,释放内存。
stu=p; //将下一个指针的内容赋值给现在指针所指的内容。
}
stu=0; //指针地址重新赋值为0

}

void operate::addinformation() //添加
{
student q;
operate s;
string name1;
long num1;
int age1;
string sex1,kemu1,yuanxi1;//grade;
float math1,com1,eng1,zongfen1,AA1;
string zhuanye1,banji1;
system("cls");//清屏
cout<<"
****************增加的学生基本信息************"< cout cout cin>>name1;
cout<<"学号:"< cin>>num1;
cout<<"年龄:"< cin>>age1;
cout<<"性别(M/F):"< cin>>sex1;
cout<<"数学成绩:"< cin>>math1;
cout<<"语文:"< cin>>com1;
cout<<"英语成绩:"< cin>>eng1;
zongfen1=math1+com1+eng1;
AA1=(math1+com1+eng1)/3;
cout<<"总分:"< cout cout cin>>yuanxi1;
cout<<"专业"< cin>>zhuanye1;
cout<<"班级"< cin>>banji1;
q=new student(name1,num1,age1,sex1,math1,com1,eng1,AA1,zongfen1,yuanxi1,zhuanye1,banji1); //动态分配内存空间。
q->next=0;
q->AA=(math1+com1+eng1)/3; //AA是一个变量,q->AA是特指对象的平均成绩。
q->zongfen=math1+com1+eng1;
q->yuanxi=yuanxi1;
if(stu) //stu是对象,stu看是否存在,若存在,则if(stu)其中stu=1,则执行括号里面的,否则执行else
{
student *t; //定义对象指针
t=stu; //对象指针之间的赋值
if(t->num==num1)
{
cout<<"学号已存在,请重新输入"< return;
}
while(t->next)//t->text若为1,指还没到链表的末尾,说明后面还有学生,若为0,则指已经是最后一个学生。
{
if(t->num==num1)
{
cout<<"学号已存在,请重新输入"< return;
}
else
t=t->next; //将后面指的学生地址赋给t
}
t->next=q; //后面已经没有学生,直接将q所指的学生赋给t->next.
}
else
{
stu=q; //若对象不存在,直接将q所指的地址赋给stu.
}
cout<<"输入完毕..."<<endl;
getch();

}

void operate::deleteinformation() //删除
{

    int num1; 
    system("cls");//清屏
    cout<<"********** 删除学生信息 **********"<<endl; 
    cout<<"*****请输入想要删除学生的学号:****"<<endl; 
    cin>>num1;  
    student *p1,*p2; //定义两个对象指针。
    p1=stu; 
    while(p1) //p1所指对象存在.即p1还没指到结尾。
    { 
        if(p1->num==num1) //若指到的num=num1,则跳出循环。
            break; 
        else //若找不到
        { 
            p2=p1; //首先p1和p2指的是同一个对象。
            p1=p1->next; //p1地址向后移动一个。
        } 
    } 
    if(p1!=NULL)//p1所指对象不为空
    { 
        cout<<setw(10)<<"姓名"<<setw(10)<<"学号"<<setw(10)<<"年龄"<<setw(10)<<"性别"<<setw(10)<<"数学成绩"<<setw(15)<<"语文成绩"<<
            setw(10)<<"英语成绩"<<setw(10)<<"平均分"<<setw(10)<<"总分"<<setw(10)<<"院系"<<setw(10)<<"专业"<<setw(10)<<"班级"<<endl;
        p1->play(); 
        cout<<"确定删除吗?[Y/N]"<<endl; 
        char c;
        cin>>c; 
        if(toupper(c)!='Y')
            return;     
        if(p1==stu) //如果对象就是p1所指的对象
        { 
            stu=p1->next;//把p1所指地址的下一位赋值给stu. 
            delete p1; //删除原来p1所指的对象。
        } 
        else 
        { 
            p2->next=p1->next; //p1和p2所指的地址都向后移动一下。
            delete p1; 
        } 
        cout<<"找到学号为"<<num1<<"的学生,并删除\n"; 
    } 
    else 
        cout<<"未找到想要删除的学生!\n"; 
    getch();
}

void operate::modifyinformation()//修改

{

    student *p1,*p2;
    long num1; 
    system("cls");//清屏 
    cout<<"************ 修改学生信息 **********"<<endl;
    cout<<"********输入要修改学生的学号:*******"<<endl; 
    cin>>num1; 

    //查找要修改的结点  
    p1=stu; //对象地址和p1地址一样。
    while(p1) //如果p1所指对象存在,即p1还没指到结尾。
    { 
        if(p1->num==num1) 
            break; 
        else 
        { 
            p2=p1; 
            p1=p1->next; 
        } 
    } 
    if(p1!=NULL) //如果p1所指对象不为空。
    { 
        cout<<"学号是"<<num1<<"的学生的信息"<<endl; 
        cout<<"姓名 "<<p1->name<<endl;
        cout<<"年龄"<<p1->age <<endl;
        cout<<"性别"<<p1->sex <<endl;
        cout<<"数学"<<p1->math<<endl;
        cout<<"语文"<<p1->com<<endl;
        cout<<"英语"<<p1->eng<<endl;
        cout<<"院系"<<p1->yuanxi <<endl;
        cout<<"专业"<<p1->zhuanye <<endl;
        cout<<"班级"<<p1->banji <<endl;
        cout<<"请输入修改后的信息:"<<endl; 
        cout<<"姓名:"<<endl; 
        cin>>p1->name;
        cout<<"性别:"<<endl;
        cin>>p1->sex;
        cout<<"年龄"<<endl;
        cin>>p1->age ;
        cout<<"数学成绩:"<<endl;
        cin>>p1->math;
        cout<<"语文:"<<endl;
        cin>>p1->com;
        cout<<"英语成绩:"<<endl;
        cin>>p1->eng;
        cout<<"院系"<<endl;
        cin>>p1->yuanxi ;
        cout<<"专业"<<endl;
        cin>>p1->zhuanye ;
        cout<<"班级"<<endl;
        cin>>p1->banji ;
        cout<<"修改成功"<<endl;

    } 
    else //未找到接点 
        cout<<"未找到!\n";
    getch();
}

void operate::searchinformation()//查询函数
{

system("cls");//清屏
cout<<"********** 查询学生信息 *********"<<endl;
cout<<"*********************************"<<endl;
cout<<"*********************************"<<endl;
cout<<"**********请输入查询方式:*******"<<endl; 
cout<<"*********1.按学号查询************"<<endl; 
cout<<"*********2.按姓名查询***********"<<endl;  
cout<<"*********3.返回*****************"<<endl; 
cout<<"********************************"<<endl;
cout<<"********************************"<<endl;
char c; 
cin>>c;
switch (c) 
{ 
    case '1'://按学号查找
    { 
        long num1; 
        system("cls");//清屏
        cout<<"请输入要查询的学号:"<<endl; 
        cin>>num1;      
        student *p1,*p2; 
        p1=stu; 
        while(p1) 
        { 
            if(p1->num==num1) 
                break;
            else 
            { 
                p2=p1; 
                p1=p1->next; 
            } 
        }       
        if(p1!=NULL) 
        { 
            cout<<"学号是"<<num1<<"的学生的信息"<<endl;
            cout<<"姓名:"<<p1->name<<endl;
            cout<<"性别:"<<p1->sex<<endl;
            cout<<"年龄"<<p1->age <<endl;
            cout<<" 数学:"<<p1->math<<endl;
            cout<<" 语文:"<<p1->com<<endl;
            cout<<" 英语:"<<p1->eng<<endl;
            cout<<"平均分:"<<p1->AA<<endl;
            cout<<"总分:"<<p1->zongfen<<endl;
            cout<<"院系:"<<p1->yuanxi<<endl;
            cout<<"专业"<<p1->zhuanye <<endl;
            cout<<"班级"<<p1->banji <<endl;
            cout<<"查询完毕..."; 
        } 
        else 
            cout<<endl<<"未找到!\n"; 
        getch();
        break; 
    } 
case '2': //按姓名查找
    { 
        string name1;
        system("cls");
        cout<<"请输入要查询的学生姓名:"<<endl; 
        cin>>name1;     
        student *p1,*p2; 
        p1=stu; 
        while(p1) 
        { 
            if(p1->name==name1) 
                break; 
            else 
            { 
                p2=p1; 
                p1=p1->next; 
            } 
        } 

        if(p1!=NULL) 
        { 
            cout<<name1<<"的学生的信息"<<endl; 
            cout<<"学号:"<<p1->num<<endl;
            cout<<"性别:"<<p1->sex<<endl;
            cout<<"年龄:"<<p1->age <<endl;
            cout<<"数学:"<<p1->math<<endl;
            cout<<"语文:"<<p1->com<<endl;
            cout<<"英语:"<<p1->eng<<endl;
            cout<<"平均分:"<<p1->AA<<endl;
            cout<<"总分:"<<p1->zongfen<<endl;
            cout<<"院系:"<<p1->yuanxi<<endl;
            cout<<"专业:"<<p1->zhuanye <<endl;
            cout<<"班级:"<<p1->banji <<endl;
            cout<<"查询完毕..."<<endl; 
        } 
        else 
            cout<<endl<<"未找到!\n"; 
        getch();
        break; 
    } 

case '3':
    return; 
}

}
void operate::saveinformation()//保存
{

    char c; 
    system("cls");
    cout<<"\n保存学生信息(将覆盖原文件),是否继续?[Y/N]:"; 
    cin>>c; 
    if(toupper(c)!='Y') 
        return; 
    ofstream tfile("date.txt",ios_base::binary);//以二进制方式打开文件 
    student *p=stu; 
    while(p)            // 写入文件 ,如果指针p的地址存在
    { 
        tfile<<p->name<<"\t"<<p->num<<"\t"<<p->sex<<"/t"<<p->age<<"\t"<<p->math<<"\t"<<p->com<<"\t"<<p->eng<<"/t"<<p->yuanxi <<"/t"<<p->zhuanye <<"/t"<<p->banji <<endl; 
        tfile<<endl; 
        p=p->next; 
    } 
    tfile.close(); 
    cout<<"保存完毕..."<<endl;
    getch();
}

求大神指导怎么在添加函数中调用保存函数,使得每一个添加的学生信息都可以保存

  • 写回答

2条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能