#include<stdio.h>#include<stdlib.h>#include<string.h>#include<malloc.h>#define _CRT_SECURE_NO_WARNINGSstruct Node* list = createList; struct commodity{ int num; char name[20]; char price[3]; int storage;};struct Node{//int data;struct commodity data;struct Node* next;}Node;struct Node* createlist(){ struct Node* headNode=(struct Node*)malloc(sizeof(struct Node)); headNode->next =NULL; return headNode;}//創造節點struct Node* createNode(struct commodity data){ struct Node* newNode=(struct Node*)malloc(sizeof(struct Node)); newNode->data =data; newNode->next =NULL; return newNode;}//插入void insertNodeByHead(struct Node* headNode, struct commodity data){ struct Node* newNode = createNode(data); newNode->next = headNode->next; headNode->next = newNode;}//查找struct Node*findByName(struct Node* headNode, char * name){struct Node* pMove = headNode->next;while (pMove){if (!strcmp(pMove->data.name,name)){return pMove;}//沒有往下走pMove = pMove->next;}return NULL;}//洗掉 void deleteNodeByName(struct Node* headNode, char * name){//找到指定位置那個結點,以及制定位置前面那個結點//要有兩個相鄰的指標struct Node* p = headNode;struct Node* q = headNode->next;if (q == NULL){ printf("么有(沒有)可用資訊,無法洗掉 ");}else{ while (strcmp(q->data.name,name)){ p = q; q = p->next; if (q == NULL) { printf("未找到制定位置,無法洗掉 ");return;}}p->next = q->next;free(q);}}//列印void printList(struct Node*headNode){ struct Node* pMove = headNode->next;printf("商品代碼 商品名稱 商品單價 商品庫存量");while (pMove){printf("%d %s %s %d ", pMove->data.num, pMove->data.name, pMove->data.price, pMove->data.storage);pMove = pMove->next;}printf(" ");}void menu(){printf(" ");printf(" 0.退出系統 ");printf(" 1.錄入資訊 ");printf(" 2.顯示資訊 ");printf(" 3.洗掉資訊 ");printf(" 4.查找資訊 ");printf(" 5.保存到檔案 ");}void menuOfDelete(){printf(" 1.按照商品名稱洗掉 ");printf(" 2.按照商品代碼洗掉 ");}voidkeyDownOfDelete(){int choice;char name[20];int num;scanf("%d", &choice);switch (choice){case 1://1.按照商品名稱洗掉printf("請輸入要洗掉的商品名稱:");scanf("%s", name);struct deleteNodeByName(list, name);break;case 2://2.按照商品代碼洗掉printf("請輸入要洗掉的商品代碼:");scanf("%d", num);struct deleteNodeByNum(list, num);break;default:printf("輸入錯誤,無法洗掉 ");}}//保存到檔案void saveInfoToFile(struct Node* list, char *filePath, char *mode){struct Node* pMove = list->next;FILE *fp =fopen(filePath, mode);while (pMove){fprintf(fp, "%d %s %s %d ", pMove->data.num, pMove->data.name, pMove->data.price, pMove->data.storage);pMove = pMove->next;}fclose(fp);}//讀檔案void readInfoFromFile(struct Node* list, char *filePath, char *mode){struct commodity data;while (fscanf(fp, "%d %s %s %d ", &data.num, data.name, data.price, &data.storage) !=EOF){insertNodeByHead(list, data);}fclose(fp);}//互動void keyDown(){int choice;struct commodity comInfo;char temp= ' ';switch (choice){case 0:system("pause");exit(0);break;case 1://1.錄入資訊while (1){printf("請輸入商品代號,商品名稱,商品價格,商品庫存量:");fflush(stdin);scanf("%d%s%s%d", &comInfo.num, comInfo.name, comInfo.price, &comInfo.storage);//鏈表的插入insertNodeByHead(list, comInfo);printf("是否繼續?(N)");fflush(stdin);temp = getchar;if (temp == 'N' ||temp== 'n')break;}break;case 2://2.顯示資訊printList(list);break;case 3://3.洗掉資訊menuOfDelete;struct keyDownOfDelete;break;case 4://4.查找資訊printf("請輸入要查找的商品名稱:");scanf("%s", comInfo.name);if (findByName(list, comInfo.name) != NULL){printf("%d %s %s %d ", findByName(list, comInfo.name)->data.num,findByName(list, comInfo.name)->data.name, findByName(list, comInfo.name)->data.price,findByName(list, comInfo.name)->data.storage);}else{printf("未找到相關資訊! ");}break;case 5://5.保存到檔案saveInfoToFile(list,"1.txt", "w");break;default:printf("輸入錯誤,重新輸入 ");break;}}int main(){readInfoFromFile(list, "1.txt", "r");while (1){menu();keyDown();system("pause");system("cls");}return 0;}

uj5u.com熱心網友回復:
加油奧利給????轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/266062.html
標籤:C語言
上一篇:子函式回圈問題(KMP演算法)
下一篇:尖頭所指那行是什么意思?
