ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ Η/Υ Ακαδημαϊκό έτος 2001-2002 ΑΣΚΗΣΗ #5 Προτεινόμενη Λύση #include <stdio.h> #include <string.h> #define TRUE 0 #define FALSE -1 #define SIZE 4 /* Το μέγεθος του πίνακα */ typedef struct /* Ο τύπος δεδομένων που θα αποθηκεύονται τα στοιχεία του ανταλλακτικού */ int code; char description[20]; char manufacturer[25]; float price; int quantity; } Part_record; Part_record part, temp; /* Δήλωση της μεταβλητής part ως τύπου Part_record */ Part_record storehouse[size]; /* Δήλωση του πίνακα των εγγραφών, ως τύπου Part_record */ void show_menu(void); /* Δήλωση των συναρτήσεων που θα χρησιμοποιήσουμε */ Part_record get_part_data(part_record part); int get_part_code(char proceed[50]); void insert_part(part_record part, Part_record store[]); int remove_part(int code, Part_record store[]); int find_part(int code,part_record store[]); void get_part_data_mod (void); int modify_part(int code,part_record store[]); int print_part(int code,part_record store[]); void sort(part_record store[]); int get_part_quantity(char proceed[50]); void print_report(part_record []); main() int choice; show_menu(); scanf("%d", &choice); while (choice!=7) switch(choice) case 1: temp = get_part_data(part);
insert_part(temp, storehouse); case 2: temp.code = get_part_code("διαγραφη ΑΝΤΑΛΛΑΚΤΙΚΟΥ"); if (remove_part(temp.code,storehouse) == TRUE) printf("\nτο ανταλλακτικό διαγράφηκε. Η αποθήκη ενημερώθηκε...\n"); else printf("\nto ανταλλακτικό δεν υπάρχει στην αποθήκη!\n"); case 3: temp.code = get_part_code("αναζητηση ΑΝΤΑΛΛΑΚΤΙΚΟΥ"); if (find_part(temp.code,storehouse) == TRUE) printf("\nτο ανταλλακτικό βρέθηκε στην αποθήκη.\n"); else printf("\nto Το ανταλλακτικό δεν βρέθηκε!\n"); case 4: temp.code = get_part_code("τροποποιηση ΑΝΤΑΛΛΑΚΤΙΚΟΥ"); if (modify_part(temp.code,storehouse) == TRUE) printf("\nto ανταλλακτικό τροποποιήθηκε. Η αποθήκη ενημερώθηκε...\n"); else printf("\nto ανταλλακτικό δεν βρέθηκε!\n"); case 5: temp.quantity = get_part_quantity("εκτυπωση ΥΠΟΛΟΙΠΩΝ"); if (print_part(temp.quantity,storehouse)!= TRUE) printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nόλα ανταλλακτικά ξεπερνούν τη συγκεκριμένη ποσότητα!"); case 6: sort(storehouse); print_report(storehouse); default: printf("\nλανθασμένη Επιλογή!"); } show_menu(); scanf("%d",&choice); } /* Τέλος while */ } /* Τέλος της main */ void show_menu(void) /* Εμφανίζει το μενού επιλογών */ printf("\n\n******** ΔΙΑΧΕΙΡΙΣΗ ΑΠΟΘΗΚΗΣ ********\n\n"); printf("\n 1. ΕΙΣΑΓΩΓΗ ΑΝΤΑΛΛΑΚΤΙΚΟΥ"); printf("\n 2. ΔΙΑΓΡΑΦΗ ΑΝΤΑΛΛΑΚΤΙΚΟΥ"); printf("\n 3. ΑΝΑΖΗΤΗΣΗ ΑΝΤΑΛΛΑΚΤΙΚΟΥ"); printf("\n 4. ΤΡΟΠΟΠΟΙΗΣΗ ΑΝΤΑΛΛΑΚΤΙΚΟΥ"); printf("\n 5. ΕΚΤΥΠΩΣΗ ΥΠΟΛΟΙΠΩΝ"); printf("\n 6. ΕΚΤΥΠΩΣΗ ΗΜΕΡΗΣΙΑΣ ΑΝΑΦΟΡΑΣ"); printf("\n 7. ΕΞΟΔΟΣ\n\n"); printf("\n Επιλογή: "); return; }/* Τέλος της show_menu */ τα
Part_record get_part_data(part_record part) /* Δέχεται τα δεδομένα εισόδου */ printf("\n-------- EIΣΑΓΩΓΗ ΑΝΤΑΛΛΑΚΤΙΚΟΥ --------\n"); printf("\nkωδικός : "); scanf("%d", &part.code); printf("περιγραφή : "); scanf("%s", part.description); printf("κατασκευαστής : "); scanf("%s", part.manufacturer); printf("κόστος Αγοράς : "); scanf("%f", &part.price); printf("ποσότητα : "); scanf("%d", &part.quantity); return(part); }/* Τέλος της get_part_data */ int get_part_code(char proceed[50]) int code; printf("\n-------- %s --------\n",proceed); printf("\nκωδικός : "); scanf("%d", &code); return(code); }/* Τέλος της get_part_code */ void insert_part(part_record part, Part_record store[]) /*Καταχωρεί το ανταλλακτικό στον πίνακα */ int i, found=false, eleghos_dipl=true; for(i=0;(i<size && eleghos_dipl == TRUE);i++) if (store[i].code == part.code) eleghos_dipl=false; i=0; while (i<size && found == FALSE && eleghos_dipl == TRUE) if (store[i].code == 0) store[i].code = part.code; strcpy(store[i].description,part.description); strcpy(store[i].manufacturer, part.manufacturer); store[i].price = part.price; store[i].quantity = part.quantity; if (eleghos_dipl == FALSE) printf("\nυπάρχει άλλο ανταλλακτικό με τον ίδιο κωδικό!\n"); if (found == FALSE && eleghos_dipl == TRUE) printf("\nτο ανταλλακτικό δεν καταχωρήθηκε. Η αποθήκη είναι γεμάτη!\n"); if (found == TRUE && eleghos_dipl == TRUE) printf("\nτο ανταλλακτικό καταχωρήθηκε. Η αποθήκη ενημερώθηκε...\n"); return; }/* Τέλος της insert_part */
int remove_part(int code,part_record store[]) /* Διαγράφει ένα ανταλλακτικό από την αποθήκη */ if (store[i].code == code) store[i].code = 0; strcpy(store[i].description, " "); strcpy(store[i].manufacturer, " "); store[i].price = 0.00; store[i].quantity = 0; }/* Τέλος της remove_part */ int find_part(int code,part_record store[]) /* Αναζητεί ένα ανταλλακτικό από την αποθήκη και το εκτυπώνει στην οθόνη*/ if (store[i].code == code) printf("\nkwdikos : %d",store[i].code); printf("\nperigrafh : %s",store[i].description); printf("\nkataskevasths : %s",store[i].manufacturer); printf("\nkostos Agoras : %.2f",store[i].price); printf("\nposothta : %d\n\n",store[i].quantity); }/* Τέλος της find_part */ void show_menu_mod(void) printf("τροποποίηση :\n"); printf("\t\t1. Περιγραφή\n"); printf("\t\t2. Κατασκευαστής\n"); printf("\t\t3. Κόστος Αγοράς\n"); printf("\t\t4. Ποσότητα\n"); printf("\n ** Για πολλαπλές επιλογές χρησιμοποιήστε: ',' (π.χ. 1,2,3)"); }/* Τέλος της show_menu_mod */
void get_part_data_mod(void) char choice[11]; int i=0; Gpdm: printf("\nεπιλογή : "); scanf("%s", &choice); while (choice[i]!='\0') switch (choice[i]) case '1': printf("περιγραφή : "); scanf("%s", &temp.description); case '2': printf("κατασκευαστής : "); scanf("%s", &temp.manufacturer); case '3': printf("κόστος Αγοράς : "); scanf("%f", &temp.price); case '4': printf("ποσότητα : "); scanf("%d", &temp.quantity); case ',': default: printf("λάθος επιλογή!"); goto Gpdm; }/* Τέλος της switch */ }/* Τέλος τη while*/ } int modify_part(int code,part_record store[]) /* Τροποποιεί ένα ανταλλακτικό από την αποθήκη και το εκτυπώνει στην οθόνη*/ char mod_choice[6]; if (store[i].code == code) show_menu_mod(); get_part_data_mod(); store[i].code = temp.code; strcpy(store[i].description, temp.description); strcpy(store[i].manufacturer, temp.manufacturer); store[i].price = temp.price; store[i].quantity = temp.quantity;
}/* Τέλος της modify_part */ int get_part_quantity(char proceed[50]) int quantity; printf("\n-------- %s --------\n",proceed); printf("\nμέγιστη Ποσότητα : "); scanf("%d", &quantity); return(quantity); }/* Τέλος της get_part_quantity */ int print_part(int quantity,part_record store[]) /* Εκτυπώνει όλα τα ανταλλακτικά με διαθέσιμη ποσότητα*/ if (i == 0) printf("\nκωδικός Περιγραφή\t\tΠοσότητα\n"); if (store[i].quantity <= quantity && store[i].code!= 0) printf("%-5d %-20s\t%d",store[i].code,store[i].description,store[i].quantity); printf("\n\n"); }/* Τέλος της print_part */ void sort(part_record store[]) /* Ταξινομεί τον πίνακα της αποθήκης με τη μέθοδο της φυσαλίδας */ int i, pass; Part_record hold; for (pass = 0; pass < SIZE; pass++) for (i=0; i< SIZE-1; i++) if (store[i].code > store[i+1].code) hold = store[i]; store[i] = store[i+1]; store[i+1] = hold; }/* Τέλος της sort*/ void print_report(part_record store[]) /* Τυπώνει την ημερήσια αναφορά */
int i; printf("\n********************************* ΕΚΤΥΠΩΣΗ ΗΜΕΡΗΣΙΑΣ ΑΝΑΦΟΡΑΣ ********************************************\n"); printf("\na/a Κωδικός Περιγραφή\t\tΚατασκευαστής\t\tΚόστος\tΠοσότητα\n"); for (i=0; i<size; i++) printf(" %-2d %-5d %-20s %-25s%.2f\t%- 5d\n",i+1,store[i].code,store[i].description,store[i].manufacturer,store[i].price,store[i].qua ntity); printf("\n***************************************************************** ******************************************\n"); }/* Τέλος της print_report*/