1.学生管理系统
注释:函数名是其意义的拼音,函数ss()是文件内内容读到链表,函数save_stu(stu p)是将结构体写入文件
里面有很多注释不是我懒,那都是我的心酸历程留着纪念吧
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
typedef struct student {
char name[4];
char xingbie[10];
int nianling;
int daying;
int gaoshu;
int jindaishi;
int tiyu;
int jidao;
int score;
int j;
int nianjipaiming;
struct student *next;
int x;
}Stu;
#define N sizeof(Stu)
typedef Stu *stu;
stu head;
void luru();
void shanchu();
void xiugai();
void chaxun();
void zonglan();
void tuichu();
stu ss();//从文件中提取到链表中
void save_stu(stu p);//将结构体存到文件中
void menu();
int yanzhengma();
stu paixu(stu head);
int yanzhengma() {
int h = 3;
printf("您共有3次机会。");
while (h) {
int a[2];
int b, c;
//int a[5]={0};
srand(time(NULL));
for (int i = 0; i < 2; i++) {
a[i] = rand() % 15;
}
switch (rand() % 4) {
case 0:
b = '+', c = a[0] + a[1];
break;
case 1:
b = '-', c = a[0] - a[1];
break;
case 2:
b = '*', c = a[0] * a[1];
break;
//case 3:b='/',c=a[0]/a[1];break;
default:
b = '/', c = a[0] / a[1];
break;//break;
}
printf("%d %c %d", a[0], b, a[1]);
printf("请输入验证码(舍弃小数,别打字母会下死你):");
int ch;
scanf("%d", &ch);getchar();
if (ch == c) {
break;
}
printf("您还有%d次机会",--h);
}
if (h) {
return 1;
} else {
return 0;
}
}
//for(int i=0;i<3;i++){
//scanf("%d",a+i);
//}
//return a;
void save_stu(stu p) {
FILE *fp;
fp = fopen("student", "ab");
if (fp == NULL) {
printf("不能打开文件!!!\n");
}
if (fwrite(p, N, 1, fp) != 1) {
printf("书写错误!!!\n");
}
fclose(fp);
}
void luru() {
while (1) {
system("cls");
stu p;
p = (stu )malloc(sizeof(Stu));
printf("请输入该学生姓名:");
p->name ;
scanf("%s", p->name);
getchar();
printf("请输入该学生性别:");
scanf("%s", p->xingbie);
getchar();
printf("请输入该学生年龄:");
scanf("%2d", &p->nianling);
getchar();
printf("请输入该学生大学英语:");
scanf("%3d", &p->daying);
getchar();
printf("请输入该学生高等数学:");
scanf("%3d", &p->gaoshu);
getchar();
printf("请输入该学生近代史:");
scanf("%3d", &p->jindaishi);
getchar();
printf("请输入该学生体育:");
scanf("%3d", &p->tiyu);
getchar();
printf("请输入该学生计算机导论:");
scanf("%3d", &p->jidao);
getchar();
p->score = p->jidao + p->jindaishi + p->daying + p->gaoshu + p->tiyu;
p->nianjipaiming = 0;
save_stu(p);
system("cls");
printf("正在保存. . . \n");
Sleep(500);
system("cls");
printf("________________________________________\n");
printf("| |\n");
printf("| |\n");
printf("| ^__^保存成功!^__^ @是否继续@ |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| 1.是 2.否 |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("|______________________________________|\n");
char t;
while (1) {
t=getch();
if (t == '1')
break;
if (t == '2'){
system("cls");
menu();
}
}
}
}
void menu() {
printf("| @1.录入学生信息@ |\n");
printf("| @2.查找学生信息@ |\n");
printf("| @3.修改学生信息@ |\n");
printf("| @4.总览学生信息@ |\n");
printf("| @5.删除学生信息@ |\n");
printf("| @6.退出学生信息管理系统@ |\n");
int q = 0;
while (1) {
q = getch();
switch (q) {
case '1':
luru();
break;
case '2':
chaxun();
break;
case '3':
xiugai();
break;
case '4':
zonglan();
break;
case '5':
shanchu();
break;
case '6':
tuichu();
break;
default :
break;
}
}
}
stu ss() {
FILE *fp;
stu p, p2, pr = NULL;
stu head=NULL;
fp = fopen("student", "ab+");
if (fp == NULL) {
printf("不能打开文件!!!\n");
}
int t = 0;
while (! feof(fp)) {
p = (stu)malloc(sizeof(Stu));
t++;
if (p == NULL) {
printf("开辟失败\n");
}
fread(p,N,1,fp);
if (t == 1) {
//pr = p;
p2 = p;
//p->next = NULL;
head=p;
} else {
pr = p2;
p2->next = p;
//p->next = NULL;
//pr=p2;
p2 = p;
}
}
if (pr != NULL)
pr->next = NULL;
else
head = NULL;
fclose(fp);
return head;
}
void chaxun() {
char a[4];
system("cls");
printf("请输入该学生姓名:");
gets(a);
while (1) {
int z = yanzhengma();
if (z) {
printf("验证成功!!!\n");
getch();
break;
} else {
printf("验证失败!!!\n");
Sleep(500);
printf("是否选择继续进行查找操作?\n");
printf("若继续进行查找操作请按1;若返回菜单请按2。\n");
if (getch() - '1') {
system("cls");
menu();
} else {
continue;
}
}
}
printf("************************************学生信息****************************************************\n");
printf("------------------------------------------------------------------------------------------------\n");
printf(" 姓名 性别 年龄 大学英语 高等数学 近代史 体育 计算机导论 总分 年级排名\n");
printf("------------------------------------------------------------------------------------------------\n");
int t = 1;
//while (1) {
stu p = NULL;
//char a[100];
p = ss();
//printf("4321");
p=paixu(p);
//printf("\n\n%10d%10d%10d%10d%10d%10d\n\n",head->score ,head->next->score ,head->next->next->score ,head->next->next->next ->score ,head->next->next->next->next->score ,head->nianjipaiming );
//printf("1231");
while (p != NULL) {
if (strcmp(p->name, a)== 0) { //a不可写为a[10]
printf(" %s %s%6d%8d%10d%10d%8d%12d%11d%11d \n", p->name, p->xingbie, p->nianling, p->daying,
p->gaoshu, p->jindaishi, p->tiyu, p->jidao, p->score, p->nianjipaiming);
}
p = p->next;
t++;
}
//}
printf("请输出任意键返回!\n");
getch();
system("cls");
menu();
}
void shanchu() {
system("cls");
FILE *fp;
stu head, p, pre,pr,pra = NULL;
int j = 0, i = 11, x = 0;
head = ss();
head=paixu(head);
char name[100];
p = head;
if (p != NULL) {
p->x = 0;
p = p->next;
}
p=head;
while(1){
while (1) {
system("cls");
printf("请输出您要删除的同学姓名:");
//for(int i=0;i<10;i++){
scanf("%s", name);
//getchar();
int z = yanzhengma();
if (z) {
printf("验证成功!!!\n");
getch();
break;
} else {
printf("验证失败!!!\n");
Sleep(500);
printf("是否选择继续进行删除操作?\n");
printf("若继续进行删除操作请按1;若返回菜单请按2。\n");
while (1) {
char get = getch();
if (get == 2) {
menu();
} else {
if (get == 1)
continue;
}
}
}
}
p=head;
while(p!=NULL){
p->j =0;
p=p->next ;
}
p = head;
j=0;
system("cls");
printf("************************************学生信息****************************************************\n");
printf("------------------------------------------------------------------------------------------------\n");
printf("序号 姓名 性别 年龄 大学英语 高等数学 近代史 体育 计算机导论 总分 年级排名\n");
printf("------------------------------------------------------------------------------------------------\n");
while (p != NULL) {
if (p != NULL && strcmp(p->name, name) == 0) {
j++;
p->j =j;
printf("%d %s %s %d %d %d %d %d %d %d %d \n", j, p->name, p->xingbie, p->nianling, p->daying,
p->gaoshu, p->jindaishi, p->tiyu, p->jidao, p->score, p->nianjipaiming);
//i++;
}
p=p->next;
}
p=pra;
if(j==0){
printf("没有找到您要删除的信息,抱歉!\n");
printf("请问您是否继续进行删除操作,谢谢。\n");
printf("如进行请按1,如返回菜单请按0。\n");
while (1) {
int ch = getch();
if (ch == '0')
menu();
else if (ch == '1') {
break;
}
}
}
//if (ch == '1')
//continue;
int d;
//system("cls");
printf("请输入想要删除同学的序号:");
scanf("%d",&d);
while(p!=NULL){
if(p->j ==d&&strcmp(p->name ,name)==0){
break;
}
pr=p;
p=p->next;
}
//printf("234");
int h;
if(p=head ){
printf("123");
h=1;
}
else{
printf("345");
h=0;
}
system("cls");
printf("______________________________________\n");
printf("| |\n");
printf("| |\n");
printf("| 确认删除? |\n");
printf("| |\n");
printf("| 1.确认 2.抱歉,打错了 |\n");
printf("| |\n");
printf("|____________________________________|\n");
while (1) {
//printf("123");
char sh;
sh = getch();
if (sh == '1') {
//printf("456");
if(h){
//printf("234");
head=head->next;
}
else{
printf("567");
pr->next =p->next;
}
break;
} else if (sh == '2') {
menu();
}
}
//printf("123");
p = head;
while (p != NULL && p->x != x) {
pre = p;
p = p->next;
}
if (p != NULL) {
if (pre == NULL) {
head = head->next;
} else {
pre->next = p->next;
}
}
//free(p);
fp = fopen("student", "wb");
p=head;
if (fp == NULL) {
printf("我真笨连一个文件都开不出来。\n");
}
while (p != NULL) {
if (fwrite(p, N, 1, fp) != 1) {
printf("写错了\n");
}
p = p->next;
}
fclose(fp);
system("cls");
printf("正在删除,请稍后.....\n");
Sleep(500);
system("cls");
printf("____________________________________________\n");
printf("| |\n");
printf("| 删除成功,是否继续? |\n");
printf("| |\n");
printf("| 1.是 2.否 |\n");
printf("| |\n");
printf("| |\n");
printf("|__________________________________________|\n");
char yh;
yh = getch();
while (1) {
if (yh == '1') {
break;
}
if (yh == '2') {
system("cls");
menu();
}
}
}
}
void xiugai() {
stu p;
char a;
FILE *fp;
stu head = ss();
p=head;
p=paixu(p);
head=p;
char namer[100];
while (1) {
system("cls");
printf("请输入您要修改的学生姓名:\n");
scanf("%s", namer);
int z = yanzhengma();
if (z) {
printf("验证成功!!!\n");
getch();
break;
} else {
printf("验证失败!!!\n");
Sleep(500);
printf("是否选择继续进行修改操作?\n");
printf("若继续进行修改操作请按1;若返回菜单请按2。\n");
while (1) {
char get = getch();
if (get == 2) {
menu();
} else {
if (get == 1)
continue;
}
}
}
}
int j=0;
//while (1) {
// pr=p;
// p = p->next;
//if (p != NULL && strcmp(pr->name, namer) != 1)
// break;
// if (p = NULL) {
// system("cls");
// printf("非常抱歉没找到\n");
// }
//} while (p != NULL) {
printf("***************************************学生信息****************************************************\n");
printf("------------------------------------------------------------------------------------------------\n");
printf("序号 姓名 性别 年龄 大学英语 高等数学 近代史 体育 计算机导论 总分 年级排名\n");
printf("------------------------------------------------------------------------------------------------\n");
while (p != NULL){
if (strcmp(p->name, namer)== 0) {
j++;
p->j=j;//a不可写为a[10]
printf("%d %s %s%6d%8d%10d%10d%8d%12d%11d%11d \n",p->j , p->name, p->xingbie, p->nianling, p->daying,
p->gaoshu, p->jindaishi, p->tiyu, p->jidao, p->score, p->nianjipaiming);
}
p = p->next;
// t++;
}
if(j==0){
printf("很抱歉没找到!!!\n");
printf("请按任意键返回!!!");
getch();
system("cls");
menu();
}
printf("请输出您要修改的学生的序号:");
scanf("%d",&j);
p=head;
//printf("%d",p->score);//名字重复如何修改
while(p!=NULL){
if(p->j==j){
break;
}
p=p->next;
}
//printf("%d",p->score);
//printf(" %s %s %d %d %d %d %d %d %d %d \n", pr->name, pr->xingbie, pr->nianling, pr->daying,
// pr->gaoshu, pr->jindaishi, pr->tiyu, pr->jidao, pr->score, pr->nianjipaiming);
printf("请问您想要修改哪条信息?\n");
printf("1.姓名 2.性别 3.年龄 4.大学英语 5.高等数学 6.近代史 \n\n ");
printf("7.体育 8.计算机导论 \n");
while (1) {
a = getch();
printf("请输入将要改为的信息:\n");
//printf("%d",a);
//printf("2");
if (a == '1') {
char namer[100];
gets(namer);
//getchar();
strcpy(p->name, namer);
break;
}
if (a == '2') {
char xingbie[10];
scanf("%s",xingbie);
//printf("234");
//getchar();
strcpy(p->xingbie, xingbie);
//printf("345");
break;
}
if (a == '3') {
//printf("1");
int nianling;
scanf("%d", &nianling);
getchar();
p->nianling = nianling;
break;
}
if (a == '4') {
int daying;
scanf("%d", &daying);
getchar();
p->daying = daying;
}
if (a == '5') {
int gaoshu;
scanf("%d", &gaoshu);
getchar();
p->gaoshu = gaoshu;
}
if (a == '6') {
int jindaishi;
scanf("%d", &jindaishi);
getchar();
p->jindaishi = jindaishi;
}
if (a == '7') {
int tiyu;
scanf("%d", &tiyu);
getchar();
p->tiyu = tiyu;
}
if (a == '8') {
int jidao;
scanf("%d", &jidao);
getchar();
p->jidao = jidao;
}
//printf("123");
if (a >= '4' && a <= '8') {
p->score = p->daying + p->gaoshu + p->jindaishi + p->tiyu + p->jidao;
//head=paixu(head);
break;
}
}
//printf("%s",p->xingbie );
//getchar();getchar();
//printf("876");
system("cls");
printf("________________________________________________________\n");
printf("| |\n");
printf("| |\n");
printf("| 是否确认修改!!! |\n");
printf("| |\n");
printf("| |\n");
printf("| 1.是 ^^ 2.否^^ |\n");
printf("| |\n");
printf("| |\n");
printf("|______________________________________________________|\n");
while (1) {
char wh;
wh = getch();
if (wh == '1'){
system("cls");
printf("修改中请稍后......");
Sleep(500);
p=head;
fp=fopen("student","wb");
if(fp==NULL)
{
printf("打不开文件!!!");
}
while(p!=NULL){
if(fwrite(p,N,1,fp)!=1)
{
printf("写不进去!!!");
break;
}
p=p->next ;
}
fclose(fp);
system("cls");
menu();
break;
}
else if(wh=='2'){
system("cls");
menu();
}
}
//system("cls");
// printf("正在修改请稍等.....");
//Sleep(500);
// fp = fopen("student", "wb");
// if (fp == NULL) {
// system("cls");
// printf("文件打不开了。\n");
// }
// while (p != NULL) {
// if (fwrite(p, N, 1, fp) != 1) {
// system("cls");
// printf("写不进去\n");
// }
// p = p->next;
// }
// fclose(fp);
}
stu paixu(stu head)
{
if(head==NULL){
//printf("1");
return head;
}
if(head->next ==NULL){
//printf("2");
head->nianjipaiming =1;
return head;
}
//printf("3");
stu p, pr, pre,tail;
tail=NULL;
p=head;
pr=head->next;
int n=3;
int j=1;
while(head->next!=tail&&j!=0){
n++;
p=head;
pr=head->next ;
//printf("\n%d",n);
if(p->score < pr->score ){
//printf("%d",p->score);
p->next=pr->next;
pr->next=p;
head=pr;
// printf("%20d",head->score);
//printf("%10d\n",p->score);
}
pre=head;
p=head->next;
pr=head->next->next;
//printf("%10d%10d%10d%10d\n",pre->score,p->score,pr->score,pr->next->score);
int m=0;
j=0;
while(pr!=tail){
//printf("%d",m);
if(p->score <pr->score ){
p->next=pr->next;
pr->next=p;
pre->next=pr;
j++;
// printf("%10d%10d%10d%10d",j,pre->score ,pr->score ,p->score );
}
pre=pre->next;
p=pre->next;
pr=p->next;
}
tail=p;
pre=head;
p=head->next;
pr=head->next->next;
}
//printf("\n9");
int num = 1;
p=head;
while (1) {
p->nianjipaiming = num;
p = p->next;
num++;
if (p == NULL) {
break;
}
}
//printf("\n\n%10d%10d%10d%10d%10d%10d\n\n",head->score ,head->next->score ,head->next->next->score ,head->next->next->next ->score ,head->next->next->next->next->score ,head->nianjipaiming );
//printf("\n10");
return head;
}
void tuichu() {
char ch;
system("cls");
printf("____________________________________________\n");
printf("| |\n");
printf("| |\n");
printf("| 是否确认退出 |\n");
printf("| |\n");
printf("| |\n");
printf("| 1.是^^ 2.否^^ |\n");
printf("| |\n");
printf("|__________________________________________|\n");
while (1) {
ch=getch();
switch(ch )
{
case '1':
system("cls");
printf("欢迎再次使用白氏学生管理系统!!!再见!!!");
exit(0);break;
case '2':
menu();break;
default :break;
}
}
}
void zonglan() {
while(1){
system("cls");
int z = yanzhengma();
if (z) {
printf("验证成功!!!\n");
getch();
break;
}
else {
printf("验证失败!!!\n");
Sleep(500);
printf("是否选择继续进行总览操作?\n");
printf("若继续进行总览操作请按1;若返回菜单请按2。\n");
}
while(1)
{
int g;
g=getch();
if(g==1){
break;
}
else{
if(g==2){
menu();
break;
}
}
}
}
system("cls");
stu head, p;
head = ss();
p=paixu(head);
//printf("123");
printf("***********************************学生管理系统*********************************************\n");
printf("--------------------------------------------------------------------------------------------\n");
printf(" 姓名 性别 年龄 大学英语 高等数学 近代史 体育 计算机导论 总分 年级排名\n");
printf("--------------------------------------------------------------------------------------------\n");
if (head == NULL) {
printf("这里没有学生信息哦~~~亲~~~\n");
printf("请按任意键返回...");
getch();
system("cls");
menu();
printf("234");
}
//p = head;
while (p != NULL)
{
printf("%s %s %d %d %d %d %d %d %d %d\n", p->name, p->xingbie, p->nianling, p->daying,
p->gaoshu, p->jindaishi, p->tiyu, p->jidao, p->score, p->nianjipaiming);
p = p->next;
}
printf("请按任意键返回...\n");
getch();
system("cls");
menu();
}
int main() {
menu();
return 0 ;
}
2.’\r’与’\n’
'\r’将光标移到本行最左端ASCII码13
'\n’将光标向下移一位ASCII码10
Unix系统里,每行结尾只有"<换行>“即”\n"。
windows系统里面,每行结尾是"<回车><换行>“即”\r\n"。
mac系统里,每行结尾是"<回车>",即"\r"。
所以这也可以间接证明printf()函数是从右往左读其中%等值为从左往右读
并且引号外括号内的要算(完了词穷了,看代码吧)
#include<stdio.h>
int main()
{
int a=1;
printf("%d",a,++a);
return 0;
}
#include<stdio.h>
int main()
{
int a=1;
printf("%d",a,a++);
return 0;
}
#include<stdio.h>
int main()
{
int a=1;
printf("%d,%d",++a,a);
return 0;
}
3.if()语句的判断再解析
如果if()语句中判断条件为赋值语句请这样理解,先向其中变量赋值再判断变量所存的值是否为非零值(在文件读取过程中的NULL也被视为零哦)
#include<stdio.h>
int main()
{
int a;
if(a=1){//如果将一改为零则没有输出先看赋值语句然后再a的值
printf("3");
}
return 0;
}
#include<stdio.h>
int main()
{
int a;
if(a=0){//如果将一改为零则没有输出先看赋值语句然后再a的值
printf("3");
}
return 0;
}
一口气攒了三周嘿嘿嘿嘿!!!!
加油加油