Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************
- Erik Lindgren, eln17011
- Programmering, DVA117
- Laboration Lab5
- *****************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #define arrsize 30
- //namnen på strukturerna följer mallen fuctionname+shopping, så jag vet vilket struct jag jobbar med och vad jag kallar på
- struct foodtype
- {
- char food[arrsize];
- char unit_type[arrsize];
- char weight_number[arrsize];
- };
- void print_options(); //printar ut de val man kan göra i programmet
- int checkUnit(struct foodtype *checkShopping, int number_of_entries); //kvalitetskontroll för unit_type, kollar t.ex efter alfabetiska tecken
- int checkWeight(struct foodtype *checkShopping,int number_of_entries); // kvalitetskontroll för weight_number, kollar t.ex så det bara finns siffror
- void add_food(struct foodtype *addshopping,int number_of_entries); //lägger till ett mat item
- void add_unit(struct foodtype *addshopping,int number_of_entries); //lägger till en enhet
- float add_weight(struct foodtype *addshopping,int number_of_entries, float *converted_float); // lägger till en kvantitet
- void printShoppinglist(struct foodtype *printshopping,int number_of_entries, float converted_float); //printar hela listan
- void add_memory(struct foodtype **addfood, int number_of_entries); //lägger till extra minne för ett nytt objekt
- void remove_memory(struct foodtype **removefood, int number_of_entries); //tar bort minne när ett objekt tas bort
- void addItem(struct foodtype *addshopping,int number_of_entries, float *converted_float); //kallar på alla add_item-funktioner
- void modifyItem(struct foodtype *modifyshopping, int number_of_entries, float *converted_float); //låter dig ändra ett objekt
- int removeItem(struct foodtype *removeshopping, int number_of_entries, float converted_float); //tar bort ett objekt
- void saveToFile(struct foodtype *saveshopping, int number_of_entries); //sparar till en binär fil
- void loadFromFile(struct foodtype **loadshopping, int *number_of_entries, float *converted_float); ////laddar in en redan sparad fil
- int main()
- {
- int number_of_entries=0, choose_starting_options;
- struct foodtype *mainfood;
- float converted_float;
- mainfood = (struct foodtype*)malloc(sizeof(struct foodtype)*0);
- printf("Welcome to your shoppinglist!");
- do
- {
- choose_starting_options = 0;
- print_options();
- printf("What would you like to do? ");
- scanf("%d", &choose_starting_options);
- while(getchar()!='\n'); //rensar läsbuffert så att fel inmatning inte ger en oändlig loop
- switch(choose_starting_options)
- {
- case 1:
- system("cls");
- add_memory(&mainfood, number_of_entries);
- addItem(mainfood, number_of_entries, &converted_float);
- number_of_entries++;
- system("cls");
- break;
- case 2:
- system("cls");
- printShoppinglist(mainfood, number_of_entries, converted_float);
- break;
- case 3:
- system("cls");
- modifyItem(mainfood, number_of_entries, &converted_float);
- break;
- case 4:
- system("cls");
- number_of_entries = removeItem(mainfood, number_of_entries, converted_float);
- break;
- case 5:
- system("cls");
- loadFromFile(&mainfood, &number_of_entries, &converted_float);
- break;
- case 6:
- system("cls");
- saveToFile(mainfood, number_of_entries);
- break;
- case 7:
- system("cls");
- printf("\nThank you for using the program\n");
- free(mainfood);
- break;
- default:
- system("cls");
- printf("\nThere is no function for that statement\n");
- break;
- }
- }
- while(choose_starting_options!=7);
- return 0;
- }
- void print_options()
- {
- printf("\n*-----------------------------------*\n1 - Add a food item\n2 - Print your current shopping list\n3 - Modify an object\n4 - Remove an object\n5 - Load from file\n6 - Save to file\n7 - Terminate the program\n*-----------------------------------*\n");
- }
- int checkUnit(struct foodtype *checkShopping, int number_of_entries) //Kollar ifall unit_number är något annat än en siffra, och om den inte är det, returnerna 0, annars returnera 1
- {
- int j,i, checkdigit=0;
- char checker[50] = {NULL};
- for(j=0; j<strlen(checkShopping[number_of_entries].unit_type); j++)
- {
- checker[j] = checkShopping[number_of_entries].unit_type[j]; //lägger arrayen i en med kortare namn för lättare kod
- }
- for(i=0; i<strlen(checker); i++)
- {
- if(isalpha(checker[i])==0)
- {
- checkdigit++;
- }
- }
- if(checkdigit>0)
- {
- return 0;
- }
- else if(checkdigit==0)
- {
- return 1;
- }
- return 3;
- }
- int checkWeight(struct foodtype *checkShopping,int number_of_entries)
- {
- int k, j, i, checknondigit=0;
- char checker[50] = {NULL};
- for(j=0; j<strlen(checkShopping[number_of_entries].weight_number); j++)
- {
- checker[j] = checkShopping[number_of_entries].weight_number[j]; //lägger arrayen i en med kortare namn för lättare kod
- }
- for(i=0; i<strlen(checker); i++)
- {
- if(checker[i]==',')
- {
- checker[i]='.';
- }
- }
- for(k=0; k<strlen(checker); k++)
- {
- if(checker[k]=='-') //så negativa tal ej är tillåtna
- {
- checknondigit = checknondigit + 2;
- }
- else if((checker[k]=='.'||checker[k]==',')&&isdigit(checker[k-1])==1&&isdigit(checker[k+1])==1)
- {
- checknondigit = checknondigit + 1;
- }
- else if(isdigit(checker[k])==0) //ifall en icke siffra stöts på, minska size variabeln
- {
- checknondigit = checknondigit+2;
- }
- }
- if(checknondigit>1)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- return 3;
- }
- void add_food(struct foodtype *addshopping,int number_of_entries)
- {
- printf("Enter a food: ");
- gets(addshopping[number_of_entries].food);
- }
- void add_unit(struct foodtype *addshopping,int number_of_entries)
- {
- int k;
- do
- {
- printf("Enter a unit type: ");
- gets(addshopping[number_of_entries].unit_type);
- k=checkUnit(addshopping, number_of_entries);
- if(k==1)
- {
- }
- else if(k==0)
- {
- printf("That is not a valid entry, please use only alphabetical characters\n");
- }
- }
- while(k==0);
- }
- float add_weight(struct foodtype *addshopping,int number_of_entries, float *converted_float)
- {
- int i;
- do
- {
- printf("Enter a quantity of the food: ");
- gets(addshopping[number_of_entries].weight_number);
- i = checkWeight(addshopping, number_of_entries);
- if(i==1)
- {
- *converted_float = atof(addshopping->weight_number);
- }
- else if(i==0)
- {
- printf("You did not write a valid entry, try again\n");
- }
- }
- while(i==0);
- return *converted_float;
- }
- void printShoppinglist(struct foodtype *printshopping,int number_of_entries, float converted_float)
- {
- int i;
- if(number_of_entries<1)
- {
- printf("\n*------------------------------*\n\n");
- printf("Your shopping list is empty\n\n");
- printf("*------------------------------*\n\n");
- }
- else
- {
- printf("\nYour shopping list contains:\n");
- printf("*------------------------------*\n");
- for(i=0; i<(number_of_entries); i++)
- {
- printf("Nr: %d - %.2f",(i+1), converted_float);
- printf(" %s", printshopping[i].unit_type);
- printf(" %s\n", printshopping[i].food);
- }
- printf("*------------------------------*\n\n");
- }
- }
- void add_memory(struct foodtype **addfood, int number_of_entries)
- {
- struct foodtype *temp = (struct foodtype*)realloc(*addfood,sizeof(struct foodtype)*(number_of_entries+1));
- if(temp==NULL)
- {
- printf("ERROR WITH MEMORY\n");
- }
- else
- {
- *addfood = temp;
- }
- }
- void remove_memory(struct foodtype **removefood, int number_of_entries)
- {
- if(number_of_entries==0)
- {
- *removefood = (struct foodtype*)malloc(sizeof(struct foodtype)*1);
- }
- else
- {
- struct foodtype *temp = (struct foodtype*)realloc(*removefood,sizeof(struct foodtype)*(number_of_entries));
- if(temp==NULL)
- {
- printf("ERROR WITH MEMORY\n");
- }
- else
- {
- *removefood = temp;
- }
- }
- }
- void addItem(struct foodtype *addshopping,int number_of_entries, float *converted_float)
- {
- printf("\n");
- add_food(addshopping, number_of_entries);
- add_weight(addshopping, number_of_entries, converted_float);
- add_unit(addshopping, number_of_entries);
- }
- void modifyItem(struct foodtype *modifyshopping, int number_of_entries, float *converted_float)
- {
- if(number_of_entries<1)
- {
- printf("\nYour shoppinglist is empty, there is nothing to modify\n");
- }
- else
- {
- int object_to_modify, i, quality_check=0;
- printShoppinglist(modifyshopping, number_of_entries, *converted_float);
- do
- {
- printf("Which entry would you like to modify? ");
- i = scanf("%d", &object_to_modify);
- if(i==1&&object_to_modify>0&&object_to_modify<=number_of_entries)
- {
- object_to_modify = object_to_modify - 1;
- while(getchar()!='\n');
- add_weight(modifyshopping, object_to_modify, converted_float);
- quality_check = 1;
- }
- else
- {
- printf("That is not an allowed entry, please try again...\n");
- while(getchar()!='\n');
- }
- }
- while(quality_check==0);
- }
- }
- int removeItem(struct foodtype *removeshopping, int number_of_entries, float converted_float)
- {
- int i, entry_to_remove, quality_check=0;
- if(number_of_entries<1)
- {
- printf("\nYour shoppinglist is empty, there is nothing to remove\n");
- }
- else
- {
- printShoppinglist(removeshopping, number_of_entries, converted_float);
- do
- {
- if(number_of_entries==0)
- {
- printf("Your shoppinglist is empty, there is nothing to remove...");
- }
- printf("Which object would you like to remove? ");
- scanf("%d", &entry_to_remove);
- if(entry_to_remove>=1&&entry_to_remove<=number_of_entries)
- {
- entry_to_remove--;
- for(i=entry_to_remove; i<number_of_entries; i++)
- {
- removeshopping[i]=removeshopping[i+1];
- }
- number_of_entries = number_of_entries-1;
- remove_memory(&removeshopping,number_of_entries);
- quality_check = 1;
- }
- else
- {
- printf("Not an allowed entry, please try again.\n");
- while(getchar()!='\n');
- }
- }
- while(quality_check==0);
- }
- return number_of_entries;
- }
- void saveToFile(struct foodtype *saveshopping, int number_of_entries)
- {
- FILE *fp;
- char filename[50];
- printf("Name of file to be saved: ");
- gets(filename);
- fp = fopen(filename, "wb");
- if(fp!=NULL)
- {
- fwrite(&number_of_entries,sizeof(int),1,fp);
- fwrite(saveshopping,sizeof(struct foodtype),number_of_entries,fp);
- printf("File %s was saved successfully!\n", filename);
- }
- else
- {
- printf("File was NOT successfully saved!\n");
- }
- fclose(fp);
- }
- void loadFromFile(struct foodtype **loadshopping, int *number_of_entries, float *converted_float)
- {
- int quality=0;
- FILE *fp;
- char filename[50];
- char y;
- if(*number_of_entries>0){
- printf("You currently have an unsaved shoppinglist, would you like to save it? If you do not, it will be removed.\n(Enter y to save): ");
- scanf(" %c", &y);
- while(getchar()!='\n');
- if(y=='y'){
- saveToFile(*loadshopping,*number_of_entries);
- }
- else{
- printf("File was not saved!\n");
- }
- }
- free(loadshopping);
- *loadshopping = (struct foodtype*)malloc(sizeof(struct foodtype)*1);
- do{
- printf("Enter a filename to open: ");
- gets(filename);
- fp = fopen(filename,"rb");
- if(fp!=NULL)
- {
- fread(number_of_entries,sizeof(int),1,fp);
- //printf("%d\n", number_of_entries);
- *loadshopping = (struct foodtype*)realloc(*loadshopping, sizeof(struct foodtype)*(*number_of_entries));
- fread(*loadshopping, sizeof(struct foodtype), *number_of_entries,fp);
- *converted_float = atof((*loadshopping)->weight_number);
- quality=1;
- }
- else
- {
- printf("File could not be opened, try again..\n");
- }
- fclose(fp);
- }while(quality==0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement