//问题与代码
/*Copyright (c) 2015,烟台大学计算机与控制工程学院
*All rights reserved. *文件名称:piao.h,main.cpp,zdy.cpp *作者:陈梦萍 *完成日期:2015年12月30日 *版本号:v1.0 * *问题描述: 学生投票系统 *输入描述:竞选学生信息 *程序输出:学生数据 */
#include "stdio.h" #include "stdlib.h" #include "string.h" # include "conio.h" //getch() # define N sizeof(struct student) //测结构体变量的大小// typedef struct student { char number[20]; char name[20]; char sex[20]; char Job[20]; char jieshao[40]; int piao; //得票 struct student *next; } stu; void disp1(stu *p); void disp2(stu *p); void hiNum(stu *h); stu *shuru(stu *head); stu *delet(stu *p0); stu *xiugai(stu *h); void findNum(stu *h); stu *buble(stu *head); int MainMenu(); int ManageMenu(); int StudentMenu();
#include "piao.h" void disp1(stu *p) //显示全部信息// { while (p != NULL) { printf("\n学号:%s 姓名:%s 性别:%s 目前职务:%s 自我介绍:%s \n", p->number, p->name, p->sex, p->Job, p->jieshao); p = p->next; } printf("\n按任意键继续!"); getch(); } void disp2(stu *p) //显示全部信息// { while (p != NULL) { printf("\n学号:%s 姓名:%s 性别:%s 目前职务:%s 自我介绍:%s 得票:%d\n", p->number, p->name, p->sex, p->Job, p->jieshao,p->piao); p = p->next; } printf("\n按任意键继续!"); getch(); } void hiNum(stu *h) //票票+1 { char num[20]; stu *p; char shifou; printf("\n请输入你要投票的学生的学号:"); scanf("%s", num); for (p = h; strcmp(p->number, num) != 0; p = p->next); printf("\n学号:%s 姓名:%s 性别:%s 目前职位:%s 自我介绍:%s \n\n", p->number, p->name, p->sex, p->Job, p->jieshao); printf("\n是Y,否N:"); shifou = getch(); if (shifou == 'Y' || shifou == 'y') { p->piao = p->piao +1; } printf("按任意键继续!"); getch(); } stu *shuru(stu *head) //录入信息// { int piao; stu *p; p = (stu *)malloc(N); printf("\n请输入学生信息:\n"); printf("\n学号:"); scanf("%s", p->number); printf("\n姓名:"); scanf("%s", p->name); printf("\n性别:"); scanf("%s", p->sex); printf("\n目前职务:"); scanf("%s", p->Job); printf("\n自我介绍:"); scanf("%s", p->jieshao); printf("\n得票:"); scanf("%d", &piao); p->piao = piao; if (head!= NULL) { p->next = head; head = p; } else { head = p; p->next = NULL; } printf("\n录入成功!输入任意键继续!"); getch(); return head; } stu *delet(stu *p0) //删除信息// { char num[20]; stu *p; char choice; if (p0== NULL) { printf("信息出错!按任意键退出!"); getch(); } else { printf("\n请输入要删除学生的学号:"); scanf("%s", num); for (p = p0->next; strcmp(p->number, num) != 0; p0 = p, p = p->next); //找到要删除的学生的信息// printf("你要删除的信息如下,请确认是否删除:\n"); printf("\n学号:%s 姓名:%s 性别:%s 目前职位:%s 自我介绍:%s 票数%s \n\n", p->number, p->name, p->sex, p->Job, p->jieshao,p->piao); printf("\n是Y,否N:"); //显示要删除的学生的信息// choice = getch(); if (choice == 'Y' || choice == 'y') { p0->next = p->next; free(p); printf("\n文件删除成功!"); } printf("\n按任意键结束!"); getch(); return p0; } } stu *xiugai(stu *h) //修改信息// { int piao; char num[20]; stu *p; char choice; printf("\n请输入要修改学生信息的学号:"); scanf("%s", num); for (p = h; strcmp(p->number, num) != 0; p = p->next); //找到要的学生的信息// while (1) { printf("请输入修改后学生的信息:\n"); printf("学号:"); scanf("%s", p->number); printf("\n姓名:"); scanf("%s", p->name); printf("\n性别:"); scanf("%s", p->sex); printf("\n目前职务:"); scanf("%s", p->Job); printf("\n自我介绍:"); scanf("%s", p->jieshao); printf("\n得票:"); scanf("%d", &piao); p->piao = piao; printf("修改后学生信息如下:\n"); printf("\n学号:%s 姓名:%s 性别:%s 目前职务:%s jieshao:%s 得票:%d\n\n", p->number, p->name, p->sex, p->Job, p->jieshao, p->piao); printf("\n确认?是Y否N:"); choice = getch(); if (choice == 'y' || choice == 'Y') break; } printf("\n修改成功,输入任意键继续!"); getch(); return h; } void findNum(stu *h) //查询信息// { char num[20]; stu *p; printf("\n请输入你要查找的学生的学号:"); scanf("%s", num); for (p = h; strcmp(p->number, num) != 0; p = p->next); printf("你查找的学生信息如下:\n"); printf("\n学号:%s 姓名:%s 性别:%s 目前职务:%s 自我介绍:%s \n\n", p->number, p->name, p->sex, p->Job, p->jieshao); printf("输入任意键继续!"); getch(); } stu *buble(stu *head) //排序 { stu* pSearch; stu* pSearch2; char number[20]; char name[20]; char sex[20]; char Job[20]; char jieshao[40]; int piao;//得票 pSearch = head; while (pSearch!=NULL) { pSearch2 = pSearch; while (pSearch2!=NULL) { if (pSearch2->piao>pSearch->piao) { //完成数值交换 strcpy(number, pSearch2->number); strcpy(name, pSearch2->name); strcpy(sex, pSearch2->sex); strcpy(Job, pSearch2->Job); strcpy(jieshao, pSearch2->jieshao); piao = pSearch2->piao; //赋值 strcpy(pSearch2->number, pSearch->number); strcpy(pSearch2->name, pSearch->name); strcpy(pSearch2->sex, pSearch->sex); strcpy(pSearch2->Job, pSearch->Job); strcpy(pSearch2->jieshao, pSearch->jieshao); pSearch2->piao = pSearch->piao; strcpy(pSearch->number, number); strcpy(pSearch->name, name); strcpy(pSearch->sex, sex); strcpy(pSearch->Job, Job); strcpy(pSearch->jieshao, jieshao); pSearch->piao = piao; } pSearch2 = pSearch2->next;//指向下一个节点 } pSearch = pSearch->next;//指向下一个节点 } disp2(head); printf("输入任意键继续!"); getch(); return(head); /*返回头指针*/ } //============================系统主菜单========================== int MainMenu() { int ok; system("cls"); printf("=========================================================================\n"); printf(" 学生投票页面! \n"); printf(" 1.管理员 \n"); printf(" 2.学生 \n"); printf(" 0.退出 \n"); scanf("%d", &ok); return ok; } //============================管理员菜单========================== int ManageMenu() { int choice; system("cls"); printf(" 1. 添加竞选学生 \n"); printf(" 2. 删除学生信息 \n"); printf(" 3. 修改学生信息 \n"); printf(" 4. 查询学生信息 \n"); printf(" 5. 查看投票情况 \n"); printf(" 0. 返 回 \n"); printf("请输入你要执行的操作代码:"); scanf("%d", &choice); return choice; } //============================学生菜单========================== int StudentMenu() { int choice; system("cls"); printf("========================== *** 学生菜单 *** =========================\n"); printf(" 1. 查询学生信息 \n"); printf(" 2. 投票 \n"); printf(" 0. 返 回 \n"); printf("请输入你要执行的操作代码:");
scanf("%d", &choice); return choice; }
#include "piao.h" int main() { int Choose1,Choose2,Choose3,End1 = 1,End2 = 1,End3 = 1; char sName[10]; char sPassword[10]; int i; //choice用于选择对数据执行操作,i循环,ok主界面选择// stu *head; head = NULL; while (End1) { Choose1 = MainMenu(); switch (Choose1) { case 0: End1 = 0; break; case 1: printf("==================================\n"); printf("请输入账号:\n"); scanf("%s", sName); printf("==================================\n"); printf("请输入密码:\n"); scanf("%s", sPassword); if (strcmp(sName,"admin")!=0) { printf("账号错误!\n"); } else { if (strcmp(sPassword,"abc123")!=0) { printf("密码错误!\n"); } else { End2 = 1; while (End2) { Choose2 = ManageMenu(); switch (Choose2) { case 1: head = shuru(head); break; case 2: head = delet(head); break; case 3: head = xiugai(head); break; case 4: findNum(head); break; case 5: head = buble(head); break; case 0: End2 = 0; break; default: break; } } } } break; case 2: End3 = 1; while (End3) { Choose3 = StudentMenu(); switch (Choose3) { case 0: End3 = 0; break; case 1: disp1(head); break; case 2: hiNum(head); default: break; } } break; default: break; } } }
这次的课程设计,终于在我的不断抓狂中结束了,回想这几天的历程,觉得确实是辛苦又值得,了解到许多与专业相关的知识,和一些经验教训,在我的设计中,我使用了单链表进行存储,并进行整体投票工作。
今天才发上来,当初我写完之后存草稿里了,然后就没有然后了。。。不知道是当初没存好还是怎么样,运行结果的图片丢失了,我下了codeblocks之后发现还要装gcc,然后各种搞不定。。。就这么发上来了。。。