这个学期快要结束了,学校布置的课程设计也要完成并检验,因为之前自学c语言的原因因此我还是比较快的完成了课程设计,虽然其中也有一些bug,而其中c++的关键还是在类与对象的使用上,这个新生入学管理系统中,完成增删改查文件的保存与读取
代码中建立两个类一个是存储学生信息,一个用于存储各种功能
1,创建链表
先开辟一个空间基本套路使其不断循环将内容存于空间中,且当输入姓名为0的时候退出
2,添加
首先先开辟一片空间,将内容输入后再将其添加在已存在的链表的后面
3.删除
一种是头结点:直接让头指针移向下一位,再把原结点free掉。其他结点,令p1指针先向后移动一位,若不是要删除的,就每次向后位移。直到出现要删除结点,把该指针的上一个next搭到它的下一个,把它隔过去,再free掉。
4.搜索
通过对链表的遍历,当名字相同时打印出信息
5.修改
首先重新定义学生信息的几个内容,通过遍历和判断找到需要修改的学生信息,在函数重新定义的学生信息中输入的内容,通过copy将原有的信息更换
6.排序
我所用的单链表的选择排序,通过找出最小值,通过memcpy,strcpy,"="将内容排序1
7.统计
通过性别的统计,定义两个字符串,与链表中已有的性别进行比较,当相同时对定义的两个不同的整数加一
8.读文件
首先创建一个新链表,头结点为空,将文件中的内容逐个输入到该链表中
9.存文件
把头赋给p1后,只要p1存值,就把它写入到out所新建的那个文件里,再让p1移动下一个
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstring>
#include<stdlib.h>
using namespace std;
int n=0;
char stop[12]= {"0"};
char name[12];
class Student
{
public:
char name[12];
char sex[3];
char study[12];
char addr[12];
int birth[3];
int engli;
Student *next;
};
class information
{
private:
Student *head,*p1,*p2,*p3;
public:
information () {};
Student *creat();
Student *add(Student *head);
Student *delet(Student *head);
void *search(Student *head);
Student *modify(Student *head);
Student *sort(Student*head);
Student *statistics(Student*head);
void display(Student *head);
void displaynode(Student *head);
void write_file(Student *head);
Student * read_file();
~information () {};
};
class manage
{
public:
char menu();
int choose();
};
information s;
Student *information::creat()
{
p1=p2=new Student;
head=NULL;
char sex1[3];
char sex2[3];
strcpy(sex1, "男");
strcpy(sex2, "女");
cout<<"请输入学生的基本信息:以姓名为0结束。\n";
while(1)
{
cout<<"姓名: \t";
cin>>p1->name;
if( strcmp(stop,p1->name)==0 )
{
break;
}
cout<<"性别: \t";
cin>>p1->sex;
while(strcmp(sex1,p1->sex)!=0 &&strcmp(sex2,p1->sex)!=0 )
{
cout<<"请输入格式 男/女,请重新输入!"<<endl;
cout<<"性别: \t";
cin>>p1->sex;
}
cout<<"专业: \t";
cin>>p1->study;
cout<<"家庭住址: \t";
cin>>p1->addr;
cout<<"出生年:";
cin>>p1->birth[0];
while(p1->birth[1]<1 || p1->birth[1]>12)
{
cout<<"出生月:";
cin>> p1->birth[1];
if(p1->birth[1]<1 || p1->birth[1]>12)
{
cout<<"月份应为1-12月,请重新输入!"<<endl;
}
}
cout<<"出生日:";
cin>>p1->birth[2];
while(p1->birth[2]<1 || p1->birth[2]>31)
{
cout<<"每月应为1-31天,请重新输入!"<<endl;
cout<<"出生日:";
cin>>p1->birth[2];
}
cout<<"英语:";
cin>>p1->engli;
while(p1->engli<0 || p1->engli>100)
{
cout<<"英语成绩必须在0-100之间,请重新输入!"<<endl;
cout<<"英语:";
cin>>p1->engli;
}
system("cls");
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new Student;
}
p2->next=NULL;
return head;
}
Student *information::statistics(Student*head)
{
p1=new Student;
p1=head;
int x=0,y=0;
char sex1[3];
char sex2[3];
strcpy(sex1, "男");
strcpy(sex2, "女");
for(p1=head; p1!=NULL; p1=p1->next)
{
if(strcmp(sex1,p1->sex)==0 )
{
x=x+1;
}
if(strcmp(sex2,p1->sex)==0)
{
y=y+1;
}
}
cout<<"男: "<<x<<endl;
cout<<"女: "<<y<<endl;
return head;
}
Student *information::add(Student *head)
{
p1=p3=new Student;
p1=head;
char sex1[3];
char sex2[3];
strcpy(sex1, "男");
strcpy(sex2, "女");
if(p1==NULL)
{
cout<<"在添加前,先新建!"<<endl;
return 0;
}
else
{
cout<<"请输入要添加学生的信息!\n";
cout<<"姓名: \t";
cin>>p3->name;
cout<<"性别: \t";
cin>>p3->sex;
while(strcmp(sex1,p3->sex)!=0 &&strcmp(sex2,p3->sex)!=0 )
{
cout<<"请输入格式 男/女,请重新输入!"<<endl;
cout<<"性别: \t";
cin>>p3->sex;
}
cout<<"专业: \t";
cin>>p3->study;
cout<<"家庭住址: \t";
cin>>p3->addr;
cout<<"出生年:";
cin>>p3->birth[0];
while(p3->birth[1]<1 || p3->birth[1]>12)
{
cout<<"出生月:";
cin>> p3->birth[1];
if(p3->birth[1]<1 || p3->birth[1]>12)
{
cout<<"月份应为1-12月,请重新输入!"<<endl;
}
}
cout<<"出生日:";
cin>>p3->birth[2];
while(p3->birth[2]<1 || p3->birth[2]>31)
{
cout<<"每月应为1-31天,请重新输入!"<<endl;
cout<<"出生日:";
cin>>p3->birth[2];
}
cout<<"英语:";
cin>>p3->engli;
while(p3->engli<0 || p3->engli>100)
{
cout<<"英语成绩必须在0-100之间,请重新输入!"<<endl;
cout<<"英语:";
cin>>p3->engli;
}
cout<<"\n";
p3->next=p1->next;
p1->next=p3;
cout<<"添加成功!\n";
}
return head;
}
Student *information::delet(Student *head)
{
p2=p1=new Student;
if(head==NULL)
{
cout<<"没有数据,无法删除";
return 0;
}
cout<<"请输入要删除学生的名字:\n";
cin>>name;
p2=p1=head;
int j=0;
if( ( strcmp(name,head->name)==0 ) && (head!=NULL))
{
head=head->next;
free(p1);
j=1;
}
else
{
p1=head->next;
while(p1!=NULL)
{
if(strcmp(name,p1->name)==0)
{
p2->next=p1->next;
free(p1);
j=1;
break;
}
else
{
p2=p1;
p1=p2->next;
}
}
}
if(j==0)
cout<<"此学生不存在,删除失败!\n";
else
cout<<"删除成功!\n";
return head;
}
void *information::search(Student *head)
{
char name[20];
p1=new Student;
cout<<"请输入要查找学生的姓名:\n";
cin>>name;
p1=head;
for(p1=head; p1!=NULL; p1=p1->next)
{
if(strcmp(name,p1->name)==0 )
{
cout<<"姓名:"<<p1->name<<" 性别:"<<p1->sex<<" 专业:"<<p1->study;
cout<<" 家庭住址:"<<p1->addr<<endl;
cout<<p1->birth[0]<<"年";
cout<<p1->birth[1]<<"月:"<<p1->birth[2]<<"日 出生:";
cout<<"年龄:"<<2017-p1->birth[0]<<endl;
cout<<"英语成绩:"<<p1->engli<<"\n\n";
}
}
return head;
};
Student *information::modify(Student *head)
{
if(head==NULL)
{
cout<<"没有数据,先新建吧";
return 0;
}
char name1[12];
char sex[3];
char study[12];
char addr[12];
int birth[3];
int engli;
p1=new Student;
int j=0;
p1=head;
cout<<"请输入你要更改学生的姓名:\n";
cin>>name;
if(strcmp( name, head->name)==0)
{
cout<<"显示要修改学生的信息:\n";
s.displaynode(p1);
cout<<"请输入要更改学生的信息:\n";
cout<<"姓名: \t";
cin>>name1;
cout<<"性别: \t";
cin>>sex;
cout<<"专业: \t";
cin>>study;
cout<<"家庭住址: \t";
cin>>addr;
cout<<"出生年:";
cin>>birth[0];
cout<<"出生月:";
cin>>birth[1];
cout<<"出生日:";
cin>>birth[2];
cout<<"英语:";
cin>>engli;
strcpy(head->name,name1);
strcpy(head->sex,sex);
strcpy(head->study,study);
strcpy(head->addr,addr);
head->birth[0]=birth[0];
head->birth[1]=birth[1];
head->birth[2]=birth[2];
head->engli=engli;
j=1;
}
else
{
p1=head->next;
while(p1!=NULL)
{
if(strcmp(p1->name,name)==0)
cout<<"显示要修改学生的信息:\n";
s.displaynode(p1);
cout<<"请输入要更改学生的信息:\n";
cout<<"姓名: \t";
cin>>name1;
cout<<"性别: \t";
cin>>sex;
cout<<"专业: \t";
cin>>study;
cout<<"家庭住址: \t";
cin>>addr;
cout<<"出生年:";
cin>>birth[0];
cout<<"出生月:";
cin>>birth[1];
cout<<"出生日:";
cin>>birth[2];
cout<<"英语:";
cin>>engli;
strcpy(p1->name,name1);
strcpy(p1->sex,sex);
strcpy(p1->study,study);
strcpy(p1->addr,addr);
p1->birth[0]=birth[0];
p1->birth[1]=birth[1];
p1->birth[2]=birth[2];
p1->engli=engli;
j=1;
break;
}
}
if(j==0)
cout<<"没有找到你要更改的学生,更改失败!\n";
else
cout<<"更改成功!\n";
return head;
}
void information::display(Student *head)
{
p1=head;
if(p1==NULL)
cout<<"这是一个空表!请先输入学生信息。"<<endl;
else
{
while(p1!=NULL)
{
s.displaynode(p1);
p1=p1->next;
}
}
}
void information::displaynode(Student *head)
{
p1=head;
if(p1==NULL)
cout<<"这是一个空表!请先输入学生信息。"<<endl;
else
{
cout<<"姓名:"<<p1->name<<" 性别:"<<p1->sex<<" 专业:"<<p1->study;
cout<<" 家庭住址:"<<p1->addr<<endl;
cout<<"生日: "<<p1->birth[0]<<"年";
cout<<p1->birth[1]<<"月"<<p1->birth[2]<<"日"<<endl;
cout<<"年龄:"<<2017-p1->birth[0]<<endl;
cout<<"英语成绩:"<<p1->engli<<"\n\n";
}
}
Student *information::read_file()
{
int i=0;
char name2[12];
char study[12];
char sex[3];
char addr[12];
int birth[3];
int engli;
p1=p2=new Student;
head=NULL;
ifstream in;
in.open("myC++work.txt");
if(!in)
{
cout<<"打开文件失败!"<<endl;
}
else
{
while(in>>name2>>sex>>study>>addr>>birth[0]>>birth[1]>>birth[2]>>engli)
{
strcpy(p1->name,name2);
strcpy(p1->sex,sex);
strcpy(p1->study,study);
strcpy(p1->addr,addr);
p1->birth[0]=birth[0];
p1->birth[1]=birth[1];
p1->birth[2]=birth[2];
p1->engli=engli;
i++;
if(i==1)
{
head=p2=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=new Student;
p1->next=NULL;
}
cout<<"成功读取文件!内容如下:"<<endl;
}
return head;
}
void information::write_file(Student *head)
{
ofstream out;
out.open("myC++work.txt");
if(!out)
{
cout<<"打开文件失败!"<<endl;
}
p1=NULL;
p1=head;
while(p1)
{
out<<p1->name<<setw(5)<<p1->sex<<setw(5)<<p1->study<<setw(5)<<p1->addr<<setw(5)<<p1->birth[0]<<setw(5)<<p1->birth[1]<<setw(5)<<p1->birth[2]<<setw(5)<<p1->engli<<endl;
p1=p1->next;
}
out.close();
cout<<"写入完毕。";
}
Student *information::sort(Student *head)
{
Student *p,*q,*small;
char name[12];
char sex[3];
char study[12];
char addr[12];
int birth[3];
int engli;
p=head;
for(; p->next!=NULL; p=p->next)
{
small=p;
for(q=p->next; q; q=q->next)
{
if(q->engli<small->engli)
{
small=q;
}
}
if(small!= p)
{
engli=p->engli;
p->engli=small->engli;
small->engli=engli;
strcpy(name,p->name);
strcpy(p->name,small->name);
strcpy(small->name,name);
strcpy(study,p->study);
strcpy(p->study,small->study);
strcpy(small->study,study);
memset(birth,0,sizeof(birth));
memcpy(birth,p->birth,sizeof(birth));
memcpy(p->birth,small->birth,sizeof(birth));
memcpy(small->birth,birth,sizeof(birth));
strcpy(addr,p->addr);
strcpy(p->addr,small->addr);
strcpy(small->addr,addr);
strcpy(sex,p->sex);
strcpy(p->sex,small->sex);
strcpy(small->sex,sex);
}
}
return head;
}
char manage::menu()
{
char ch;
cout<<endl;
cout<<endl;
cout<<" *** *** "<<endl;
cout<<" ***** ***** "<<endl;
cout<<" *****************学生信息管理****************"<<endl;
cout<<" 1.新增学生信息 ********* "<<endl;
cout<<" 2.插入学生信息 ***** "<<endl;
cout<<" 3.删除学生信息 *** "<<endl;
cout<<" 4.学生信息搜索 "<<endl;
cout<<" 5.修改学生信息 "<<endl;
cout<<" 6.按照(英语)成绩排序 "<<endl;
cout<<" 7.统计男/女人数 "<<endl;
cout<<" 8.读取学生信息 "<<endl;
cout<<" 9.保存信息退出 "<<endl;
cout<<" ********************WPSEC********************"<<endl;
cin>>ch;
return ch;
}
int manage::choose()
{
manage m;
information s;
Student *head;
head=NULL;
int n=0;
char c;
while(1)
switch (m.menu())
{
case'1':
head=s.creat();
system("pause");
system("cls");
break;
case'2':
head=s.add(head);
system("pause");
system("cls");
break;
case'3':
head=s.delet(head);
system("pause");
system("cls");
break;
case'4':
s.search(head);
system("pause");
system("cls");
break;
case'5':
head=s.modify(head);
system("pause");
system("cls");
break;
case'6':
s.sort(head);
s.display(head);
system("pause");
system("cls");
break;
case'7':
s.statistics(head);
system("pause");
system("cls");
break;
case'8':
head=s.read_file();
s.display(head);
system("pause");
system("cls");
break;
case'9':
s.write_file(head);
cout<<"谢谢使用!再见!\n";
system("pause");
system("cls");
return 0;
default:
cout<<"选择有错,请重新选择\n";
}
}
int main ()
{
manage m;
m.choose();
return 0;
}
网友评论