Sie sind auf Seite 1von 4

#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.

h> #define MAX 100 void init_list(void), report(void); change_item(void); new(void); int menu(void),find_free(void),auth(void); void sale(void); struct itm* get_item(char*); struct itm { char item[30]; float cost; int on_hand; } inventory[MAX]; main(void){ char choice; init_list(); for(;;){ if(auth()){ choice =menu(); switch(choice) { case 1:new(); break; case 2:change_item(); break; case 3:sale(); break; case 4:report(); break; case 5:return 0; } } else { printf("wrong password\n\n"); exit(1); } } } void init_list(void) { register int t; for(t=0;t<MAX;++t) inventory[t].item[0]='\0'; } int menu(void) {

char s[80]; int c; printf("\n"); printf("1.new item\n"); printf("2.modify\n"); printf("3.sell\n"); printf("4.report\n"); printf("5.exit\n"); do{ printf("\nenter your choice:"); gets(s); c=atoi(s); } while(c<0||c>5); return c;

new(void) { int slot; struct itm *item; char s[30]; slot =find_free(); if(slot==-1){ printf("\nlist full!\n"); return; } printf("enter item:"); gets(s); item = get_item(s); if(item){ printf("\nIt already exist\n"); return; } strcpy(inventory[slot].item, s); printf("enter cost:"); scanf("%f",&inventory[slot].cost); printf("enter num on hand:"); scanf("%d", &inventory[slot].on_hand); getchar(); return; } struct itm* get_item(char *name) { int t; for(t=0; inventory[t].item[0]&&t<MAX;++t) if(strcmp(name, inventory[t].item)==0)return &inventory[t]; return NULL;

int find_free(void) { register int t; for(t=0; inventory[t].item[0]&&t<MAX;++t) if(t==MAX)return -1; return t; } change_item(void){ int slot, c; struct itm *item; char s[80]; if(auth()){ printf("item:"); gets(s); item = get_item(s); if(!item){ printf("\nNot available in the inventory\n"); return; } printf("on_hande:"); gets(s); c=atoi(s); item->on_hand = c; return; } else { printf("wrong password\n\n"); menu(); } }

void sale(void) { int slot, c; struct itm *item; char s[80];

printf("item:"); gets(s); item = get_item(s); if(!item){ printf("\nNot available in the inventory\n"); return; } printf("quantity:"); gets(s); c=atoi(s); if(c>item->on_hand){ printf("\nRequested quantity not available\n"); return; }

item->on_hand = item->on_hand-c; return; }

void report(void) { struct itm *item; float total=0; if(auth()){ printf("item\t\tcost\t\ton hand\n\n"); for(item=inventory;item<(inventory+MAX);item++) if(item->item[0]){ printf("%s\t\t",item->item); printf("%f\t\t",item->cost); printf("%d\n", item->on_hand); total = total + (item->on_hand*item->cost); } printf("total value of inventory: %f\n\n, total"); } else {

} } int auth(){

printf("wrong password\n\n"); menu();

char pass[] = "$MAHmoud11"; char temp[80]; printf("\nEnter the password: "); gets(temp); if(strcmp(pass, temp) == 0) return 1; else return 0; }

Das könnte Ihnen auch gefallen