每运行一个功能 数据自动重复叠加一次,请问如何避免 修改
#include<iostream>
#include<fstream>
#include<string.h>
#include<windows.h>
using namespace std;
class parent{
public:
string parentname;
string num;
string message;
};
class K {
public:
string name;
double height;
double weight;
string gender;
string address;
parent pa;
K *next;
};
class kids:private K {
public:
kids() {
head = new K; head->next = NULL;
}
void modify();//修改信息
void findkid();//查找儿童信息
void findparent();//查找父母信息
void census();//统计性别
void read();//读取文件
void show();//显示儿童信息;
void shengao();//身高排序
void function();
~kids() {};
private:
K *head;
K *tail;
};
int main() {
kids kid;
while (1) {
kid.read();
kid.function();
char select;
cout<< "请输入调用功能" << endl;
cin >> select;
switch (select) {
case '1':kid.findkid();break;
case '2':kid.findparent();break;
case '3':kid.modify();break;
case '4':kid.show();break;
case '5':kid.census();break;
case '6':kid.shengao();break;
case '7':return 0;
}
}
}
void kids::function()
{
cout << "*************************************************" << endl;
cout << " 1. 查找儿童信息 " << endl;
cout << " 2. 查找家长信息 " << endl;
cout << " 3. 修改信息 " << endl;
cout << " 4. 显示信息 " << endl;
cout << " 5. 统计儿童人数(性别) " << endl;
cout << " 6. 按身高排序 " << endl;
cout << " 7. 退出系统 " << endl;
cout << " 请选择:" << endl;
}
void kids::findkid() {
K *p;
p = head->next;
K* q;
q = head->next;
if (q== NULL) {
cout << "暂无该儿童" << endl;
system("pause");
return;
}
string kn;
cout << "请输入需要查询的儿童姓名" << endl;
cin >> kn;
while (p) {
if (!strcmp(kn.c_str(), p->name.c_str())) {
cout << "姓名:" << p->name << "\t";
cout << "性别:" << p->gender << "\t";
cout << "身高:" << p->height << "\t";
cout << "体重:" << p->weight << "\t";
cout << "家庭住址:" << p->address << "\t";
}
p = p->next;
}
system("pause");
system("cls");
}
void kids::findparent() {
K *f;
f = head->next;
K* q;
q = head->next;
if (q == NULL) {
cout << "暂无该儿童" << endl;
system("pause");
return;
}
string pn;
cout << "请输入需要查询父母的孩子姓名" <<endl;
cin >> pn;
while (f) {
if (!strcmp(pn.c_str(), f->name.c_str())) {
cout << "父母姓名为:" << f->pa.parentname << "\t";
cout << "父母电话为:" << f->pa.num << "\t";
cout << "留言:" << f->pa.message << "\t";
}f = f->next;
}system("pause");
system("cls");
}
void kids::modify() {
K* mod;
mod = head->next;
if (mod == NULL) {
cout << "暂无该学生信息" << endl;
system("pause");
return;
}
string nam;
cout << "请输入想要修改的学生姓名" << endl;
cin >> nam;
while (mod) {
if (nam == mod->name) {
cout << "请重新输入性别" << endl;
cin >> mod->gender;
cout << "请重新输入身高 体重" << endl;
cin >> mod->height >> mod->weight;
cout << "请重新输入家庭地址" << endl;
cin >> mod->address;
break;
}
else {
mod = mod->next;
}
}
cout << "修改成功" << endl;
system("pause");
system("cls");
}
void kids::show() {
K* f;
f = head->next;
if (f == NULL) {
cout << "无" << endl;
system("pause");
return;
}
while (f) {
cout << "姓名:" << f->name << endl;
cout << "性别:" << f->gender << endl;
cout << "身高:" << f->height << endl;
cout << "体重:" << f->weight << endl;
cout << "家庭住址:" << f->address << endl;
f = f->next;
}
system("pause");
system("cls");
}
void kids::census() {
K* tj;
tj = head->next;
if (tj == NULL)
{
cout << "暂无儿童信息\n" << endl;
system("pause");
return;
}
int sexshu = 0;
string fname;
cout << "请输入所要查询的儿童性别" << endl;
cin >> fname;
while (tj) {
if (!strcmp(fname.c_str(), tj->gender.c_str())) {
sexshu++;
}
tj = tj->next;
}
cout << "统计该性别数为:" << sexshu << endl;
system("pause");
system("cls");
}
void kids::shengao() {
K* q, * p, * r, * w;
p = head;
q = head->next;
while (p->next != NULL)
{
while (q->next != NULL)
{
if (p->next->height < q->next->height )
{
r = p->next;
p->next = q->next;
q->next = q->next->next;
p->next->next = r;
}
else
{
q = q->next;
}
}
p = p->next;
q = p->next;
}
w = head->next;
cout << "按身高排序如下" << endl;
while (w)
{
cout << "姓名:" << w->name << endl;
w = w->next;
}
system("pause");
system("cls");
}
void kids::read() {
ifstream ifs;
ifs.open("D:\\caozuo.txt", ios::in);
if (!ifs.is_open())
{
cout << "open caozuo.txt error!" << endl;
}
K* p;
p = head;
while (p->next != NULL)
{
p = p->next;
}
while (!ifs.eof())
{
K* s = new K;
ifs >> s->name >> s->gender >> s->height >> s->weight >> s->address >> s->pa.parentname >> s->pa.num >> s->pa.message;
if (ifs.get() == -1)
{
break;
}
p->next = s;
p = s;
p->next = NULL;
}
ifs.close();
cout << "读取成功!" << endl;
}