Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- /*Menu Macros:*/
- #define OPTION_ADD 1
- #define OPTION_LIST 2
- #define OPTION_DISPLAY_TOTAL_VALUE 3
- #define OPTION_SEARCH_ID 4
- #define OPTION_SEARCH_NAME 5
- #define OPTION_DISPLAY_CHEAPEST 6
- #define OPTION_DISPLAY_NEED_SUPPLY 7
- #define OPTION_MODIFY_PRICE 8
- #define OPTION_DELETE 9
- #define OPTION_EXIT 10
- /*Max buffer macro*/
- #define MAX_BUFFER 256
- /*File name*/
- #define FILE_NAME "input.txt"
- typedef struct{
- int id;
- char name[32];
- char features[32];
- int quantity;
- int price;
- }product;
- /*Header area*/
- void menu();
- void add();
- /*Main function*/
- int main(){
- menu();
- return 0;
- }
- /*Implementation area*/
- void add(){
- char buffer[32];
- product p;
- printf("Please add an id:\n");
- fseek(stdin, 0, SEEK_END);
- scanf("%d",&(p.id));
- printf("Please add a name:\n");
- fseek(stdin, 0, SEEK_END);
- fgets(buffer,32,stdin);
- buffer[strlen(buffer)-1]='\0';
- strncpy(p.name,buffer,32);
- printf("Please add the features of the product:\n");
- fseek(stdin, 0, SEEK_END);
- fgets(buffer,32,stdin);
- buffer[strlen(buffer)-1]='\0';
- strncpy(p.features,buffer,32);
- printf("Please add a quantity:\n");
- fseek(stdin, 0, SEEK_END);
- scanf("%d",&(p.quantity));
- printf("Please add a price:\n");
- fseek(stdin, 0, SEEK_END);
- scanf("%d",&(p.price));
- FILE *out=fopen(FILE_NAME,"a");
- fprintf(out,"%d,%s,%s,%d,%d\n",p.id,p.name,p.features,p.quantity,p.price);
- fclose(out);
- }
- void list(){
- FILE *in=fopen(FILE_NAME,"r");
- char buffer[256];
- char *ptr;
- while(fgets(buffer,256,in)!=NULL){
- ptr=strtok(buffer,",");
- printf("ID:%s\n",ptr);
- ptr=strtok(NULL,",");
- printf("Name:%s\n",ptr);
- ptr=strtok(NULL,",");
- printf("Features:%s\n",ptr);
- ptr=strtok(NULL,",");
- printf("Quantity:%s\n",ptr);
- ptr=strtok(NULL,",");
- printf("Price:$%s\n\n",ptr);
- }
- fclose(in);
- }
- void total_value(){
- FILE *in=fopen(FILE_NAME,"r");
- char buffer[256];
- char *ptr;
- int total=0;
- int aux_q,aux_p;
- while(fgets(buffer,256,in)!=NULL){
- ptr=strtok(buffer,",");
- ptr=strtok(NULL,",");
- ptr=strtok(NULL,",");
- ptr=strtok(NULL,",");
- aux_q=atoi(ptr);
- ptr=strtok(NULL,",");
- aux_p=atoi(ptr);
- total+=aux_p*aux_q;
- }
- printf("Total price:%d\n",total);
- fclose(in);
- }
- void menu(){
- char *buffer=malloc(MAX_BUFFER*sizeof(char));
- int option=-1;
- do{
- printf("%d. Add a product\n",OPTION_ADD);
- printf("%d. List products\n",OPTION_LIST);
- printf("%d. Display total value\n",OPTION_DISPLAY_TOTAL_VALUE);
- printf("%d. Search by id\n",OPTION_SEARCH_ID);
- printf("%d. Search by name\n",OPTION_SEARCH_NAME);
- printf("%d. Display the cheapest\n",OPTION_DISPLAY_CHEAPEST);
- printf("%d. Display the ones that need to be resupplied\n",OPTION_DISPLAY_NEED_SUPPLY);
- printf("%d. Modify the price/quantity\n",OPTION_MODIFY_PRICE);
- printf("%d. Delete product\n",OPTION_DELETE);
- printf("%d. Exit\n",OPTION_EXIT);
- fseek(stdin, 0, SEEK_END);
- fgets(buffer,MAX_BUFFER,stdin);
- option=atoi(buffer);
- switch(option){
- case OPTION_ADD:
- printf("Adding a new product...\n");
- add();
- break;
- case OPTION_LIST:
- printf("Listing products...\n");
- list();
- break;
- case OPTION_DISPLAY_TOTAL_VALUE:
- printf("Displaying the total value...\n");
- total_value();
- break;
- case OPTION_SEARCH_ID:
- printf("Searching by id...\n");
- break;
- case OPTION_SEARCH_NAME:
- printf("Searching by name...\n");
- break;
- case OPTION_DISPLAY_CHEAPEST:
- printf("Displaying the cheapest product...\n");
- break;
- case OPTION_DISPLAY_NEED_SUPPLY:
- printf("Displaying the needed supplies...\n");
- break;
- case OPTION_MODIFY_PRICE:
- printf("Modifying the price...\n");
- break;
- case OPTION_DELETE:
- printf("Deleting a product...\n");
- break;
- case OPTION_EXIT:
- printf("Exiting...\n");
- break;
- default:
- printf("Please Enter a valid option!\n");
- }
- }while(option!=OPTION_EXIT);
- free(buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement