廈大小學期C語言程式設計實踐(五)

2021-08-14 19:17:13 字數 3572 閱讀 7769

•      使用動態鍊錶完成乙個簡單的商品庫存資訊管理系統。

•      商品資訊包括如下字段:商品號、商品名稱、商品庫存

•      函式

create:接收使用者輸入的商品號和商品名稱的資訊,建立鍊錶;庫存初始化為0,沒有進貨之前不允許銷售;商品號為0表示使用者輸入結束。本函式用於初始化,如果第二次被呼叫的時候,首先要執行destroy清除舊鍊錶。

destroy:給定鍊錶的頭指標,刪除鍊錶的所有節點,並釋放相應的空間。本函式在程式退出前應至少被呼叫一次。在呼叫此函式前,必須給予使用者提示,使使用者在刪除前有反悔的機會。

sell:商品銷售,由引數傳入商品號和銷售數量。如果不存在給定商品號的商品或銷售數量大於相應商品的庫存則出錯;否則,從指定商品的庫存中扣除相應的銷售數量。當商品庫存為0,則從鍊錶中刪除該商品。

stock:商品進貨,由引數傳入商品號和進貨數量。如果不存在給定商品號的商品則在鍊錶中插入新商品,並提示使用者輸入該商品名稱;否則,增加指定商品的庫存量。

list:列出所有商品的情況。

•      主程式

程式執行後,迴圈顯示如下選單:

1. 輸入商品資訊2. 銷售3. 進貨4. 列舉商品資訊5. 清除所有商品6.退出

根據使用者的選擇進一步提示使用者輸入並呼叫對應函式。

#include #include #include #define product_no_size     15

#define product_name_size 50

struct product

;struct listnode

;struct listnode *list;

int create();

int destroy();

int sell(char *no, int quantity);

void stock(char *no, int quantity);

void list();

void inte***ce();

int create()

getchar();

}char no[product_no_size];

char name[product_name_size];

struct listnode *p;

list = (struct listnode *)malloc(sizeof(struct listnode));

list->next = null;

while (1)

p = (struct listnode *)malloc(sizeof(struct listnode));

strcpy(p->product.no, no);

printf("please enter the name: ");

fgets(name, product_name_size, stdin);

name[strlen(name) - 1] = 0;

strcpy(p->product.name, name);

p->product.quantity = 0;

p->next = list->next;

list->next = p;

}return 1;

}int destroy()

list = null;

printf("finish destroying!\n");

return 1;

}int sell(char *no, int quantity)

printf("finish to selling!\n");

return 1;

}else

}q = p;

p = p->next;

}printf("no such record! failed to sell!\n");

return 0;

}void stock(char *no, int quantity)

p = p->next;

}char name[product_name_size] = ;

q = (struct listnode *)malloc(sizeof(struct listnode));

strcpy(q->product.no, no);

printf("please enter the name: ");

fgets(name, product_name_size, stdin);

name[strlen(name) - 1] = 0;

strcpy(q->product.name, name);

q->product.quantity = quantity;

q->next = list->next;

list->next = q;

}void list()

printf("*** list ***\n");

struct listnode *p = list->next;

if (p)

}else

}void inte***ce()

; int quantity;

while (1)

printf("please enter the no: ");

fgets(no, product_no_size, stdin);

no[strlen(no) - 1] = 0;

printf("please enter the quantity: ");

scanf("%d", &quantity);

sell(no, quantity);

break;

case 3:

printf("*** stock ***\n");

if (!list)

printf("please enter the no: ");

fgets(no, product_no_size, stdin);

no[strlen(no) - 1] = 0;

printf("please enter the quantity: ");

scanf("%d", &quantity);

getchar();

stock(no, quantity);

break;

case 4:

list();

break;

case 5:

destroy();

break;

case 6:

printf("thank you for your using!\n");

return;

default:

printf("unrecognized command.\n");}}

}int main()

測試資料:

product no	product name

dr100 juice

dr209 coke cola

food302 bread

veg091b tomato

veg091a potato

bk991m1 intro. to alg.

C語言程式設計實踐 C語言應用實踐

閱讀2 下面的鏈結中,是銀行系統的原始碼,請閱讀並執行 需要的檔案請自行建立 銀行系統 第一版 原始碼 第10周實踐 請完成下面的專案,併發部落格作為解題報告 專案1 編寫選擇結構程式,輸入個人月收入總額,計算出他本月應繳稅款和稅後收入 計算辦法見附 關於個人所得稅的有關背景知識 可以在下面程式的基...

C 語言程式設計 程式設計實踐

程式編譯流程與 gcc 編譯器 c 語言程式設計 基本語法 c 語言程式設計 基本資料型別 c 語言程式設計 變數與常量 c 語言程式設計 運算子 c 語言程式設計 邏輯控制語句 c 語言程式設計 函式 c 語言程式設計 高階資料型別 指標 c 語言程式設計 高階資料型別 陣列 c 語言程式設計 高...

c 小學期大作業攻略(五)基於QSS的樣式美化

這回真的是最後一篇了。前面說過,我們開發過程中暫時不搭理樣式問題,等最後再一起處理,那麼現在就是最後處理時刻了!看到網上說qss跟css差不多,我還覺得自己可以幹回老本行了,結果用起來發現,qss是要啥啥沒有吧?不管怎麼說還是跌跌撞撞地假裝完成了美化。最新,估計也是最後版本的exe也上傳到了雲盤 開...