โปรแกรมร้านโทรศัพท์ ภาษา C
#include<stdio.h> #include<conio.h> #include<string.h> void Adddata(); void Sell(); void Show(); struct phonedata { char id[20]; char brand[10]; char series [10]; char colour [10]; int price; int num; struct size { float width; float height; float lengt; } s; struct properties { char camera [5]; char wifi [5]; } p; struct memory { char inside [10]; char card [10]; } m; int amount; } ph;
//----------------------------------------------------------------------------
int main() { int n; do { printf("\t<<===========================================>>\n"); printf("\t<< Welcome to the shop selling mobile phones.>>\n"); printf("\t<<------------------------------------------->>\n"); printf("\t<< Please choose to >>\n"); printf("\t<<------------------------------------------->>\n"); printf("\t<< 1 = Add 3 = Show >>\n"); printf("\t<< 2 = Sell 0 = Exit >>\n"); printf("\t<<===========================================>>\n"); printf("\n\tEnter your choose : "); scanf("%d",&n); printf("\n\n");
if(n==1){ Adddata(); }else if(n==2){ Sell(); }else if(n==3){ Show(); } } while(n!=0); return 0; }
//----------------------------------------------------------------------------
void Adddata() { FILE *fp; int n=0; int choose; char stock[20]; printf("\t1.Add new phone\n\t2.Update Stock\n"); printf("\tEnter you choose : "); scanf("%d",&choose); if(choose==1) { fp=fopen("D://phone2.txt","a"); if(fp==NULL) { printf("errer open file\n"); } else { do { printf("\tinput id: "); scanf("%s",ph.id); printf("\tinput brand: "); scanf("%s",ph.brand); printf("\tinput series: "); scanf("%s",ph.series); printf("\tinput colour: "); scanf("%s",ph.colour); printf("\tinput price: "); scanf("%d",&ph.price); printf("\tinput width: "); scanf("%f",&ph.s.width); printf("\tinput height: "); scanf("%f",&ph.s.height); printf("\tinput length: "); scanf("%f",&ph.s.lengt); printf("\tinput camera: "); scanf("%s",ph.p.camera); printf("\tinput wifi: "); scanf("%s",ph.p.wifi); printf("\tinput inside: "); scanf("%s",ph.m.inside); printf("\tinput card: "); scanf("%s",ph.m.card ); ph.amount =1; // fprintf(fp,"%s %s %s %s %d %f %f %f %s %s %s %s %d\n",ph.id,ph.brand,ph.series,ph.colour,ph.price, // ph.s.width,ph.s.height,ph.s.lengt,ph.p.camera,ph.p.wifi,ph.m.inside,ph.m.card,ph.amount); fwrite(&ph,1,sizeof(ph),fp); printf("\n\tContinue enter 1 exit 2 ... "); scanf("%d",&n); }while(n==1); fclose(fp); } } else { fp=fopen("D://phone2.txt","r+"); if(fp==NULL) { printf("errer open file\n"); } else { printf("\n\tInput ID : "); scanf("%s",stock); do { if(strcmp(stock,ph.id)==0) { ph.amount=ph.amount+1; printf("\t%d",ph.amount); printf("\n\n\t!!! Update Successful !!!\n"); } }while(fread(&ph, sizeof(ph),1,fp) == 1); rewind(fp); } printf("\n\n"); fclose(fp); } }
//----------------------------------------------------------------------------
void Sell() { FILE *fp; char name[10],series[10],confirm[5]; int con,product; fp=fopen("D://phone2.txt","r+t"); do { printf("\tEnter name phone : "); scanf("%s",name); printf("\tEnter Series : "); scanf("%s",series); while(!feof(fp)) { fscanf(fp,"%s %s %s %s %d %f %f %f %s %s %s %s %d\n",ph.id,ph.brand,ph.series,ph.colour,&ph.price, &ph.s.width,&ph.s.height,&ph.s.lengt,ph.p.camera,ph.p.wifi,ph.m.inside,ph.m.card,&ph.amount); if((strcmp(name,ph.brand)==0)&&(strcmp(series,ph.series)==0)) { printf("\n\n"); printf(" ========================================================================\n"); printf(" BRAND SERIES PRICE COLOUR CAMERA WIFI SIZEMEMORY\n"); printf(" ========================================================================\n"); printf("\n %8s%10s%9d%10s%10s%7s%12s\n\n\n",ph.brand,ph.series,ph.price,ph.colour,ph.p.camera,ph.p.wifi,ph.m.card);
printf("\tDo you want to buy this product\n\t1.Buy\n\t2.No... "); scanf("%d",&product); printf("\n"); if(product==1) { printf("\n\tYou total pay %6d Bath\n\n",ph.price); printf("\tConfirm [Y/N] : "); scanf("%s",confirm); if(strcmp("y",confirm)==0) { ph.amount=ph.amount-1; printf("%d",ph.amount); printf("\n\tSucessful !!!\n\n"); fprintf(fp,"%s %s %s %s %d %f %f %f %s %s %s %s %d\n",ph.id,ph.brand,ph.series,ph.colour,ph.price, ph.s.width,ph.s.height,ph.s.lengt,ph.p.camera,ph.p.wifi,ph.m.inside,ph.m.card,ph.amount); } else { break; } getch(); break; } else { break; } } } rewind(fp); printf("\tContinue enter 1 exit 2 ... "); scanf("%d",&con); printf("\n"); } while(con==1); fclose(fp); }
//----------------------------------------------------------------------------
void Show() { FILE *fp; fp=fopen("D://phone2.txt","r"); if(fp==NULL) { printf("\t\t\tError open file!!!\n\n"); } else { printf("\n\n"); printf("\n ****** Welcome to the shop selling mobile phones******\n\n "); printf("\t ==============================================\n"); printf("\t BRAND SERIES PRICE AMOUNT\n"); printf("\t ==============================================\n");
while(!feof(fp)) { fscanf(fp,"%s %s %s %s %d %f %f %f %s %s %s %s %d\n",ph.id,ph.brand,ph.series,ph.colour,&ph.price, &ph.s.width,&ph.s.height,&ph.s.lengt,ph.p.camera,ph.p.wifi,ph.m.inside,ph.m.card,&ph.amount); printf("\t %8s\t%8s%11d%12d\n",ph.brand,ph.series,ph.price,ph.amount); } printf("\n\n"); fclose(fp); } }
คือ โปรแกรมมีปัญหา 2 อย่างคือ มันไม่ยอมอัพเดต สต๊อกโทรศัพท์ แล้วเมื่อขายโทรศัพท์ โปรแกรมไม่ยอม แทนที่ข้อมูลเดิมแต่เพ่ือขึ้นมาใหม่แทน
|