scanf后必须跟地址 这个看书就好了
DO IT YOURSELF !
#include <glib.h> #include <stdio.h> #include <float.h> #include <string.h> #include <ctype.h> typedef struct product { int sn; char name[30]; double price; char etc[30]; } item; void eateline(void) { while(getchar() != '\n') continue; } char showmenu(void) { char ans; puts("请选择菜单项:\n"); puts("i) 录入数据 p) 输出数据"); puts("s) 价格排序 c) 高价计数"); puts("u) 价格调整 m) 返回菜单"); puts("q) 退出程序\n"); ans = tolower(getchar()); eateline(); while(strchr("ipscumq", ans) == NULL) { puts("请输入 i, p, s, c, u, m or q:"); ans = tolower(getchar()); eateline(); } return ans; } int reg_item(item *p) { int flag = 0; if(scanf("%d%s%lf%s", &p->sn, p->name, &p->price, p->etc) == 4) flag = 1; eateline(); return flag; } void show_item(item *p) { printf("%d\t%s\t%.1f\t%s\n", p->sn, p->name, p->price, p->etc); } void prt_arr(GArray *garr, int len) { int i; if(len > 0) { for(i = 0; i < len; i++) { show_item(&g_array_index(garr, item, i)); } } else puts("无数据可供输出..."); } int cmp(item *p1, item *p2) { double a1 = p1->price; double a2 = p2->price; if(a1 - a2 < DBL_EPSILON) return -1; else if(a1 == a2) return 0; else return 1; } void sort_prt_arr(GArray *garr, int len) { g_array_sort_with_data(garr, (GCompareDataFunc)cmp, NULL); } int max_count(GArray *garr, int len) { int count = 0 , i; for(i = 0; i < len; i++) { if(g_array_index(garr, item, i).price == g_array_index(garr, item, len - 1).price) { count++; show_item(&g_array_index(garr, item, i)); } } return count; } void update_price(GArray *garr, int snum, char *sname, double range, int len) { int i, flag = 1; for(i = 0; i < len; i++) { if(strcmp(g_array_index(garr, item, i).name, sname) == 0 && g_array_index(garr, item, i).sn == snum) { g_array_index(garr, item, i).price *= (range + 1); flag = 0; break; } } if(flag) puts("未找到可供调价的商品"); } int main(int argc, char** argv) { int len = 0, maxcount = 0, snum = 0; char sname[30] = {0}, choice; double range = 0.0; GArray *garr; item newitem, *p = &newitem; garr = g_array_new(FALSE, FALSE, sizeof(item)); while((choice = showmenu()) != 'q') { switch(choice) { case 'i': puts("\n***************** 录入数据 ********************\n"); while(reg_item(p)) { g_array_append_val(garr, newitem); len++; } puts("\n***********************************************\n"); break; case 'p': puts("\n***************** 输出数据 ********************\n"); prt_arr(garr, len); puts("\n***********************************************\n"); break; case 's': puts("\n***************** 价格排序 ********************\n"); sort_prt_arr(garr, len); prt_arr(garr, len); puts("\n***********************************************\n"); break; case 'c': puts("\n***************** 高价计数 ********************\n"); maxcount = max_count(garr, len); printf("最高价的商品数: %d\n", maxcount); puts("\n***********************************************\n"); break; case 'u': puts("\n***************** 价格调整 ********************\n"); if(len) { puts("输入要调整的编号、商品名、调整比率"); scanf("%d%s%lf", &snum, sname, &range); eateline(); update_price(garr, snum, sname, range, len); prt_arr(garr, len); } else puts("无商品可供调价"); puts("\n***********************************************\n"); break; case 'm': puts("\n***********************************************\n"); break; } } g_array_free(garr, TRUE); return 0; }