用c++编写学生管理系统

用c++编写学生管理系统,第1张

今天卧病在家,抽时间写的这么个简单的管理系统,完全是命令行界面的,对于一些异常的内容考虑的还不是很周到。不知道你具体用于做什么。不过希望对你有帮助,你可以基于源码进行修改。如下内容在VC2005上编译并测试通过基本场景。如果你需要图形界面,你可以尝试用MFC或者C#等包装,时间有限,只能先做到这些了。good luck!

// mytest.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include <iostream>

#include <map>

#include <string>

#include <algorithm>

class Student

{

public:

Student(std::string name,

unsigned long id,

std::string sex,

std::string political,

std::string address)

:

m_name(name),

m_id(id),

m_sex(sex),

m_political(political),

m_address(address)

{

}

public:

unsigned long m_id

std::string m_name

std::string m_sex

std::string m_political

std::string m_address

}

typedef std::map<unsigned long, Student>StudentCont

class StudentMgr

{

public:

StudentMgr()

:

m_num(0)

{

}

void add()

{

std::cin.clear()

std::cin.sync()

std::string name("")

std::cout <<"please input student's name: "

std::getline(std::cin, name)

std::string sex("")

std::cout <<"please input student's sex: "

std::getline(std::cin,sex)

std::string political("")

std::cout <<"please input student's political apperance: "

std::getline(std::cin,political)

std::string address("")

std::cout <<"please input student's address: "

std::getline(std::cin,address)

m_num++

Student student(name, m_num, sex, political, address)

m_students.insert(std::make_pair(m_num, student))

std::cout <<"Student " <<m_num <<" " <<name <<" " <<sex <<" " <<political <<" " <<address <<" add successfully!" <<std::endl

}

void del()

{

unsigned long id

std::cout <<"Please input the student's id:"

std::cin >>id

StudentCont::iterator iter = m_students.find(id)

if(iter != m_students.end())

{

m_students.erase(id)

std::cout <<"Id number: " <<id <<" was deleted successfully!" <<std::endl

}

else

{

std::cout <<"Id number: " <<id <<" doesn't exist in the system!" <<std::endl

}

}

void mod()

{

unsigned long id

std::cout <<"Please input the student's id:"

std::cin >>id

StudentCont::iterator iter = m_students.find(id)

if(iter != m_students.end())

{

std::cout <<"Id number: " <<id <<" is found, please input the information you want to change!" <<std::endl

std::cout <<"please input your choice number:" <<std::endl

std::cout <<"--------------------------------" <<std::endl

std::cout <<"-[1] name. -" <<std::endl

std::cout <<"-[2] sex. -" <<std::endl

std::cout <<"-[3] political apperance. -" <<std::endl

std::cout <<"-[4] address. -" <<std::endl

std::cout <<"-[5] Quit! -" <<std::endl

std::cout <<"--------------------------------" <<std::endl

unsigned long choiceNum

std::cin >>choiceNum

switch(choiceNum)

{

case 1:

{

std::string name

std::cin.clear()

std::cin.sync()

std::cout <<"please input new student's name: "<<std::endl

std::getline(std::cin,name)

(*iter).second.m_name = name

break

}

case 2:

{

std::string sex

std::cin.clear()

std::cin.sync()

std::cout <<"please input correct student's sex: "<<std::endl

std::getline(std::cin,sex)

(*iter).second.m_sex = sex

break

}

case 3:

{

std::string political

std::cin.clear()

std::cin.sync()

std::cout <<"please input student's new political apperance: "<<std::endl

std::getline(std::cin,political)

(*iter).second.m_political = political

break

}

case 4:

{

std::string address

std::cin.clear()

std::cin.sync()

std::cout <<"please input student's new address: "<<std::endl

std::getline(std::cin,address)

(*iter).second.m_address = address

break

}

case 5:

break

default:

{

std::cerr <<"Input choice number:" <<choiceNum <<" is wrong!" <<std::endl

break

}

}

}

else

{

std::cout <<"Id number: " <<id <<" doesn't exist in the system, return to the menu!" <<std::endl

}

std::cout <<"modify sucessfull!" <<std::endl

}

void query()

{

unsigned long id

std::cout <<"Please input the student's id:"

std::cin >>id

StudentCont::iterator iter = m_students.find(id)

if(iter != m_students.end())

{

std::cout <<"The student's information is: "

<<(*iter).second.m_id <<" "

<<(*iter).second.m_name <<" "

<<(*iter).second.m_address <<" "

<<(*iter).second.m_political <<" "

<<(*iter).second.m_sex <<std::endl

}

else

{

std::cout <<"Id number: " <<id <<" doesn't exist in the system, return to the menu!" <<std::endl

}

}

private:

unsigned long m_num

StudentCont m_students

}

void printMenu()

{

std::cout <<"please input your choice number:" <<std::endl

std::cout <<"--------------------------------" <<std::endl

std::cout <<"-[1] Add a student.-" <<std::endl

std::cout <<"-[2] Delete a student. -" <<std::endl

std::cout <<"-[3] Modify a student. -" <<std::endl

std::cout <<"-[4] Query students. -" <<std::endl

std::cout <<"-[5] Quit! -" <<std::endl

std::cout <<"--------------------------------" <<std::endl

}

void startstudentManagement()

{

std::cout <<"Thank you for using this simple student management system, Welcome!" <<std::endl

printMenu()

std::auto_ptr<StudentMgr>studentMgr(new StudentMgr)

bool stop = false

do

{

unsigned long choiceNum = 0

std::cout <<"Your choice number is: "

std::cin.clear()

std::cin.sync()

std::cin >>choiceNum

switch(choiceNum)

{

case 1:

studentMgr->add()

break

case 2:

studentMgr->del()

break

case 3:

studentMgr->mod()

break

case 4:

studentMgr->query()

break

case 5:

stop = true

break

default:

std::cerr <<"The input number is none of the choice, please try again." <<std::endl

stop = true

break

}

}

while(!stop)

}

void stopstudentManagement()

{

std::cout <<"Thank you for using this simple student management system, Bye!" <<std::endl

}

int main(void)

{

startstudentManagement()

stopstudentManagement()

return 0

}

以上回答你满意么?

我以前做的....要按你这个做不是一下两下弄得完的吧........

你看看能改着用就改这用吧

vc,tc,win-tc编译通过

33.学生成绩管理程序设计

(1)选项菜单集成各功能函数模块

(2)录入学生成绩(姓名、学号、语文、数学成绩,总分自动计算)

(3)按总分排名次

(4)按姓名查找某学生成绩

(5)统计各课程的平均分、及格率、最高分、最低分

#include "stdio.h"

#include "windows.h"

int sum=0,x,data=0

struct STU

{

char name[20]

char num[20]

int chinese_great

int math_great

int all

}stu[200]/*定义学生结构体以及学生结构体数组*/

struct STU *temp

struct STU *p[200]

void in()/*自定义学生信息输入函数*/

void all_swap()/*总分排名*/

void menu()/*菜单*/

void name_swap()/*姓名查找*/

void tongji()/*统计*/

int ssame(char a[],char b[])/*字符串比对函数*/

void over()/*退出*/

void main()

{

menu()

switch (x)

{

case 1 : data=1system("cls")in()break

case 2 : system("cls")all_swap()break

case 3 : system("cls")name_swap()break

case 4 : system("cls")tongji()break

}

}

/*菜单*/

void menu()

{

system("cls")

printf("******************************学生成绩管理系统****************************\n")

printf("请选择你需要的 *** 作:\n")

printf("1.录入学生成绩(姓名、学号、语文、数学成绩,总分自动计算)\n")

printf("2.按总分排名次\n")

printf("3.按姓名查找某学生成绩\n")

printf("4.统计各课程的平均分、及格率、最高分、最低分\n")

printf("5.打印成绩表\n")

scanf("%d",&x)

}

/*学生信息输入函数*/

void in()

{

int i

int end=1

for(i=0i<200i++)

{

sum++

printf("请输入第%d个学生的姓名: ",i+1)

scanf("%s",&stu.name)

printf("请输入第%d个学生的学号: ",i+1)

scanf("%s",&stu.num)

printf("请输入第%d个学生的语文成绩: ",i+1)

scanf("%d",&stu.chinese_great)

printf("请输入第%d个学生的数学成绩: ",i+1)

scanf("%d",&stu.math_great)

stu.all=stu.chinese_great+stu.math_great

printf("\n\n\n\n\n \n 1:继续录入\n 2:结束录入\n 请选择:")

scanf("%d",&end)

if(end==2)

break

else if (end==1) system("cls")

else over()

}/*FOR结束*/

system("cls")

main()

}/*学生信息输入函数结束*/

/*按总分排名*/

void all_swap()

{

int i,j,q=0

for(i=0i<=sumi++)

p=&stu

for(i=0i<=sum-1i++)

for (j=i+1j<=sumj++)

{

if(stu.all<stu[j].all)

{

temp=p

p=p[j]

p[j]=temp

}

}

printf("名次姓名学号总成绩\n")

for(i=0i<sumi++)

printf("%d %s %s %d\n",i+1,p->name,p->num,p->all)

printf("输入 1 返回主菜单,输入其他退出")

scanf("%d",&q)

if(q==1)

{

system("cls")

main()

}

else over()

}/*总分查询结束*/

/*名字查询*/

void name_swap()

{

int i,q

char temp[20]

for(i=0i<=sumi++)

p=&stu

printf("\n\n\n\n\n\n\n\n\n\ 请输入您要查询的学生姓名\n")

scanf("%s",&temp)

for (i=0i<=sumi++)

{

if(ssame(temp,p->name)==0)

{

system("cls")

printf("你所查找的学生已找到\n 姓名:%s \n 学号: %s \n 数学成绩:%d \n 语文成绩:%d \n 总成绩:%d ",p->name,p->num,p->chinese_great,p->math_great,p->all)

printf("\n\n\n\n\n\n 输入 1 返回主菜单,输入其他退出")

scanf("%d",&q)

if(q==1)

{

system("cls")

main()

}

else over()

}

else

{

system("cls")

printf("\n\n\n\n\n\n 未找到与%s有关的记录",temp)

printf("\n\n\n\n\n\n 输入 1 返回主菜单,输入其他退出")

scanf("%d",&q)

if(q==1)

{

system("cls")

main()

}

else over()

}

}

} /*名字查询结束*/

/*统计*/

void tongji()

{

int i,q,chinese_max=0,chinese_min=9999,math_max=0,math_min=9999long sum_chinese=0,sum_math=0

float chinese_v,math_v,chinese_p,math_p,p_c=0,p_m=0

if(data==1)

{

for(i=0i<=sumi++)

{

p=&stu

sum_chinese=p->chinese_great+sum_chinese

sum_math=p->math_great+sum_math

if(p->chinese_great>=60)

p_c++

if(p->math_great>=60)

p_m++

}

chinese_v=sum_chinese/sum/*语文平均分*/

math_v=sum_math/sum/*数学平均分*/

chinese_p=(p_c/sum)*100/*语文及格率*/

math_p=(p_m/sum)*100/*数学及格率*/

for(i=0i<sumi++)

{

chinese_max=max(chinese_max,p->chinese_great)

chinese_min=min(chinese_min,p->chinese_great)

math_max=max(math_max,p->math_great)

math_min=min(math_min,p->math_great)

}

printf("科目平均分及格率 最高分最低分\n")

printf("语文 %f 百分之%f %d%d \n",chinese_v,chinese_p,chinese_max,chinese_min)

printf("数学 %f 百分之%f %d%d \n",math_v,math_p,math_max,math_min)

printf("\n\n\n\n\n\n 输入 1 返回主菜单,输入其他退出")

scanf("%d",&q)

if(q==1)

{

system("cls")

main()

}

else over()

}

else main()

}

/*字符串比对*/

int ssame(char a[],char b[])

{

int p=0,flag=0

while (a[p]!='\0'||b[p]!='\0')

{

if((a[p]>b[p]) || (a[p]<b[p]))

{

flag=1break

}

p++

}

if(flag==0)

{

if((a[p]!='\0') || (b[p]!='\0'))

flag=1

}

return flag

}/*字符串比对结束*/

/*退出*/

void over()

{

int q

system("cls")

printf("\n\n\n\n\n\n\n\n\n 此 *** 作将导致你退出本程序。\n1:确认退出 \n2:返回主菜单")

scanf("%d",&q)

if(q==1)

exit(1)

else if (q==2)

main()

else over()

}

/*好吧,其实你可以上网看看,那么多的,你可以自己修改下*/

#include <stdio.h>

#include <malloc.h>

#include <string.h>

#include <stdlib.h>

#define NULL 0

#define LEN sizeof(struct student)

struct student

{

char name[20]

long int num

char sex[4]

int age

char address[30]

double tele_num

char aihao[20]

struct student *next

}//定义一个结构题

int TOTAL_NUM = 0//学生总数

struct student *head = NULL

void welcome ()//欢迎界面

void mainmenu ()//主界面

void record ()//记录数据

void insert(struct student *stu)//插入数据

void display(struct student *stu)//显示一个学生的信息

void displayAll ()//显示所有学生的信息

void query_by_num ()//按学号查询学生信息

void readData ()//读取文件里学生的信息

void writeData ()//向文件写入学生信息

void freeAll ()//清空链表内容

void del ()//删除学生信息

void change ()//更改学生信息

void devise (struct student *p)//选择更改内容

int main (void)

{

welcome ()

//显示主菜单

mainmenu ()

return 0

}

//显示欢迎信息

void welcome ()

{

printf ("+----------------------------------------------------+\n")

printf ("||\n")

printf ("| 欢迎使用网络工程学院信息安全092班学生信息管理系统 |\n")

printf ("||\n")

printf ("+----------------------------------------------------+\n")

}

//系统主菜单

void mainmenu ()

{

int choice

choice = -1

readData ()

printf ("\n温馨提示:为保证您的 *** 作得到保存,请按正常顺序退出系统^_^\n")

do

{

printf ("\n\n\n")

printf ("\t\t\t--------------------------------------------\n")

printf ("\t\t\t+信息安全092班学生信息管理系统 |\n")

printf ("\t\t\t--------------------------------------------\n")

printf ("\t\t\t+[1]----录入学生信息|\n")

printf ("\t\t\t+[2]----浏览学生信息|\n")

printf ("\t\t\t+[3]----查询学生信息|\n")

printf ("\t\t\t+[4]----删除学生信息|\n")

printf ("\t\t\t+[5]----修改学生信息|\n")

printf ("\t\t\t+[0]----退出系统|\n")

printf ("\t\t\t+*·*·*·*·*·*·*·*·*·*·*·*·*·*·*|\n")

printf ("\t\t\t--------------------------------------------\n")

printf ("请输入您的选择:")

scanf ("%d", &choice)

switch (choice)

{

case 0:

writeData ()

freeAll ()

exit (0)

case 1:

record ()

break

case 2:

displayAll ()

break

case 3:

query_by_num ()

break

case 4:

del ()

break

case 5:

change ()

break

default:

printf ("\n无效选项!")

break

}

}

while (choice != 0)

}

//录入学生信息

void record ()

{

struct student *p0

p0 = (struct student *)malloc(LEN)

printf ("请输入学生的姓名:")

scanf ("%s",p0->name)

printf ("请输入学生的学号:")

scanf ("%ld",&p0->num)

printf ("请输入学生的性别:")

scanf ("%s",p0->sex)

printf ("请输入学生的年龄:")

scanf ("%d",&p0->age)

printf ("请输入学生的地址:")

scanf ("%s",p0->address)

printf ("请输入学生的电话:")

scanf ("%lf",&p0->tele_num)

printf ("请输入学生的爱好:")

scanf ("%s",p0->aihao)

insert (p0)

printf ("该学生的信息为:\n")

printf ("-------------------------------------------------------------------------------\n")

printf ("姓名\t学号\t\t年龄\t性别\t地址\t\t电话\t\t爱好\n")

display (p0)

}

void insert (struct student *stu)

{

struct student *p0, *p1, *p2

p1 = head

p0 = stu

if (head == NULL)

{

head = p0

p0->next = NULL

}

else

{

while ((p0->num >p1->num)&&(p1->next != NULL))

{

p2 = p1

p1 = p1->next

}

if (p0->num <= p1->num)

{

if (head == p1)

head = p0

else

p2->next = p0

p0->next = p1

}

else

{

p1->next = p0

p0->next = NULL

}

}

TOTAL_NUM++

}

void display (struct student *p)

{

printf ("%s\t%ld\t%d\t%s\t%s\t\t%.0f\t%s\n", p->name, p->num, p->age, p->sex, p->address, p->tele_num, p->aihao)

}

//浏览学生信息

void displayAll()

{

struct student *p

printf("学生总数:%d\n", TOTAL_NUM)

p = head

if (head != NULL)

{

printf("\n姓名\t学号\t\t年龄\t性别\t地址\t\t电话\t\t爱好\n")

printf("-------------------------------------------------------------------------------\n")

do

{

display(p)

p = p->next

}

while(p != NULL)

}

printf ("\n")

}

//按学号查询学生信息

void query_by_num ()

{

int num

struct student *p1

printf("请输入学生的学号:")

scanf("%ld", &num)

if(head==NULL)

{

printf("无学生记录!\n")

return

}

p1 = head

while (num!=p1->num &&p1->next!=NULL)

p1 = p1->next

if (num == p1->num)

{

printf ("姓名\t学号\t\t年龄\t性别\t地址\t\t电话\t\t爱好\n")

printf ("-------------------------------------------------------------------------------\n")

display (p1)

}

else

printf ("没有该学生记录,请核对!")

}

//写入文件

void writeData ()

{

FILE* fp//文件指针

struct student *p

fp = fopen("1.txt", "w")

if (!fp)

{

printf("文件打开错误!")

return

}

fprintf(fp,"%d\n", TOTAL_NUM)

for(p = headp!= NULLp= p->next)

{

fprintf(fp,"%s\t%ld\t%s\t%d\t%s\t%.0f\t%s\n", p->name, p->num, p->sex, p->age, p->address, p->tele_num, p->aihao)

}

fclose (fp)

}

void freeAll ()

{

struct student *p1, *p2

p1 = p2=head

while(p1)

{

p2=p1->next

free (p1)

p1 = p2

}

}

//读取文件

void readData ()

{

FILE* fp//文件指针

struct student *p1, *p2

fp = fopen("1.txt", "r")

if (!fp)

{

printf("文件打开错误!")

return

}

fscanf(fp,"%d\n", &TOTAL_NUM)

head = p1 = p2 = (struct student *)malloc(LEN)

fscanf(fp,"%s\t%ld\t%s\t%d\t%s\t%lf\t%s\n", p1->name, &p1->num, p1->sex, &p1->age, p1->address, &p1->tele_num, p1->aihao)

while(!feof(fp))

{

p1 = (struct student *)malloc(LEN)

fscanf(fp,"%s\t%ld\t%s\t%d\t%s\t%lf\t%s\n", p1->name, &p1->num, p1->sex, &p1->age, p1->address, &p1->tele_num, p1->aihao)

p2->next = p1

p2 = p1

}

p2->next = NULL

fclose(fp)

}

//删除学生信息

void del ()

{

struct student *p1, *p2

long int num

if (head == NULL)

{

printf("无学生记录!\n")

return

}

printf("请输入您要删除的学生的学号:")

scanf("%ld", &num)

p1 = head

while (num != p1->num &&p1->next != NULL)

{

p2 = p1

p1 = p1->next

}

if(num == p1->num)

{

if(p1 == head)

head = p1->next

else p2->next = p1->next

free(p1)

TOTAL_NUM--

}

else

printf("没有该学生记录,请核对!\n")

}

//修改学生信息

void change ()

{

struct student *p1, *p2

long int num

if (head == NULL)

{

printf ("无学生记录!\n")

return

}

printf ("请输入您要修改的学生的学号:")

scanf ("%ld", &num)

p1 = head

while (num != p1->num &&p1->next != NULL)

{

p2 = p1

p1 = p1->next

}

if(num == p1->num)

devise (p1)

else

printf("没有该学生记录,请核对!\n")

}

void devise (struct student *p)

{

int choice

choice = -1

do

{

printf("请选择您要修改的学生的信息内容:\n")

printf("+----------------------+\n")

printf("| 姓名 请按 1 |\n")

printf("| 学号 请按 2 |\n")

printf("| 性别 请按 3 |\n")

printf("| 年龄 请按 4 |\n")

printf("| 地址 请按 5 |\n")

printf("| 电话 请按 6 |\n")

printf("| 爱好 请按 7 |\n")

printf("| 取消 请按 0 |\n")

printf("+----------------------+\n")

printf("请输入您的选择:")

scanf("%d", &choice)

switch (choice)

{

case 0:

return

case 1:

printf("请输入新姓名:")

scanf("%s", p->name)

break

case 2:

printf("请输入新学号:")

scanf("%d", &p->num)

break

case 3:

printf("请输入新性别:")

scanf("%s", p->sex)

break

case 4:

printf("请输入新年龄:")

scanf("%s", &p->age)

break

case 5:

printf("请输入新地址:")

scanf("%s", p->address)

break

case 6:

printf("请输入新电话:")

scanf("%lf", &p->tele_num)

break

case 7:

printf("请输入新爱好:")

scanf("%s", p->aihao)

break

default:

printf("\n无效选项!")

break

}

}

while(choice != 0)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9985963.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存