64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <list>
#include <algorithm>
#include <ctime>
using namespace std;
class Stu
{
private:
int num;
int grade;
string name;
string sex;
string birth;
string profession;
string adress;
public:
Stu(){}
~Stu(){}
void SetStu(int nnum, int ngrade, string nname, string nsex, string nbirth, string nprofession, string nadress)
{
num = nnum;
grade = ngrade;
name = nname;
sex = nsex;
birth = nbirth;
profession = nprofession;
adress = nadress;
}
int GetNum()
{
return num;
}
int GetGrade()
{
return grade;
}
string GetName()
{
return name;
}
string GetSex()
{
return sex;
}
string GetAdress()
{
return adress;
}
string GetProfession()
{
return profession;
}
string GetBirth()
{
return birth;
}
friend class List;
};
struct Node
{
Stu data;
Node *next;
};
class List
{
private:
Node *head;
public:
friend class Stu;
List(){ head = NULL; }
void AddList();
void DelList();
void ShowList();
void SaveList();
void LoadList();
void Search();
Node* GetHead(){ return head; }
};
void List::ShowList()
{
Node *current = head;
if (head == NULL)
{
cout << "没有学生信息!" << endl;
}
while (current!=NULL)
{
cout << current->data.GetNum();
cout << current->data.GetName();
cout << current->data.GetSex();
cout << current->data.GetBirth();
cout << current->data.GetProfession();
cout << current->data.GetGrade();
cout << current->data.GetAdress();
current = current->next;
cout << endl;
}
}
void List::AddList()
{
int n;
cout << "请输入新建学生个数:";
cin >> n;
Stu *ptr1 = new Stu[n];
Node *ptr2 = new Node[n];
int nnum, ngrade;
for (int i = 0; i<n; i++)
{
string nname, nsex, nbirth, nprofession, nadress;
Node *p, *s;
cout << "请输入学生姓名:";
cin >> nname;
cout << endl << "请输入学生学号:";
cin >> nnum;
cout << endl << "请输入学生性别:";
cin >> nsex;
cout << endl << "请输入学生英语成绩:";
cin >> ngrade;
cout << endl << "请输入学生生日:";
cin >> nbirth;
cout << endl << "请输入学生专业:";
cin >> nprofession;
cout << endl << "请输入学生住址:";
cin >> nadress;
ptr1[i].SetStu(nnum, ngrade, nname, nsex, nbirth, nprofession, nadress);
ptr2[i].data = ptr1[i];
s = (Node*)new(Node);
s->data = ptr1[i];
p = head;
if (head == NULL)
{
head = s;
s->next = NULL;
}
else
{
while (p->next!=NULL)
{
p = p->next;
}
{
p->next = s;
s->next = NULL;
}
}
}
delete[] ptr1;
delete[] ptr2;
}
void List::DelList()
{
int dnum;
cout << "请输入要删除学生的编号" << endl;
cin >> dnum;
Node *p, *q;
p = head;
if (p == NULL)
{
return;
}
if (p->data.num == dnum)
{
head = p->next;
delete p;
}
else
{
while (p->data.num != dnum&&p->next != NULL)
{
q = p;
p = p->next;
if (p->data.num == dnum)
{
q->next = p->next;
delete p;
}
}
}
}
void List::SaveList()
{
ofstream fList("Stu.txt", ios::out);
Node *p = head;
while (p)
{
fList << p->data.GetNum() << p->data.GetName() << p->data.GetSex() << p->data.GetGrade() << p->data.GetProfession() << p->data.GetAdress() << p->data.GetBirth();
p = p->next;
}
}
void List::LoadList()
{
ifstream fList("Stu.txt", ios::in);
if (!fList)
{
cerr << "open error!" << endl;
abort();
}
while (!fList.eof())
{
int tnum;
int tgrade;
string tname;
string tsex;
string tbirth;
string tprofession;
string tadress;
Node *p, *q;
if (head == NULL)
{
p = new Node();
fList >> tnum >> tname >>tsex >> tgrade >> tprofession >> tadress >> tbirth;
p->data.SetStu(tnum, tgrade, tname, tsex, tbirth, tprofession, tadress);
head = p;
head->next = NULL;
}
else
{
q = head;
p = new Node();
fList >> tnum >> tname >> tsex >> tgrade >> tprofession >> tadress >> tbirth;
p->data.SetStu(tnum, tgrade, tname, tsex, tbirth, tprofession, tadress);
while (q->next != NULL)
{
q = q->next;
}
q->next = p;
p->next = NULL;
}
}
}
void menu()
{
cout << endl;
cout << "\t\t***********************************************" << endl;
cout << "\t\t** **" << endl;
cout << "\t\t** 学生信息管理系统主菜单 **" << endl;
cout << "\t\t** **" << endl;
cout << "\t\t** 1. 新增学生信息 **" << endl;
cout << "\t\t** 2. 删除学生信息 **" << endl;
cout << "\t\t** 3. 导入学生信息 **" << endl;
cout << "\t\t** 4. 学生信息搜索(按姓名) **" << endl;
cout << "\t\t** 5. 学生信息统计 **" << endl;
cout << "\t\t** 6. 按英语成绩排序 **" << endl;
cout << "\t\t** 7. 学生信息保存 **" << endl;
cout << "\t\t** 8. 显示学生信息 **" << endl;
cout << "\t\t** 0. 退出 **" << endl;
cout << "\t\t** **" << endl;
cout << "\t\t***********************************************" << endl;
cout << endl;
}
void domain()//主菜单函数实现
{
List S;
while (1)
{
system("cls");
menu();
cout << "请选择数字编号1~8" << endl;
int i1;
cin >> i1;
while (!(i1 >= 0 && i1 <= 8))
{
cout << "输入有误请重新输入" << endl;
cin >> i1;
}
switch (i1)
{
case 1:S.AddList(); break;
case 2:S.DelList(); break;
case 3:S.LoadList(); break;
//case 4:S.Search(); break;
//case 5:Sl.Statistic(); break;
//case 7:Sl.Sort(); break;
case 7:S.SaveList(); break;
case 8:S.ShowList(); break;
case 0:cout << "系统退出" << endl; exit(0);
default:break;
}
cout << "是否返回主菜单? Y/N" << endl;
string p;
cin >> p;
if (p == "n" || p == "N")
{
cout << "系统退出" << endl;
exit(0);
}
getchar();
}
}
int main()
{
menu();
domain();
return 0;
}
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
while (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
while (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。
void List::SaveList()
{
ofstream fList("Stu.txt", ios::out);
Node *p = head;
while (p)
{
fList << p->data.GetNum() << " " << p->data.GetName() << " " << p->data.GetSex() << " " << p->data.GetGrade() << " " << p->data.GetProfession() << " " << p->data.GetAdress() << " " << p->data.GetBirth() << " ";
p = p->next;
}
}
void List::LoadList()
{
int tnum;
int tgrade;
string tname;
string tsex;
string tbirth;
string tprofession;
string tadress;
ifstream fList("Stu.txt", ios::in);
if (!fList)
{
cerr << "open error!" << endl;
abort();
}
while (!fList.eof())
{
Node *p, *q;
if (head == NULL)
{
p = new Node();
fList >> tnum >> tname >> tsex >> tgrade >> tprofession >> tadress >> tbirth;
p->data.SetStu(tnum, tgrade, tname, tsex, tbirth, tprofession, tadress);
head = p;
head->next = NULL;
}
q = head;
p = new Node();
fList >> tnum >> tname >> tsex >> tgrade >> tprofession >> tadress >> tbirth;
p->data.SetStu(tnum, tgrade, tname, tsex, tbirth, tprofession, tadress);
while (q->next != NULL)
{
q = q->next;
}
q->next = p;
p->next = NULL;
}
void List::SaveList()
{
ofstream fList("Stu.txt", ios::out);
Node *p = head;
while (p)
{
fList << p->data.GetNum() << p->data.GetName() << p->data.GetSex() << p->data.GetGrade() << p->data.GetProfession() << p->data.GetAdress() << p->data.GetBirth();
p = p->next;
}
}
void List::LoadList()
{
ifstream fList("Stu.txt", ios::in);
if (!fList)
{
cerr << "open error!" << endl;
abort();
}
while (!fList.eof())
{
int tnum;
int tgrade;
string tname;
string tsex;
string tbirth;
string tprofession;
string tadress;
Node *p, *q;
if (head == NULL)
{
p = new Node();
fList >> tnum >> tname >>tsex >> tgrade >> tprofession >> tadress >> tbirth;
p->data.SetStu(tnum, tgrade, tname, tsex, tbirth, tprofession, tadress);
head = p;
head->next = NULL;
}
else
{
q = head;
p = new Node();
fList >> tnum >> tname >> tsex >> tgrade >> tprofession >> tadress >> tbirth;
p->data.SetStu(tnum, tgrade, tname, tsex, tbirth, tprofession, tadress);
while (q->next != NULL)
{
q = q->next;
}
q->next = p;
p->next = NULL;
}
}
}
因为怕你们有疑问就把我的半成品全放上来了。希望你们可以自己运行一下我不知道该怎么修改。C++老师很坑爹。当时也没有听太懂,还是要靠自己问。