C++課程設計—學生信息管理系統
雖然老套,但是很經典,於是就把自己寫的代碼放上面了,以後或許還能看到當初的幼稚
代碼下載地址在文章末尾,有需要的同學自取。
該課程設計含有兩個模塊:
- 教師權限模塊
- 學生權限模塊
學生權限模塊含有:
1 .增加 2 .顯示 3.追加 4.查找 學生信息功能
教師權限模塊含有:
1.增加 2.顯示 3.追加 4.查找 5 .修改 6.刪除 學生信息功能
用到的主要知識:
( 1 )類 ( 2 )繼承( 3 )結構體( 4 )循環( 5 )數組( 6 )函數
實現各功能模塊的主線:
- 輸入密碼,進入不同權限的菜單界面
- 進入菜單界面,做出不同的選擇,調用相關函數,進入不同功能模塊
- 進行不同的功能模塊,進行操作之後,實現相應功能
- 退出系統
缺點:
- 首先必須輸入一些學生信息,才能進行各種操作
- 沒有添加文件,將學生信息保存入文件
- 不能動態實現學生信息的輸入,浪費內存空間
**【文章福利】:**小編推薦自己的C語言交流羣:967051845!整理了一些個人覺得比較好的學習書籍、視頻資料共享在羣文件裏面,有需要的可以自行添加哦!~
代碼如下:
<strong>#include<iostream>
#include<cstring>
#include<string>
#include<fstream>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<conio.h>
#include<windows.h>
using namespace std;
class student
{
private:
long long num;
char name[20];
char sex[6];
int age;
char phone[20];
public:
static int NUM;
student() {}
~student() {}
void input();
friend void readin();
friend void show();
friend void mood();
friend void del();
friend void soort();
friend void save();
friend void findyou();
friend void clear1();
} zhuo_yue[100];
int student::NUM=0;
void readin()
{
ifstream in("inf.txt",ios::binary);
int i=1;
while(!in.eof())
{
in.read((char*) &zhuo_yue[i],sizeof(zhuo_yue[i]));
i++;
}
in.close();
student::NUM=i-2;
}
void show()
{
if(student::NUM==0)
cout<<endl<<endl<<setw(10)<<"暫無學生信息";
else
{
cout<<"學生信息顯示"<<endl<<endl;
cout<<"學號"<<setw(15)<<"姓名"<<setw(15)
<<"性別"<<setw(15)<<"年齡"<<setw(15)<<"電話"
<<endl;
for(int i=1; i<=student::NUM; i++)
{
cout<<zhuo_yue[i].num<<setw(15)<<zhuo_yue[i].name<<setw(15)
<<zhuo_yue[i].sex<<setw(15)<<zhuo_yue[i].age<<setw(15)<<zhuo_yue[i].phone
<<endl;
}
}
cout<<endl<<setw(40)<<"按任意鍵退出";
getch();
}
void mood()
{
long long L;
cout<<"請輸入欲修改的學號:";
cin>>L;
cout<<endl<<setw(40)<<"確認修改(y/n)";
if(getch()=='y')
{
system("cls");
for(int i=1; i<=student::NUM; i++)
if(L==zhuo_yue[i].num)
{
cout<<"學生信息修改"<<endl<<endl;
cout<<"學號:";
cin>>zhuo_yue[i].num;
cout<<endl<<endl;
cout<<"姓名:";
cin>>zhuo_yue[i].name;
cout<<endl<<endl;
cout<<"性別:";
cin>>zhuo_yue[i].sex;
cout<<endl<<endl;
cout<<"年齡:";
cin>>zhuo_yue[i].age;
cout<<endl<<endl;
cout<<"電話:";
cin>>zhuo_yue[i].phone;
cout<<endl<<endl;
break;
}
cout<<endl<<setw(40)<<"已修改,按任意鍵退出";
getch();
}
else
return;
}
void del()
{
int sum=0;
cout<<setw(50)<<"[1]按學號刪除 [2]按姓名刪除"<<endl;
if(getch()=='1')
{
cout<<"請輸入學號:";
long long it;
cin>>it;
cout<<endl<<setw(30)<<"您確定刪除此學生嗎?(y/n)";
if(getch()=='y')
{
system("cls");
for(int i=1; i<=student::NUM; i++)
{
if(zhuo_yue[i].num==it)
{
for(int j=i+1; j<=student::NUM; j++)
zhuo_yue[j-1]=zhuo_yue[j];
student::NUM--;
sum++;
break;
}
}
cout<<"共刪除"<<sum<<"人";
}
else
return;
}
else if(getch()=='2')
{
char name1[20];
cout<<"請輸入姓名:";
cin>>name1;
cout<<endl<<setw(30)<<"您確定刪除此學生嗎?(y/n)";
if(getch()=='y')
{
system("cls");
for(int i=1; i<=student::NUM; i++)
{
if(strcmp(zhuo_yue[i].name,name1)==0)
{
for(int j=i+1; j<=student::NUM; j++)
zhuo_yue[j-1]=zhuo_yue[j];
student::NUM--;
sum++;
}
}
cout<<"共刪除"<<sum<<"人";
}
else
return;
}
cout<<endl<<endl<<setw(40)<<"按任意鍵結束";
getch();
}
void soort()
{
cout<<setw(50)<<"[1]按學號排序 [2]按年齡排序"<<endl;
if(getch()=='1')
{
for(int i=1; i<student::NUM; i++)
for(int j=1; j<student::NUM-i+1; j++)
{
if(zhuo_yue[j].num>zhuo_yue[j+1].num)
{
student it=zhuo_yue[j];
zhuo_yue[j]=zhuo_yue[j+1];
zhuo_yue[j+1]=it;
}
}
cout<<endl<<setw(50)<<"已排序,按任意鍵退出";
getch();
}
else if(getch()=='2')
{
for(int i=1; i<student::NUM; i++)
for(int j=1; j<student::NUM-i+1; j++)
{
if(zhuo_yue[j].age>zhuo_yue[j+1].age)
{
student it=zhuo_yue[j];
zhuo_yue[j]=zhuo_yue[j+1];
zhuo_yue[j+1]=it;
}
}
cout<<endl<<setw(50)<<"已排序,按任意鍵退出";
getch();
}
}
void student::input()
{
LI:
NUM++;
cout<<"學生信息輸入"<<endl<<endl;
cout<<"學號:";
cin>>zhuo_yue[NUM].num;
cout<<endl<<endl;
cout<<"姓名:";
cin>>zhuo_yue[NUM].name;
cout<<endl<<endl;
cout<<"性別:";
cin>>zhuo_yue[NUM].sex;
cout<<endl<<endl;
cout<<"年齡:";
cin>>zhuo_yue[NUM].age;
cout<<endl<<endl;
cout<<"電話:";
cin>>zhuo_yue[NUM].phone;
cout<<endl<<endl;
cout<<"已輸入,是否繼續 (y/n)";
if(getch()=='y')
{
system("cls");
goto LI;
}
else return;
}
void save()
{
ofstream out("inf.txt",ios::binary);
cout<<endl<<setw(40)<<"已保存,按任意鍵退出";
for(int i=1; i<=student::NUM; i++)
{
out.write((char*)&zhuo_yue[i],sizeof(zhuo_yue[i]));
}
out.close();
getch();
}
void findyou()
{
int sum=0;
cout<<setw(50)<<"[1]按學號查找 [2]按姓名查找"<<endl;
if(getch()=='1')
{
cout<<"請輸入學號:";
long long it;
cin>>it;
cout<<endl<<setw(30)<<"您確定查看此學生嗎?(y/n)";
if(getch()=='y')
{
system("cls");
cout<<"查看學生信息"<<endl<<endl;
for(int i=1; i<=student::NUM; i++)
{
if(zhuo_yue[i].num==it)
{
cout<<setw(20)<<"學號:"<<zhuo_yue[i].num<<endl
<<setw(20)<<"姓名:"<<zhuo_yue[i].name<<endl
<<setw(20)<<"性別:"<<zhuo_yue[i].sex<<endl
<<setw(20)<<"年齡:"<<zhuo_yue[i].age<<endl
<<setw(20)<<"電話:"<<zhuo_yue[i].phone<<endl;
cout<<"---------------------------------------"<<endl<<endl;
sum++;
}
}
cout<<"共顯示"<<sum<<"人";
}
else
return;
}
else if(getch()=='2')
{
char name1[20];
cout<<"請輸入姓名:";
cin>>name1;
cout<<endl<<setw(30)<<"您確定查看具有此姓名的學生嗎?(y/n)";
if(getch()=='y')
{
system("cls");
cout<<"查看學生信息"<<endl<<endl;
for(int i=1; i<=student::NUM; i++)
{
if(strcmp(zhuo_yue[i].name,name1)==0)
{
cout<<setw(20)<<"學號:"<<zhuo_yue[i].num<<endl
<<setw(20)<<"姓名:"<<zhuo_yue[i].name<<endl
<<setw(20)<<"性別:"<<zhuo_yue[i].sex<<endl
<<setw(20)<<"年齡:"<<zhuo_yue[i].age<<endl
<<setw(20)<<"電話:"<<zhuo_yue[i].phone<<endl;
cout<<"---------------------------------------"<<endl<<endl;
sum++;
}
}
cout<<"共顯示"<<sum<<"人";
}
else
return;
}
cout<<endl<<endl<<setw(40)<<"按任意鍵結束";
getch();
}
char mainmenu()
{
cout<<"\n\n 歡迎進入學生信息管理系統 "<<endl<<endl;
cout<<"\n\n [1]管理員 [2]學生"<<endl<<endl;
char ff=getch();
return ff;
}
void adminmainmenu()
{
cout<<"\n\n 歡迎進入學生信息管理系統(管理者) "<<endl<<endl
<<endl
<<endl
<<" 1: 錄入學生信息 "<<endl<<endl
<<" 2: 顯示學生信息 "<<endl<<endl
<<" 3: 修改學生信息 "<<endl<<endl
<<" 4: 刪除學生信息 "<<endl<<endl
<<" 5: 查找學生信息 "<<endl<<endl
<<" 6: 保存學生信息 "<<endl<<endl
<<" 7: 排序學生信息"<<endl<<endl
<<" 8: 返回初始頁面"<<endl<<endl
<<" 0: 退出系統"<<endl;
}
void mainmenu1()
{
cout<<"\n\n 歡迎進入學生信息管理系統(學生) "<<endl<<endl
<<endl
<<endl
<<" 1: 顯示學生信息 "<<endl<<endl
<<" 2: 查找學生信息 "<<endl<<endl
<<" 3: 排序學生信息"<<endl<<endl
<<" 4: 返回初始頁面"<<endl<<endl
<<" 0: 退出系統"<<endl;
}
void clear1()
{
fstream cl;
cl.open("inf.txt",ios::out);
cl.close();
cout<<"銷燬成功";
exit(1);
}
int main()
{
LIIIII:
readin();
system("cls");
char get = mainmenu();
if(get=='1')
{
char adminname[20],mima[20];
ifstream in;
ofstream out;
in.open("admin.txt",ios::in);
if(!in)
{
cout<<"無管理員賬號,請先設定:"<<endl<<endl;
FF:
cout<<"輸入賬號名:";
cin>>adminname;
cout<<"輸入密碼:";
cin>>mima;
cout<<"確定創建(y/n)";
char fff=getch();
if(fff=='y')
{
out.open("admin.txt",ios::app);
out<<adminname<<' '<<mima<<' ';
system("cls");
cout<<" 已創建,按任意鍵返回";
getch();
out.close();
goto LIIIII;
}
else if(fff=='n')
goto LIIIII;
}
else
{
system("cls");
cout<<" 已存在用戶"<<endl<<endl;
cout<<"是否新建管理員賬戶(y/n)";
char gr=getch();
if(gr=='y')
{
system("cls");
goto FF;
}
ss1:
system("cls");
cout<<"登錄賬號:";
cin>>adminname;
cout<<"輸入密碼:";
cin>>mima;
char adminname1[20],mima1[20];
int flog=0;
while(!in.eof())
{
in>>adminname1>>mima1;
if(!strcmp(adminname,adminname1)&&!strcmp(mima1,mima))
{
flog=0;
break;
}
}
if(flog==0)
{
system("cls");
cout<<" 登陸成功 按任意鍵繼續";
getch();
}
else
{
cout<<"密碼錯誤"<<endl<<endl;
cout<<"[1]返回初始界面 [2]重新輸入密碼";
char gg=getch();
if(gg=='1')
goto LIIIII;
else
{
system("cls");
goto ss1;
}
}
}
while(1)
{
adminmainmenu();
char ch;
ch=getchar();
system("CLS");
switch(ch)
{
case '1':
zhuo_yue[student::NUM].input();
break;
case '2':
show();
break;
case '3':
mood();
break;
case '4':
del();
break;
case '5':
findyou();
break;
case '6':
save();
break;
case '7':
soort();
break;
case '8':
goto LIIIII;
case 'F':
clear1();
case '0':
cout<<"\n\n\n\n 謝謝使用~~";
exit(1);
}
}
}
else if(get=='2')
{
char stuname[20],stumima[20];
ifstream in;
ofstream out;
in.open("student.txt",ios::in);
if(!in)
{
cout<<"無學生賬號,請先設定:"<<endl<<endl;
LL:
cout<<"輸入賬號名:";
cin>>stuname;
cout<<"輸入密碼:";
cin>>stumima;
cout<<"確定創建(y/n)";
char fff=getch();
if(fff=='y')
{
out.open("student.txt",ios::app);
out<<stuname<<' '<<stumima<<' ';
system("cls");
cout<<" 已創建,按任意鍵返回";
getch();
out.close();
goto LIIIII;
}
else if(fff=='n')
goto LIIIII;
}
else
{
system("cls");
cout<<" 已存在用戶"<<endl<<endl;
cout<<"是否新建賬戶(y/n)";
char gr=getch();
if(gr=='y')
{
system("cls");
goto LL;
}
sss:
system("cls");
cout<<"登錄賬號:";
cin>>stuname;
cout<<"輸入密碼:";
cin>>stumima;
char stuname1[20],stumima1[20];
int flog=1;
while(!in.eof())
{
in>>stuname1>>stumima1;
if(!strcmp(stuname,stuname1)&&!strcmp(stumima1,stumima))
{
flog=0;
break;
}
}
if(flog==0)
{
system("cls");
cout<<" 登陸成功 按任意鍵繼續";
getch();
}
else if(flog==1)
{
system("cls");
cout<<"密碼錯誤"<<endl<<endl;
cout<<"[1]返回初始界面 [2]重新輸入密碼";
char gg=getch();
if(gg=='1')
goto LIIIII;
else
{
system("cls");
goto sss;
}
}
in.close();
}
while(1)
{
mainmenu1();
char ch;
ch=getchar();
system("CLS");
switch(ch)
{
case '1':
show();
break;
case '2':
findyou();
break;
case '3':
soort();
break;
case 'F':
clear1();
case '4':
goto LIIIII;
case '0':
cout<<"\n\n\n\n 謝謝使用~~";
exit(1);
}
}
}
}
</strong>
福利
最後,如果覺得學習資料難找的話,可以添加小編的C語言/C++交流羣:967051845! 學習資料已經共享在羣裏了,期待你的加入~