Hotel management (linked list and file operation)

zhaozj2021-02-16  48

#include #include #include

#define len sizeof (Guest) #define filename_len 10 # define null 0 # define format "/ n% 20d% 20S% 20D / N" #define true 1 # define false 0

Struct Guest {int number; / * passenger number * / char name [10]; / * name * / int money; / * amount * / struct guest * pnext;}

Typedef struct guest guest;

/ * Initialization of function * / guest * Creat (void); guest * delect (guest * pheadget, int Numselect); guest * readlist (void);

Char WelcomeAndtip (Void);

void Insert (GUEST * pHeadGet, int numInsert); void Show (GUEST * pHeadGet); void Max_Print (GUEST * pHeadGet); void ListSave (GUEST * pHeadGet); void FoundAndPrint (GUEST * pHeadGet, int number); void Update (GUEST * Pheadget, int number, int newmoney

/ * =========================== function: main, choose the various functions to return: void ========= =================== * / void main () {guest * phead = null; char select; int client, number;

Select: select = WelcomeAndtip (); / * Welcome interface * / switch (select) {copy 'i': case 'i': if (PHEAD == NULL) {PHEAD = CREAT (); System ("CLS"); Show (PHEAD);} else {system ("cls"); Printf ("/ n Link list has been read from file reading / N");} Break;

Case 'D': Case 'D': System ("CLS"); Show (PHEAD); Printf ("/ n Please enter the passenger number you want to delete:"); scanf ("% d", & client); PHEAD = Delect (PHEAD, Client); SYSTEM ("CLS"); show (PHEAD); Break; Case 's': Case 's': Printf ("Please enter the ID you want to insert (do not exceed the length of the current list) "); Scanf ("% d ", & client); insert (PHEAD, Client); SYSTEM (" CLS "); show (PHEAD); Break; Case 'q': Case 'Q': System (" CLS "); Show (PHEAD); BREAK;

Case 'M': Case 'M': Max_Print (PHEAD); BREAK;

Case 'B': Case 'B': Listsave (PHEAD); BREAK;

Case 'R': Case 'R': PHEAD = Readlist (); System ("CLS"); Show (PHEAD); Break;

Case 'F': Case 'F': / * Use Number as the only ID of the internal, named * / system ("CLS"); show (PHEAD); printf ("/ n Please enter you want to query) KeyWhere, SCANF ("% D", & Client); SCANF ("% D", & Client); FoundandPrint (PHEAD, Client); Break;

Case 'C': SYSTEM ("CLS"); Show (PHEAD); Printf ("/ n Please enter the guest number and settlement amount you want to guest (separated by space):"); scanf (" % D% D ", & Number, & Client); System (" CLS ");

Update (Phet, Number, Client); Break; Default: System ("Pause"); System ("CLS"); Printf ("/ N Please reselect / n"); Break;} goto select;

} / * =========================== Function: Welcome message and operation prompt starting with print: Void Incoming: void === ================================================================================3 T / T / T hotel management (file operation) / N "); for (client = 0; client <80; client ) {printf (" = ");} printf (" / n "); Printf (" i: Create / TD: Delete / TQ: Display / TS: Insert / TM: Maximum / TB: Save / TR: Read / TF: Find / TC: Settlement / N "); for (Client = 0; Client <80; client {Printf ("=");} printf ("/ n Please enter the action you want to do:"); scanf ("% c", & select); return select;} / * ======== =================== Function: Output The entire node returns: Void incoming: the head pointer of the list ================ ============ * / void show (guest * pheadget) {guest * pclient; pclient = PHEADGET

IF (pClient == NULL) {printf ("/ n data is empty without data can be printed / N");} else {printf ("/ t / t passenger number / t / t name / t / t / t / t amount" ); Do {printf (Format, PClient-> Number, PClient-> Name, PClient-> Money); PClient = PClient-> PNext;} while (pClient! = Null);}} / * ====== ============================================================================================================================================================================================================= = Function: Create the entire node, not control the input data, and error judgment. Return: Link table head pointer passes: void ======================================== ================= * / guest * creat (void) {guest * pHEAD = null, * pone = null, * ptwo = null; int numberTemp = 1; PONE = *) Malloc (len); if (pone == null) {Printf ("/ n open unsuccessful, please retry / N");} else {ptwo = pone; PHEAD = PONE; PTWO-> PNext = NULL; Printf ("/ n Please enter the passenger number, amount, amount separated by spaces, end input with 0 0:", "% D% D", & Pone-> Number, & Pone-> Money) _Flushall (); Printf ("/ n Please enter the name of the first DPAC:", Numbertemp); gets (pone-> name);

While (Pone-> Number! = 0) {PTWO = PONE; // Before opening up, you should give the front pointer Pone = (Guest *) malloc (LEN) Malloc (Numbertemp ; printf ("/ n please Enter the passenger number, amount, space, space separation with spaces: ", Numbertemp); Scanf ("% D% D ", & Pone-> Number, & Pone-> Money); _flushall (); printf (" / n please Enter the name: ", Numbertemp; gets (pone-> name); ptwo-> pnext = pone;} ptwo-> pnext = null; free (pone); pone = null; // free PTWO); Space is not open, how to release // ptwo = null; return (PHEAD);} / * ========================= ============ Function: Remove the node of the specified passenger number Returns: Void incoming: the head pointer of the list and the node order to delete ============ ============================================================================================================================================= PONE = PHEADGET; if (phetget == null) {printf ("/ n data is empty without data can be deleted / N");} else {while ((Pone-> Number! = Numselect) && (Pone-> PNext! = NULL)) {PTWO = PONE; PONE = Pone-> pnext;}} (Numselect == Pone-> Number) {if (pone == PHEADGET) {PHEADGET = PONE-> PNEXT; Free (PONE);} else {Ptwo-> pnext = pone-> pnext; free (pone);}} else {printf ("/ N did not find to delete Guest: / n ");}}} Return Pheadget; / * If the data is on the top section, you need to return to the head section * /}

/ * =========================== function: Find data for the custom passenger directory Return: Void Incompination: PHEAD (head pointer), Number (Passenger number) ================================================================================================================================================================================ INT TEMP = false; pClient = PHEADGET; if (pClient == null) {printf ("/ n data is empty without data can be found);} else {while (pclient! = null) {if (Number == PClient-> Number) {Printf ("/ t / t passenger number / t / t name / t / t / t amount"); Printf (Format, PClient-> Number, PClient-> Name, PClient-> Money); Temp = true; break;} pclient = pClient-> pnext;} if (! Temp) {printf ("/ N did not find the data you want, please verify! / n");}}} / * ==== ======================= Features: The specified (behind the data) Insert node returns: Void incoming: the head pointer and insert of the list Location ===================================================================================================================================== NULL, * PINSERT = NULL; int TEMP;

PTWO = PONE = PHEADGET; / * The algorithm here is very bad, how to quickly locate the specified node? * / For (Temp = 1; Temp PNext;} Pone = PTWO-> PNEXT; PINSERT = (Guest *) Malloc (LEN); Printf ("/ n Please enter the guest Number, amount, space separation: "); SCANF ("% D% D ", & pinsert-> number, & pinsert-> Money); _flushall (); printf (" / n Please enter the inserted guest name: "); Gets (Pinsert-> name); ptwo-> pnext = pinsert; pinsert-> pnext = PONE;} / * ========================== = Function: Find the maximum consumption amount of guest data and print back: Void incoming: the head pointer of the list =========================== * / void max_print (guest * phetget) {int max; guest * pscan = null, * pPUT = null; if (phetget == null) {printf ("/ n data is empty without data comparison / N");} Else {Max = PHEADGET-> Money; PSCAN = PHEADGET-> PNEXT; PPUT = PHEADGET; / * Initializing print node * / if (pscan! = null) / * Judgment is a list of nodes * / {DO {IF (PSCAN-> Money> max) {max = pscan-> Money; PPUT = pscan;} else {pput = Pheadget;} pscan = pscan-> pnext;} while (pscan! = null);} system ("CLS" PRINTF ("Best Guest: / N"); Printf ("/ T / T Guestbook / T / T Name / T / T)); Printf (Format, PPUT-> Number, PPUT -> Name, PPUT-> MONEY);}}

/ * ================================================================================================================================================================ ============ Features: Update the customer's current consumption. When a customer consumption, if the total amount of consumption is more than 10 million yuan, it will be 60% off this time, more than 50 million yuan, 30% off; more than 1,000 yuan, hit 10% off, and update the total amount of this customer's consumption . Return: Void Intrinsic: PHEAD (head pointer of the list), Number, NewMoney (the latest consumption) ========================= ============================================ * / void update (Guest * Pheadget, int Number, int newmoney) {Guest * pClient = NULL; int Temp = false; pClient = PHEADGET; if (pClient == NULL) {printf ("/ n data is empty without data can be updated / N");} else {while (pclient! = NULL) {if (Number == PClient-> Number) {if (PClient-> Money> 10000) Printf ("/ n This guest requires payment:% d / n", (int) (newMoney * 0.6)); Else IF ((PClient-> Money <10000) && (PClient-> Money> 5000) Printf ("/ n This guest requires payment:% d / n", (int) (newMoney * 0.8)); Else Printf ("/ n This guest needs payment:% d / n", newmoney); pClient-> Money = newMoney; temp = true; break;} pclient = pclient-> pnext;} if (! Temp) {Printf "/ N did not find the data item you want to update, verify! / n");}}}

/ * =========================== function: Save the current linked list back: Void incoming: the head pointer of the list ==== ============================================ INT Listlen = 0; if (phetget == null) {printf ("/ n data is empty without data can be saved / N");} else {while (pone) {PONE = PONE -> PNEXT; Listlen ;} / * Get the length of the list * /

Printf ("/ n Please enter the file name, in txt mode (* .txt):"); _flushall (); gets (filename); if (filename, "w") == null) { Printf ("/ n open file failed / N"); exit (0);} pone = pHeadget; // fprintf (fposition, "/ t / t passenger number / t / t name / t / t / t amount") While (PONE! = Null) {if (! Fprintf (fposition, format, pone-> number, pone-> name, pone-> Money)) {printf ("/ n read file error / N");} PONE = Pone-> PNext;} fclose (fclose);}}

/ * =========================== function: read the file and create it for the current list: PHEAD income: void ==== =================================================================394493953953 * ptwo = null, * PHEAD = NULL;

Printf ("/ n Please enter the file name you want to open, in txt mode (* .txt):"); _flushall (); gets (filename); if (filename, "r")) = = NULL) {Printf ("/ N Open Failed / N"); exit (0); Pone = (Guest *) Malloc (LEN); PTWO = PONE; PHEAD = PONE; PHEAD-> PNEXT = NULL; / / Fseek 60L, 0); While (! Feof (fscanf (fscanf (fscanf (fscanf (fposition, format, & pone-> number, & pone-> name, & pone-> money)) Printf (" / n read file error, maybe the file is empty, or the file does not exist! / n "); PTWO = PONE; PONE = (guest *) malloc (len); Ptwo-> pnext = PONE;} Ptwo-> pnext = NULL; Free (PONE); / * Releases extra creation * / PONE = null; return phead;}

转载请注明原文地址:https://www.9cbs.com/read-21660.html

New Post(0)