| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 369 人关注过本帖
标题:c对txt的学生管理软件 哪错了?
只看楼主 加入收藏
woshihsz
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-2
结帖率:50%
收藏
 问题点数:0 回复次数:3 
c对txt的学生管理软件 哪错了?
#include <iostream.h>
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <fstream.h>
# include <conio.h>
//*****定义一个学生原子的的数据结构*****//
struct stuatom
{
char *name;
int id;
char sex;
char address;
char date;
char cons;
char ps;
void show();
void setup();
};

//*********定义一系列对学生的操作**********//
class student
{
private:
  stuatom ob[100];
  int stulen;
public:
  student();
  void input();
  void save();
  void check();
  void clean();
  void query();
  void read();
  void add();
  void del();
  void modify();
};

//********对学生数据的初始化(类的构造函数)**********//
student::student()
{
//用for循环对全部数组中的数据初始化
for(int i=0;i <100;i++)
{
  ob[i].id =0;
  ob[i].sex =' ';
  ob[i].name =NULL;
  ob[i].address=' ';
  ob[i].date=' ';
  ob[i].cons=' ';
  ob[i].ps=' ';
}
this->stulen =0;
}

//********输入学生的数据,并判断是否在规定数据域内*******//
void stuatom::setup()
{
char n[20]; char s;
int b;

//如果输入学号在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
do
{
  cout<<" 学号(20090000与20099999之间): ";
  cin>>b;
  if(b>20099999||b <20090000)
  cout<<" 数据有误!!重新输入.."<<endl<<endl;
}while (b <20099999||b>20090000);

id=b;

//如果输入姓名在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
do
{
  name=new char[strlen(n)+1];
  cout<<" 姓名: ";
  cin>>n;

}while ( strlen(n)>6 && strlen(n) <4 );         

strcpy(name,n);
cout<<" 性别(m/f):" ;
cin>>s;

//如果输入性别在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
while (s!='m' && s!='f')
{
  cout<<" 数据有误!!重新输入.."<<endl<<endl;
  cout<<" 性别(m/f):";
  cin>>s;
}

//*******按照规定格式把该学生的数据显示在屏幕上******//
void stuatom::show()
{
cout.setf(ios::left);
cout.width(6);
cout<<""<<id<<"     ";
cout.width(8);
cout<<name;
cout.width(10);
cout<<sex;
cout.width(9);
cout<<address;
cout.width(9);
cout<<date;
cout.width(9);
cout<<cons;
cout.width(11);
cout<<ps;
cout.width(10);
cout<<endl;
}

//**************输入学生的数据***********************//
void student::input()
{
int n;
cout <<endl <<" 输入将要录入的学生数目: ";
cin>>n;
int j,i;

//通过循环输入要求输入学生个数的学生的数据。
for(j=(this->stulen+1) ; j <=(this->stulen+n); j++)
{ cout <<" 输入学生信息 " <<j <<endl;
  ob[j-1].setup();}

//检查学号是否有重复
int a[100];
int b=0,c=0,d;
char Y;
if(this->stulen!=0)
{
  for (i=0;i<this->stulen ;i++)
  {
   for(j=this->stulen ;j<(this->stulen+n);j++)
   {
    if (ob[i].id==ob[j].id)
    {
     if (c==0)       //c只是为了美观而已。。
     {
      cout<<" 错误!系统检测到第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
      a[b]=j;
      b++;c++;
     }
     else
     {
      cout<<"                 第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
      a[b]=j;
      b++;c++;
     }
    }
   }
  }
  for(c=0;c<b;c++)
  {
   cout << " 是否重新输入第" << (a[c]+1)<< "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
   cin>>Y;
   while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
   {
    cout<<" 无效的命令,请重新输入..\n";
    cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
    cin>>Y;
   }
   if(Y=='y'||Y=='Y')
   {
    d=a[c];
    ob[d].setup();
   }
   else
   {
    n--;
    for(d=a[c];d<(this->stulen+n);d++)
     ob[d]=ob[d+1];
    for(d=(c+1);d<b;d++)
     a[d]--;
   }
  }
}
else
{
  for(i=0;i<n-1;i++)
  {
   for(j=i+1;j<n;j++)
   {
    if(ob[i].id==ob[j].id)
    {
     if (c==0)
     {
      cout<<" 错误!系统检测到第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
      a[b]=j;
      b++;c++;
     }
     else
     {
      cout<<"                 第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
      a[b]=j;
      b++;c++;
     }
    }
   }
  }
  for(c=0;c<b;c++)
  {
   cout << " 是否重新输入第" << (a[c]+1) << "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
   cin>>Y;
   while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
   {
    cout<<" 无效的命令,请重新输入..\n";
    cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
    cin>>Y;
   }
   if(Y=='y'||Y=='Y')
   {
    d=a[c];
    ob[d].setup();
   }
   else
   {
    n--;
    for(d=a[c];d<n;d++)
     ob[d]=ob[d+1];
    for(d=(c+1);d<b;d++)
     a[d]--;
   }
  }
}

//学生数据显示格式
system("cls");
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;

//通过循环输出所有学生数据。
for(j=this->stulen ; j <(this->stulen+n); j++)
{
  ob[j].show();
}
this->stulen+=n;     //学生个数赋值
}

void student::query()
{
 int x;
 cout <<endl <<" 输入要查询学生的学号: ";
 cin>>x;
 
 int i;
 for(i=0;i <this->stulen ;i++)
 {
  if (x==ob[i].id)
   break;
 }
 if(i<this->stulen )
 {
  cout <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
  cout.setf(ios::left);
  cout.width(6);
  cout <<"" <<ob[i].id <<"     ";
  cout.width(8);
  cout <<ob[i].name;
  cout.width(10);
  cout <<ob[i].sex;
  cout.width(9);
  cout <<ob[i].address;
  cout.width(9);
  cout <<ob[i].date;
  cout.width(11);
  cout <<ob[i].cons;
  cout.width(9);
  cout <<ob[i].ps;
  cout.width(10);
  cout<<endl;
 }
 else
  cout<<" 没有找到该学生成绩...";
 _getch();
}



//*******************保存学生数据**************************//
void student::save()
{
int i;
ofstream outfile;
outfile.open("list.txt",ios::trunc);
if(!outfile)
{
  cout <<" 无法打开数据文件!\n";
}

//通过循环把所有的学生数据保存在list.txt里边。
for(i=0; i <this->stulen; i++)
{
  outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
  ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
}
outfile.close();
ofstream outfile1;
outfile1.open("stulen.txt",ios::trunc );
outfile1<<this->stulen ;
outfile1.close();
}

void student::clean()
{
int i;
char b[20];
ifstream infile;
infile.open("list.txt",ios::in);
if(!infile)
{
  for(int i=(this->stulen-1);i <100;i++)
  {
  
   ob[i].id =0;
   ob[i].sex =' ';
   ob[i].name =NULL;
   ob[i].address=' ';
   ob[i].date=' ';
   ob[i].cons=' ';
   ob[i].ps=' ';
  }
  this->stulen=0;
  ifstream infile1;
  infile1.open("stulen.txt",ios::in);
  infile1>>this->stulen ;
  infile1.close();
}
else
{
  ifstream infile1;
  infile1.open("stulen.txt",ios::in);
  infile1>>this->stulen ;
  infile1.close();
  for(i=0;i<this->stulen ;i++)
  {
   infile>>ob[i].id;
   ob[i].name =new char[strlen(b)+1];
   infile>>b;
   strcpy(ob[i].name,b);
   infile>>ob[i].sex ;
   infile>>ob[i].math ;
   infile>>ob[i].address;
   infile>>ob[i].date;
   infile>>ob[i].cons;
   infile>>ob[i].ps ;
  }
  infile.close ();
}
}

//*************显示所有学生数据*********************//
void student::read()
{
int i;  
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
for(i=0; i <this->stulen; i++)
{
  ob[i].show();
}
cout <<"\n\n\n";

//*******************一个学生的数据****************//
void student::add()
{
int i, d=this->stulen ;
int c=0;
cout <<" 输入要添加学生的信息:" <<endl;
do
{
  cout<<"\n";
  ob[d].setup();
  for(i=0;i<d;i++)
  {
   if(ob[i].id==ob[d].id )
   {
    cout<<" 系统检测到此次输入的学号和已有学生学号相同,请核实再输..";
    _getch();
    ob[d].address=' ';
    ob[d].date=' ';
    ob[d].cons=' ';
    ob[d].ps=' ';
    ob[d].id =0;
    ob[d].sex =' ';
    ob[d].name =NULL;
    c=1;
    break;
   }
   else c=0;
  }
}while(c!=0);

cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<"       学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;

for(i=0; i <=d; i++)
{
  ob[i].show();
}
this->stulen++;

ofstream fout("list.txt",ios::app);

if(!fout)
{
  cout <<" 无法打开数据文件!\n,";
}

//把添加的学生数据添加到list.txt里边去。
fout <<ob[d].id <<" " <<ob[d].name <<" " <<ob[d].sex <<" " <<
ob[d].address <<" " <<ob[d].date <<" " <<ob[d].cons <<" " <<ob[d].ps <<endl;

fout.close();
ofstream outfile1;
outfile1.open("stulen.txt",ios::trunc );
outfile1<<this->stulen ;
outfile1.close();
_getch();
}

//*********************删除指定名字学生的数据*******************//
void student::del()
{
int i,p;
int x;
p=-1;
if(this->stulen==0)
{
  cout<<" 目前没有学生数据!";
  _getch();
}
else
{
  cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
  cout <<endl <<"        学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;

  for(i=0; i <this->stulen; i++)
  {
   ob[i].show();
  }
  char a;
  cout <<"\n\n 输入要删除的学生学号:" ;
  cin>>x;

  //通过for循环查找要删除学生的学号
  for(i=0; i <stulen; i++)
  {
   if(x==ob[i].id )
   {
    p=i;  
    cout <<endl <<"        学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
    cout <<"      " <<ob[i].id <<"       " <<ob[i].name <<"      " <<ob[i].sex <<"      " <<ob[i].address <<"      " <<ob[i].date <<"      " <<ob[i].cons <<"       " <<ob[i].ps<<endl;
    break;
   }
   else
    continue;
  }
  if(p==-1)
  {
   cout<<" 没有此学生数据!";
   _getch();
  }
  else
  {
   cout<<" 确定删除此学生数据?<Y/N>";
   cin>> a;
   if (a=='y'|| a=='Y')
   {
    cout <<" 删除成功!" <<endl;
  
    //删除了之后,应该把后面的数据往前移,把要删除的数据覆盖,并且学生长度减1
    stulen--;
    for(i;i <stulen;i++)
    {
     ob[i]=ob[i+1];
    }
    ofstream outfile;
    outfile.open("list.txt",ios::trunc);
    for(i=0; i <this->stulen; i++)
    {
     outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
     ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
    }
    outfile.close();
    ofstream outfile1;
    outfile1.open("stulen.txt",ios::trunc );
    outfile1<<this->stulen ;
    outfile1.close();
    this->read ();

  
    //_getch();
   }
   else
   {
    cout<<" 回到主界面...";
    _getch();
   }
  }
}
}

//********修改学生信息********//
void student::modify()
{
int a,i,p,b;
p=-1;
system("cls");
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;

for(i=0; i <this->stulen; i++)
{
  ob[i].show();
}
    cout <<"\n\n 请输入要修改学生的学号:";
cin>>a;
for(i=0; i <stulen; i++)
{
  if(a==ob[i].id)
  {
   p=i;  
   cout <<endl <<"       学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
   cout <<"      " <<ob[i].id <<"     " <<ob[i].name <<"      " <<ob[i].sex <<"      " <<ob[i].address <<"      " <<ob[i].date <<"      " <<ob[i].cons <<"       " <<ob[i].ps <<endl;
   break;
  }
  else
   continue;
}
if(p==-1)
{
  cout<<" 没有此学生数据!";
  _getch();
}
else
{
  cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
  cin>>b;
  while(b!=1 &&b!=2 &&b!=3 &&b!=4)
  {
    cout<<" 无效输入,请重新输入!\n";
    cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
    cin>>b;
  }
  switch(b)
  {
   case 1:
    {
     float m;
     cout<<" 工作部门(40以内): ";
     cin>>m;

     //如果输入成绩在数据域内,跳出循环并且赋值。
     //如果不在数据域内,一直循环到输入数据符合数据域为止
     while (m <0 || m>40)
     {
      cout<<" 数据有误!!重新输入.."<<endl<<endl;
      cout<<" 工作部门(40以内): ";
      cin>>m;
     }

     ob[p].address=m;break;
    }
   case 2:
    {
     float e;
     cout<<" 工作日期(40以内): ";
     cin>>e;

     while (e <0 || e>40)
     {
      cout<<" 数据有误!!重新输入.."<<endl<<endl;
      cout<<" 工作日期(40以内): ";
      cin>>e;
     }

     ob[p].date=e; break;
    }
   case 3:
    {
     float co;
     cout<<" 工作内容(40以内): ";
     cin>>co;

     while (co <0 || co>40)
     {
      cout<<" 数据有误!!重新输入.."<<endl<<endl;
      cout<<"  工作内容(40以内): ";
      cin>>co;
     }

     ob[p].cons=co;
    }
    case 4:
    {
     float ss;
     cout<<" 备注(40以内): ";
     cin>>ss;

     while (ss <0 || ss>40)
     {
      cout<<" 数据有误!!重新输入.."<<endl<<endl;
      cout<<"  备注(40以内): ";
      cin>>ss;
     }

     ob[p].ps=ss;
    }
  }

  ofstream outfile;
  outfile.open("list.txt",ios::trunc);
  for(i=0; i <this->stulen; i++)
  {
   outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
   ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
  }
  outfile.close();
  cout<<" 成绩修改成功!回车返回主界面...";
  _getch();
}
}

//***************程序运行前先检查以前是否有数据存在*****************//
void student::check()
{
int i;
char b[20];
ifstream infile;
infile.open("list.txt",ios::in );
if(!infile)
{
  cout<<" 系统没有检测到以前输入的数据,此次操作将建立一个新的数据文件...";
  _getch();
}
else
{
  ifstream infile1;
  infile1.open("stulen.txt",ios::in);
  infile1>>this->stulen ;
  infile1.close();
  for(i=0;i<this->stulen ;i++)
  {
   infile>>ob[i].id;
   ob[i].name =new char[strlen(b)+1];
   infile>>b;
   strcpy(ob[i].name,b);
   infile>>ob[i].sex ;
   infile>>ob[i].address ;
   infile>>ob[i].date;
   infile>>ob[i].cons;
   infile>>ob[i].ps;
  }
  infile.close ();

  system("cls");
  cout<<" 系统检测到以前输入过数据,数据内容如下:\n\n";
  cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
  cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;

//通过循环输出所有学生数据。
  for(i=0; i<this->stulen; i++)
  {
   ob[i].show();
  }
  cout<<"\n\n\n";
  cout<<" 原始数据是否覆盖?(Y/N)";
  char q;
  cin>>q;
  while(q!='y'&& q!='Y'&& q!='N'&& q!='n')
  {
   cout<<" 无效的命令,请重新输入..\n";
   cout<<" 原始数据是否覆盖?(Y/N)";
   cin>>q;
  }
  if(q=='y'||q=='Y')
  {
   for(int i=0;i <100;i++)
   {
    ob[i].address=' ';
    ob[i].date=' ';
    ob[i].cons=' ';
    ob[i].ps=' ';
    ob[i].id =0;
    ob[i].sex =' ';
    ob[i].name =NULL;
   }
   this->stulen =0;
   ofstream outfile1;
   outfile1.open("stulen.txt",ios::trunc);
   outfile1<<this->stulen ;
   outfile1.close();
   system("del list.txt");
  }
/* else*/
}
}

//**************按照一定格式显示所要查询学生的信息。**************//
void menu()
{
cout <<"\n\n";
cout <<"------------------ 学生成绩系统 -----------------" <<endl <<endl;
cout <<"\t1.录入与保存学生信息:\n";
cout <<"\t2.读取学生信息:\n";
cout <<"\t3.删除学生信息:\n";
cout <<"\t4.追加学生信息:\n";
cout <<"\t5.查询学生信息:\n";
cout <<"\t6.修改学生信息:\n";
cout <<"\t7.退出系统......\n\n\n";
cout <<"\t请选择要实现的功能: ";
}


void main()
{
student a;
a.check();
while(1)
{
  int SEL;
  system("cls");
  menu();
  cin>>SEL;
  switch(SEL)
  {
   case 1:
    system("cls");
    a.input();
    cout <<endl;
    cout <<" 是否保存? (Y/N): ";
    char Y;
    cin>>Y;
    while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
    {
     cout<<" 无效的命令,请重新输入..\n";
     cout<<" 是否保存? (Y/N)";
     cin>>Y;
    }
    system("cls");
    if(Y=='y'||Y=='Y')
     a.save();
    else/* if(Y=='n'||Y=='N')*/
     a.clean();break;
   case 2:
    system("cls"); a.read(); break;
   case 3:
    system("cls"); a.del(); break;
   case 4:
    system("cls"); a.add();break;
   case 5:
    system("cls"); a.query();break;
   case 6:
    system("cls"); a.modify();break;
   case 7:
    exit(0);
   default:
    cout <<"        无效的命令!";
    _getch();
    break;
  }
}
}

[ 本帖最后由 woshihsz 于 2011-9-2 17:37 编辑 ]
搜索更多相关主题的帖子: 管理软件 
2011-09-02 17:27
laznrbfe
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:482
专家分:1599
注 册:2011-5-22
收藏
得分:0 
#include <iostream.h>
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <fstream.h>
# include <conio.h>
//*****定义一个学生原子的的数据结构*****//
struct stuatom
{
    char *name;
    int id;
    int math;/////////////////////////////////没有定义math,我帮你定义了,但不知道是不是你想要的
    char sex;
    char address;
    char date;
    char cons;
    char ps;
    void show();
    void setup();
};

//*********定义一系列对学生的操作**********//
class student
{
private:
    stuatom ob[100];
    int stulen;
public:
    student();
    void input();
    void save();
    void check();
    void clean();
    void query();
    void read();
    void add();
    void del();
    void modify();
};

//********对学生数据的初始化(类的构造函数)**********//
student::student()
{
    //用for循环对全部数组中的数据初始化
    for(int i=0;i <100;i++)
    {
        ob[i].id =0;
        ob[i].sex =' ';
        ob[i].name =NULL;
        ob[i].address=' ';
        ob[i].date=' ';
        ob[i].cons=' ';
        ob[i].ps=' ';
    }
    this->stulen =0;
}

//********输入学生的数据,并判断是否在规定数据域内*******//
void stuatom::setup()
{
    char n[20]; char s;
    int b;
   
    //如果输入学号在数据域内,跳出循环并且赋值。
    //如果不在数据域内,一直循环到输入数据符合数据域为止
    do
    {
        cout<<" 学号(20090000与20099999之间): ";
        cin>>b;
        if(b>20099999||b <20090000)
            cout<<" 数据有误!!重新输入.."<<endl<<endl;
    }while (b <20099999||b>20090000);
   
    id=b;
   
    //如果输入姓名在数据域内,跳出循环并且赋值。
    //如果不在数据域内,一直循环到输入数据符合数据域为止
    do
    {
        name=new char[strlen(n)+1];
        cout<<" 姓名: ";
        cin>>n;
        
    }while ( strlen(n)>6 && strlen(n) <4 );         
   
    strcpy(name,n);
    cout<<" 性别(m/f):" ;
    cin>>s;
   
    //如果输入性别在数据域内,跳出循环并且赋值。
    //如果不在数据域内,一直循环到输入数据符合数据域为止
    while (s!='m' && s!='f')
    {
        cout<<" 数据有误!!重新输入.."<<endl<<endl;
        cout<<" 性别(m/f):";
        cin>>s;
    }
}//////////////////////////////////////////////////////////////////////////////////////缺少},我帮你加上了
//*******按照规定格式把该学生的数据显示在屏幕上******//
void stuatom::show()
{
    cout.setf(ios::left);
    cout.width(6);
    cout<<""<<id<<"     ";
    cout.width(8);
    cout<<name;
    cout.width(10);
    cout<<sex;
    cout.width(9);
    cout<<address;
    cout.width(9);
    cout<<date;
    cout.width(9);
    cout<<cons;
    cout.width(11);
    cout<<ps;
    cout.width(10);
    cout<<endl;
}

//**************输入学生的数据***********************//
void student::input()
{
    int n;
    cout <<endl <<" 输入将要录入的学生数目: ";
    cin>>n;
    int j,i;
   
    //通过循环输入要求输入学生个数的学生的数据。
    for(j=(this->stulen+1) ; j <=(this->stulen+n); j++)
    { cout <<" 输入学生信息 " <<j <<endl;
    ob[j-1].setup();}
   
    //检查学号是否有重复
    int a[100];
    int b=0,c=0,d;
    char Y;
    if(this->stulen!=0)
    {
        for (i=0;i<this->stulen ;i++)
        {
            for(j=this->stulen ;j<(this->stulen+n);j++)
            {
                if (ob[i].id==ob[j].id)
                {
                    if (c==0)       //c只是为了美观而已。。
                    {
                        cout<<" 错误!系统检测到第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
                        a[b]=j;
                        b++;c++;
                    }
                    else
                    {
                        cout<<"                 第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
                        a[b]=j;
                        b++;c++;
                    }
                }
            }
        }
        for(c=0;c<b;c++)
        {
            cout << " 是否重新输入第" << (a[c]+1)<< "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
            cin>>Y;
            while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
            {
                cout<<" 无效的命令,请重新输入..\n";
                cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
                cin>>Y;
            }
            if(Y=='y'||Y=='Y')
            {
                d=a[c];
                ob[d].setup();
            }
            else
            {
                n--;
                for(d=a[c];d<(this->stulen+n);d++)
                    ob[d]=ob[d+1];
                for(d=(c+1);d<b;d++)
                    a[d]--;
            }
        }
    }
    else
    {
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(ob[i].id==ob[j].id)
                {
                    if (c==0)
                    {
                        cout<<" 错误!系统检测到第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
                        a[b]=j;
                        b++;c++;
                    }
                    else
                    {
                        cout<<"                 第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
                        a[b]=j;
                        b++;c++;
                    }
                }
            }
        }
        for(c=0;c<b;c++)
        {
            cout << " 是否重新输入第" << (a[c]+1) << "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
            cin>>Y;
            while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
            {
                cout<<" 无效的命令,请重新输入..\n";
                cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
                cin>>Y;
            }
            if(Y=='y'||Y=='Y')
            {
                d=a[c];
                ob[d].setup();
            }
            else
            {
                n--;
                for(d=a[c];d<n;d++)
                    ob[d]=ob[d+1];
                for(d=(c+1);d<b;d++)
                    a[d]--;
            }
        }
    }
   
    //学生数据显示格式
    system("cls");
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
    cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
   
    //通过循环输出所有学生数据。
    for(j=this->stulen ; j <(this->stulen+n); j++)
    {
        ob[j].show();
    }
    this->stulen+=n;     //学生个数赋值
}

void student::query()
{
    int x;
    cout <<endl <<" 输入要查询学生的学号: ";
    cin>>x;
   
    int i;
    for(i=0;i <this->stulen ;i++)
    {
        if (x==ob[i].id)
            break;
    }
    if(i<this->stulen )
    {
        cout <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
        cout.setf(ios::left);
        cout.width(6);
        cout <<"" <<ob[i].id <<"     ";
        cout.width(8);
        cout <<ob[i].name;
        cout.width(10);
        cout <<ob[i].sex;
        cout.width(9);
        cout <<ob[i].address;
        cout.width(9);
        cout <<ob[i].date;
        cout.width(11);
        cout <<ob[i].cons;
        cout.width(9);
        cout <<ob[i].ps;
        cout.width(10);
        cout<<endl;
    }
    else
        cout<<" 没有找到该学生成绩...";
    _getch();
}



//*******************保存学生数据**************************//
void student::save()
{
    int i;
    ofstream outfile;
    outfile.open("list.txt",ios::trunc);
    if(!outfile)
    {
        cout <<" 无法打开数据文件!\n";
    }
   
    //通过循环把所有的学生数据保存在list.txt里边。
    for(i=0; i <this->stulen; i++)
    {
        outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
            ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
    }
    outfile.close();
    ofstream outfile1;
    outfile1.open("stulen.txt",ios::trunc );
    outfile1<<this->stulen ;
    outfile1.close();
}

void student::clean()
{
    int i;
    char b[20];
    ifstream infile;
    infile.open("list.txt",ios::in);
    if(!infile)
    {
        for(int i=(this->stulen-1);i <100;i++)
        {
            
            ob[i].id =0;
            ob[i].sex =' ';
            ob[i].name =NULL;
            ob[i].address=' ';
            ob[i].date=' ';
            ob[i].cons=' ';
            ob[i].ps=' ';
        }
        this->stulen=0;
        ifstream infile1;
        infile1.open("stulen.txt",ios::in);
        infile1>>this->stulen ;
        infile1.close();
    }
    else
    {
        ifstream infile1;
        infile1.open("stulen.txt",ios::in);
        infile1>>this->stulen ;
        infile1.close();
        for(i=0;i<this->stulen ;i++)
        {
            infile>>ob[i].id;
            ob[i].name =new char[strlen(b)+1];
            infile>>b;
            strcpy(ob[i].name,b);
            infile>>ob[i].sex ;
            infile>>ob[i].math ;
            infile>>ob[i].address;
            infile>>ob[i].date;
            infile>>ob[i].cons;
            infile>>ob[i].ps ;
        }
        infile.close ();
    }
}

//*************显示所有学生数据*********************//
void student::read()
{
    int i;  
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
    cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
    for(i=0; i <this->stulen; i++)
    {
        ob[i].show();
    }
    cout <<"\n\n\n";
}
//*******************一个学生的数据****************//
void student::add()
{
    int i, d=this->stulen ;
    int c=0;
    cout <<" 输入要添加学生的信息:" <<endl;
    do
    {
        cout<<"\n";
        ob[d].setup();
        for(i=0;i<d;i++)
        {
            if(ob[i].id==ob[d].id )
            {
                cout<<" 系统检测到此次输入的学号和已有学生学号相同,请核实再输..";
                _getch();
                ob[d].address=' ';
                ob[d].date=' ';
                ob[d].cons=' ';
                ob[d].ps=' ';
                ob[d].id =0;
                ob[d].sex =' ';
                ob[d].name =NULL;
                c=1;
                break;
            }
            else c=0;
        }
    }while(c!=0);
   
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
    cout <<endl <<"       学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
   
    for(i=0; i <=d; i++)
    {
        ob[i].show();
    }
    this->stulen++;
   
    ofstream fout("list.txt",ios::app);
   
    if(!fout)
    {
        cout <<" 无法打开数据文件!\n,";
    }
   
    //把添加的学生数据添加到list.txt里边去。
    fout <<ob[d].id <<" " <<ob[d].name <<" " <<ob[d].sex <<" " <<
        ob[d].address <<" " <<ob[d].date <<" " <<ob[d].cons <<" " <<ob[d].ps <<endl;
   
    fout.close();
    ofstream outfile1;
    outfile1.open("stulen.txt",ios::trunc );
    outfile1<<this->stulen ;
    outfile1.close();
    _getch();
}

//*********************删除指定名字学生的数据*******************//
void student::del()
{
    int i,p;
    int x;
    p=-1;
    if(this->stulen==0)
    {
        cout<<" 目前没有学生数据!";
        _getch();
    }
    else
    {
        cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
        cout <<endl <<"        学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
        
        for(i=0; i <this->stulen; i++)
        {
            ob[i].show();
        }
        char a;
        cout <<"\n\n 输入要删除的学生学号:" ;
        cin>>x;
        
        //通过for循环查找要删除学生的学号
        for(i=0; i <stulen; i++)
        {
            if(x==ob[i].id )
            {
                p=i;  
                cout <<endl <<"        学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
                cout <<"      " <<ob[i].id <<"       " <<ob[i].name <<"      " <<ob[i].sex <<"      " <<ob[i].address <<"      " <<ob[i].date <<"      " <<ob[i].cons <<"       " <<ob[i].ps<<endl;
                break;
            }
            else
                continue;
        }
        if(p==-1)
        {
            cout<<" 没有此学生数据!";
            _getch();
        }
        else
        {
            cout<<" 确定删除此学生数据?<Y/N>";
            cin>> a;
            if (a=='y'|| a=='Y')
            {
                cout <<" 删除成功!" <<endl;
               
                //删除了之后,应该把后面的数据往前移,把要删除的数据覆盖,并且学生长度减1
                stulen--;
                for(i;i <stulen;i++)
                {
                    ob[i]=ob[i+1];
                }
                ofstream outfile;
                outfile.open("list.txt",ios::trunc);
                for(i=0; i <this->stulen; i++)
                {
                    outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
                        ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
                }
                outfile.close();
                ofstream outfile1;
                outfile1.open("stulen.txt",ios::trunc );
                outfile1<<this->stulen ;
                outfile1.close();
                this->read ();
               
               
                //_getch();
            }
            else
            {
                cout<<" 回到主界面...";
                _getch();
            }
        }
    }
}

//********修改学生信息********//
void student::modify()
{
    int a,i,p,b;
    p=-1;
    system("cls");
    cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
    cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
   
    for(i=0; i <this->stulen; i++)
    {
        ob[i].show();
    }
    cout <<"\n\n 请输入要修改学生的学号:";
    cin>>a;
    for(i=0; i <stulen; i++)
    {
        if(a==ob[i].id)
        {
            p=i;  
            cout <<endl <<"       学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
            cout <<"      " <<ob[i].id <<"     " <<ob[i].name <<"      " <<ob[i].sex <<"      " <<ob[i].address <<"      " <<ob[i].date <<"      " <<ob[i].cons <<"       " <<ob[i].ps <<endl;
            break;
        }
        else
            continue;
    }
    if(p==-1)
    {
        cout<<" 没有此学生数据!";
        _getch();
    }
    else
    {
        cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
        cin>>b;
        while(b!=1 &&b!=2 &&b!=3 &&b!=4)
        {
            cout<<" 无效输入,请重新输入!\n";
            cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
            cin>>b;
        }
        switch(b)
        {
        case 1:
            {
                float m;
                cout<<" 工作部门(40以内): ";
                cin>>m;
               
                //如果输入成绩在数据域内,跳出循环并且赋值。
                //如果不在数据域内,一直循环到输入数据符合数据域为止
                while (m <0 || m>40)
                {
                    cout<<" 数据有误!!重新输入.."<<endl<<endl;
                    cout<<" 工作部门(40以内): ";
                    cin>>m;
                }
               
                ob[p].address=m;break;
            }
        case 2:
            {
                float e;
                cout<<" 工作日期(40以内): ";
                cin>>e;
               
                while (e <0 || e>40)
                {
                    cout<<" 数据有误!!重新输入.."<<endl<<endl;
                    cout<<" 工作日期(40以内): ";
                    cin>>e;
                }
               
                ob[p].date=e; break;
            }
        case 3:
            {
                float co;
                cout<<" 工作内容(40以内): ";
                cin>>co;
               
                while (co <0 || co>40)
                {
                    cout<<" 数据有误!!重新输入.."<<endl<<endl;
                    cout<<"  工作内容(40以内): ";
                    cin>>co;
                }
               
                ob[p].cons=co;
            }
        case 4:
            {
                float ss;
                cout<<" 备注(40以内): ";
                cin>>ss;
               
                while (ss <0 || ss>40)
                {
                    cout<<" 数据有误!!重新输入.."<<endl<<endl;
                    cout<<"  备注(40以内): ";
                    cin>>ss;
                }
               
                ob[p].ps=ss;
            }
        }
        
        ofstream outfile;
        outfile.open("list.txt",ios::trunc);
        for(i=0; i <this->stulen; i++)
        {
            outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
                ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
        }
        outfile.close();
        cout<<" 成绩修改成功!回车返回主界面...";
        _getch();
    }
}

//***************程序运行前先检查以前是否有数据存在*****************//
void student::check()
{
    int i;
    char b[20];
    ifstream infile;
    infile.open("list.txt",ios::in );
    if(!infile)
    {
        cout<<" 系统没有检测到以前输入的数据,此次操作将建立一个新的数据文件...";
        _getch();
    }
    else
    {
        ifstream infile1;
        infile1.open("stulen.txt",ios::in);
        infile1>>this->stulen ;
        infile1.close();
        for(i=0;i<this->stulen ;i++)
        {
            infile>>ob[i].id;
            ob[i].name =new char[strlen(b)+1];
            infile>>b;
            strcpy(ob[i].name,b);
            infile>>ob[i].sex ;
            infile>>ob[i].address ;
            infile>>ob[i].date;
            infile>>ob[i].cons;
            infile>>ob[i].ps;
        }
        infile.close ();
        
        system("cls");
        cout<<" 系统检测到以前输入过数据,数据内容如下:\n\n";
        cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
        cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
        
        //通过循环输出所有学生数据。
        for(i=0; i<this->stulen; i++)
        {
            ob[i].show();
        }
        cout<<"\n\n\n";
        cout<<" 原始数据是否覆盖?(Y/N)";
        char q;
        cin>>q;
        while(q!='y'&& q!='Y'&& q!='N'&& q!='n')
        {
            cout<<" 无效的命令,请重新输入..\n";
            cout<<" 原始数据是否覆盖?(Y/N)";
            cin>>q;
        }
        if(q=='y'||q=='Y')
        {
            for(int i=0;i <100;i++)
            {
                ob[i].address=' ';
                ob[i].date=' ';
                ob[i].cons=' ';
                ob[i].ps=' ';
                ob[i].id =0;
                ob[i].sex =' ';
                ob[i].name =NULL;
            }
            this->stulen =0;
            ofstream outfile1;
            outfile1.open("stulen.txt",ios::trunc);
            outfile1<<this->stulen ;
            outfile1.close();
            system("del list.txt");
        }
        /* else*/
    }
}

//**************按照一定格式显示所要查询学生的信息。**************//
void menu()
{
    cout <<"\n\n";
    cout <<"------------------ 学生成绩系统 -----------------" <<endl <<endl;
    cout <<"\t1.录入与保存学生信息:\n";
    cout <<"\t2.读取学生信息:\n";
    cout <<"\t3.删除学生信息:\n";
    cout <<"\t4.追加学生信息:\n";
    cout <<"\t5.查询学生信息:\n";
    cout <<"\t6.修改学生信息:\n";
    cout <<"\t7.退出系统......\n\n\n";
    cout <<"\t请选择要实现的功能: ";
}


void main()
{
    student a;
    a.check();
    while(1)
    {
        int SEL;
        system("cls");
        menu();
        cin>>SEL;
        switch(SEL)
        {
        case 1:
            system("cls");
            a.input();
            cout <<endl;
            cout <<" 是否保存? (Y/N): ";
            char Y;
            cin>>Y;
            while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
            {
                cout<<" 无效的命令,请重新输入..\n";
                cout<<" 是否保存? (Y/N)";
                cin>>Y;
            }
            system("cls");
            if(Y=='y'||Y=='Y')
                a.save();
            else/* if(Y=='n'||Y=='N')*/
                a.clean();break;
        case 2:
            system("cls"); a.read(); break;
        case 3:
            system("cls"); a.del(); break;
        case 4:
            system("cls"); a.add();break;
        case 5:
            system("cls"); a.query();break;
        case 6:
            system("cls"); a.modify();break;
        case 7:
            exit(0);
        default:
            cout <<"        无效的命令!";
            _getch();
            break;
        }
    }
}
2011-09-03 22:45
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:0 
直接给出编译错误信息!
2011-09-04 12:05
yiwenfei
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-9-4
收藏
得分:0 
有三个地方错误,具体忘了。你看看我改过后的程序(可以运行,显示正确)。
#include <iostream.h>
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <fstream.h>
# include <conio.h>
//*****定义一个学生原子的的数据结构*****//
struct stuatom
{
char *name;
int id;
char sex;
char address;
char date;
char cons;
char ps;
void show();
void setup();
};

//*********定义一系列对学生的操作**********//
class student
{
private:
  stuatom ob[100];
  int stulen;
public:
  student();
  void input();
  void save();
  void check();
  void clean();
  void query();
  void read();
  void add();
  void del();
  void modify();
};

//********对学生数据的初始化(类的构造函数)**********//
student::student()
{
//用for循环对全部数组中的数据初始化
for(int i=0;i <100;i++)
{
  ob[i].id =0;
  ob[i].sex =' ';
  ob[i].name =NULL;
  ob[i].address=' ';
  ob[i].date=' ';
  ob[i].cons=' ';
  ob[i].ps=' ';
}
this->stulen =0;
}

//********输入学生的数据,并判断是否在规定数据域内*******//
void stuatom::setup()
{
char n[20]; char s;
int b;

//如果输入学号在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
do
{
  cout<<" 学号(20090000与20099999之间): ";
  cin>>b;
  if(b>20099999||b <20090000)
  cout<<" 数据有误!!重新输入.."<<endl<<endl;
}while (b <20099999||b>20090000);

id=b;

//如果输入姓名在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
do
{
  name=new char[strlen(n)+1];
  cout<<" 姓名: ";
  cin>>n;

}while ( strlen(n)>6 && strlen(n) <4 );         

strcpy(name,n);
cout<<" 性别(m/f):" ;
cin>>s;

//如果输入性别在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
while (s!='m' && s!='f')
{
  cout<<" 数据有误!!重新输入.."<<endl<<endl;
  cout<<" 性别(m/f):";
  cin>>s;
}
}
//*******按照规定格式把该学生的数据显示在屏幕上******//
void stuatom::show()
{
cout.setf(ios::left);
cout.width(6);
cout<<""<<id<<"     ";
cout.width(8);
cout<<name;
cout.width(10);
cout<<sex;
cout.width(9);
cout<<address;
cout.width(9);
cout<<date;
cout.width(9);
cout<<cons;
cout.width(11);
cout<<ps;
cout.width(10);
cout<<endl;
}

//**************输入学生的数据***********************//
void student::input()
{
int n;
cout <<endl <<" 输入将要录入的学生数目: ";
cin>>n;
int j,i;

//通过循环输入要求输入学生个数的学生的数据。
for(j=(this->stulen+1) ; j <=(this->stulen+n); j++)
{ cout <<" 输入学生信息 " <<j <<endl;
  ob[j-1].setup();}

//检查学号是否有重复
int a[100];
int b=0,c=0,d;
char Y;
if(this->stulen!=0)
{
  for (i=0;i<this->stulen ;i++)
  {
   for(j=this->stulen ;j<(this->stulen+n);j++)
   {
    if (ob[i].id==ob[j].id)
    {
     if (c==0)       //c只是为了美观而已。。
     {
      cout<<" 错误!系统检测到第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
      a[b]=j;
      b++;c++;
     }
     else
     {
      cout<<"                 第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
      a[b]=j;
      b++;c++;
     }
    }
   }
  }
  for(c=0;c<b;c++)
  {
   cout << " 是否重新输入第" << (a[c]+1)<< "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
   cin>>Y;
   while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
   {
    cout<<" 无效的命令,请重新输入..\n";
    cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
    cin>>Y;
   }
   if(Y=='y'||Y=='Y')
   {
    d=a[c];
    ob[d].setup();
   }
   else
   {
    n--;
    for(d=a[c];d<(this->stulen+n);d++)
     ob[d]=ob[d+1];
    for(d=(c+1);d<b;d++)
     a[d]--;
   }
  }
}
else
{
  for(i=0;i<n-1;i++)
  {
   for(j=i+1;j<n;j++)
   {
    if(ob[i].id==ob[j].id)
    {
     if (c==0)
     {
      cout<<" 错误!系统检测到第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
      a[b]=j;
      b++;c++;
     }
     else
     {
      cout<<"                 第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
      a[b]=j;
      b++;c++;
     }
    }
   }
  }
  for(c=0;c<b;c++)
  {
   cout << " 是否重新输入第" << (a[c]+1) << "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
   cin>>Y;
   while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
   {
    cout<<" 无效的命令,请重新输入..\n";
    cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
    cin>>Y;
   }
   if(Y=='y'||Y=='Y')
   {
    d=a[c];
    ob[d].setup();
   }
   else
   {
    n--;
    for(d=a[c];d<n;d++)
     ob[d]=ob[d+1];
    for(d=(c+1);d<b;d++)
     a[d]--;
   }
  }
}

//学生数据显示格式
system("cls");
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;

//通过循环输出所有学生数据。
for(j=this->stulen ; j <(this->stulen+n); j++)
{
  ob[j].show();
}
this->stulen+=n;     //学生个数赋值
}

void student::query()
{
int x;
cout <<endl <<" 输入要查询学生的学号: ";
cin>>x;

int i;
for(i=0;i <this->stulen ;i++)
{
  if (x==ob[i].id)
   break;
}
if(i<this->stulen )
{
  cout <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
  cout.setf(ios::left);
  cout.width(6);
  cout <<"" <<ob[i].id <<"     ";
  cout.width(8);
  cout <<ob[i].name;
  cout.width(10);
  cout <<ob[i].sex;
  cout.width(9);
  cout <<ob[i].address;
  cout.width(9);
  cout <<ob[i].date;
  cout.width(11);
  cout <<ob[i].cons;
  cout.width(9);
  cout <<ob[i].ps;
  cout.width(10);
  cout<<endl;
}
else
  cout<<" 没有找到该学生成绩...";
_getch();
}



//*******************保存学生数据**************************//
void student::save()
{
int i;
ofstream outfile;
outfile.open("list.txt",ios::trunc);
if(!outfile)
{
  cout <<" 无法打开数据文件!\n";
}

//通过循环把所有的学生数据保存在list.txt里边。
for(i=0; i <this->stulen; i++)
{
  outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
  ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
}
outfile.close();
ofstream outfile1;
outfile1.open("stulen.txt",ios::trunc );
outfile1<<this->stulen ;
outfile1.close();
}

void student::clean()
{
int i;
char b[20];
ifstream infile;
infile.open("list.txt",ios::in);
if(!infile)
{
  for(int i=(this->stulen-1);i <100;i++)
  {
  
   ob[i].id =0;
   ob[i].sex =' ';
   ob[i].name =NULL;
   ob[i].address=' ';
   ob[i].date=' ';
   ob[i].cons=' ';
   ob[i].ps=' ';
  }
  this->stulen=0;
  ifstream infile1;
  infile1.open("stulen.txt",ios::in);
  infile1>>this->stulen ;
  infile1.close();
}
else
{
  ifstream infile1;
  infile1.open("stulen.txt",ios::in);
  infile1>>this->stulen ;
  infile1.close();
  for(i=0;i<this->stulen ;i++)
  {
   infile>>ob[i].id;
   ob[i].name =new char[strlen(b)+1];
   infile>>b;
   strcpy(ob[i].name,b);
   infile>>ob[i].sex ;

   infile>>ob[i].address;
   infile>>ob[i].date;
   infile>>ob[i].cons;
   infile>>ob[i].ps ;
  }
  infile.close ();
}
}

//*************显示所有学生数据*********************//
void student::read()
{
int i;  
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;
for(i=0; i <this->stulen; i++)
{
  ob[i].show();
}
cout <<"\n\n\n";
}

//*******************一个学生的数据****************//
void student::add()
{
int i, d=this->stulen ;
int c=0;
cout <<" 输入要添加学生的信息:" <<endl;
do
{
  cout<<"\n";
  ob[d].setup();
  for(i=0;i<d;i++)
  {
   if(ob[i].id==ob[d].id )
   {
    cout<<" 系统检测到此次输入的学号和已有学生学号相同,请核实再输..";
    _getch();
    ob[d].address=' ';
    ob[d].date=' ';
    ob[d].cons=' ';
    ob[d].ps=' ';
    ob[d].id =0;
    ob[d].sex =' ';
    ob[d].name =NULL;
    c=1;
    break;
   }
   else c=0;
  }
}while(c!=0);

cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<"       学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;

for(i=0; i <=d; i++)
{
  ob[i].show();
}
this->stulen++;

ofstream fout("list.txt",ios::app);

if(!fout)
{
  cout <<" 无法打开数据文件!\n,";
}

//把添加的学生数据添加到list.txt里边去。
fout <<ob[d].id <<" " <<ob[d].name <<" " <<ob[d].sex <<" " <<
ob[d].address <<" " <<ob[d].date <<" " <<ob[d].cons <<" " <<ob[d].ps <<endl;

fout.close();
ofstream outfile1;
outfile1.open("stulen.txt",ios::trunc );
outfile1<<this->stulen ;
outfile1.close();
_getch();
}

//*********************删除指定名字学生的数据*******************//
void student::del()
{
int i,p;
int x;
p=-1;
if(this->stulen==0)
{
  cout<<" 目前没有学生数据!";
  _getch();
}
else
{
  cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
  cout <<endl <<"        学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;

  for(i=0; i <this->stulen; i++)
  {
   ob[i].show();
  }
  char a;
  cout <<"\n\n 输入要删除的学生学号:" ;
  cin>>x;

  //通过for循环查找要删除学生的学号
  for(i=0; i <stulen; i++)
  {
   if(x==ob[i].id )
   {
    p=i;  
    cout <<endl <<"        学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
    cout <<"      " <<ob[i].id <<"       " <<ob[i].name <<"      " <<ob[i].sex <<"      " <<ob[i].address <<"      " <<ob[i].date <<"      " <<ob[i].cons <<"       " <<ob[i].ps<<endl;
    break;
   }
   else
    continue;
  }
  if(p==-1)
  {
   cout<<" 没有此学生数据!";
   _getch();
  }
  else
  {
   cout<<" 确定删除此学生数据?<Y/N>";
   cin>> a;
   if (a=='y'|| a=='Y')
   {
    cout <<" 删除成功!" <<endl;
  
    //删除了之后,应该把后面的数据往前移,把要删除的数据覆盖,并且学生长度减1
    stulen--;
    for(i;i <stulen;i++)
    {
     ob[i]=ob[i+1];
    }
    ofstream outfile;
    outfile.open("list.txt",ios::trunc);
    for(i=0; i <this->stulen; i++)
    {
     outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
     ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
    }
    outfile.close();
    ofstream outfile1;
    outfile1.open("stulen.txt",ios::trunc );
    outfile1<<this->stulen ;
    outfile1.close();
    this->read ();

  
    //_getch();
   }
   else
   {
    cout<<" 回到主界面...";
    _getch();
   }
  }
}
}

//********修改学生信息********//
void student::modify()
{
int a,i,p,b;
p=-1;
system("cls");
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;

for(i=0; i <this->stulen; i++)
{
  ob[i].show();
}
    cout <<"\n\n 请输入要修改学生的学号:";
cin>>a;
for(i=0; i <stulen; i++)
{
  if(a==ob[i].id)
  {
   p=i;  
   cout <<endl <<"       学号     姓名    性别    工作部门    工作日期     工作内容       备注"  <<endl;
   cout <<"      " <<ob[i].id <<"     " <<ob[i].name <<"      " <<ob[i].sex <<"      " <<ob[i].address <<"      " <<ob[i].date <<"      " <<ob[i].cons <<"       " <<ob[i].ps <<endl;
   break;
  }
  else
   continue;
}
if(p==-1)
{
  cout<<" 没有此学生数据!";
  _getch();
}
else
{
  cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
  cin>>b;
  while(b!=1 &&b!=2 &&b!=3 &&b!=4)
  {
    cout<<" 无效输入,请重新输入!\n";
    cout<<"\n\n 请输入要修改的数据(1.工作部门, 2.工作日期,3.工作内容,4.备注):";
    cin>>b;
  }
  switch(b)
  {
   case 1:
    {
     float m;
     cout<<" 工作部门(40以内): ";
     cin>>m;

     //如果输入成绩在数据域内,跳出循环并且赋值。
     //如果不在数据域内,一直循环到输入数据符合数据域为止
     while (m <0 || m>40)
     {
      cout<<" 数据有误!!重新输入.."<<endl<<endl;
      cout<<" 工作部门(40以内): ";
      cin>>m;
     }

     ob[p].address=m;break;
    }
   case 2:
    {
     float e;
     cout<<" 工作日期(40以内): ";
     cin>>e;

     while (e <0 || e>40)
     {
      cout<<" 数据有误!!重新输入.."<<endl<<endl;
      cout<<" 工作日期(40以内): ";
      cin>>e;
     }

     ob[p].date=e; break;
    }
   case 3:
    {
     float co;
     cout<<" 工作内容(40以内): ";
     cin>>co;

     while (co <0 || co>40)
     {
      cout<<" 数据有误!!重新输入.."<<endl<<endl;
      cout<<"  工作内容(40以内): ";
      cin>>co;
     }

     ob[p].cons=co;
    }
    case 4:
    {
     float ss;
     cout<<" 备注(40以内): ";
     cin>>ss;

     while (ss <0 || ss>40)
     {
      cout<<" 数据有误!!重新输入.."<<endl<<endl;
      cout<<"  备注(40以内): ";
      cin>>ss;
     }

     ob[p].ps=ss;
    }
  }

  ofstream outfile;
  outfile.open("list.txt",ios::trunc);
  for(i=0; i <this->stulen; i++)
  {
   outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
   ob[i].address <<" " <<ob[i].date <<" " <<ob[i].cons <<" " <<ob[i].ps <<endl;
  }
  outfile.close();
  cout<<" 成绩修改成功!回车返回主界面...";
  _getch();
}
}

//***************程序运行前先检查以前是否有数据存在*****************//
void student::check()
{
int i;
char b[20];
ifstream infile;
infile.open("list.txt",ios::in );
if(!infile)
{
  cout<<" 系统没有检测到以前输入的数据,此次操作将建立一个新的数据文件...";
  _getch();
}
else
{
  ifstream infile1;
  infile1.open("stulen.txt",ios::in);
  infile1>>this->stulen ;
  infile1.close();
  for(i=0;i<this->stulen ;i++)
  {
   infile>>ob[i].id;
   ob[i].name =new char[strlen(b)+1];
   infile>>b;
   strcpy(ob[i].name,b);
   infile>>ob[i].sex ;
   infile>>ob[i].address ;
   infile>>ob[i].date;
   infile>>ob[i].cons;
   infile>>ob[i].ps;
  }
  infile.close ();

  system("cls");
  cout<<" 系统检测到以前输入过数据,数据内容如下:\n\n";
  cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
  cout <<endl <<"      学号     姓名    性别    工作部门    工作日期     工作内容       备注" <<endl;

//通过循环输出所有学生数据。
  for(i=0; i<this->stulen; i++)
  {
   ob[i].show();
  }
  cout<<"\n\n\n";
  cout<<" 原始数据是否覆盖?(Y/N)";
  char q;
  cin>>q;
  while(q!='y'&& q!='Y'&& q!='N'&& q!='n')
  {
   cout<<" 无效的命令,请重新输入..\n";
   cout<<" 原始数据是否覆盖?(Y/N)";
   cin>>q;
  }
  if(q=='y'||q=='Y')
  {
   for(int i=0;i <100;i++)
   {
    ob[i].address=' ';
    ob[i].date=' ';
    ob[i].cons=' ';
    ob[i].ps=' ';
    ob[i].id =0;
    ob[i].sex =' ';
    ob[i].name =NULL;
   }
   this->stulen =0;
   ofstream outfile1;
   outfile1.open("stulen.txt",ios::trunc);
   outfile1<<this->stulen ;
   outfile1.close();
   system("del list.txt");
  }
/* else*/
}
}

//**************按照一定格式显示所要查询学生的信息。**************//
void menu()
{
cout <<"\n\n";
cout <<"------------------ 学生成绩系统 -----------------" <<endl <<endl;
cout <<"\t1.录入与保存学生信息:\n";
cout <<"\t2.读取学生信息:\n";
cout <<"\t3.删除学生信息:\n";
cout <<"\t4.追加学生信息:\n";
cout <<"\t5.查询学生信息:\n";
cout <<"\t6.修改学生信息:\n";
cout <<"\t7.退出系统......\n\n\n";
cout <<"\t请选择要实现的功能: ";
}


void main()
{
student a;
a.check();
while(1)
{
  int SEL;
  system("cls");
  menu();
  cin>>SEL;
  switch(SEL)
  {
   case 1:
    system("cls");
    a.input();
    cout <<endl;
    cout <<" 是否保存? (Y/N): ";
    char Y;
    cin>>Y;
    while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
    {
     cout<<" 无效的命令,请重新输入..\n";
     cout<<" 是否保存? (Y/N)";
     cin>>Y;
    }
    system("cls");
    if(Y=='y'||Y=='Y')
     a.save();
    else/* if(Y=='n'||Y=='N')*/
     a.clean();break;
   case 2:
    system("cls"); a.read(); break;
   case 3:
    system("cls"); a.del(); break;
   case 4:
    system("cls"); a.add();break;
   case 5:
    system("cls"); a.query();break;
   case 6:
    system("cls"); a.modify();break;
   case 7:
    exit(0);
   default:
    cout <<"        无效的命令!";
    _getch();
    break;
  }
}
}
           
2011-09-04 16:37
快速回复:c对txt的学生管理软件 哪错了?
数据加载中...
 
   



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

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