作者自述
这份代码没有加过多的注释,但难度系数并不高,只是在设计方面的优化和多次调用比较难点。
不墨迹了,直接上代码,在设计选课系统时问了很多学长学姐们,此处也感谢他们的大力支持。
代码
#include<iostream>
#include<fstream>
#include<cstring>
#include <conio.h>
#include<iomanip>
#include<stdio.h>
using namespace std;
void menu();
void student1();
void student2();
void student3();
void student4();
void teacher();
void teacher1();
void teacher2();
void teacher3();
int tpassword();
int spassword();
void save();
void turn();
int u,d;
class course
{
public:
char cname[10];
course * next;
}*chead=NULL;
class student
{
public:
int number;
char name[10];
char password[7];
int cnumber;
char course[6][20];
student * next;
}*head=NULL;
void menu()// 菜单
{
menu:
system("cls");
cout<<endl<<endl;
cout<<"*******************************************\n"
<<"* *\n"
<<"* 选修课系统 *\n"
<<"* *\n"
<<"* *\n"
<<"* 操作方式: *\n"
<<"* 1.选修课系统学生端 *\n"
<<"* *\n"
<<"* 2.选修课系统教师端 *\n"
<<"* *\n"
<<"* 3.退出系统 *\n"
<<"*******************************************\n"<<endl;
while(1)
{
int a;
cout<<"\n\t\t请选择登入方式: ";
cin>>a;
if(a==1) {student1();goto menu;}
else if(a==2) {teacher();goto menu;}
else if(a==3) {save(); break;}
else cout<<"\t\t\t\t\t输入有误!";
}
}
void teacher() //老师菜单
{
int m;
m=tpassword();
if(m==1)
{
teacher1:
system("cls");
cout<<"\n\n\n"
<<"※※※※※※※※※※※※※※※※※※※※※※※※※\n"
<<"※ ※\n"
<<"※ 选修课系统教师端 ※\n"
<<"※ ※\n"
<<"※ 操作方式: ※\n"
<<"※ 1.增加学生 2.增加课程 ※\n"
<<"※ 3.选课情况 4.退出系统 ※\n"
<<"※ ※\n"
<<"※※※※※※※※※※※※※※※※※※※※※※※※※\n"<<endl;
while(1)
{
cout<<"\t\t\t请选择操作方式: ";
int a;cin>>a;
if(a==1)
{
teacher1();
cout<<" 是否返回上一界面(y/n)";
char q;cin>>q;
if(q=='y')goto teacher1;
else if(q=='n')break;
}
else if(a==2)
{
teacher2();
cout<<" 是否返回上一界面(y/n)";
char q;cin>>q;
if(q=='y')goto teacher1;
else if(q=='n')break;
}
else if(a==3)
{
teacher3();
cout<<" 是否返回上一界面(y/n)";
char q;cin>>q;
if(q=='y')goto teacher1;
else if(q=='n')break;
}
else if(a==4) {system("cls");break;}
else cout<<"\n\n\t\t\t\t输入有误!";
}
}
}
int tpassword()
{
int k=0,i;
char A[]="123456";
char B[10];
char a;
system("cls");
for(i=0;i<8;i++)
cout<<endl;
while(1)
{
cout<<"\t\t\t请输入教师密码:";
i=0;
while(1)
{
a=getch();
if(a==13)
break;
B[i]=a;
i++;
cout<<"*";
}
B[i]='\0';
if(strcmp(A,B)==0) return 1;
else
{
k++;
cerr<<"\n\n\t\t\t密码输入错误!请重新输入!\n"<<endl;
}
if(k==2) return 0;
}
}
void teacher1()//增加学生
{
ofstream outstuf;
cout<<"是否已存在学生信息:(y/n)";
char k;cin>>k;
if(k=='n')
{
student * s,* q;
s=new student;head=q=s;
if(u==1)outstuf.open("student1.txt",ios::out);
else if(u==2)outstuf.open("student2.txt",ios::out);
cout<<"\t输入想要增加的学生数:";
int k;cin>>k;
cout<<"\t输入学生的学号,姓名,初始密码,初始已选课程数及课程(请输入1 none)\n";
cin>>s->number>>s->name>>s->password>>s->cnumber>>s->course[0];
outstuf<<s->number<<'\t'<<setw(8)<<s->name<<'\t'<<s->password<<'\t'<<s->cnumber<<'\t'<<s->course[0]<<'\n';
int i=1;
for(i=1;i<k;i++)
{
s=new student;
cin>>s->number>>s->name>>s->password>>s->cnumber>>s->course[i];
outstuf<<s->number<<'\t'<<setw(8)<<s->name<<'\t'<<s->password<<'\t'<<s->cnumber<<'\t'<<s->course[i]<<'\n';
q->next=s;
q=s;
}
q->next=NULL;
outstuf.close();
}
else if(k=='y')
{
student * s,* q;
s=new student;
for(q=head;q->next!=NULL;q=q->next);q->next=s;s=q;
if(u==1)outstuf.open("student1.txt",ios::app);
else if(u==2)outstuf.open("student2.txt",ios::app);
cout<<"\t输入想要增加的学生数:";
int k;cin>>k;
cout<<"\t输入学生的学号,姓名,初始密码,初始已选课程数及课程(请输入1 1)\n";
cin>>s->number>>s->name>>s->password>>s->cnumber>>s->course[0];
outstuf<<s->number<<'\t'<<setw(8)<<s->name<<'\t'<<s->password<<'\t'<<s->cnumber<<'\t'<<s->course[0]<<'\n';
int i;
for(i=1;i<k;i++)
{
s=new student;
cin>>s->number>>s->name>>s->password>>s->cnumber>>s->course[i];
outstuf<<s->number<<'\t'<<setw(8)<<s->name<<'\t'<<s->password<<'\t'<<s->cnumber<<'\t'<<s->course[i]<<'\n';
q->next=s;
q=s;
}
q->next=NULL;
outstuf.close();
}
}
void teacher2()//增加课程
{
ofstream outstuf;
cout<<"是否已存在课程信息:(y/n)";
char k;cin>>k;
if(k=='n')
{
course * s,* q;
s=new course;chead=q=s;
if(u==1)outstuf.open("course1.txt",ios::out);
else if(u==2)outstuf.open("course2.txt",ios::out);
cout<<"\t输入想要增加的课程数:";
int k;cin>>k;
cout<<"\t输入课程名称:\n";
cin>>s->cname;
outstuf<<s->cname<<'\n';
for(int i=1;i<k;i++)
{
s=new course;
cin>>s->cname;
outstuf<<s->cname<<'\n';
q->next=s;
q=s;
}
q->next=NULL;
outstuf.close();
}
else if(k=='y')
{
course * s,* q;
s=new course;
for(q=chead;q->next!=NULL;q=q->next);q->next=s;q=s;
if(u==1)outstuf.open("course1.txt",ios::app);
else if(u==2)outstuf.open("course2.txt",ios::app);
cout<<"\t输入想要增加的课程数:";
int k;cin>>k;
cout<<"\t输入课程名称:\n";
cin>>s->cname;
outstuf<<s->cname<<'\n';
for(int i=1;i<k;i++)
{
s=new course;
cin>>s->cname;
outstuf<<s->cname<<'\n';
q->next=s;
q=s;
}
q->next=NULL;
outstuf.close();
}
}
void teacher3()//查询学生信息
{
student * p;p=head;
system("cls");cout<<"\n\n\n\n\n";
cout<<" "<<setw(5)<<"学号"<<" "<<setw(5)<<"姓名"<<" "<<setw(5)<<"课程"<<endl;
while(p!=NULL)
{
cout<<" "<<setw(5)<<p->number<<" "<<setw(5)<<p->name<<" ";
for(int i=0;i<p->cnumber;i++)cout<<p->course[i]<<" ";
cout<<endl;
p=p->next;
}
}
void student1()//学生登录
{
cout<<"\t\t\t请输入你的学号:";
int m, b;cin>>b;d=b-1;
m=spassword();
if(m==1)
{
student1:
system("cls");//清屏
cout<<"\n\n\n";
cout<<"※※※※※※※※※※※※※※※※※※※※※※※※※※\n"
<<"※ 选修课系统学生端 ※\n"
<<"※ ※\n"
<<"※ 操作方式: ※\n"
<<"※ 1、 学生选课 ※\n"
<<"※ 2、 密码修改 ※\n"
<<"※ 3、 选课结果 ※\n"
<<"※ 4、 返回上一层 ※\n"
<<"※ ※\n"
<<"※※※※※※※※※※※※※※※※※※※※※※※※※※\n"<<endl;
while(1)
{
int a,k=1;
cout<<"\t\t\t请选择操作方式: ";
cin>>a;
if(a==1)
{
student2();
cout<<" 是否返回上一界面(y/n)";
char q;cin>>q;
if(q=='y')goto student1;
else if(q=='n')break;
}
else if(a==2) {student3();goto student1;}
else if(a==3)
{
student4();
cout<<" 是否返回上一界面(y/n)";
char q;cin>>q;
if(q=='y')goto student1;
else if(q=='n')break;
}
else if(a==4){system("cls");break;}
else cout<<"\t\t\t\t\t输入有误!"<<endl;
}
}
}
int spassword()//判断密码是否正确
{
student * p;p=head;
for(int k=0;k<d;k++)
p=p->next;
int k=0;char B[7];char c[10];
gets(c);system("cls");
while(1)
{
cout<<"\n\n";
cout<<"\t\t请输入密码:";
gets(B);
if(strcmp(p->password,B)==0) return 1;
else
{
k++;cout<<"\n\n\t\t\t密码输入错误!请重新输入!";
if(k>=3) { system("cls");return 0;}
}
}
}
void student2()//选课
{
student * p;p=head;
for(int k=0;k<d;k++)
p=p->next;
system("cls");
course *q;
cout<<'\n\n\n';
cout<<"\t\t\t课程列表:\n";
for(q=chead;q!=NULL;q=q->next)
cout<<"\t\t\t"<<q->cname<<endl;
cout<<"\t\t\t输入你想选几门课程(最多五门): ";
cin>>p->cnumber;
cout<<"\t\t\t输入要选的课程:\n";
int i;
for(i=0;i<p->cnumber;i++)
{
cout<<"\t\t\t ";
cin>>p->course[i];
}
save();
}
void student3()//修改密码
{
student * p;p=head;
for(int k=0;k<d;k++)
p=p->next;
t:
system("cls");
cout<<"\n\n\n";
cout<<"请输入新密码:";
char q[7],w[7];
for(int i=0;i<6;i++)
cin>>q[i];
cout<<"\n请再输入一遍:";
for(int i=0;i<6;i++)
cin>>w[i];
while(1)
{
int k;
for(k=0;k<6;k++)
{
if(q[k]==w[k]){}
else break;
}
if(k==6)
{
int i=0;
while(q[i])
{p->password[i]=q[i];i++;}
p->password[6]='\0';
break;
}else goto t;
}save();
}
void student4()//查寻选课结果
{
student * p;p=head;
for(int k=0;k<d;k++)
p=p->next;
cout<<"\t\t已选课程:\n";
for(int i=0;i<p->cnumber;i++)
{
cout<<"\t\t ";
cout<<p->course[i]<<endl;
}
}
void save() //保存
{
ofstream outstuf1;
ofstream outstuf2;
if(u==1){outstuf1.open("student1.txt",ios::trunc);outstuf2.open("course1.txt",ios::trunc);}
if(u==2){outstuf1.open("student2.txt",ios::trunc);outstuf2.open("course2.txt",ios::trunc);}
student * s;
for(s=head;s!=NULL;s=s->next)
{
int i;
outstuf1<<s->number<<'\t'<<s->name<<'\t'<<s->password<<'\t'<<s->cnumber;
for(i=0;i<s->cnumber;i++) outstuf1<<'\t'<<s->course[i];
outstuf1<<'\n';
}
course * q;
q=chead;
for(q=chead;q!=NULL;q=q->next)
{outstuf2<<q->cname<<'\n';}
outstuf1.close();outstuf2.close();
}
void turn()//读入
{
ifstream instuf1;ifstream instuf2;
if(u==1){instuf1.open("student1.txt",ios::in);instuf2.open("course1.txt",ios::in);}
if(u==2){instuf1.open("student2.txt",ios::in);instuf2.open("course2.txt",ios::in);}
student * s,* q; int i;
while(!instuf1.eof())
{
s=new student;
if(head==NULL)
{
head=q=s;
instuf1>>s->number>>s->name>>s->password>>s->cnumber;
for(i=0;i<s->cnumber;i++)instuf1>>s->course[i];
}
else
{
s=new student;
instuf1>>s->number>>s->name>>s->password>>s->cnumber;
for(i=0;i<s->cnumber;i++)instuf1>>s->course[i];
q->next=s;
q=s;
}
}
q->next=NULL;
course * z,* x;
z=new course;chead=x=z;
instuf2>>z->cname;
while(!instuf2.eof())
{
z=new course;
instuf2>>z->cname;
x->next=z;
x=z;
}
x->next=NULL;
instuf1.close();instuf2.close();
}
int main()
{
//
cout<<endl<<endl;
cout<<"*******************************************\n"
<<"* *\n"
<<"* 选修课系统 *\n"
<<"* *\n"
<<"* 操作方式: *\n"
<<"* 1.一班 *\n"
<<"* *\n"
<<"* 2.二班 *\n"
<<"* *\n"
<<"* 3.退出系统 *\n"
<<"*******************************************\n"<<endl;
cout<<"操作选择: ";
cin>>u;
switch(u)
{
case 1:
{
cout<<endl;
cout<<" 是否为第一次使用:(y/n)";
char a;cin>>a;if(a=='n'){turn();menu();}
else if(a=='y')menu();
}break;
case 2:
{
cout<<endl;
cout<<" 是否为第一次使用:(y/n)";
char a;cin>>a;if(a=='n'){turn();menu();}
else if(a=='y')menu();
}break;
case 3:break;
}
}
喂!不要照抄,要参考!要在自己的代码上有更高的突破!