Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- struct groceryList{
- int ID;
- float amount;
- char grocery[100];
- char unit[10];
- };
- /*funktion som kollar om användaren skriver in chars i "amount".*/
- void errorHandling(struct groceryList *pgroceries, int listCounter)
- {
- int i, scanString = 0, count;
- char arr[20];
- do{
- fflush(stdin);
- gets(arr);
- scanString = strlen(arr);
- count = 0;
- for(i = 0; i < scanString;)
- {
- if(isdigit(arr[i]) != 0 || arr[i] == '.') //Om arr:s index inte är alfabetiskt eller är en punkt. Hoppa till nästa index
- {
- i++;
- }
- else
- {
- printf("Wrong input. Try again!");
- count++;
- break;
- }
- }
- }while(count > 0);
- pgroceries[listCounter].amount = atof(arr); //ger amount arr:s numeriska symboler.
- }
- /* funktion som listar alla varor och mängden av varan.*/
- void listingGroceries(struct groceryList *pgroceries, int listCounter)
- {
- fflush(stdin);
- pgroceries[listCounter].ID = listCounter + 1; //listar varorna
- printf("What grocery do you wanna put in the list? ");
- gets(pgroceries[listCounter].grocery);
- printf("What's the amount? ");
- errorHandling(pgroceries, listCounter);
- printf("What's the unit? ");
- gets(pgroceries[listCounter].unit);
- }
- /*Skriver ut alla varor med formaterad text.*/
- void printGroceries(struct groceryList *pgroceries, int listCounter)
- {
- int j;
- if(pgroceries == 0)
- {
- printf("There are no groceries to print.\n");
- }
- else
- {
- for(j = 0; j < listCounter; j++)
- {
- printf("Grocery %2d:", pgroceries[j].ID); //Skriver ut varorna.
- printf("%-15s:", pgroceries[j].grocery);
- printf("%10.2f:", pgroceries[j].amount);
- printf("%-8s\n", pgroceries[j].unit);
- }
- }
- }
- int writeToFile(struct groceryList **pgroceries, int listCounter)
- {
- char userInput[50];
- FILE *fp = 0;
- printf("What would you like to call the file?\n"); //Funktion som läser in varorna till en fil. Felhantering finns.
- scanf("%s", userInput);
- fp = fopen(userInput,"wb");
- if(fp == NULL)
- {
- printf("Could not write to file, sorry!\n");
- return 0;
- }
- fwrite(*pgroceries, sizeof(struct groceryList), listCounter, fp);
- printf("%d groceries where saved to the file!\n", listCounter);
- fclose(fp);
- fflush(stdin);
- return 0;
- }
- int readFromFile(struct groceryList **pgroceries, int listCounter)
- {
- char userInput[50];
- int byteSize = 0;
- FILE *fp = 0;
- printf("What file would you like to open?\n");
- scanf("%s", userInput);
- fp = fopen(userInput,"rb"); //funktion som läser in från en fil.
- if(fp == NULL)
- {
- printf("Could not read from file, sorry!\n");
- return 0;
- }
- else
- {
- fseek(fp, 0, SEEK_END); //letar efter filens slut.
- byteSize = ftell(fp); //Get bytesize filpekarens antal minnesplatser som en siffra.
- rewind(fp); //Sätter filpekaren i början av filen igen.
- *pgroceries = (struct groceryList*)realloc(*pgroceries, sizeof(struct groceryList) * (byteSize/sizeof(struct groceryList))); //allokerar minne
- listCounter = fread(*pgroceries, sizeof(struct groceryList), byteSize/sizeof(struct groceryList), fp); // läser in varorna och ger listcounter
- printf("%d groceries where read from the file!\n", listCounter); //antalet varor som läses in.
- fclose(fp);
- }
- fflush(stdin);
- return listCounter;
- }
- //funktion som modiferar en vara i listan.
- int modifyGrocery(struct groceryList **pgroceries, int listCounter)
- {
- printf("Type in the ID of the grocery you would like to modify. ");
- scanf("%d", &listCounter);
- if(*pgroceries == NULL)
- {
- printf("No groceries to modify. Add groceries to the list first!\n");
- fflush(stdin);
- return 0;
- }
- else
- {
- listingGroceries(*pgroceries,listCounter-1);
- printf("ID %d has been modified.", listCounter);
- }
- fflush(stdin);
- return listCounter;
- }
- void setMemory(struct groceryList **pgroceries, int listCounter)
- {
- struct groceryList *tp = (struct groceryList*) realloc(*pgroceries,listCounter*sizeof(struct groceryList)); // Allokerar minne varje gång man lägger till en vara.
- if(tp != NULL)
- {
- *pgroceries = tp;
- }
- else
- printf("Something went wrong");
- }
- int removeGrocery(struct groceryList **pgroceries, int listCounter)
- {
- int pickedElement;
- printf("What grocery would you like to remove? ");
- scanf("%d", &pickedElement);
- if(*pgroceries == NULL)
- {
- printf("No groceries to remove. Add groceries first!\n");
- fflush(stdin);
- return 0;
- }
- else if(pickedElement == 0 || pickedElement > listCounter)
- {
- printf("No grocery with that number.\n");
- fflush(stdin);
- return 0;
- }
- else
- {
- pickedElement--;
- strcpy((*pgroceries)[pickedElement].grocery, (*pgroceries)[listCounter-1].grocery); // tar bort ett element, och lägger sista elementet på dess plats.
- (*pgroceries)[pickedElement].amount = (*pgroceries)[listCounter-1].amount;
- strcpy((*pgroceries)[pickedElement].unit, (*pgroceries)[listCounter-1].unit);
- listCounter--;
- setMemory(pgroceries, listCounter);
- fflush(stdin);
- }
- printf("Grocery %d is annihilated.\n", pickedElement+1);
- fflush(stdin);
- return listCounter;
- }
- int* menu(struct groceryList **pgroceries)
- {
- char menuBrowser[2];
- int listCounter = 0;
- do{
- printf("Menu\nWhat would you like to do?\n");
- printf("\n-----------------------------------\n");
- printf("1. Add grocery\n");
- printf("2. Print your list\n");
- printf("3. Add groceries to file\n");
- printf("4. Read groceries from file\n");
- printf("5. Modify grocery\n");
- printf("6. Remove grocery\n");
- printf("7. Exit the program\n");
- printf("\n-----------------------------------\n");
- gets(menuBrowser);
- switch(menuBrowser[0]){
- case '1':
- {
- listCounter++;
- setMemory(pgroceries, listCounter);
- listingGroceries(*pgroceries, listCounter-1);
- break;
- }
- case '2':
- {
- printGroceries(*pgroceries, listCounter);
- break;
- }
- case '3':
- {
- writeToFile(pgroceries, listCounter);
- break;
- }
- case '4':
- {
- listCounter = readFromFile(pgroceries, listCounter);
- break;
- }
- case '5':
- {
- modifyGrocery(pgroceries, listCounter);
- break;
- }
- case '6':
- {
- listCounter = removeGrocery(pgroceries, listCounter);
- break;
- }
- case '7':
- {
- free(pgroceries);
- return 0;
- }
- }
- }while(1);
- }
- int main()
- {
- char loop = 'y';
- struct groceryList *groceries = NULL;
- do{
- menu(&groceries);
- printf("\nDo you want to play the program again? Y/N ");
- do
- {
- scanf("%c", &loop);
- }
- while(loop != 'y' && loop != 'n');
- fflush(stdin);
- }while(loop == 'y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement