|
1楼 #include <iostream.h> #include <string.h> #include <conio.h> int adminLogin() { char s[80], adminPWD[]="123456",c; int k ; cout<<"\n\n\t\t\t请输入管理员密码:"; cin>>s; if( strcmp( s, adminPWD ) == 0 ) k=1; /*密码正确, 返回 */ else k=0; return(k); } struct student { int no; char name[12]; char classes[12]; char sex; }tstudent; void getinformation() { char s[80],c1; for(; ;) { cout<<"\n\n\t\t\t请输入学号:"; cin>>tstudent.no; if(tstudent.no<0) {cout<<"\n\n\t\t\t您输入的学号错误!\n"; continue; } break; } for(; ;) { cout<<"\n\n\t\t\t请输入姓名:"; cin>>s; if(strlen(s)>12) {cout<<"\n\n\t\t\t您输入的姓名过长!\n"; continue; } strcpy(tstudent.name,s); break; } for(; ;) { cout<<"\n\n\t\t\t请输入班级:"; cin>>s; if(strlen(s)>12) { cout<<"\n\n\t\t\t对不起,班级名称过长!\n"; continue; } strcpy(tstudent.classes,s); break; } for(; ;) { cout<<"\t\t\t请输入性别(m/f):"; cin>>c1; if(c1!='m'&&c1!='f') { cout<<"\t\t\t性别输入错误!\n"; continue; } tstudent.sex=c1; break; } } void infoManage() { char c ; for( ;; ) { cout<<"\n\n\n\t\t\t\t信息维护"<<endl; cout<<"\t\t\t 1、建立学生基本信息表\n"; cout<<"\t\t\t 2、建立学生成绩表\n" ; cout<<"\t\t\t 3、添加学生记录\n"; cout<<"\t\t\t 4、删除学生记录\n"; cout<<"\t\t\t 5、修改学生记录\n"; cout<<"\t\t\t 0、返回\n\n"; cout<<"\t\t\t 请输入你的选择:" ; cin>>c; if( c == '0' ) return ; else if( c == '1' ) getinformation(); else if( c == '2' ) ; else if( c == '3' ) ; else if( c == '4' ) ; else if( c == '5' ) ; } } void infoStatistics() { char c ; for( ;; ) { cout<<"\n\n\n\n\n\t\t\t信息统计"; cout<<"\t\t\t 1、成绩统计\n"; cout<<"\t\t\t 2、其它信息统计\n"; cout<<"\t\t\t 0、返回\n\n"; cout<<"\t\t\t 请输入你的选择:"; cin>>c; if( c == '0' ) return ; else if( c == '1' ) ;//statisCourse() ; else if( c == '2' ) ;//statisOthers() ; } } void infoRetrieve() { char c ; for( ;; ) { cout<<"\n\n\n\t\t\t信息检索"<<endl; cout<<"\t\t\t 1、按姓名查找\n"; cout<<"\t\t\t 2、按班级查找\n" ; cout<<"\t\t\t 3、按其它信息查找\n" ; cout<< "\t\t\t 0、返回\n\n"; cout<<"\t\t\t 请输入你的选择:" ; cin>>c; if( c == '0' ) return ; else if( c == '1' ) ;//retrieveByName() ; else if( c == '2' ) ;//retrieveByClass() ; else if( c == '3' ) ;//retrieveByOther() ; } } void main() { char c ; int adminLogin(); if( adminLogin() ==0) /* 检查管理员密码 */ goto Exit ; /* 下面显示一级菜单 */ for( ;; ) { cout<<"\n\n\n\n\n\n\t\t欢迎使用学生信息管理系统"<<endl; cout<<"\t\t 1、信息维护\n"; cout<<"\t\t 2、信息检索\n"; cout<<"\t\t 3、信息统计\n"; cout<<"\t\t 0、退出系统\n\n"; cout<<"\t\t 请输入你的选择:"; cin>>c; if( c == '0' ) goto Exit ; else if( c == '1' ) infoManage(); else if( c == '2' ) infoRetrieve() ; else if( c == '3' ) infoStatistics() ; } Exit: cout<<"\n\n\n\t\t\t\t退出系统,再见。\n\n"; } |
|
|
|
|
2楼 |
|
|
|
|
3楼 一、用cin输入数组会引起缓冲区溢出。 如在 void getinformation() 中 s[80];cin>>s; 解决办法:1 用cin::getline(char *buf, streamsize num); 如 cin::getline(s, 79); // 记住留一个位置给NULL. 2 如用标准C I/O : 用fgets(char *str, int num, FILE *stream); 如fgets(s, 79, stdin); 二、例如在 void infoManage() 中 c 为 char, cin>>c; 会在缓冲区留下多余的字符, cin 是一直等到回车才返回,如果用户键入多于一个字符,下一个cin的会是缓冲区留下的多余字符,结果不可预料。 解决办法:标准C : c = (char)fgetc(stdin); fflush(stdin); /* 我们只要一个字符*/ 标准C: cin::get©; cin::flush(); 另外,Turbo/Borland C/C++ 和 Micro$oft C/C++/VC++/VC.NET 都有一个非标准的扩展 int getch(); 在 conio.h 中。此函数不带回显,非常好用,你还可以用此限制用户只输入你要的字符。 三、密码硬编码在程序中,如adminPWD[]="123456", 随便一个十六进制编辑器就可破解,危险哪。 解决办法:在程序中储存加密的,最简单的可用 ~ 或 ^ 运算。有兴趣回帖,我再具体说。 总之要小心缓冲区溢出,黑客利用的系统漏洞大都属于这类。 |
|
|
|
| 222.200.55.* |
7楼 |
|
|
| 218.76.17.* |
9楼 |
|
|
| 221.0.170.* |
12楼 |
|
|
| 61.187.64.* |
13楼 |
|
|
|
14楼 |
|
|
|
15楼 |
|
|
|
| 202.206.84.* |
16楼 |
|
|
| 202.101.213.* |
17楼 |
|
|
| 61.180.240.* |
18楼 |
|
|
| 222.209.200.* |
19楼 include <iostream.h>这个文件,帮帮我好吗,谢谢了 |
|
|
| 218.22.198.* |
20楼 |
|
|
| 219.145.90.* |
21楼 帮我写一个学生公寓管理系统!!!!!!!!!!!!用C语言 |
|
|
| 222.183.184.* |
22楼 |
|
|
| 61.243.231.* |
23楼 |
|
|
| 221.8.169.* |
24楼 |
|
|
| 222.65.89.* |
25楼 |
|
|
| 218.65.77.* |
26楼 xuliang19852@163.com 小弟在这谢谢大家了,救命的啊,考试题目啊!! |
|
|
| 220.175.59.* |
27楼 #include <stdio.h> #include <stdlib.h> #define LEN sizeof(struct scorenode) #define DEBUG #include <string.h> struct scorenode {int number;/*学号*/ char name[10];/*姓名*/ float yuwen;/*语文成绩*/ float yingyu;/*英语成绩*/ float shuxue;/*数学成绩 */ struct scorenode *next; }; typedef struct scorenode score; int n,k;/*n,k为全局变量,本程序中的函数均可以使用它*/ /*==============================================================================================*/ score *creat2311(void) /*函数creat2311,功能:创建链表,此函数带回一个指向链表头的指针*/ { score*head; score *p1,*p2,*p3,*max; int i,j; float fen; char t[10]; n=0; p1=p2=p3=(score *)malloc(LEN);head=p3; /*开辟一个新单元*/ printf("请输入学生资料,输0退出!\n"); repeat1: printf("请输入学生学号(学号应大于0):");/*输入学号,学号应大于0*/ scanf("%d",&p1->number); while(p1->number<0) {getchar(); printf("输入错误,请重新输入学生学号:"); scanf("%d",&p1->number);} /*输入学号为字符或小于0时,程序报错,提示重新输入学号*/ if(p1->number==0) goto end;/*当输入的学号为0时,转到末尾,结束创建链表*/ else { p3=head; if(n>0) {for(i=0;i<n;i++) {if(p1->number!=p3->number) p3=p3->next; else {printf("学号重复,请重输!\n"); goto repeat1; /*当输入的学号已经存在,程序报错,返回前面重新输入*/ } } } } printf("请输入学生姓名:"); scanf("%s",&p1->name);/*输入学生姓名*/ printf("请输入语文成绩(0~100):");/*输入语文成绩,成绩应在0-100*/ scanf("%f",&p1->yuwen); while(p1->yuwen<0||p1->yuwen>100) {getchar(); printf("输入错误,请重新输入语文成绩");/*输入错误,重新输入语文成绩直到正确为止*/ scanf("%f",&p1->yuwen);} printf("请输入英语成绩(0~100):");/*输入英语成绩,成绩应在0-100*/ scanf("%f",&p1->yingyu); while(p1->yingyu<0||p1->yingyu>100) {getchar(); printf("输入错误,请重新输入英语成绩");/*输入错误,重新输入英语成绩直到正确为止*/ scanf("%f",&p1->yingyu);} printf("请输入数学成绩(0~100):");/*输入数学成绩,成绩应在0-100*/ scanf("%f",&p1->shuxue); while(p1->shuxue<0||p1->shuxue>100) {getchar(); printf("输入错误,请重新输入数学成绩"); scanf("%f",&p1->shuxue);}/*输入错误,重新输入数学成绩直到正确为止*/ head=NULL; while(p1->number!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(score *)malloc(LEN); printf("请输入学生资料,输0退出!\n"); repeat2:printf("请输入学生学号(学号应大于0):"); scanf("%d",&p1->number);/*输入学号,学号应大于0*/ while(p1->number<0) {getchar(); printf("输入错误,请重新输入学生学号:"); scanf("%d",&p1->number);} /*输入学号为字符或小于0时,程序报错,提示重新输入学号*/ if(p1->number==0) goto end;/*当输入的学号为0时,转到末尾,结束创建链表*/ else { p3=head; if(n>0) {for(i=0;i<n;i++) {if(p1->number!=p3->number) p3=p3->next; else {printf("学号重复,请重输!\n"); goto repeat2; /*当输入的学号已经存在,程序报错,返回前面重新输入*/ } } } } printf("请输入学生姓名:"); scanf("%s",&p1->name);/*输入学生姓名*/ printf("请输入语文成绩(0~100):"); scanf("%f",&p1->yuwen);/*输入语文成绩,成绩应在0-100*/ |
|
|
| 220.175.59.* |
28楼 {getchar(); printf("输入错误,请重新输入语文成绩"); scanf("%f",&p1->yuwen);}/*输入错误,重新输入语文成绩直到正确为止*/ printf("请输入英语成绩(0~100):"); scanf("%f",&p1->yingyu);/*输入英语成绩,成绩应在0-100*/ while(p1->yingyu<0||p1->yingyu>100) {getchar(); printf("输入错误,请重新输入英语成绩"); scanf("%f",&p1->yingyu);}/*输入错误,重新输入英语成绩直到正确为止*/ printf("请输入数学成绩(0~100):"); scanf("%f",&p1->shuxue);/*输入数学成绩,成绩应在0-100*/ while(p1->shuxue<0||p1->shuxue>100) {getchar(); printf("输入错误,请重新输入数学成绩"); scanf("%f",&p1->shuxue);}/*输入错误,重新输入数学成绩直到正确为止*/ } end: p1=head; p3=p1; for(i=1;i<n;i++) { for(j=i+1;j<=n;j++) { max=p1; p1=p1->next; if(max->number>p1->number) { k=max->number; max->number=p1->number; p1->number=k; /*交换前后结点中的学号值,使得学号大者移到后面的结点中*/ strcpy(t,max->name); strcpy(max->name,p1->name); strcpy(p1->name,t); /*交换前后结点中的姓名,使之与学号相匹配*/ fen=max->yuwen; max->yuwen=p1->yuwen; p1->yuwen=fen; /*交换前后结点中的语文成绩,使之与学号相匹配*/ fen=max->yingyu; max->yingyu=p1->yingyu; p1->yingyu=fen; /*交换前后结点中的英语成绩,使之与学号相匹配*/ fen=max->shuxue; max->shuxue=p1->shuxue; p1->shuxue=fen; /*交换前后结点中的数学成绩,使之与学号相匹配*/ } } max=head;p1=head;/*重新使max,p指向链表头*/ } p2->next=NULL;/*链表结尾*/ printf("输入的学生数为:%d个!\n",n); return(head); } /*==============================================================================================*/ /*==============================================================================================*/ score *load2311(score *head) /*函数load2311,功能:从文件读入学生记录*/ { score *p1,*p2; int m=0; char filepn[10]; FILE *fp; printf("请输入文件路径及文件名:"); scanf("%s",filepn);/*输入文件路径及名称*/ if((fp=fopen(filepn,"r+"))==NULL) { printf("不能打开文件!\n"); return 0; } fscanf(fp," 考试成绩管理系统 \n"); fscanf(fp,"作者:周纯钢 班级: 信息023 学号:11 \n"); fscanf(fp,"-----------------------------------------\n"); fscanf(fp,"|学号\t|姓名\t|语文\t|英语\t|数学\t|\n"); fscanf(fp,"-----------------------------------------\n");/*读入表格域*/ printf(" 考试成绩管理系统 \n"); printf(" 作者:周纯钢 班级: 信息023 学号:11 \n"); printf("-----------------------------------------\n"); printf("|学号\t|姓名\t|语文\t|英语\t|数学\t|\n"); printf("-----------------------------------------\n");/*打印表格域*/ m=m+1; if(m==1) { p1=(score *)malloc(LEN); /*开辟一个新单元*/ fscanf(fp,"%d%s%f%f%f",&p1->number,p1->name,&p1->yuwen,&p1->yingyu,&p1->shuxue); printf("|%d\t|%s\t|%.1f\t|%.1f\t|%.1f\t|\n",p1->number,p1->name,p1->yuwen,p1->yingyu,p1->shuxue); /*文件读入与显示*/ head=NULL; do { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(score *)malloc(LEN); /*开辟一个新单元*/ fscanf(fp,"%d%s%f%f%f\n",&p1->number,p1->name,&p1->yuwen,&p1->yingyu,&p1->shuxue); |
|
|
| 220.175.59.* |
29楼 /*文件读入与显示*/ }while(!feof(fp)); p2->next=p1; p1->next=NULL; n=n+1; }printf("-----------------------------------------\n");/*表格下线*/ fclose(fp);/*结束读入,关闭文件*/ return (head); } /*==============================================================================================*/ /*==============================================================================================*/ score *add2311(score *head,score *stu) /*函数add2311,功能:追加学生资料,并且将所有学生资料按学号排序*/ { score *p0,*p1,*p2,*p3,*max; int i,j; float fen; char t[10]; p3=stu=(score *)malloc(LEN);/*开辟一个新单元*/ printf("\n输入要增加的学生的资料!"); repeat4: printf("请输入学生学号(学号应大于0):"); scanf("%d",&stu->number); /*输入学号,学号应大于0*/ while(stu->number<0) {getchar(); printf("输入错误,请重新输入学生学号:"); scanf("%d",&stu->number);}/*输入错误,重新输入学号*/ /******************************************************/ if(stu->number==0) goto end2;/*当输入的学号为0时,转到末尾,结束追加*/ else { p3=head; if(n>0) {for(i=0;i<n;i++) {if(stu->number!=p3->number) p3=p3->next; else {printf("学号重复,请重输!\n"); goto repeat4; /*当输入的学号已经存在,程序报错,返回前面重新输入*/ } } } } /******************************************************/ printf("输入学生姓名:"); scanf("%s",stu->name); /*输入学生姓名*/ printf("请输入语文成绩(0~100):"); scanf("%f",&stu->yuwen); /*输入语文成绩,成绩应在0-100*/ while(stu->yuwen<0||stu->yuwen>100) {getchar(); printf("输入错误,请重新输入语文成绩"); scanf("%f",&stu->yuwen);} /*输入错误,重新输入语文成绩直到正确为止*/ printf("请输入英语成绩(0~100):"); scanf("%f",&stu->yingyu);/*输入英语成绩,成绩应在0-100*/ while(stu->yingyu<0||stu->yingyu>100) {getchar(); printf("输入错误,请重新输入英语成绩"); scanf("%f",&stu->yingyu);}/*输入错误,重新输入英语成绩直到正确为止*/ printf("请输入数学成绩(0~100):"); scanf("%f",&stu->shuxue);/*输入数学成绩,成绩应在0-100*/ while(stu->shuxue<0||stu->shuxue>100) {getchar(); printf("输入错误,请重新输入数学成绩"); scanf("%f",&stu->shuxue);}/*输入错误,重新输入数学成绩直到正确为止*/ p1=head; p0=stu; if(head==NULL) {head=p0;p0->next=NULL;}/*当原来链表为空时,从首结点开始存放资料*/ else/*原来链表不为空*/ { if(p1->next==NULL)/*找到原来链表的末尾*/ { p1->next=p0; p0->next=NULL;/*将它与新开单元相连接*/ } else { while(p1->next!=NULL)/*还没找到末尾,继续找*/ { p2=p1;p1=p1->next; } p1->next=p0; p0->next=NULL; } } n=n+1; p1=head; p0=stu; for(i=1;i<n;i++) { for(j=i+1;j<=n;j++) { max=p1; p1=p1->next; if(max->number>p1->number) { k=max->number; max->number=p1->number; p1->number=k; /*交换前后结点中的学号值,使得学号大者移到后面的结点中*/ strcpy(t,max->name); strcpy(max->name,p1->name); strcpy(p1->name,t); /*交换前后结点中的姓名,使之与学号相匹配*/ fen=max->yuwen; max->yuwen=p1->yuwen; |
|
|
| 220.175.59.* |
30楼 /*交换前后结点中的语文成绩,使之与学号相匹配*/ fen=max->yingyu; max->yingyu=p1->yingyu; p1->yingyu=fen; /*交换前后结点中的英语成绩,使之与学号相匹配*/ fen=max->shuxue; max->shuxue=p1->shuxue; p1->shuxue=fen; /*交换前后结点中的数学成绩,使之与学号相匹配*/ } } max=head;p1=head;/*重新使max,p指向链表头*/ } end2: printf("现在的学生数为:%d个!\n",n); return(head); } /*==============================================================================================*/ /*==============================================================================================*/ score *search2311(score *head) /*函数search2311,功能:查询学生成绩*/ {int number; score *p1,*p2; printf("输入要查询的学生的学号,"); scanf("%d",&number); while(number!=0) { if(head==NULL) {printf("\n没有任何学生资料!\n");return(head);} printf("-----------------------------------------\n"); printf("|学号\t|姓名\t|语文\t|英语\t|数学\t|\n"); printf("-----------------------------------------\n");/*打印表格域*/ p1=head; while(number!=p1->number&&p1->next!=NULL) {p2=p1;p1=p1->next;} if(number==p1->number) {printf("|%d\t|%s\t|%.1f\t|%.1f\t|%.1f\t|\n",p1->number,p1->name,p1->yuwen,p1->yingyu,p1->shuxue); printf("-----------------------------------------\n");}/*打印表格域*/ else printf("%d不存在此学生!\n",number); printf("输入要查询的学生的学号,"); scanf("%d",&number); } printf("已经退出了!\n"); return(head);} /*==============================================================================================*/ /*==============================================================================================*/ score *del2311(score *head)/*函数del2311,功能:删除学生资料*/ { score *p1,*p2; int number; printf("输入要删除的学生的学号(输入0时退出):"); scanf("%d",&number); getchar(); while(number!=0)/*输入学号为0时退出*/ { if(head==NULL) { printf("\n没有任何学生资料!\n"); return(head); } p1=head; while(number!=p1->number&&p1->next!=NULL) /*p1指向的不是所要找的首结点,并且后面还有结点*/ { p2=p1;p1=p1->next; } /*p1后移一个结点*/ if(number==p1->number) /*找到了*/ { if(p1==head) head=p1->next; /*若p1指向的是首结点,把地二个结点地址赋予head*/ else p2->next=p1->next; /*否则将下一个结点地址 赋给前一结点地址*/ printf("删除:%d\n",number);n=n-1; } else printf("%d不存在此学生!\n",number); /*找不到该结点*/ printf("输入要删除的学生的学号:"); scanf("%d",&number); getchar(); } #ifdef DEBUG printf("已经退出了!\n"); #endif printf("现在的学生数为:%d个!\n",n); return(head); } /*==============================================================================================*/ /*==============================================================================================*/ void print2311(score *head) /*函数print2311,功能:显示学生成绩*/ { score *p; if(head==NULL) {printf("\n没有任何学生资料!\n");} else {printf("%d\n",n); printf("-----------------------------------------\n"); printf("|学号\t|姓名\t|语文\t|英语\t|数学\t|\n"); |
|
|
