Home > Back-end >  For bosses to change
For bosses to change

Time:03-05

# include # include # include # include # 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; } the Node; Struct Node * createlist () {struct Node * headNode=(struct Node *) malloc (sizeof (struct Node)); HeadNode - & gt; Next=NULL; Return headNode; }//to create nodes struct Node * createNode (struct commodity data) {struct Node * newNode=(struct Node *) malloc (sizeof (struct Node)); NewNode - & gt; Data=https://bbs.csdn.net/topics/data; NewNode -> next=NULL; Return the newNode; }//insert void insertNodeByHead (struct Node * headNode, struct commodity data) {struct Node * newNode=createNode (data); NewNode -> next=headNode -> next; HeadNode -> next=newNode; }//find struct Node * findByName (struct Node * headNode, char * name) {struct Node * pMove=headNode -> next; While (pMove) {if (! STRCMP (pMove -> data. The name, the name)) {return pMove; }//not go down pMove=pMove -> next; } return NULL; }//delete void deleteNodeByName (struct Node * headNode, char * name) {//find the Node specified location, and formulate a position in front of the Node//have two adjacent pointer struct Node * p=headNode; Q=headNode -> struct Node * next; If (q==NULL) {printf (" it is (not) available information, unable to delete "); } else {while (STRCMP (q - & gt; Data. The name, the name)) {p=q; Q=p - & gt; next; If (q==NULL) {printf (" set location is not found, unable to delete "); return; }} - p & gt; Next=q - & gt; next; Free (q); }}//print void printList (struct Node * headNode) {struct Node * pMove=headNode - & gt; next; Printf (" commodity code name of commodity goods unit price goods inventory "); While (pMove) {printf (" % d % d % s % s ", pMove - & gt; Data. The num, pMove - & gt; Data. The name, pMove - & gt; Data. Price, pMove - & gt; Data. Storage); PMove=pMove - & gt; next; } printf (" "); } void menu () {printf (" "); Printf (" 0. Log out "); Printf (" 1. Input information "); Printf (" (2) display information "); Printf (" 3. Delete information "); Printf (" 4. Find information "); Printf (" 5. Save to file "); } void menuOfDelete () {printf (" 1. According to the name of commodity to delete "); Printf (" (2) according to the commodity code to delete "); } voidkeyDownOfDelete () {int choice; Char name [20]. int num; The scanf (" % d ", & amp; Choice); Switch (choice) {case 1://1. According to the name of commodity to delete printf (" please type in the name of the commodity to delete: "); The scanf (" % s ", name); Struct deleteNodeByName (list, name); break; Case 2://2. According to the commodity code to delete printf (" please enter the product code: to delete "); The scanf (" % d ", num); Struct deleteNodeByNum (list, num); break; Default: printf (" input error, unable to delete "); }}//save to file void saveInfoToFile (struct Node * list, char * filePath, char * mode) {struct Node * pMove=list - & gt; next; The FILE * fp=fopen (filePath, mode). While (pMove) {fprintf (fp, "% d % d % s % s", pMove - & gt; Data. The num, pMove - & gt; Data. The name, pMove - & gt; Data. Price, pMove - & gt; Data. Storage); PMove=pMove - & gt; next; } fclose (fp); }//read file void readInfoFromFile (struct Node * list, char * filePath, char * mode) {struct commodity data; While (fscanf (fp, "% d % d % s % s", & amp; Data. The num, data. The name, data. Price, & amp; The data storage)! EOF)={insertNodeByHead (list, data); } fclose (fp); }//interaction void keyDown () {int choice; Struct commodity comInfo; Char temp=' '; The switch (choice) {case 0: system (" pause "); exit(0); break; Case 1://1. Input information while (1) {printf (" please enter the product code, name of commodity, commodity prices, commodity inventories: "); The fflush (stdin); The scanf (" % d % d % s % s ", & amp; ComInfo num, comInfo. Name, comInfo. Price, & amp; ComInfo. Storage);//list of insert insertNodeByHead (list, comInfo); Printf (" whether to continue? (N) "); The fflush (stdin); Temp=getchar; If (temp=='N' | | temp=='N') break; }break; Case 2://2. The display information printList (list); break; Case 3://3. Delete information menuOfDelete; Struct keyDownOfDelete; break; Case 4://4. Find information printf (" please type in the name of the commodity to find: "); The scanf (" % s ", comInfo. Name); If (findByName (list, comInfo. Name)!=NULL) {printf (" % d % d % s % s ", findByName (list, comInfo. Name) - & gt; Data. The num, findByName (list, comInfo. Name) - & gt; Data. The name, findByName (list, comInfo. Name) - & gt; Data. Price, findByName (list, comInfo. Name) - & gt; Data. Storage); } else {printf (" relevant information not found! "); }break; Case 5://5. Save the file saveInfoToFile (list, "1. TXT", "w"); break; Default: printf (" input error, enter again "); break; }} int main () {readInfoFromFile (list, "1. TXT", "r"); While (1) {menu (); KeyDown (); system("pause"); system("cls"); } return 0; }
  • Related