C++学生信息管理系统
我大一时,使用C++写了一个简单的学生管理系统,主要是为了完成老师布置的课程设计。现在又快到那个验收课程设计的时候了,现在分享给大家。如果你要的是用C写的代码,这个也是可以参考的,你可以自己进行修改成C的代码。先上截图简单的介绍下这个程序大致上就这12个功能,你们也可以自己测试测试,像年龄的范围可以设置成18-24岁,性别设置成只可以输入男或女,这些你们都可以在完善下,因为我现在不学C...
·
我大一时,使用C++写了一个简单的学生信息管理系统,现在又快到那个验收课程设计的时候了,现在分享给大家。如果你要的是用C写的代码,这个也是可以参考的,你可以自己进行修改成C的代码。
先上截图简单的介绍下这个程序
大致上就这12个功能,你们也可以自己测试测试,像年龄的范围可以设置成18-24岁,性别设置成只可以输入男或女,这些你们都可以在完善下。
我的IDE是DEV C++,为了测试的一致性,最好和我选用一样的IDE。
好了,最后附上源代码。
/*题目
新生基本信息统计软件
链表类实现并写入文件和实现登录
*/
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct Student_Node //单链表的结点的定义
{
//数据域
int num;
int age;
char name[20];
char sex[10];
char major[10];
char born[10];
char address[10];
int English;
//指针域
struct Student_Node *next;
};
//定义学生类
class Student
{
public:
Student()
{
head=new Student_Node;
head->next=NULL;
}
void menu();//主菜单
void create();//录入学生的信息
void add();//增添学生信息
void pass();//删除学生信息
void seek();//查找学生的信息
void insert();//插入学生的信息
void rewrite();//修改学生的信息
void sort();//按照学生的英语成绩进行排序
void show();//显示全部学生的信息
void getfile();//将学生的信息写入文件之中
void putfile();//将学生的信息显示在终端上
virtual void count()=0;//统计学生的信息
void major_();
void sex_();
void age_();
private:
struct Student_Node *head;
};
class Freshman:public Student
{
public:
void count()//统计学生的信息
{
cout<<"\t\t\t\t1.按专业统计学生的信息"<<endl;
cout<<"\t\t\t\t2.按性别统计学生的信息"<<endl;
cout<<"\t\t\t\t3.按年龄统计学生的信息"<<endl;
cout<<"\t\t\t\t请输入您的选择......."<<endl<<"\t\t\t\t";
int i;cin>>i;
switch(i)
{
case 1: major_(); break;
case 2: sex_(); break;
case 3: age_(); break;
}
}
private:
struct Student_Node *head;
};
void Student::major_()
{
struct Student_Node *p1=head->next;
cout<<"\t\t\t\t专业:";
int cnt=0;char c[20];cin>>c;
while(p1)
{
if(strcmp(c,p1->major)==0)
{
cnt++;
}
p1=p1->next;
}
cout<<"\t\t\t\t"<<c<<":"<<cnt<<"人"<<endl;getchar();
}
void Student::sex_()
{
struct Student_Node *p1=head->next;
cout<<"\t\t\t\t性别:";
int cnt=0;char c[10];cin>>c;
while(p1)
{
if(strcmp(c,p1->sex)==0)
{
cnt++;
}
p1=p1->next;
}
cout<<"\t\t\t\t"<<c<<":"<<cnt<<"人"<<endl;getchar();
}
void Student::age_()
{
struct Student_Node *p1=head->next;
cout<<"\t\t\t\t年龄:";
int cnt=0;int c;cin>>c;
while(p1)
{
if(p1->age==c)
{
cnt++;
}
p1=p1->next;
}
cout<<"\t\t\t\t"<<c<<"岁"<<":"<<cnt<<"人"<<endl;getchar();
}
//类成员函数的实现
void Student::create()//创建一个链表,用来录入学生的信息
{
cout<<"\t\t\t\t请输入您要创建的学生的数量:"<<endl<<"\t\t\t\t";
int i;
cin>>i;
struct Student_Node *p1=head;
struct Student_Node *p2;
for(int j=1;j<i+1;j++)
{
p2=new Student_Node;
cout<<"\t\t\t\t请输入第"<<j<<"个学生的学号:"<<endl<<"\t\t\t\t";
cin>>p2->num;
cout<<"\t\t\t\t请输入第"<<j<<"个学生的年龄:"<<endl<<"\t\t\t\t";
cin>>p2->age;
cout<<"\t\t\t\t请输入第"<<j<<"个学生的姓名:"<<endl<<"\t\t\t\t";
cin>>p2->name;
cout<<"\t\t\t\t请输入第"<<j<<"个学生的性别:"<<endl<<"\t\t\t\t";
cin>>p2->sex;
cout<<"\t\t\t\t请输入第"<<j<<"个学生的专业:"<<endl<<"\t\t\t\t";
cin>>p2->major;
cout<<"\t\t\t\t请输入第"<<j<<"个学生的出生日期:"<<endl<<"\t\t\t\t";
cin>>p2->born;
cout<<"\t\t\t\t请输入第"<<j<<"个学生的家庭住址:"<<endl<<"\t\t\t\t";
cin>>p2->address;
cout<<"\t\t\t\t请输入第"<<j<<"个学生的英语成绩:"<<endl<<"\t\t\t\t";
cin>>p2->English;
p2->next=NULL;
p1->next=p2;
p1=p2;
}
getchar();
}
void Student::add()//增加学生的信息
{
struct Student_Node *p1=head->next;
struct Student_Node *p2;
int a=1,b=1;
int c; //学生的学号
while(p1->next!=NULL)
{
p1=p1->next;
a++;
}
cout<<"\t\t\t\t请输入学生的学号:"<<endl<<"\t\t\t\t";
cin>>c;
p1=head->next;
for(int i=1;i<=a;i++)//判断学生的学号是否重复
{
if(p1->num==c)
{
cout<<"\t\t\t\t出错:学号重复!!!"<<endl;
b=0;
break;
}
p1=p1->next;
}
if(b!=0)//判断
{
p1=head;
while(p1->next!=NULL)
{
p1=p1->next;
}
p2=new Student_Node;
p2->num=c;
cout<<"\t\t\t\t请输入学生的年龄:"<<endl<<"\t\t\t\t";cin>>p2->age;
cout<<"\t\t\t\t请输入学生的姓名:"<<endl<<"\t\t\t\t";cin>>p2->name;
cout<<"\t\t\t\t请输入学生的性别:"<<endl<<"\t\t\t\t";cin>>p2->sex;
cout<<"\t\t\t\t请输入学生的专业:"<<endl<<"\t\t\t\t";cin>>p2->major;
cout<<"\t\t\t\t请输入学生的出生日期:"<<endl<<"\t\t\t\t";cin>>p2->born;
cout<<"\t\t\t\t请输入学生的家庭住址:"<<endl<<"\t\t\t\t";cin>>p2->address;
cout<<"\t\t\t\t请输入学生的英语成绩:"<<endl<<"\t\t\t\t";cin>>p2->English;
p2->next=NULL;
p1->next=p2;
p1=p2;
}
getchar();
}
void Student::pass()//删除学生的信息
{
struct Student_Node *p1;
p1=head;
struct Student_Node *p2;
int i=1;
if(p1->next==NULL)
{
cout<<"\t\t\t\t删除失败!!!"<<endl;
cout<<"\t\t\t\t无可用的数据!!!"<<endl;
i=0;
}
if(i==1)
{
int j;
cout<<"\t\t\t\t请输入您想要删除的学生的学号:"<<endl<<"\t\t\t\t";
cin>>j;
while(p1->next!=NULL)
{
if(p1->next->num==j)
{
cout<<"\t\t\t\t******删除成功********"<<endl;
getchar();
p2=p1->next;
p1->next=p2->next;
delete p2;
return ;
}
p1=p1->next;
}
cout<<"\t\t\t\t删除错误!!!无法找到此学生的信息!!!"<<endl;
}
getchar();
}
void Student::seek() //查找学生的信息
{
int i=0;
struct Student_Node *p1=head->next;
int j;
cout<<"\t\t\t\t|--------------------------|"<<endl;
cout<<"\t\t\t\t| |"<<endl;
cout<<"\t\t\t\t|【1】请输入学生的学号查询 |"<<endl;
cout<<"\t\t\t\t|【2】请输入学生的姓名查询 |"<<endl;
cout<<"\t\t\t\t| |"<<endl;
cout<<"\t\t\t\t|--------------------------|"<<endl;
cout<<"\t\t\t\t";
cin>>j;
switch(j)
{
case 1:
int a;
cout<<"\t\t\t\t请输入学生的学号:"<<endl<<"\t\t\t\t";
cin>>a;
while(p1!=NULL)
{
if(p1->num==a)
{
cout<<"\t\t\t\t该学生的学号:\t\t\t\t";
cout<<p1->num<<endl;
cout<<"\t\t\t\t该学生的年龄:\t\t\t\t";
cout<<p1->age<<endl;
cout<<"\t\t\t\t该学生的姓名:\t\t\t\t";
cout<<p1->name<<endl;
cout<<"\t\t\t\t该学生的性别:\t\t\t\t";
cout<<p1->sex<<endl;
cout<<"\t\t\t\t该学生的专业:\t\t\t\t";
cout<<p1->major<<endl;
cout<<"\t\t\t\t该学生的出生日期:\t\t\t";
cout<<p1->born<<endl;
cout<<"\t\t\t\t该学生的家庭住址:\t\t\t";
cout<<p1->address<<endl;
cout<<"\t\t\t\t该学生的英语成绩:\t\t\t";
cout<<p1->English<<endl;
i++;
}
p1=p1->next;
}
if(i==0)
{
cout<<"\t\t\t\t查询错误!!!"<<endl;
cout<<"\t\t\t\t没有此学生的数据!!!"<<endl;
}
break;
case 2:
char xingming[20];
cout<<"\t\t\t\t请输入学生的姓名:"<<endl;
cout<<"\t\t\t\t";
cin>>xingming;
while(p1!=NULL)
{
if(strcmp(p1->name,xingming)==0)
{
cout<<"\t\t\t\t该学生的学号:\t\t\t\t";
cout<<p1->num<<endl;
cout<<"\t\t\t\t该学生的年龄:\t\t\t\t";
cout<<p1->age<<endl;
cout<<"\t\t\t\t该学生的姓名:\t\t\t\t";
cout<<p1->name<<endl;
cout<<"\t\t\t\t该学生的性别:\t\t\t\t";
cout<<p1->sex<<endl;
cout<<"\t\t\t\t该学生的专业:\t\t\t\t";
cout<<p1->major<<endl;
cout<<"\t\t\t\t该学生的出生日期:\t\t\t";
cout<<p1->born<<endl;
cout<<"\t\t\t\t该学生的家庭住址:\t\t\t";
cout<<p1->address<<endl;
cout<<"\t\t\t\t该学生的英语成绩:\t\t\t";
cout<<p1->English;
i++;
}
p1=p1->next;
}
if(i==0)
{
cout<<"\t\t\t\t查询错误!!!"<<endl;
cout<<"\t\t\t\t没有此学生的数据!!!"<<endl;
}
break;
default:
cout<<"\t\t\t\t信息错误!!!"<<endl;
}
getchar();
}
void Student::insert() //插入学生的信息
{
struct Student_Node *p1,*p2;
struct Student_Node *p3;
int i;
cout<<"\t\t\t\t请输入要在哪个学号之后插入学生信息:"<<endl<<"\t\t\t\t";
cin>>i;
p1=new Student_Node;
cout<<"\t\t\t\t请输入待插入学生的学号:"<<endl<<"\t\t\t\t";
cin>>p1->num;
cout<<"\t\t\t\t请输入待插入学生的年龄:"<<endl<<"\t\t\t\t";
cin>>p1->age;
cout<<"\t\t\t\t请输入待插入学生的姓名:"<<endl<<"\t\t\t\t";
cin>>p1->name;
cout<<"\t\t\t\t请输入待插入学生的性别:"<<endl<<"\t\t\t\t";
cin>>p1->sex;
cout<<"\t\t\t\t请输入待插入学生的专业:"<<endl<<"\t\t\t\t";
cin>>p1->major;
cout<<"\t\t\t\t请输入待插入学生的出生日期:"<<endl<<"\t\t\t\t";
cin>>p1->born;
cout<<"\t\t\t\t请输入待插入学生的英语成绩:"<<endl<<"\t\t\t\t";
cin>>p1->English;
cout<<"\t\t\t\t请输入待插入学生的家庭住址:"<<endl<<"\t\t\t\t";
cin>>p1->address;
p2=head->next;
p3=head;
while(p2)
{
p3=p2;
if(p3->num==i)
{
break;
}
p2=p2->next;
}
p1->next=p3->next;
p3->next=p1;
getchar();
}
void Student::rewrite()//修改学生的信息
{
struct Student_Node *p1=head->next;
struct Student_Node *p2=head->next;
cout<<"\t\t\t\t请输入需要修改的学生的学号:"<<endl;
int i;
int j=0;
int k=1;
cout<<"\t\t\t\t";
cin>>i;
while(p1)
{
if(p1->num==i)//对是否有该生的信息进行判断
{
j=1;
}
p1=p1->next;
}
if(j==0)
{
cout<<"\t\t\t\t错误!!!"<<endl;
cout<<"\t\t\t\t没有该生的信息!!!"<<endl;
}
if(j==1)
{
p1=head->next;
while(p1)
{
if(p1->num==i)
{
cout<<"\t\t\t\t-----------------------------"<<endl;
cout<<"\t\t\t\t|---【1】修改学生的学号------"<<endl;
cout<<"\t\t\t\t|---【2】修改学生的姓名------"<<endl;
cout<<"\t\t\t\t|---【3】修改学生的性别------"<<endl;
cout<<"\t\t\t\t|---【4】修改学生的专业------"<<endl;
cout<<"\t\t\t\t|---【5】修改学生的出生日期--"<<endl;
cout<<"\t\t\t\t|---【6】修改学生的家庭住址--"<<endl;
cout<<"\t\t\t\t|---【7】修改学生的英语成绩--"<<endl;
cout<<"\t\t\t\t|---【8】修改学生的年龄------"<<endl;
cout<<"\t\t\t\t|----------------------------"<<endl;
int x;
cout<<"\t\t\t\t";
cin>>x;
switch(x)
{
case 1:
cout<<"\t\t\t\t请输入修改后的学号:"<<endl;
int y;
cout<<"\t\t\t\t";
cin>>y;
while(p2)
{
if(p2->num==y)
{
cout<<"\t\t\t\t出错!!!"<<endl;
cout<<"\t\t\t\t学号重复!!!"<<endl;
break;
k=0;
}
p2=p2->next;
}
if(k!=0)
{
p1->num=y;
}
break;
case 2:
cout<<"\t\t\t\t请输入姓名:"<<endl<<"\t\t\t\t";
cin>>p1->name;
break;
case 3:
cout<<"\t\t\t\t请输入性别:"<<endl<<"\t\t\t\t";
cin>>p1->sex;
break;
case 4:
cout<<"\t\t\t\t请输入专业:"<<endl<<"\t\t\t\t";
cin>>p1->major;
break;
case 5:
cout<<"\t\t\t\t请输入出生日期:"<<endl<<"\t\t\t\t";
cin>>p1->born;
break;
case 6:
cout<<"\t\t\t\t请输入家庭住址:"<<endl<<"\t\t\t\t";
cin>>p1->address;
break;
case 7:
cout<<"\t\t\t\t请输入英语成绩:"<<endl<<"\t\t\t\t";
cin>>p1->English;
break;
case 8:
cout<<"\t\t\t\t请输入年龄:"<<endl<<"\t\t\t\t";
cin>>p1->age;
break;
}
break;
}
}
}
getchar();
}
void Student::sort()// 按照英语成绩排序
//由高分到低分排列
{
int i=1;
if(head->next==NULL)
{
i=0;
}
if(i==0)
{
cout<<"\t\t\t\t出错!!!"<<endl;
cout<<"\t\t\t\t没有找到学生的数据!!!"<<endl;
}
if(i==1)
{
struct Student_Node *p1,*p2;
int num_1;
int age_1;
char name_1[20];
char sex_1[10];
char major_1[10];
char born_1[10];
char address_1[10];
int English_1;
int num_2;
int age_2;
char name_2[20];
char sex_2[10];
char major_2[10];
char born_2[10];
char address_2[10];
int English_2;
for(p1=head->next;p1!=NULL;p1=p1->next)
{
for(p2=p1->next;p2!=NULL;p2=p2->next)
{
if(p1->English<p2->English)
{
num_1=p1->num;
age_1=p1->age;
strcpy(name_1,p1->name);
strcpy(sex_1,p1->sex);
strcpy(major_1,p1->major);
strcpy(born_1,p1->born);
strcpy(address_1,p1->address);
English_1=p1->English;
num_2=p2->num;
age_2=p2->age;
strcpy(name_2,p2->name);
strcpy(sex_2,p2->sex);
strcpy(major_2,p2->major);
strcpy(born_2,p2->born);
strcpy(address_2,p2->address);
English_2=p2->English;
p1->num=num_2;
p1->age=age_2;
strcpy(p1->name,name_2);
strcpy(p1->sex ,sex_2);
strcpy(p1->major ,major_2);
strcpy(p1->born ,born_2);
strcpy(p1->address ,address_2);
p1->English=English_2;
p2->num=num_1;
p2->age=age_1;
strcpy(p2->name,name_1);
strcpy(p2->sex,sex_1);
strcpy(p2->major,major_1);
strcpy(p2->born,born_1);
strcpy(p2->address,address_1);
p2->English=English_1;
}
}
}
cout<<"\t\t\t\tWonderful*****排序成功*****"<<endl;
}getchar();
}
void Student::show()//显示全部学生信息
{
struct Student_Node *p1=head->next;int i=1;
if(p1==NULL)
{
i=0;
}
if(i==0)
{
cout<<"\t\t\t\t出错!!!"<<endl;
cout<<"\t\t\t\t没有找到学生的信息!!!"<<endl;
}
if(i=1)
{
while(p1)
{
cout<<endl;
cout<<"学生的学号:\t\t\t"<<p1->num<<endl;
cout<<"学生的年龄:\t\t\t"<<p1->age<<endl;
cout<<"学生的姓名:\t\t\t"<<p1->name<<endl;
cout<<"学生的性别:\t\t\t"<<p1->sex<<endl;
cout<<"学生的专业:\t\t\t"<<p1->major<<endl;
cout<<"学生的出生日期:\t\t\t"<<p1->born<<endl;
cout<<"学生的家庭住址:\t\t\t"<<p1->address<<endl;
cout<<"学生的英语成绩:\t\t\t"<<p1->English;
p1=p1->next;
}
}getchar();
}
void Student::getfile()//将学生的信息写入文件之中
{
ofstream outfile(".studentlist.txt");
if(!outfile)
{
cout<<"打开文件失败!"<<endl;return ;
}
struct Student_Node *p1;
p1=head->next;
while(p1)
{
outfile<<"学号:"<<p1->num<<endl;
outfile<<"年龄:"<<p1->age<<endl;
outfile<<"姓名:"<<p1->name<<endl;
outfile<<"性别:"<<p1->sex<<endl;
outfile<<"专业:"<<p1->major<<endl;
outfile<<"出生日期:"<<p1->born<<endl;
outfile<<"家庭地址:"<<p1->address<<endl;
outfile<<"英语成绩:"<<p1->English<<endl;
p1=p1->next;
}
outfile.close();
cout<<"保存成功!"<<endl;
getchar();
}
void Student::putfile()//将学生的信息显示在终端上
{
FILE *fp;
char str[1024];
fp=fopen(".studentlist.txt","r");
if(!fp)
{
return ;
}
while(fgets(str,1024,fp))
{
cout<<str;
}
fclose(fp);
cout<<endl<<endl;
system("pause");
}
void Student::menu()
{
cout<<"\t\t\t\t------------------------------------"<<endl;
cout<<"\t\t\t\t| 主菜单 |"<<endl;
cout<<"\t\t\t\t| 【1】创建学生的信息 |"<<endl;
cout<<"\t\t\t\t| 【2】增添学生信息 |"<<endl;
cout<<"\t\t\t\t| 【3】删除学生信息 |"<<endl;
cout<<"\t\t\t\t| 【4】查找学生的信息 |"<<endl;
cout<<"\t\t\t\t| 【5】插入学生的信息 |"<<endl;
cout<<"\t\t\t\t| 【6】显示学生的信息 |"<<endl;
cout<<"\t\t\t\t| 【7】修改学生的信息 |"<<endl;
cout<<"\t\t\t\t| 【8】按照学生的英语成绩进行排序 |"<<endl;
cout<<"\t\t\t\t| 【9】将录入的学生的信息进行保存 |"<<endl;
cout<<"\t\t\t\t| 【10】将所保存的学生的信息进行读取|"<<endl;
cout<<"\t\t\t\t| 【11】退出新生基本信息统计软件 |"<<endl;
cout<<"\t\t\t\t| 【12】统计学生的信息 |"<<endl;
cout<<"\t\t\t\t------------------------------------|"<<endl;
cout<<"\t\t\t\t请输入您的选择:"<<endl<<"\t\t\t\t";
}
void start_page()
{
cout<<"\t\t\t\t|------------------------------------|"<<endl;
cout<<"\t\t\t\t|------------------------------------|"<<endl;
cout<<"\t\t\t\t|------------------------------------|"<<endl;
cout<<"\t\t\t\t|------------------------------------|"<<endl;
cout<<"\t\t\t\t|------------------------------------|"<<endl;
cout<<"\t\t\t\t|欢迎您使用新生基本信息统计软件!!!|"<<endl;
cout<<"\t\t\t\t|------------------------------------|"<<endl;
cout<<"\t\t\t\t|------------------------------------|"<<endl;
cout<<"\t\t\t\t|------------------------------------|"<<endl;
cout<<"\t\t\t\t|------------------------------------|"<<endl;
cout<<"\t\t\t\t请按Enter进行下一步!!!"<<endl<<"\t\t\t\t";
getchar();
system("cls");
}
void selection_function()
{
Student *p=new Freshman();
while(1)
{
p->menu();int i;cin>>i;
switch(i)
{
case 1: p->create();break;
case 2: p->add();break;
case 3: p->pass();break;
case 4: p->seek();break;
case 5: p->insert();break;
case 6: p->show();break;
case 7: p->rewrite();break;
case 8: p->sort();break;
case 9: p->getfile();break;
case 10: p->putfile();break;
case 11: cout<<"\t\t\t\t已退出,谢谢您的使用!!!"<<endl;break;
case 12: p->count();break;
}
if(i==11)
{
break;
}
cout<<"\t\t\t\t"<<endl<<endl;
cout<<"点击Enter键进行下一步";
getchar();
system("cls");
}
}
void login()
{
char username[10],password[10];
cout<<"\t\t\t\t欢迎您进入新生基本信息统计软件的注册界面!"<<endl<<endl;
cout<<"\t\t\t\t【0】我已经注册过了!"<<endl<<endl;
cout<<"\t\t\t\t【1】未注册,现在注册!"<<endl<<endl;
cout<<"\t\t\t\t请按Entet键继续......";
getchar(); int i;
cout<<"\t\t\t\t"<<"请输入......"; cin>>i;
if(i)
{
cout<<"\t\t\t\t"<<"欢迎新用户注册!"<<endl<<endl;
cout<<"\t\t\t\t"<<"请输入您的用户名:";
cin>>username;
cout<<"\t\t\t\t"<<"请输入您的密码:";
cin>>password;
system("pause");
system("cls");
ofstream regist(".login.txt");
regist<<username;
regist<<password;
regist.close();
cout<<"注册成功!"<<endl;
getchar();
system("cls");
}
cout<<"\t\t\t\t"<<"请输入您的用户名:";
cin>>username;
cout<<"\t\t\t\t"<<"请输入您的密码:";
cin>>password;
FILE *fp;
char str[20];
fp=fopen(".login.txt","r");
if(!fp)
{
return ;
}
while(fgets(str,20,fp))
{
}
fclose(fp);
if(strcmp(strcat(username,password),str)==0)
{
selection_function();
}
else
{
cout<<"\t\t\t\t账号或密码错误,请输入正确的账号和密码!!!"<<endl;
cout<<"\t\t\t\t为了保证您输入的信息的完整性,请您正常退出!!!"<<endl;
}
getchar();
}
int main()
{
system("color 1f");
start_page();
login();
return 0;
}
更多推荐
已为社区贡献1条内容
所有评论(0)