首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

【帮忙把这个C++改写成C的】,该怎么解决

2012-02-25 
【帮忙把这个C++改写成C的】C/C++ code#includeiostream#includestring#includefstream#includeioman

【帮忙把这个C++改写成C的】

C/C++ code
#include<iostream>#include<string>#include<fstream>#include<iomanip>                              using namespace std;class student                                        {protected:                                              int number;   char name[20];   char sex[6];   char tel[20];   char nation[6];   char birth[20];   char party[10];   char id[20];   float score[6];public:                                              student *next;                                   student(){ }                                 ~student(){ }                                    char* getname(){ return name; }                        int getnumber(){ return number;}   double getscore(int i) { return score[i];}   float getg(){ return (score[0]+score[1]+score[2]+score[3]+score[4]+score[5]); }   void input()   {    int e=1;    cout<<"\t\t\t按提示输入:"<<endl;    cout<<"\t\t输入学号: ";    cin>>number;    cout<<"\t\t输入姓名: ";    cin>>name;    cout<<"\t\t输入性别: ";    cin>>sex;    if(strcmp(sex,"男")==0 || strcmp(sex,"女")==0)      {        cout<<"\t\t输入电话号码: ";        cin>>tel;        cout<<"\t\t输入民族:";        cin>>nation;        cout<<"\t\t输入出生日期(如1990-08-15):";        cin>>birth;        cout<<"\t\t输入政治面貌: ";        cin>>party;         cout<<"\t\t输入身份证号: ";        cin>>id;        cout<<"\t\t输入高数分数: ";        cin>>score[0];        cout<<"\t\t输入大学英语分数: ";        cin>>score[1];        cout<<"\t\t输入面向对象程序设计分数:";        cin>>score[2];        cout<<"\t\t输入大学物理分数:";        cin>>score[3];        cout<<"\t\t输入数据结构分数:";        cin>>score[4];        cout<<"\t\t输入马克思列宁主义哲学分数:";        cin>>score[5];        e=0;      }         else      {        cout<<"\t\t\t无此类型性别!重新输入!"<<endl;        e=1;      }    while(e);       return ;   }   void input(ifstream & is)                              {    is>>number>>name>>sex>>tel>>nation>>birth>>party>>id      >>score[0]>>score[1]>>score[2]>>score[3]>>score[4]>>score[5];    is.get();                                       }   void output()   {    cout<<"学生基本信息如下:"<<endl;       cout<<"学号:"<<number        <<"   姓名:"<<name        <<"   性别:"<<sex        <<"   电话号码:"<<tel        <<"   民族:"<<nation        <<"   出生日期:"<<birth        <<"   政治面貌:"<<party<<endl        <<"身份证号:"<<id        <<"   高数:"<<score[0]        <<"   大学英语英语:"<<score[1]        <<"   面向对象程序设计:"<<score[2]        <<"   大学物理:"<<score[3]        <<"   数据结构:"<<score[4]        <<"   马克思列宁主义哲学:"<<score[5]        <<"   总分:"<<getg()<<endl;   }   void output(ofstream & os)                         {os<<setw(6)<<number      <<setw(15)<<name      <<setw(6)<<sex      <<setw(20)<<tel      <<setw(6)<<nation      <<setw(20)<<birth      <<setw(20)<<party      <<setw(20)<<id      <<setw(6)<<score[0]      <<setw(6)<<score[1]      <<setw(6)<<score[2]      <<setw(6)<<score[3]      <<setw(6)<<score[4]      <<setw(6)<<score[5]<<endl;   }};class school                                         {public:                                                 school(){ head=new student; head->next=NULL; key=0; }   ~school(){ delete head; }                              void input();   void mend();   void del();   int find(student **p,int num,char *pn="^");   void found();   void show();   void count();   void save();   void begin();   void clear();   char mainmenu();   int getkey(){ return key;}   void setkey(int k){ key=k; }private:                                                  student *head;                                      int key;};//录入函数void school::input(){   student *p,*p2=NULL;   p=head;                                             int n;   while(p->next)    p=p->next;   while(n)   {          p2=new student;       p2->input();       p->next=p2;       p2->next=NULL;       p=p->next;                                       school::setkey(1);       cout<<"\t\t\t按1继续,按0返回 : ";       cin>>n;   }}//子查找函数int school::find(student **p1,int num,char *pn){      student *p;   p=head;   while(p->next)   {      (*p1)=p;         if( (p->next)->getnumber()==num||!strcmp( (p->next)->getname(),pn ) )     return 1;      p=p->next;   }   return 0;}//查找函数void school::found(){   student *p;   int num=-1,n=9;   char name[20]="^";   do   {         cout<<"\t\t1:按学号查找,2:按姓名查找: ";      cin>>n;   }while(n<1||n>2);   if(n==1)   {    cout<<"\t\t\t输入学号: ";       cin>>num;   }   if(n==2)   {    cout<<"\t\t\t输入姓名: ";    cin>>name;   }   if(!find(&p,num,name) )   {    cout<<"\t\t找不到你要查找的内容!"<<endl;    return;   }   (p->next)->output();}//删除函数void school::del(){   student *p,*p2;   int num;   cout<<"\t\t\t输入学号: ";   cin>>num;   if( !find(&p,num,"^") )   {    cout<<"\t\t找不到你要删除的内容!"<<endl;    return;   }   (p->next)->output();   p2=p->next;   p->next=p2->next;   delete p2;   cout<<"删除成功,显示结果请选择菜单2!"<<endl;   school::setkey(1);}//显示函数void school::show(){   student *p;   p=head;   while(p->next)   {    (p->next)->output();    p=p->next;   }}//修改函数void school::mend(){   student *p;   int num=-1,n;   char name[20]="^";   do   {         cout<<"\t\t1:按学号修改,2:按姓名修改: ";      cin>>n;   }while(n<1||n>2);   if(n==1)   {    cout<<"\t\t\t输入学号: ";       cin>>num;   }   if(n==2)   {    cout<<"\t\t\t输入姓名: ";    cin>>name;   }   if( !find(&p,num,name) )   {    cout<<"\t\t找不到你要修改的内容!"<<endl;    return;   }   (p->next)->output();   (p->next)->input();   school::setkey(1);}//保存函数void school::save(){   student *p;   p=head;   ofstream os("student.txt",ios::out);   if (school::getkey()==1)   {       while(p->next)    {        (p->next)->output(os);        p=p->next;    }   }   cout<<"\t\t\t文件已保存! "<<endl;   school::setkey(0);}//初始化函数void school::begin(){   student *p,*p2;   p=head;   clear();   long t;   ifstream is("student.txt",ios::in);    if(!is)   {    ofstream os("student.txt",ios::out);    os.close();    return ;   }   int num=-1;   while(1)   {          num=-1;       t=is.tellg();       is>>num;    is.seekg(t);       if(num<0)    {        is.close();     return;    }    p2=new student;    p2->input(is);    p->next=p2;    p2->next=NULL;    p=p->next;   }}//清空函数 void school::clear(){   student *p,*p2;   p=head->next;   while( p )   {    p2=p;    p=p->next;    delete p2;   }}//统计函数       void school::count(){   student *p;   p=head;   int n=0;   double g[6]={0,0,0,0,0};   float j[6]={0,0,0,0,0};   while(p->next)   {      p=p->next;       n++;      for(int i=0;i<6;i++)      {        g[i]=g[i]+( p->getscore(i) );      (p->getscore(i) )>=60? j[i]++ : 0 ;    }   }   cout<<"\t\t\b\b\b\b高数总分:"<<g[0]<<"   平均分:"<<g[0]/n    <<"   及格率:"<<j[0]/n<<endl<<"\t\t\b\b\b\b大学英语总分:"<<g[1]    <<"   平均分:"<<g[1]/n<<"   及格率:"<<j[1]/n<<endl    <<"\t\t\b\b\b\b面向对象程序设计总分: "<<g[2]<<"   平均分: "<<g[2]/n    <<" 及格率:"<<j[2]/n<<endl    <<"\t\t\b\b\b\b大学物理总分:"<<g[3]<<" 平均分:"<<g[3]/n    <<"   及格率:"<<j[3]/n<<endl<<"\t\t\b\b\b\b数据结构总分:"<<g[4]    <<"   平均分:"<<g[4]/n<<"   及格率:"<<j[4]/n<<endl    <<"\t\t\b\b\b\b马克思列宁主义哲学总分: "<<g[5]<<"   平均分: "<<g[5]/n    <<" 及格率:"<<j[5]/n<<endl;}//主选菜单函数char school::mainmenu(){   char n[6];   cout<<"\n\n  ☆☆☆☆欢迎进入攀枝花学院学生学籍管理系统☆☆☆☆"<<endl<<endl;   cout<<"                计算机学院2010级软件工程班              "<<endl<<endl    <<"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl    <<"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl    <<"* *                1:     录入学生信息                  * *"<<endl    <<"* *                2:     显示学生信息                  * *"<<endl    <<"* *                3:     查找学生信息                  * *"<<endl    <<"* *                4:     删除学生信息                  * *"<<endl    <<"* *                5:     修改学生信息                  * *"<<endl    <<"* *                6:     统计学生成绩                  * *"<<endl    <<"* *                7:     保存学生信息                  * *"<<endl    <<"* *                0:     退出系统                      * *"<<endl    <<"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl    <<"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl    <<"                          请选择:";   cin>>n;   return n[0];}//主函数void main(){   school pp;   int k=1;   char n;   pp.begin();   while(k==1)   {    n=pp.mainmenu();    switch(n)    {      case '1':pp.input(); break;      case '2':pp.show(); break;         case '3':pp.found(); break;      case '4':pp.del(); break;      case '5':pp.mend(); break;      case '6':pp.count(); break;      case '7':pp.save(); break;      case '0':     if(pp.getkey()==1)     {      cout<<"\t\t\t是否保存?   1 : 保存 0:不保存 : ";      cin>>k;      if(k==1)        pp.save();     }     pp.clear();     k=0;     break;    }   }} 



[解决办法]
纯C语言的话 把那两类的数据提出来做为STRUCT 结构 然后把类的成员函数写成全局的过程函数
[解决办法]
有改的功夫,还不如自己做这作业,或者直接google个c版的。

要改也不是很难。成员函数隐含一个this指针
char* getname(){ return name; }
其实就是
char* getname(student *this){ return this->name; } 

调用
obj.getname();
就是
getname(&obj);
 
够造和析构函数自己写init(student *this)和destroy(student *this)函数来做就行了。
[解决办法]
lz放弃吧,重新找c版的。
[解决办法]
http://blog.csdn.net/Hackbuteer1/archive/2011/06/26/6568892.aspx
给楼主看一下我写的学生学籍管理系统,是C语言版本的。。

热点排行