學生管理系統的源碼
Ⅰ c++課程設計源代碼
c++課程設計 學生管理系統源代碼
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
typedef struct stu
{
char name[20];
long int number;
int snum;
char sex[20];
char add[30] ;
char time[20];
char tel[20];
struct stu *next;
}stu,*student;
int init(student &s);
void insert(student &s);
void print(student &s);
int delete(student &s);
void find (student &s);
void findname(student &s);
void findnum(student &s);
void modifay(student &s);
void putfile(student &s);
void getfile(student &s);
void main()
{
student s;
init(s);
cout<<"歡迎使用學生管理系統."<<endl;
cout<<" "<<endl;
cout<<endl;
while(1)
{
int i;
mainint:
cout<<"請選擇相關操作:"<<endl<<"1.建立學生資料文件."<<endl<<"2.瀏覽."<<endl<<"3.刪除."<<endl;
cout<<"4.查詢."<<endl<<"5.修改."<<endl<<"6.打開學生文件."<<endl<<"7.退出管理系統." <<endl<<"請選擇:";
cin>>i;
if(i<0||i>7)
{
cout<<"輸入了錯誤的數字,Again!"<<endl<<endl;
goto mainint;
}
switch(i)
{
case 1: insert(s);break;
case 2: print(s);
putfile(s);
break;
case 3: ldelete(s);break;
case 4: find(s);break;
case 5: modifay(s);
break;
case 6: getfile(s);break;
case 7: exit(0);
}
}
}
int init(student &s)
{
s=(student)malloc(sizeof(stu));
if (s)
{
s->next=NULL;
return 0;
}
else return -1;
}
void insert(student &s) //insert
{
cout<<endl;
student p,q;
p=(student)malloc(sizeof(stu));
cout<<"請輸入學生信息:"<<endl;
cout<<"姓名:";
cin>>p->name;
cout<<endl;
cout<<"學號:";
cin>>p->number;
cout<<endl;
cout<<"性別:";
cin>>p->sex;
cout<<endl;
cout<<"家庭住址:" ;
cin>>p->add;
cout<<endl;
cout<<"出生年月:" ;
cin>>p->time;
cout<<endl;
cout<<"宿舍號:";
cin>>p->snum;
cout<<endl;
cout<<"宿舍號碼:";
cin>>p->tel;
q=s;
while(!(q->next==NULL)&&(q->next->number<p->number))
q=q->next;
p->next=q->next;
q->next=p;
//if()p->next=NULL;
//free(p);
}
int ldelete(student &s) //delete
{
cout<<endl;
student p,a;
p=s ;
cout<<"請輸入刪除的學號:";
long int number;
cin>>number;
while(p)
{ if (p->number==number)
{
a->next=p->next;
free(p); return(0);}
else
{ a=p;
p=p->next;} }
cout<<"沒有找到你要刪除的選項!"<<endl<<endl;return(-1);
}
void print(student &s)
{
int a;
a=0;
student p;
p=s->next;
cout<<"姓名 "<<"學號 " <<"性別 "<<"家庭住址 "<<"出生年月 "<<"宿舍號"<<"宿舍號碼"<<endl;
while(p)
{
a++;
cout<<p->name<<" "<<p->number<<" "<<p->sex<<" "<<p->add<<" "<<p->time<<" "<<p->snum<<" "<<p->tel<<" "<<endl;
p=p->next;
}
cout<<endl;
if (a==0)
cout<<"還沒有學生信息!"<<endl<<endl ;
}
void find (student &s) //find
{
findl:
cout<<"請選擇查找方法:"<<endl<<"1.按姓名查找."<<endl<<"2.按學號查找."<<endl;
cout<<"請選擇:";
int k;
cin>>k;
if(k<0||k>3)
{
cout<<"輸了入錯誤數字,Again!"<<endl<<endl;
goto findl;
}
switch(k)
{
case 1: findname(s);break;
case 2: findnum(s);break;
}
}
void findname(student &s) //find by name
{
student p;
p=s->next;
cout<<"請輸入姓名:";
char name[20];
int j;
j=0;
cin>>name;
cout<<"你要查找的資料是:"<<endl;
cout<<"姓名 "<<"學號 " <<"性別 "<<"家庭住址 "<<"出生日期 "<<"宿舍號 "<<"宿舍號碼"<<endl;
while(p)
{
if (strcmp(p->name,name)==0)
{
cout<<p->name<<" "<<p->number<<" "<<p->sex<<" "<<p->add<<" "<<p->time<<" "<<p->snum<<" "<<p->tel<<" "<<endl;
j++;
}
p=p->next;
}
cout<<endl;
if(j==0)
cout<<"對不起,沒找到你要的信息!"<<endl<<endl ;
}
void findnum(student &s) //find by number
{
student p;
p=s->next;
cout<<"請輸入學號:";
long int number;
int j;
j=0;
cin>>number;
cout<<"你要查找的資料是:"<<endl;
cout<<"姓名 "<<"學號 "<<"性別 "<<"家庭住址 "<<"出生日期 "<<"宿舍號 "<<"宿舍電話"<<endl;
while(p)
{
if (p->number==number)
{
cout<<p->name<<" "<<p->number<<" "<<p->sex<<" "<<p->add<<" "<<p->time<<" "<<p->snum<<" "<<p->tel<<" "<<endl;
j++;
}
p=p->next;
}
cout<<endl;
if(j==0)
cout<<"對不起,沒找到你要的信息"<<endl<<endl ;
}
void modifay(student &s) //modifay
{
student q,p,l,m;
int j;
j=0;
q=s->next;
l=s;
m=s;
cout<<"請輸入要修改的學號:" ;
long int num;
cin>>num;
cout<<"姓名 "<<"學號 "<<"性別 "<<"家庭住址 "<<"入學時間 "<<"宿舍號 "<<"電話號碼"<<endl;
while(q)
{
if (q->number==num)
{
cout<<"你要修改的信息是:"<<endl;
cout<<q->name<<" "<<q->number<<" "<<q->sex<<" "<<q->add<<" "<<q->time<<" "<<q->snum<<" "<<q->tel<<" "<<endl;
j++;
p=(student)malloc(sizeof(stu));
cout<<"請輸入新的學生信息:"<<endl;
cout<<"姓名:";
cin>>p->name;
cout<<endl;
cout<<"學號:";
cin>>p->number;
cout<<endl;
cout<<"年齡:";
cin>>p->age;
cout<<endl;
cout<<"性別:";
cin>>p->sex;
cout<<endl;
cout<<"家庭住址:" ;
cin>>p->add;
cout<<endl;
cout<<"入學時間:" ;
cin>>p->time;
cout<<endl;
cout<<"電話號碼:";
cin>>p->tel;
l->next=q->next;
free(q);
goto tt;
}
else
{
l=q;
q=q->next;
}
}
cout<<endl;
if (j==0) cout<<"沒找到你要的數據!"<<endl<<endl ;
tt:
while(!(m->next==NULL)&&(m->next->number<p->number))
m=m->next;
p->next=m->next;
m->next=p;
}
void putfile(student &s)
{
student p;
p=s->next;
FILE *fp;
if((fp=fopen("information.txt","w"))==NULL)
{
cout<<"打不開文件"<<endl<<endl;
exit(0);
}
while(p)
{
if(fwrite(p,sizeof(struct stu),1,fp)!=1 )
{
cout<<"文件寫入錯誤"<<endl<<endl;
return;
}
p=p->next;
}
fclose(fp) ;
}
void getfile(student &s)
{
student p,q;
q=s;
FILE *fp1 ;
if((fp1=fopen("information.txt","r"))==NULL)
{
cout<<"打不開文件"<<endl;
exit(0);
}
cout<<"姓名 "<<"學號 "<<"性別 "<<"家庭住址 "<<"入學時間 "<<"宿舍號 "<<"電話號碼"<<endl;
p=(student)malloc(sizeof(stu));
while(fread(p,sizeof(struct stu),1,fp1)!=0)
{
cout<<p->name<<" "<<p->number<<" "<<p->sex<<" "<<p->add<<" "<<p->time<<" "<<p->age<<" "<<p->tel<<endl;
while(!(q->next==NULL)&&(q->next->number<p->number))
q=q->next;
p->next=q->next;
q->next=p;
p=(student)malloc(sizeof(stu));
}
fclose(fp1);
cout<<endl;
}
Ⅱ 學生信息管理系統C++源代碼
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define INIT_SIZE 10
#define INCRE_SIZE 10
#define SUBJECT_NUM 3
#define LEN 3
void show_Start();
void show_Table();
void addRecord();
void Info_delete();
void deleteRecord();
void delete_Num(int);
void delete_Name(char tarName[]);
void Info_modify();
void modifyRecord();
void modify_Num(int);
void modify_Name(char[]);
void Info_query();
void queryRecord();
void query_Num(int);
void query_Name(char[]);
void display();
void quit();
void menu_CMD();
char *subject[SUBJECT_NUM] = {"高代","數分","c語言"};
struct STUDENT
{
int num;
char name[20];
char sex;
float score[SUBJECT_NUM];
};
//struct STUDENT stu[LEN + 1];
//STUDENT *record = (STUDENT*)malloc(sizeof(STUDENT)*INIT_SIZE);
int static stuNum = 0;
//先暫時定義三個學生吧...
STUDENT *record = (STUDENT*)malloc(sizeof(STUDENT)*INIT_SIZE);;
int main()
{
//record = (STUDENT*)malloc(sizeof(STUDENT)*INIT_SIZE);
//STUDENT *record = (STUDENT*)malloc(sizeof(STUDENT)*INIT_SIZE);
/*
record[1].num = 1001;
strcpy(record[1].name,"Jason");
record[1].sex = 'M';
record[1].score[0] = 85.0;
record[1].score[1] = 90.0;
record[1].score[2] = 95.0;
record[2].num = 1002;
strcpy(record[2].name,"Jerry");
record[2].sex = 'M';
record[2].score[0] = 85.0;
record[2].score[1] = 90.0;
record[2].score[2] = 95.0;
record[3].num = 1003;
strcpy(record[3].name,"Jessie");
record[3].sex = 'F';
record[3].score[0] = 85.0;
record[3].score[1] = 90.0;
record[3].score[2] = 95.0;
*/
/*
Info_modify();
int key;
cout<<"請輸入您的選擇 : ";
cin>>key;
if(key == 1)
{
int targetNum;
cout<<"請輸入您欲修改的學生的學號 : ";
cin>>targetNum;
modify_Num(targetNum);
cout<<endl;
display();
}
if(key == 2)
{
char targetName[20];
cout<<"請輸入您欲修改學生的姓名 : ";
cin>>targetName;
modify_Name(targetName);
cout<<endl;
display();
}
if(key == 3)
{
exit(0);
}
*/
show_Start();
menu_CMD();
return 0;
}
//修改完後還應該顯示
void show_Start()
{
//cout<<endl;
cout<<" **************************************** "<<endl;
cout<<" 這是一個 "<<endl;
cout<<" 學生成績管理系統 "<<endl;
cout<<" 可以對學生成績進行管理 "<<endl;
cout<<" 歡迎大家使用 "<<endl;
cout<<" Made by Jason "<<endl;
cout<<" **************************************** "<<endl;
}
// 顯示表頭信息,即是 : 學號,姓名,性別,高代,數分,C語言.
void show_Table()
{
cout<<"學號"<<"\t"<<"姓名"<<"\t"<<"性別";
cout<<"\t"<<subject[0]<<"\t"<<subject[1]<<"\t"<<subject[2];
cout<<endl;
}
void menu_CMD()
{
int key;
while(1)
{
cout<<"1. 增加學生信息"<<endl;
cout<<"2. 刪除學生信息"<<endl;
cout<<"3. 修改學生信息"<<endl;
cout<<"4. 查詢學生信息"<<endl;
cout<<"5. 顯示學生信息"<<endl;
cout<<"6. 退出"<<endl;
cout<<"請輸入您的選擇 : ";
cin>>key;
while(1)
{
if((key < 1)||(key > 6))
{
int key;
cout<<"您的輸入有誤,請重新輸入!"<<endl;
cout<<"請選(1 - 5) : ";
cin>>key;
}
else
{
break;
}
}
switch(key)
{
case 1:
addRecord();
break;
case 2:
deleteRecord();
break;
case 3:
modifyRecord();
break;
case 4:
queryRecord();
break;
case 5:
display();
break;
case 6:
quit();
break;
}
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//增加學生信息
void addRecord()
{
if(stuNum == 0)
{
cout<<"原來沒有記錄,現在建立新表!"<<endl;
stuNum++;
}
else
{
cout<<"現在在當前表的末尾添加新的信息!"<<endl;
stuNum++;
}
//如果數組空間不夠,重新申請空間
if(stuNum > INIT_SIZE)
{
cout<<"內存空間不夠,現在重新申請新的內存空間!"<<endl;
record = (STUDENT*)realloc(record,(INIT_SIZE + INCRE_SIZE)*sizeof(STUDENT));
cout<<"空間申請完成!"<<endl;
}
cout<<"您現在要添加一組新的信息,您確定嗎?"<<endl;
cout<<"請輸入您的選擇(Y/N) : ";
char choi;
cin>>choi;
if((choi == 'Y')||(choi == 'y'))
{
cout<<"請輸入學號 : ";
cin>>record[stuNum].num;
cout<<"請輸入姓名 : ";
cin>>record[stuNum].name;
cout<<"請輸入性別(M為男,F為女) : ";
cin>>record[stuNum].sex;
int i;
for(i = 0;i < SUBJECT_NUM;i++)
{
cout<<"請輸入"<<subject[i]<<"的成績 : ";
cin>>record[stuNum].score[i];
}
}
if((choi == 'N')||(choi == 'n'))
{
cout<<"退出添加新學生信息!"<<endl;
cout<<endl;
}
cout<<"現在已經有"<<stuNum<<"條學生的信息了!"<<endl;
cout<<endl;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//刪除信息 晚上完成...
//顯示deleteRecord的表頭信息
void Info_delete()
{
cout<<"請輸入刪除方式 : "<<endl;
cout<<"1. 按學號刪除"<<endl;
cout<<"2. 按姓名刪除"<<endl;
cout<<"3. 退出刪除"<<endl;
}
//刪除學生的信息,包含兩個子函數
void deleteRecord()
{
int key;
cout<<endl;
Info_delete();
cout<<"請輸入您的選擇 : ";
cin>>key;
if(key == 1)
{
int targetNum;
cout<<"請輸入您欲刪除學生的學號 : ";
cin>>targetNum;
//按學號刪除
delete_Num(targetNum);
cout<<endl;
}
if(key == 2)
{
char targetName[20];
cout<<"請輸入您欲刪除學生的姓名 : ";
cin>>targetName;
//按姓名刪除
delete_Name(targetName);
cout<<endl;
}
if(key == 3)
{
while(1)
{
menu_CMD();
}
}
}
//按學號刪除學生信息
//只用完成刪除操作,而不必輸出. 輸出的操作可以在主菜單中進行
void delete_Num(int tarNum)
{
int i;
for(i = 1;i <= stuNum;i++)
{
if(record[i].num == tarNum)
{
//刪除還要分兩種情況討論
//1. 欲刪除的學生信息是最後一位
//2. 欲刪除的學生信息不是最後一位
//第一種情況,欲刪除的學生是最後一位
if(i = stuNum)
{
cout<<"您所要刪除的學生信息是 : "<<endl;
show_Table();
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex<<"\t"
<<record[i].score[0]<<record[i].score[1]<<"\t"<<record[i].score[2];
cout<<endl;
cout<<endl<<"刪除後學生信息表為 : "<<endl;
show_Table();
for(int i = 1;i <= stuNum - 1;i++)
{
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex;
for(int j = 0;j < SUBJECT_NUM;j++)
{
cout<<"\t"<<record[i].score[j];
}
cout<<endl;
}
//顯示信息應該放在後面
/*
stuNum--;
cout<<"現在還剩下"<<stuNum<<"條學生的信息";
cout<<endl;
*/
}
//2.第二種情況,欲刪除的學生不是最後一位
if(i != stuNum)
{
cout<<"您所要刪除的學生信信息是 : "<<endl;
show_Table();
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex<<"\t"
<<record[i].score[0]<<"\t"<<record[i].score[1]<<"\t"<<record[i].score[2];
for(int j = i+1;j <= stuNum;j++)
{
record[j-1] = record[j];
}
//接著完成輸出
cout<<endl;
cout<<"刪除後學生信息表為 : "<<endl;
show_Table();
for(int i = 1;i <= stuNum-1;i++)
{
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex;
for(int j = 0;j < SUBJECT_NUM;j++)
{
cout<<"\t"<<record[i].score[j];
}
cout<<endl;
}
/*
stuNum--;
cout<<"現在還剩下"<<stuNum<<"條學生的信息";
cout<<endl;
*/
}
stuNum--;
cout<<"現在還是剩下"<<stuNum<<"條學生的信息";
cout<<endl;
}
}
}
/*
//方法同上
void delete_Name(char tarName[])
{
int i;
for(i = 1;i <= stuNum;i++)
{
if(strcmp(record[i].name,tarName) == 0)
{
//刪除還要分兩種情況討論
//1. 欲刪除的學生信息是最後一位
//2. 欲刪除的學生信息不是最後一位
//第一種情況 : 欲刪除學生是最後一位
if(i = stuNum)
{
cout<<"您所要刪除的學生信息是 : "<<endl;
show_Table();
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex<<"\t"
<<record[i].score[0]<<record[i].score[1]<<"\t"<<record[i].score[2];
cout<<endl;
cout<<endl<<"刪除後學生信息表為 : "<<endl;
show_Table();
for(int i = 1;i <= stuNum - 1;i++)
{
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex;
for(int j = 0;j < SUBJECT_NUM;j++)
{
cout<<"\t"<<record[i].score[j];
}
cout<<endl;
}
}
//第二種情況 : 欲刪除學生不是最後一位
if(i != stuNum)
{
cout<<"您所要刪除的學生信信息是 : "<<endl;
show_Table();
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex<<"\t"
<<record[i].score[0]<<"\t"<<record[i].score[1]<<"\t"<<record[i].score[2];
//整體往前 前移一位
for(int j = i+1;j <= stuNum;j++)
{
record[j-1] = record[j];
}
cout<<endl;
//接著完成輸出
cout<<"刪除後學生信息表為 : "<<endl;
show_Table();
for(int i = 1;i <= stuNum-1;i++)
{
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex;
for(int j = 0;j < SUBJECT_NUM;j++)
{
cout<<"\t"<<record[i].score[j];
}
cout<<endl;
}
cout<<endl;
}
}
}
}
*/
void delete_Name(char tarName[])
{
int i;
for(i = 1;i <= stuNum;i++)
{
//刪除還要分兩種情況討論
//1. 欲刪除的學生信息是最後一位
//2. 欲刪除的學生信息不是最後一位
//當欲刪除的學生是最後一位,直接輸出前面LEN-1位學生的信息
if(strcmp(record[i].name,tarName) == 0)
{
if(i == stuNum)
{
cout<<"您所要刪除的學生信息是 : "<<endl;
show_Table();
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex<<"\t"
<<record[i].score[0]<<"\t"<<record[i].score[1]<<"\t"<<record[i].score[2];
cout<<endl;
cout<<"刪除後學生信息表為 : "<<endl;
show_Table();
for(int i = 1;i <= stuNum-1;i++)
{
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex;
for(int j = 0;j < SUBJECT_NUM;j++)
{
cout<<"\t"<<record[i].score[j];
}
cout<<endl;
}
/*
stuNum--;
cout<<"現在還剩下"<<stuNum<<"條學生的信息";
cout<<endl;
*/
}
//當欲刪的學生不是最後一位,整體往前前移一位
if(i != stuNum)
{
cout<<"您所要刪除的學生信息是 : "<<endl;
show_Table();
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex<<"\t";
cout<<record[i].score[0]<<"\t"<<record[i].score[1]<<"\t"<<record[i].score[2];
cout<<endl;
//整體往前前移一位
for(int j = i+1;j <= stuNum;j++)
{
record[j-1] = record[j];
}
//然後輸出
cout<<endl;
cout<<"刪除後學生信息表為 : "<<endl;
show_Table();
for(int i = 1;i <= stuNum-1;i++)
{
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex;
for(int j = 0;j < SUBJECT_NUM;j++)
{
cout<<"\t"<<record[i].score[j];
}
cout<<endl;
}
/*
stuNum--;
cout<<"現在還剩下"<<stuNum<<"條學生的信息";
cout<<endl;
*/
}
stuNum--;
cout<<"現在還剩下"<<stuNum<<"條學生的信息";
cout<<endl;
}
}
}
/*****************************************************************************
******************************************************************************/
//顯示modifyRecord的表頭信息
void Info_modify()
{
cout<<"請輸入修改方式 : "<<endl;
cout<<"1. 按學號修改"<<endl;
cout<<"2. 按姓名修改"<<endl;
cout<<"3. 退出修改"<<endl;
}
//查詢學生的成績,當然裡麵包括兩個子函數
void modifyRecord()
{
int key;
cout<<endl;
Info_modify();
cout<<"請輸入您的選擇 : ";
cin>>key;
//按學號修改
if(key == 1)
{
int targetNum;
cout<<"請輸入您欲修改的學生的學號 : ";
cin>>targetNum;
modify_Num(targetNum);
cout<<endl;
//display();
}
//按姓名修改
if(key == 2)
{
char targetName[20];
cout<<"請輸入您欲修改學生的姓名 : ";
cin>>targetName;
modify_Name(targetName);
cout<<endl;
//display();
}
//退出修改
if(key == 3)
{
while(1)
{
menu_CMD();
}
}
}
//按學號修改
void modify_Num(int tarNum)
{
int i;
for(i = 1;i <= stuNum;i++)
{
if(record[i].num == tarNum)
{
cout<<endl<<"請修改該學生的信息"<<endl;
cout<<"請輸入該學生的學號 : ";
cin>>record[i].num;
cout<<"請輸入該學生的姓名 : ";
cin>>record[i].name;
cout<<"請輸入該學生的性別 : ";
cin>>record[i].sex;
cout<<"請輸入"<<subject[0]<<"的成績 : ";
cin>>record[i].score[0];
cout<<"請輸入"<<subject[1]<<"的成績 : ";
cin>>record[i].score[1];
cout<<"請輸入"<<subject[2]<<"的成績 : ";
cin>>record[i].score[2];
}
}
}
//按姓名修改
void modify_Name(char tarName[])
{
int i;
for(i = 1;i <= stuNum;i++)
{
if(strcmp(record[i].name,tarName) == 0)
{
cout<<endl<<"請修改該學生的信息 : "<<endl;
cout<<"請輸入該學生的學號 : ";
cin>>record[i].num;
cout<<"請輸入該學生的姓名 : ";
cin>>record[i].name;
cout<<"請輸入該學生的性別 : ";
cin>>record[i].sex;
cout<<"請輸入"<<subject[0]<<"的成績 : ";
cin>>record[i].score[0];
cout<<"請輸入"<<subject[1]<<"的成績 : ";
cin>>record[i].score[1];
cout<<"請輸入"<<subject[2]<<"的成績 : ";
cin>>record[i].score[2];
}
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//顯示queryRecord的表頭信息
void Info_query()
{
cout<<"請輸入查詢方式 : "<<endl;
cout<<"1. 按學號查詢"<<endl;
cout<<"2. 按姓名查詢"<<endl;
cout<<"3. 退出查詢"<<endl;
}
//查詢學生信息queryRecord
void queryRecord()
{
int key;
cout<<endl;
Info_query();
cout<<"請輸入您的選擇 : ";
cin>>key;
if(key == 1)
{
int targetNum;
cout<<"請輸入您欲查詢學生的學號 : ";
cin>>targetNum;
query_Num(targetNum);
cout<<endl;
}
if(key == 2)
{
char targetName[20];
cout<<"請輸入您欲查詢學生的學號 : ";
cin>>targetName;
query_Name(targetName);
cout<<endl;
}
//退出查詢,退回到主菜單吧...
if(key == 3)
{
while(1)
{
menu_CMD();
}
}
}
//按學號查詢
void query_Num(int tarNum)
{
int i;
for(i = 1;i <= stuNum;i++)
{
if(record[i].num == tarNum)
{
//如果表中有該學生信息的話,僅用輸出該學生的信息即可.
//輸出該學生的信息
cout<<"該學生的信息如下 : "<<endl;
//顯示表頭信息
show_Table();
//顯示該學生具體的信息
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex;
cout<<"\t"<<record[i].score[0]<<"\t"<<record[i].score[1]<<"\t"<<record[i].score[2];
cout<<endl;
}
}
}
//按姓名查詢
void query_Name(char tarName[])
{
int i;
for(i = 1;i <= stuNum;i++)
{
if(strcmp(record[i].name,tarName) == 0)
{
cout<<"該學生的信息如下 : "<<endl;
show_Table();
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex;
cout<<"\t"<<record[i].score[0]<<"\t"<<record[i].score[1]<<"\t"<<record[i].score[2];
cout<<endl;
}
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//先顯示所有學生的信息吧
//顯示record里所有學生的成績
void display()
{
show_Table();
int i,j;
for(i = 1;i <= stuNum;i++)
{
//cout<<"學號"<<"\t"<<"姓名"<<"\t"<<"性別";
cout<<record[i].num<<"\t"<<record[i].name<<"\t"<<record[i].sex;
for(j = 0;j < SUBJECT_NUM;j++)
{
cout<<"\t"<<record[i].score[j];
}
cout<<endl;
}
cout<<endl;
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//退出
void quit()
{
char choi;
cout<<"您確定要退出嗎?"<<endl;
cout<<"請輸入您的選擇(Y/N) : ";
cin>>choi;
if((choi == 'Y')||(choi == 'y'))
{
cout<<"現在退出學生信息管理系統"<<endl;
exit(0);
}
//如果不是退出,則接著退回到主界面
else
{
cout<<endl;
menu_CMD();
}
}
這個是原創的... 在C-Free 4.0里跑過,可以正常運行
你可以試著跑一下,如果有什麼問題可以和我聯系
Ⅲ c語言學生成績管理系統1000行源代碼
/*首次使用,新建score.in文件,在裡面打入0
可用程序
#include<stdio.h>
FILE *fin
main()
{
fin=fopen("score.in","r");
fprintf(fin,"0");
return 0;
}
完成
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define n 999
FILE *fin;
main()
{
int a[n]={0},ori=0,i=0,b=0,add[n]={0},con=0,orix[n]={0},t=0,te[n]={0},TRUEt,ixi,fx,abc=0,sum,TURE,xx;
long long f[n]={0},tex[n]={0};
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t學生成績管理系統\n\t\t\t\t\t\t Please wait...");
Sleep(2000);system("cls");
int xp,xi;
fin=fopen("score.in","r+");
fscanf(fin,"%d",&xp);
i=xp;
for(xi=1;xi<=xp;xi++)
{fscanf(fin,"%d%d%d",&f[xi],&a[xi],&te[xi]);te[xi]--;
printf("Id:%lld score:%d ranking:%d\n",f[xi],a[xi],te[xi]+1);
add[xi]=xi;orix[xi]=a[xi];
}
fclose(fin);
int TRUE1;
pe:
while(1)
{ TRUE1=1;i++;
printf("Id(輸入-1:刪除,輸入-2:修改成績,輸入-3:刪除所有內容,輸入-4:保存到score.in並退出):");
scanf("%lld",&f[i]);
for(sum=1;sum<i;sum++)
if(f[sum]==f[i])
{system("cls");for(con=1;con<=i;con++) {if(a[con]==0)continue;printf("Id:%lld score:%d ranking:%d\n",f[con],a[con],te[con]+1);}TRUE1=0;i--;printf("Error with two same id.\n");continue;}
if(TRUE1==0)
continue;
if(f[i]==-3)
{int r;
for(r=0;r<n;r++)
{
a[r]=0;ori=0;i=0;b=0;add[r]=0;con=0;orix[r]=0;t=0;te[r]=0;abc=0;
f[r]=0;tex[r]=0;
}
system("cls");
goto pe;
}
if(f[i]==-2)
{TURE=0;i--;
system("cls");
for(con=1;con<=i;con++)
{if(a[con]==0)continue; printf("Id:%lld score:%d ranking:%d\n",f[con],a[con],te[con]+1);}
printf("修改學號_____________號的分數.\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",f[con],a[con],te[con]+1);
scanf("%d",&xx);
for(sum=1;sum<=i;sum++)
if(f[sum]==xx)
{
TURE=1;
printf("\n修改為___________.\b\b\b\b\b\b\b\b\b\b\b\b");
scanf("%d",&a[sum]);
printf("\n修改成功!將在下一次輸入後更新名次。\n\n");
continue;
}
if(TURE==0)
printf("No id is %d\n",xx);
continue;
}
if(f[i]==-4)
{
i--;
fin=fopen("score.in","r+");
fprintf(fin,"%d\n",i);
for(xi=1;xi<=i;xi++)fprintf(fin,"%d %d %d\n",f[xi],a[xi],te[xi]+1);
for(xi=i+1;xi<=xp;xi++)fprintf(fin," \n");
fclose(fin);
return 0;
}
if(f[i]==-1)
{TRUEt=0;
system("cls");
i--;
for(con=1;con<=i;con++){if(a[con]==0)continue;printf("Id:%lld score:%d ranking:%d\n",f[con],a[con],te[con]+1);}
printf("刪除學號_____________號.\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
scanf("%lld",&fx);
for(con=1;con<=i;con++)
if(f[con]==fx)
{TRUEt=1;abc++;
f[con]=0;
a[con]=0;
tex[abc]=te[con];
for(ixi=1;ixi<=i;ixi++)
if(te[ixi]>te[con])
te[ixi]--;
te[con]=0;
break;}
system("cls");
for(con=1;con<=i;con++){
if(a[con]==0)continue;
printf("Id:%lld score:%d ranking:%d\n",f[con],a[con],te[con]+1);}
if(TRUE==0)
printf("No Id is %lld\n",fx);
continue;
}
system("cls");
for(con=1;con<i;con++)
{if(a[con]==0)continue; printf("Id:%lld score:%d ranking:%d\n",f[con],a[con],te[con]+1);}
printf("Id:%lld score:",f[i]);
scanf("%d",&ori);system("cls");
a[i]=ori;add[i]=i;orix[i]=a[i];
system("cls");
for(b=1;b<=i;b++)
for(con=i;con>=b;con--)
if(orix[con]>orix[con-1])
{t=orix[con];orix[con]=orix[con-1];orix[con-1]=t;
t=add[con];add[con]=add[con-1];add[con-1]=t;}
for(con=1;con<=i;con++) te[add[con]]=con;
for(con=1;con<=abc;con++)
for(ixi=1;ixi<=i;ixi++)
if(te[ixi]>tex[con])
te[ixi]--;
if(te[i]==te[i-1])te[i]=0;
for(con=1;con<=i;con++)
{if(a[con]==0)continue;printf("Id:%lld score:%d ranking:%d\n",f[con],a[con],te[con]+1);}
}
return 0;
}
//求採納
Ⅳ 如何用C語言編寫學生信息管理系統
參考如下學生信息管理系統的C源代碼吧。
#include<stdio.h>
#include<string.h>
/*定義學生結構體*/
structStudent
{
charID[20];
charName[20];
floatMark1;
floatMark2;
floatMark3;
floatAverage;
};
/*聲明學生數組及學生數量*/
structStudentstudents[1000];
int腔源num=0;
/*求平均值*/
floatAvg(structStudentstu)
{
return(stu.Mark1+stu.Mark2+stu.Mark3)/3;
}
/*通過學號返回數組下標*/
intStudent_SearchByIndex(charid[])
{
inti;
for(i=0;i<num;i++)
{
if(strcmp(students[i].ID,id)==0)
{
returni;
}
}
return-1;
}
/*通過姓名返回數組下標*/
intStudent_SearchByName(charname[])
{
inti;
for(i=0;i<num;i++)
{
if(strcmp(students[i].Name,name)==0)
{
returni;
}
}
return-1;
}
/*顯示單條學生記錄*/
voidStudent_DisplaySingle(intindex)
{
printf("%10s%10s%8s%8s%8s%10s ","學號","姓名","成績","成績","成績","平均成績");
printf("------------------------------------------------------------- ");
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f ",students[index].ID,students[index].Name,
students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Average);
}
/*插入學生信息*/
voidStudent_Insert()
{
while(1)
{
printf("請輸入學號:");
scanf("%s",&students[num].ID);
getchar();
printf("請銀脊輸入姓名:");
scanf("%s",&students[num].Name);
getchar();
printf("請輸入成績:");
scanf("%f",&students[num].Mark1);
getchar();
printf("請輸入成績:");
scanf("%f",&students[num].Mark2);
getchar();
printf("請輸入成績:");
scanf("%f",&students[num].Mark3);
getchar();
students[num].Average=Avg(students[num]);
num++;
printf("是否繼續?(y/n)");
if(getchar()=='n')
{
break;
}
}
}
/*修改學生信息*/
voidStudent_Modify()
{
//floatmark1,mark2,mark3;
while(1)
{
charid[20];
intindex;
printf("請輸入要修改的學生的學號:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if(index==-1)
鋒圓滲{
printf("學生不存在! ");
}
else
{
printf("你要修改的學生信息為: ");
Student_DisplaySingle(index);
printf("--請輸入新值-- ");
printf("請輸入學號:");
scanf("%s",&students[index].ID);
getchar();
printf("請輸入姓名:");
scanf("%s",&students[index].Name);
getchar();
printf("請輸入成績:");
scanf("%f",&students[index].Mark1);
getchar();
printf("請輸入成績:");
scanf("%f",&students[index].Mark2);
getchar();
printf("請輸入成績:");
scanf("%f",&students[index].Mark3);
getchar();
students[index].Average=Avg(students[index]);
}
printf("是否繼續?(y/n)");
if(getchar()=='n')
{
break;
}
}
}
/*刪除學生信息*/
voidStudent_Delete()
{
inti;
while(1)
{
charid[20];
intindex;
printf("請輸入要刪除的學生的學號:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if(index==-1)
{
printf("學生不存在! ");
}
else
{
printf("你要刪除的學生信息為: ");
Student_DisplaySingle(index);
printf("是否真的要刪除?(y/n)");
if(getchar()=='y')
{
for(i=index;i<num-1;i++)
{
students[i]=students[i+1];//把後邊的對象都向前移動
}
num--;
}
getchar();
}
printf("是否繼續?(y/n)");
if(getchar()=='n')
{
break;
}
}
}
/*按姓名查詢*/
voidStudent_Select()
{
while(1)
{
charname[20];
intindex;
printf("請輸入要查詢的學生的姓名:");
scanf("%s",&name);
getchar();
index=Student_SearchByName(name);
if(index==-1)
{
printf("學生不存在! ");
}
else
{
printf("你要查詢的學生信息為: ");
Student_DisplaySingle(index);
}
printf("是否繼續?(y/n)");
if(getchar()=='n')
{
break;
}
}
}
/*按平均值排序*/
voidStudent_SortByAverage()
{
inti,j;
structStudenttmp;
for(i=0;i<num;i++)
{
for(j=1;j<num-i;j++)
{
if(students[j-1].Average<students[j].Average)
{
tmp=students[j-1];
students[j-1]=students[j];
students[j]=tmp;
}
}
}
}
/*顯示學生信息*/
voidStudent_Display()
{
inti;
printf("%10s%10s%8s%8s%8s%10s ","學號","姓名","成績","成績","成績","平均成績");
printf("------------------------------------------------------------- ");
for(i=0;i<num;i++)
{
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f ",students[i].ID,students[i].Name,
students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Average);
}
}
/*將學生信息從文件讀出*/
voidIO_ReadInfo()
{
FILE*fp;
inti;
if((fp=fopen("Database.txt","rb"))==NULL)
{
printf("不能打開文件! ");
return;
}
if(fread(&num,sizeof(int),1,fp)!=1)
{
num=-1;
}
else
{
for(i=0;i<num;i++)
{
fread(&students[i],sizeof(structStudent),1,fp);
}
}
fclose(fp);
}
/*將學生信息寫入文件*/
voidIO_WriteInfo()
{
FILE*fp;
inti;
if((fp=fopen("Database.txt","wb"))==NULL)
{
printf("不能打開文件! ");
return;
}
if(fwrite(&num,sizeof(int),1,fp)!=1)
{
printf("寫入文件錯誤! ");
}
for(i=0;i<num;i++)
{
if(fwrite(&students[i],sizeof(structStudent),1,fp)!=1)
{
printf("寫入文件錯誤! ");
}
}
fclose(fp);
}
/*主程序*/
voidmain()
{
intchoice;
IO_ReadInfo();
while(1)
{
/*主菜單*/
printf(" ------學生成績管理系統------ ");
printf("1.增加學生記錄 ");
printf("2.修改學生記錄 ");
printf("3.刪除學生記錄 ");
printf("4.按姓名查詢學生記錄 ");
printf("5.按平均成績排序 ");
printf("6.退出 ");
printf("請選擇(1-6):");
scanf("%d",&choice);
getchar();
switch(choice)
{
case1:
Student_Insert();
break;
case2:
Student_Modify();
break;
case3:
Student_Delete();
break;
case4:
Student_Select();
break;
case5:
Student_SortByAverage();
Student_Display();
break;
case6:
exit(0);
break;
}
IO_WriteInfo();
}
}