#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define AVAILABLE 0
#define UNAVAILABLE 1
#define MAXNAME 30
#define CLEAR "cls"
typedef struct match{
char *name;
int id;
int count;
int status;
struct match *next;
}Match;
void welcome();
void error(char *);
char land();
int player();
int usrmanager();
int viewmatch();
int addmatch();
int modmatch();
int delmatch();
void printmatch(Match *);
Match *lookup(char *, int);
int joinmatch();
int finishmatch();
Match *head = NULL;
int nmatch = 0;
//主函数
int main(){
welcome();
return 0;
}
void welcome(){
printf("*************************欢迎使用魔方比赛管理系统*************************\n");
while(1){
printf("-------------------------------------\n");
printf("请输入项目前编号执行相关操作:\n");
printf("\n[1] 管理员入口\n\n[2] 选手入口\n\n[0] 退出\n\n");
switch(land()){
case '1':
while(1){
if(usrmanager()){
continue;
}else{
break;
}
}
break;
case '2':
while(1){
if(player()){
continue;
}else{
break;
}
}
break;
case '0':
exit(0);
default:
error("采集项目参数失败");
}
}
}
char land(){
char c, flag;
int i;
while(1){
for(i = 0; (c = getchar()) != '\n'; flag = c, ++i)
;
if(i == 1){
if(flag == '1' || flag == '2' || flag == '0'){
return flag;
}
}else if(i == 0){
continue;
}
error("[请输入命令: 1 / 2 / 0 选择项目]");
}
return '\0';
}
void error(char *s){
fprintf(stderr, "%s\n", s);
}
char dostudent(){
char c, flag;
int i;
while(1){
for(i = 0; (c = getchar()) != '\n'; flag = c, ++i )
;
if(i == 1){
if(flag == '1' || flag == '2' || flag == '3' || flag == '0'){
return flag;
}
}else if(i == 0){
continue;
}
error("[请输入命令: 1 / 2 / 3/ 0 选择项目]");
}
}
int player(){
printf("-------------------------------------\n");
printf("请输入项目前编号执行相关操作:\n\n");
printf("[1] 报名比赛\n[2] 完成比赛\n[3] 查看比赛\n");
printf("[0] 返回主页\n");
switch(dostudent()){
case '1':
joinmatch();
break;
case '2':
finishmatch();
break;
case '3':
viewmatch();
break;
case '0':
return 0;
default:
error("采集项目参数失败");
break;
}
return 1;
}
int joinmatch(){
system(CLEAR);
char sname[MAXNAME];
printf("搜索比赛:\n");
scanf("%s", sname);
getchar();
Match *bp;
char *namep = NULL;
if((bp = lookup(namep = strdup(sname), 0)) == NULL){
printf("比赛 %s 不存在\n", namep);
fflush(stdin);
return -1;
}else{
printmatch(bp);
if(! bp->status){
printf("参加比赛: Y N\n");
}else{
printf("比赛进行中\n");
return 0;
}
while(1){
char c = '\0';
scanf("%c", &c);
getchar();
if(!(c == 'Y' || c == 'y' || c == 'N' || c == 'n')){
fflush(stdin);
error("指令无效");
continue;
}
if(c == 'Y' || c == 'y'){
--bp->count > -1 ? printf("成功\n") : printf("失败\n");
if(! bp->count){
bp->status = UNAVAILABLE;
}
return bp->id;
}else{
return 0;
}
}
}
return -1;
}
int finishmatch(){
system(CLEAR);
char sname[MAXNAME];
printf("完成比赛:\n");
scanf("%s", sname);
getchar();
Match *bp;
char *namep;
if((bp = lookup(namep = strdup(sname), 0)) == NULL){
fflush(stdin);
printf("此赛事没有注册\n");
return -1;
}else{
printmatch(bp);
printf("完成比赛: Y N\n");
while(1){
char c = '\0';
scanf("%c", &c);
getchar();
if(!(c == 'Y' || c == 'y' || c == 'N' || c == 'n')){
fflush(stdin);
error("指令无效");
}else if(c == 'Y' || c == 'y'){
bp->count++ ? 0: (bp->status = AVAILABLE);
printf("成功\n");
return bp->id;
}else{
return 0;
}
}
}
return -1;
}
char domanager(){
char c, flag;
int i;
while(1){
for(i = 0; (c = getchar()) != '\n'; flag = c, ++i)
;
if(i == 1){
if(flag == '1' || flag == '2' || flag == '3' || flag == '4' || flag == '0'){
return flag;
}
}else if(i == 0){
continue;
}
error("[请输入命令: 1 / 2 / 3/ 4 / 0 选择项目]");
}
return '\0';
}
int usrmanager(){
printf("-------------------------------------\n");
printf("请输入项目前编号执行相关操作:\n\n");
printf("[1] 查看比赛项目\n[2] 添加新项目\n[3] 修改已有项目\n[4] 删除比赛项目\n");
printf("[0] 返回主页\n");
switch(domanager()){
case '1':
viewmatch();
break;
case '2':
addmatch();
break;
case '3':
modmatch();
break;
case '4':
delmatch();
break;
case '0':
return 0;
default:
error("采集项目参数失败");
break;
}
return 1;
}
int viewmatch(){
system(CLEAR);
Match *bp;
for(bp =head; bp != NULL; bp = bp->next){
printmatch(bp);
}
if(nmatch){
return nmatch;
}else{
printf("比赛没有这个项目\n");
return 0;
}
return -1;
}
void printmatch(Match *bp){
printf("项目编号:%d\n", bp->id);
printf("项目名:%s\n", bp->name);
printf("项目状态:%s\n", bp->status ? "未开始" : "开始");
}
int addmatch(){
system(CLEAR);
char sname[MAXNAME];
int id, count;
char *namep;
while(1){
id = 0;
count = 0;
namep = NULL;
printf("请输入比赛名:\n");
scanf("%s", sname);
getchar();
printf("请输入小于1000的比赛编号:\n");
scanf("%d", &id);
getchar();
if(!(id > 0 && id < 1000)){
error("输入编号不符合要求");
fflush(stdin);
continue;
}
printf("请输入小于50的比赛选手数量:\n");
scanf("%d", &count);
getchar();
if(!(count > 0 && count < 50)){
error("输入数目不符合要求");
fflush(stdin);
continue;
}
Match *bp;
if((bp = lookup(namep = strdup(sname), id)) == NULL){
bp = (Match *)malloc(sizeof (Match));
if(bp == NULL || (bp->name = namep) == NULL){
error("添加比赛失败");
fflush(stdin);
continue;
}
bp->id = id;
bp->count = count;
bp->status = AVAILABLE;
++nmatch;
bp->next = head;
head = bp;
printf("比赛 %s 注册成功\n", sname);
return id;
}else{
fflush(stdin);
error("项目名或编号已经被占用");
}
}
return -1;
}
Match *lookup(char *s, int id){
Match *bp;
for(bp = head; bp != NULL; bp = bp->next){
if(id == bp->id || strcmp(s, bp->name) == 0){
return bp;
}
}
return NULL;
}
int modmatch(){
system(CLEAR);
Match *bp = NULL;
while(1){
int id = 0;
printf("请输入要修改信息的项目编号:\n");
scanf("%d", &id);
getchar();
if(!(id > 0 && id < 1000)){
fflush(stdin);
error("项目编号不符合要求");
continue;
}
if((bp = lookup("", id)) != NULL){
printf("以下为要修改的项目的现有信息:\n");
printmatch(bp);
printf("[1] 修改项目名\n[2] 修改比赛选手数量\n[3] 修改项目状态\n[0] 放弃修改\n");
while(1){
int i;
scanf("%d", &i);
getchar();
if(!(i == 1 || i == 2 || i == 3 || i == 0)){
fflush(stdin);
error("输入项目前编号选择相关项目");
continue;
}
switch(i){
case 1:
while(1){
char newname[MAXNAME], *namep;
printf("项目名:\n");
scanf("%s", newname);
getchar();
if((lookup(namep = strdup(newname), 0)) != NULL){
fflush(stdin);
error("项目名已存在");
continue;
}
if((bp->name = namep) != NULL){
printf("修改成功\n");
return id;
}
}
break;
case 2:
while(1){
int bnum;
printf("输入比赛数量:\n");
scanf("%d", &bnum);
getchar();
if(!(bnum > 0 || bnum < 1000)){
fflush(stdin);
error("数目不符合要求");
continue;
}
bp->count = bnum;
printf("修改成功\n");
return id;
}
break;
case 3:
while(1){
char c;
int status;
status = bp->status;
status == AVAILABLE ? printf("修改状态为[未开始]: Y N\n") : printf("修改状态为[开始]: Y N\n");
scanf("%c", &c);
getchar();
if(!( c == 'Y' || c == 'y' || c == 'N' || c == 'n')){
fflush(stdin);
error("指令无效");
continue;
}
if(c == 'Y' || c == 'y'){
status == AVAILABLE ? bp->status = UNAVAILABLE : (bp->status = AVAILABLE);
printf("修改成功\n");
return id;
}else{
printf("放弃修改\n");
return 0;
}
}
break;
case 0:
return 0;
default:
error("获取参数失败");
break;
}
}
}else{
printf("没有此比赛\n");
return 0;
}
}
return -1;
}
int delmatch(){
system(CLEAR);
Match *bp, *fbp;
int id = 0;
printf("请输入要注销的项目编号:\n");
scanf("%d", &id);
getchar();
if(!(id > 0 && id < 1000)){
fflush(stdin);
error("项目编号不符合要求");
return -1;
}
for(fbp = bp = head; bp != NULL; fbp = bp, bp = bp->next){
if(bp->id == id){
printmatch(bp);
break;
}
}
if(bp != NULL){
while(1){
printf("确定要注销此项目:Y N\n");
char YN;
scanf("%c", &YN);
getchar();
if(!((YN == 'Y' || YN == 'y') || (YN == 'N' || YN == 'n'))){
fflush(stdin);
error("指令无效\n");
continue;
}
if(YN == 'Y' || YN == 'y'){
bp == head ? head = bp->next : (fbp->next = bp->next);
free(bp->name);
free(bp);
bp = NULL;
printf("项目注销成功\n");
break;
}else{
printf("放弃注销\n");
return 0;
}
}
return id;
}else{
printf("要注销的项目编号不存在\n");
return -1;
}
return -1;
}
主界面包含管理员入口、选手入口、和退出功能。
管理员入口进入有如下功能
选手入口进入有如下功能