#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
char num[10];
char name[15];
char preject[20];
int reward;
};
struct Node
{
struct student data;
struct Node* next;
};
struct Node* createlist()
{
struct Node*listHeadNode=(struct Node*)malloc(sizeof(struct Node));
listHeadNode->next=NULL;
return listHeadNode;
}
struct Node* createNode(struct student data)
{
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=https://bbs.csdn.net/topics/data;
newNode->next=NULL;
return newNode;
}
//插入
void insertNodeByHead(struct Node* listHeadNode,struct student data)
{
struct Node* newNode=createNode(data);
newNode->next=listHeadNode->next;
listHeadNode->next=newNode;
}
//洗掉;
void deleteNodeByAppoinName(struct Node* listHeadNode,char* name)
{
struct Node* posFrontNode=listHeadNode;
struct Node* posNode=listHeadNode->next;
if(posNode==NULL)
{
printf("無相關內容,無法洗掉\n");
return;
}
else
{
while(strcmp(posNode->data.name,name))
{
posFrontNode=posNode;
posNode=posFrontNode->next;
if(posNode==NULL)
{
printf("無相關內容,無法洗掉\n");
}}
posFrontNode->next=posNode->next;
free(posNode);
}
}
//查找
struct Node* searchNodeByAppoinNum(struct Node* listHeadNode,char* num)
{
struct Node* pMove=listHeadNode->next;
if(pMove==NULL)
return pMove;
else
while(strcmp(pMove->data.num,num));
{
pMove=pMove->next;
if(pMove==NULL)
exit(0);
}
return pMove;
}
void printNode(struct Node* curNode)
{
printf("學號\t姓名\t專案\t獎勵\n");
printf("\t%s\t%s\t%s\t%d\n",curNode->data.num,curNode->data.name,curNode->data.preject,curNode->data.reward);
}
//列印
void printlist(struct Node* listHeadNode)
{
struct Node* pMove=listHeadNode->next;
printf("學號\t姓名\t專案\t獎勵\n");
while(pMove)
{
printf("\t%s\t%s\t%s\t%d\n",pMove->data.num,pMove->data.name,pMove->data.preject,pMove->data.reward);
pMove=pMove->next;
printf("\n");
}
}
void readinfofromfile(char* fileName,struct Node* listHeadNode)
{
FILE* fp=fopen(fileName,"r");
if(fp==NULL)
{
fp=fopen(fileName,"w");
}
struct student tempData;
while(fscanf(fp,"%s\t%s\t%s\t%d\n",tempData.num,tempData.name,tempData.preject,tempData.reward)!=EOF)
{
insertNodeByHead(listHeadNode,tempData);
memset(&tempData,0,sizeof(tempData));
}
fclose(fp);
}
void saveinfotofile(char* fileName,struct Node* listHeadNode)
{
FILE* fp=fopen(fileName,"w");
struct Node*pMove=listHeadNode->next;
while(pMove)
{
fprintf(fp,"\t%s\t%s\t%s\t%d\n",pMove->data.num,pMove->data.name,pMove->data.preject,pMove->data.reward);
pMove=pMove->next;
}
fclose(fp);
}
void systemMenu()
{printf("***************歡迎來到研究生科研獎勵系統******************\n");
printf("\t0.【退出系統】\n");
printf("\t1.【插入資訊】\n");
printf("\t2.【洗掉資訊】\n");
printf("\t3.【瀏覽資訊】\n");
printf("\t4.【修改資訊】\n");
printf("\t5.【查找資訊】\n");
printf("************************************************************\n");
printf("請輸入0~5其中一個數");
}
void keyDown()
{
int userkey;
struct student tempData;
scanf("%d,&userkey");
switch(userkey)
{
case '0':
printf("\t\t0.【退出系統】\n");
system("pause");
exit(0);
case '1':
printf("\t\t1.【插入資訊】\n");
printf("請輸入學生的學號,姓名,專案,獎勵。");
scanf("%d%s%s%d",tempData.num,tempData.name,tempData.preject,tempData.reward);
struct Node* list; insertNodeByHead(list,tempData);
saveinfotofile("student.txt",list);
break;
case '2':
printf("\t\t2.【瀏覽資訊】\n");
printlist(list);
break;
case '3':
printf("\t\t3.【洗掉資訊】\n");
printf("請輸入要洗掉的學生的姓名:");
scanf("%s",tempData.name);
deleteNodeByAppoinName(list,tempData.name);
saveinfotofile("student.txt",list);
break;
case '4':
printf("\t\t4.【修改資訊】\n");
printf("請輸入要修改學生的學號:");
scanf("%s",tempData.num);
if(searchNodeByAppoinNum(list,tempData.num)==NULL)
printf("未找到相關資訊");
else
{
struct Node* curNode=searchNodeByAppoinNum(list,tempData.num);
printf("請輸入新的學生獎勵資訊:學號,姓名,專案,獎勵");
scanf("%d%s%s%d",curNode->data.num,curNode->data.name,curNode->data.preject,curNode->data.reward);
saveinfotofile("student.txt",list);
}
break;
case '5':
printf("\t\t5.【查找資訊】\n");
printf("請輸入要查找學生的學號:");
scanf("%s",tempData.num);
if(searchNodeByAppoinNum(list,tempData.num)==NULL)
printf("未找到相關資訊");
else
printNode(searchNodeByAppoinNum(list,tempData.num));
default :printf("輸入錯誤請重新輸入\n");
}}
void main()
{
struct Node* list=createlist();
readinfofromfile("student.txt",list);
while(1)
{
systemMenu();
keyDown();
system("pause");
system("cls");
}}
uj5u.com熱心網友回復:
把每一句case陳述句后面'0'的單引號去掉。case 0:uj5u.com熱心網友回復:
計算機中,‘0’!=0,一個是字符,一個是數字,不同的ascii值。uj5u.com熱心網友回復:
void keyDown()
{
int userkey;
struct student tempData;
//scanf("%d,&userkey");
scanf("%d", &userkey);
if (userkey < 0 || userkey > 5) {
return;
}
userkey += '0';
switch(userkey)
{
case '0':
printf("\t\t0.【退出系統】\n");
system("pause");
exit(0);
case '1':
printf("\t\t1.【插入資訊】\n");
printf("請輸入學生的學號,姓名,專案,獎勵。");
scanf("%d%s%s%d",tempData.num,tempData.name,tempData.preject,tempData.reward);
struct Node* list; insertNodeByHead(list,tempData);
saveinfotofile("student.txt",list);
break;
case '2':
printf("\t\t2.【瀏覽資訊】\n");
printlist(list);
break;
case '3':
printf("\t\t3.【洗掉資訊】\n");
printf("請輸入要洗掉的學生的姓名:");
scanf("%s",tempData.name);
deleteNodeByAppoinName(list,tempData.name);
saveinfotofile("student.txt",list);
break;
case '4':
printf("\t\t4.【修改資訊】\n");
printf("請輸入要修改學生的學號:");
scanf("%s",tempData.num);
if(searchNodeByAppoinNum(list,tempData.num)==NULL)
printf("未找到相關資訊");
else
{
struct Node* curNode=searchNodeByAppoinNum(list,tempData.num);
printf("請輸入新的學生獎勵資訊:學號,姓名,專案,獎勵");
scanf("%d%s%s%d",curNode->data.num,curNode->data.name,curNode->data.preject,curNode->data.reward);
saveinfotofile("student.txt",list);
}
break;
case '5':
printf("\t\t5.【查找資訊】\n");
printf("請輸入要查找學生的學號:");
scanf("%s",tempData.num);
if(searchNodeByAppoinNum(list,tempData.num)==NULL)
printf("未找到相關資訊");
else
printNode(searchNodeByAppoinNum(list,tempData.num));
default :
printf("輸入錯誤請重新輸入\n");
}
}
供參考~
uj5u.com熱心網友回復:
試過了,沒用,
uj5u.com熱心網友回復:
單步除錯和設斷點除錯(VS IDE中編譯連接通過以后,按F10或F11鍵單步執行,按Shift+F11退出當前函式;在某行按F9設斷點后按F5執行停在該斷點處。)是程式員必須掌握的技能之一。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/247358.html
標籤:C語言
上一篇:哪位大神幫我看一下?
