永发信息网

只有显示函数能运行,其他函数判断错误

答案:2  悬赏:60  手机版
解决时间 2021-08-25 04:47
  • 提问者网友:聂風
  • 2021-08-24 12:18

#include <stdio.h>
#include <vector>
#include <iomanip.h>
using namespace std;

#define MaxStudent 100
#define MaxSize 1000
typedef struct StudentInfo
{
 char name[20];
 int Number;
 char Sex;
 int Age;
 int TeamNo;
 char Duty[50];
}Student;
 
typedef struct ClassInfo
{
 char name[20];
 char Tech_Name[30];
 int StudentCount;
}Class;

void loadClassInfo();
int add();
int del();
int change();
void find();
void exit();
void save();

void InitClassInfo(Class * clainfo)
{
 memset(clainfo->name,NULL,20);
 memset(clainfo->Tech_Name,NULL,30);
 clainfo->StudentCount = 0;
}

void InitStudentInfo(Student * Stdinfo)
{
 memset(Stdinfo->name,NULL,20);
 Stdinfo->Number = 0;
 Stdinfo->Sex = ' ';
 memset(Stdinfo->Duty,NULL,20);
 Stdinfo->Age = 0;
 Stdinfo->TeamNo = 0;
}

void PrintClassInfo(Class * clainfo)
{
 cout<<"This Class is:              "<<clainfo->name<<endl;
 cout<<"This Class Teacher is:      "<<clainfo->Tech_Name<<endl;
 cout<<"This Class has:             "<<clainfo->StudentCount<<" Student"<<endl<<endl<<endl;
}

void PrintStudentInfo(Student * student)
{
 cout<<"This student is:            "<<student->name<<endl;
 cout<<"This student number is:     "<<student->Number<<endl;
 cout<<"This student Sex:           "<<student->Sex<<endl;
 cout<<"This student has:           "<<student->Age<<" Old"<<endl; 
 cout<<"This student Duty is:       "<<student->Duty<<endl;
 cout<<"This student belong:        "<<student->TeamNo<<" Team"<<endl<<endl<<endl;
}

FILE *fp; 
Student student;
char v[MaxSize];
int j;
vector<Student> stu;
vector<Student>::iterator it;

 

void main()


 cout<<setw(50)<<"学生成绩管理系统"<<endl;
 cout<<setw(70)<<"-------08计算机学院<8008208820>"<<endl;
 cout<<endl;
 cout<<"=====>提示:文件已经打开,正在导入记录......"<<endl;
 cout<<endl;
 cout<<"=====>提示:记录导入完毕,共导入1条记录."<<endl;

int num;
 while(1)
 {
cout<<"<<<<<<<<<<<<<<<<<<<<<<请选择要进行的操作>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl<<endl;
cout<<setw(81)<<setfill('^')<<endl;
cout<<setw(35)<<setfill(' ')<<"1.添加学生资料"<<setw(30)<<"2.删除学生资料"<<endl;
cout<<setw(35)<<setfill(' ')<<"3.查询学生资料"<<setw(30)<<"4.修改学生资料"<<endl;
cout<<setw(35)<<setfill(' ')<<"5.显示学生资料"<<setw(26)<<"0.退出系统"<<endl;
cout<<"<<<<<<<<<<<<<<<<<<<<<<请选择要进行的操作>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl<<endl;
cin>>num;
  switch(num)
  {
     case 1:add();break;
  case 2:del();break;
  case 3:find();break;
  case 4:change();break;
  case 5:loadClassInfo();break;
  case 0:exit();
  }
 }

}

void loadClassInfo()
{
 
 int loadClass = 0;
 int loadStudent = 0;
 int fileintact = 0;//文件完整性判断,查看各配置信息关键字是否存在
    int temp=0;
    int conferror = 0;

 Student stdinfo;
 Class   classinfo;
 
 if((fp=fopen("c:\\ClassHR.Info","r"))==NULL)
 {
  cout<<"\nOpen ClassHR.Info file failed,faint!\n";
  
 }
 else
 {
  InitClassInfo(&classinfo);
  InitStudentInfo(&stdinfo);
  //fgets(v,sizeof(v),fp);
  while( !feof(fp) )
  {
   memset(v,NULL,1000);
   fgets( v, sizeof(v), fp );
   //cout <<"v:"<<v<<endl;
   if( strstr( v, "BeginClassInfo" ) ){
    fileintact++;
    InitClassInfo(&classinfo);
    loadClass = 1;
    //cout << "loadClass:" << loadClass << endl;
    continue;
   }
   if(  strstr( v, "ClassInfoEnd" ) ){
    fileintact++;
    PrintClassInfo(&classinfo);
    loadClass = 0;
    //cout << "loadClass:" << loadClass << endl;
    continue;
   }
   if(  strstr( v, "BeginStudentInfo" ) ){
    fileintact++;
    loadStudent = 1;
    InitStudentInfo(&stdinfo);
    //cout << "loadStudent:" << loadStudent << endl;
    continue;
   }
   if(  strstr( v, "StudentInfoEnd" ) ){
    fileintact++;
    PrintStudentInfo(&stdinfo);
    //learnStudentList(stdinfo);//添加学生信息到数据集合
    loadStudent = 0;
    //cout << "loadStudent:" << loadStudent << endl;
    continue;
   }
   
   if( loadClass == 1 ){
    //cout << "now load Class Info"<<endl;
    if(  strstr( v, "name" ) ){
     sscanf(v , "name: %s\n" , classinfo.name);
     //cout<<classinfo.name<<endl;
    }
    else if(strstr( v, "teacher" )){
     sscanf(v , "teacher:%s\n" , classinfo.Tech_Name);
    }
    else if(strstr( v, "AmountOfSTD" )){
     sscanf(v , "AmountOfSTD:%d\n" , &(classinfo.StudentCount));
    }
    else{
     //cout<<"Error Format"<<endl;
    }
    continue;
   }

   if( loadStudent == 1 ){
    //cout << "now load Student Info"<<endl;
    if(  strstr( v, "name" ) ){
     sscanf(v , "name:%s\n" , stdinfo.name);
     //cout<<classinfo.name<<endl;
    }
    else if(strstr( v, "Number" )){
     sscanf(v , "Number:%d\n" , &stdinfo.Number);
    }
    else if(strstr( v, "Duty" )){
     sscanf(v , "Duty:%s\n" , stdinfo.Duty);
    }
    else if(strstr( v, "Sex" )){
     sscanf(v , "Sex:%c\n" , &stdinfo.Sex);
    }
    else if(strstr( v, "Age" )){
     sscanf(v , "Age:%d\n" , &(stdinfo.Age));
    }
    else if(strstr( v, "TeamNo" )){
     sscanf(v , "TeamNo:%d\n" , &(stdinfo.TeamNo));
    }
    else{
     //cout<<"Error Format"<<endl;
    }
    
    continue;
   }
   
  }
 

 
 }
fclose( fp );

}
  

 

int add()                                           //添加记录
{   printf("请输入学号:");
 cin>>student.Number;
 for(it=stu.begin();it!=stu.end();it++)
 {
  if (student.Number==it->Number)
  {
   cout<<endl<<endl;

   printf("此学号已存在,退出!\n\n");
   return 0;
  }
 }
 printf("请输入姓名:");
 cin>>student.name;
    printf("请输入性别:(M||W)");
 cin>>student.Sex;
 printf("请输入年龄:");
 cin>>student.Age;
 printf("请输入组号:");
 cin>>student.TeamNo;
 printf("请输入职务:");
 cin>>student.Duty;
    stu.push_back(student);
 save();
 fclose(fp); //关闭文件
 printf("添加成功!\n");
 return 0;

}

int del()                                        //删除记录
{
 int num;
 bool flag=0;
 printf("请输入编号:");
 cin>>num;
 for(it=stu.begin();it!=stu.end();it++)
 {
  if(it->Number==num)
  {
  stu.erase(it);
  flag=1;
  break;
  }
 }
 if(flag==0)
  printf("无效编号!");
 else
  save();
 return 0;
}


int change()            //修改
{
 int num;
 bool flag=0;
 printf("请输入编号:");
 cin>>num;
 for(it=stu.begin();it!=stu.end();it++)
 {
  if(it->Number==num)
  {
   printf("请输入新的编号:");
   cin>>it->Number;
   printf("请输入新的姓名:");
   cin>>it->name;
   printf("请输入新的年龄:");
   cin>>it->Age;
   printf("请输入新的组号:");
   cin>>it->TeamNo;
   printf("请输入新的职位:");
   cin>>it->Duty;
   flag=1;
   break;
  }
 }
 if(flag==0)
  printf("此学生不存在!\n");
 else
  save();
 return 0;
}

void  find()           //查找
{
 bool flag=0;
 int num;
 printf("请输入要查找的学号:");
 cin>>num;
    for(it=stu.begin();it!=stu.end();it++)
  if(num==it->Number)
  {
   printf("Name: %s\n",it->name);
   printf("Number: %d\n",it->Number);
   printf("Age: %d\n",it->Age);
   printf("TeamNo: %d\n",it->TeamNo);
      printf("Duty: %s\n\n",it->Duty);
   flag=1;
  }
  if(flag==0)
  printf("此学生不存在!\n");
}

void exit()           //退出
{
 exit(1);
}

void save()           //保存记录
{
 if((fp=fopen("c:\\ClassHR.Info","a"))==NULL)
 {
  printf("添加失败!");
     return ;
 }
 for(it=stu.begin();it!=stu.end();it++)
 {
  fprintf(fp,"\nBeginStudentInfo:\n");
  fprintf(fp,"name:%s\n",it->name);
  fprintf(fp,"Number:%d\n",it->Number);
  fprintf(fp,"Sex:%c\n",it->Sex);
  fprintf(fp,"Age:%d\n",it->Age);
  fprintf(fp,"TeamNo:%d\n",it->TeamNo);
  fprintf(fp,"Duty:%s\n",it->Duty);
  fprintf(fp,"StudentInfoEnd\n");
 }
 
fclose(fp);

}

最佳答案
  • 五星知识达人网友:山河有幸埋战骨
  • 2021-08-24 12:24
给你看了下,主要错误原因是你载入数据之后没有保存到数据集(vector<Student> stu),我初步修改之后,发现可以用,没出现判断错误,具体代码我就不贴了,因为你用的C\C++混合编程,我对混编比较反感,不想改了。你自己改吧。
全部回答
  • 1楼网友:我住北渡口
  • 2021-08-24 13:19
trhdr
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯