Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 69 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. /*****************************************
  2. Erik Lindgren, eln17011
  3. Programmering, DVA117
  4. Laboration Lab5
  5. *****************************************/
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12.  
  13. #define arrsize 30
  14.  
  15. //namnen på strukturerna följer mallen fuctionname+shopping, så jag vet vilket struct jag jobbar med och vad jag kallar på
  16.  
  17. struct foodtype
  18. {
  19.     char food[arrsize];
  20.     char unit_type[arrsize];
  21.     char weight_number[arrsize];
  22. };
  23.  
  24. void print_options(); //printar ut de val man kan göra i programmet
  25. int checkUnit(struct foodtype *checkShopping, int number_of_entries); //kvalitetskontroll för unit_type, kollar t.ex efter alfabetiska tecken
  26. int checkWeight(struct foodtype *checkShopping,int number_of_entries); // kvalitetskontroll för weight_number, kollar t.ex så det bara finns siffror
  27. void add_food(struct foodtype *addshopping,int number_of_entries);  //lägger till ett mat item
  28. void add_unit(struct foodtype *addshopping,int number_of_entries); //lägger till en enhet
  29. float add_weight(struct foodtype *addshopping,int number_of_entries, float *converted_float); // lägger till en kvantitet
  30. void printShoppinglist(struct foodtype *printshopping,int number_of_entries, float converted_float); //printar hela listan
  31. void add_memory(struct foodtype **addfood, int number_of_entries); //lägger till extra minne för ett nytt objekt
  32. void remove_memory(struct foodtype **removefood, int number_of_entries); //tar bort minne när ett objekt tas bort
  33. void addItem(struct foodtype *addshopping,int number_of_entries, float *converted_float); //kallar på alla add_item-funktioner
  34. void modifyItem(struct foodtype *modifyshopping, int number_of_entries, float *converted_float); //låter dig ändra ett objekt
  35. int removeItem(struct foodtype *removeshopping, int number_of_entries, float converted_float); //tar bort ett objekt
  36. void saveToFile(struct foodtype *saveshopping, int number_of_entries); //sparar till en binär fil
  37. void loadFromFile(struct foodtype **loadshopping, int *number_of_entries, float *converted_float); ////laddar in en redan sparad fil
  38.  
  39. int main()
  40. {
  41.  
  42.     int number_of_entries=0, choose_starting_options;
  43.     struct foodtype *mainfood;
  44.     float converted_float;
  45.     mainfood = (struct foodtype*)malloc(sizeof(struct foodtype)*0);
  46.     printf("Welcome to your shoppinglist!");
  47.     do
  48.     {
  49.         choose_starting_options = 0;
  50.         print_options();
  51.         printf("What would you like to do? ");
  52.         scanf("%d", &choose_starting_options);
  53.         while(getchar()!='\n'); //rensar läsbuffert så att fel inmatning inte ger en oändlig loop
  54.         switch(choose_starting_options)
  55.         {
  56.         case 1:
  57.             system("cls");
  58.             add_memory(&mainfood, number_of_entries);
  59.             addItem(mainfood, number_of_entries, &converted_float);
  60.             number_of_entries++;
  61.             system("cls");
  62.             break;
  63.         case 2:
  64.             system("cls");
  65.             printShoppinglist(mainfood, number_of_entries, converted_float);
  66.             break;
  67.         case 3:
  68.             system("cls");
  69.             modifyItem(mainfood, number_of_entries, &converted_float);
  70.             break;
  71.         case 4:
  72.             system("cls");
  73.             number_of_entries = removeItem(mainfood, number_of_entries, converted_float);
  74.             break;
  75.         case 5:
  76.             system("cls");
  77.             loadFromFile(&mainfood, &number_of_entries, &converted_float);
  78.             break;
  79.         case 6:
  80.             system("cls");
  81.             saveToFile(mainfood, number_of_entries);
  82.             break;
  83.         case 7:
  84.             system("cls");
  85.             printf("\nThank you for using the program\n");
  86.             free(mainfood);
  87.             break;
  88.         default:
  89.             system("cls");
  90.             printf("\nThere is no function for that statement\n");
  91.             break;
  92.         }
  93.  
  94.     }
  95.     while(choose_starting_options!=7);
  96.  
  97.  
  98.     return 0;
  99. }
  100.  
  101. void print_options()
  102. {
  103.     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");
  104. }
  105. 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
  106. {
  107.  
  108.     int j,i, checkdigit=0;
  109.  
  110.     char checker[50] = {NULL};
  111.     for(j=0; j<strlen(checkShopping[number_of_entries].unit_type); j++)
  112.     {
  113.         checker[j] = checkShopping[number_of_entries].unit_type[j]; //lägger arrayen i en med kortare namn för lättare kod
  114.     }
  115.  
  116.     for(i=0; i<strlen(checker); i++)
  117.     {
  118.  
  119.         if(isalpha(checker[i])==0)
  120.         {
  121.             checkdigit++;
  122.         }
  123.     }
  124.     if(checkdigit>0)
  125.     {
  126.  
  127.         return 0;
  128.     }
  129.     else if(checkdigit==0)
  130.     {
  131.  
  132.         return 1;
  133.     }
  134.     return 3;
  135. }
  136.  
  137. int checkWeight(struct foodtype *checkShopping,int number_of_entries)
  138. {
  139.  
  140.     int k, j, i, checknondigit=0;
  141.     char checker[50] = {NULL};
  142.     for(j=0; j<strlen(checkShopping[number_of_entries].weight_number); j++)
  143.     {
  144.         checker[j] = checkShopping[number_of_entries].weight_number[j]; //lägger arrayen i en med kortare namn för lättare kod
  145.  
  146.     }
  147.     for(i=0; i<strlen(checker); i++)
  148.     {
  149.         if(checker[i]==',')
  150.         {
  151.             checker[i]='.';
  152.         }
  153.     }
  154.  
  155.     for(k=0; k<strlen(checker); k++)
  156.     {
  157.  
  158.         if(checker[k]=='-') //så negativa tal ej är tillåtna
  159.         {
  160.             checknondigit = checknondigit + 2;
  161.         }
  162.         else if((checker[k]=='.'||checker[k]==',')&&isdigit(checker[k-1])==1&&isdigit(checker[k+1])==1)
  163.         {
  164.             checknondigit = checknondigit + 1;
  165.         }
  166.  
  167.         else if(isdigit(checker[k])==0) //ifall en icke siffra stöts på, minska size variabeln
  168.         {
  169.  
  170.             checknondigit = checknondigit+2;
  171.         }
  172.  
  173.     }
  174.     if(checknondigit>1)
  175.     {
  176.         return 0;
  177.     }
  178.     else
  179.     {
  180.         return 1;
  181.     }
  182.  
  183.  
  184.     return 3;
  185. }
  186.  
  187.  
  188.  
  189. void add_food(struct foodtype *addshopping,int number_of_entries)
  190. {
  191.  
  192.     printf("Enter a food: ");
  193.     gets(addshopping[number_of_entries].food);
  194.  
  195. }
  196.  
  197. void add_unit(struct foodtype *addshopping,int number_of_entries)
  198. {
  199.  
  200.     int k;
  201.  
  202.     do
  203.     {
  204.         printf("Enter a unit type: ");
  205.         gets(addshopping[number_of_entries].unit_type);
  206.         k=checkUnit(addshopping, number_of_entries);
  207.         if(k==1)
  208.         {
  209.  
  210.  
  211.         }
  212.         else if(k==0)
  213.         {
  214.  
  215.             printf("That is not a valid entry, please use only alphabetical characters\n");
  216.         }
  217.     }
  218.     while(k==0);
  219.  
  220. }
  221.  
  222. float add_weight(struct foodtype *addshopping,int number_of_entries, float *converted_float)
  223. {
  224.  
  225.     int i;
  226.     do
  227.     {
  228.         printf("Enter a quantity of the food: ");
  229.         gets(addshopping[number_of_entries].weight_number);
  230.         i = checkWeight(addshopping, number_of_entries);
  231.         if(i==1)
  232.         {
  233.             *converted_float = atof(addshopping->weight_number);
  234.         }
  235.         else if(i==0)
  236.         {
  237.             printf("You did not write a valid entry, try again\n");
  238.  
  239.         }
  240.  
  241.     }
  242.     while(i==0);
  243.     return *converted_float;
  244. }
  245.  
  246.  
  247.  
  248. void printShoppinglist(struct foodtype *printshopping,int number_of_entries, float converted_float)
  249. {
  250.     int i;
  251.     if(number_of_entries<1)
  252.     {
  253.         printf("\n*------------------------------*\n\n");
  254.         printf("Your shopping list is empty\n\n");
  255.         printf("*------------------------------*\n\n");
  256.     }
  257.     else
  258.     {
  259.         printf("\nYour shopping list contains:\n");
  260.         printf("*------------------------------*\n");
  261.         for(i=0; i<(number_of_entries); i++)
  262.         {
  263.             printf("Nr: %d - %.2f",(i+1), converted_float);
  264.             printf(" %s", printshopping[i].unit_type);
  265.             printf(" %s\n", printshopping[i].food);
  266.  
  267.  
  268.  
  269.         }
  270.         printf("*------------------------------*\n\n");
  271.     }
  272. }
  273.  
  274.  
  275. void add_memory(struct foodtype **addfood, int number_of_entries)
  276. {
  277.  
  278.     struct foodtype *temp = (struct foodtype*)realloc(*addfood,sizeof(struct foodtype)*(number_of_entries+1));
  279.  
  280.     if(temp==NULL)
  281.     {
  282.         printf("ERROR WITH MEMORY\n");
  283.     }
  284.     else
  285.     {
  286.         *addfood = temp;
  287.     }
  288. }
  289.  
  290. void remove_memory(struct foodtype **removefood, int number_of_entries)
  291. {
  292.  
  293.     if(number_of_entries==0)
  294.     {
  295.         *removefood = (struct foodtype*)malloc(sizeof(struct foodtype)*1);
  296.     }
  297.     else
  298.     {
  299.         struct foodtype *temp = (struct foodtype*)realloc(*removefood,sizeof(struct foodtype)*(number_of_entries));
  300.  
  301.         if(temp==NULL)
  302.         {
  303.             printf("ERROR WITH MEMORY\n");
  304.         }
  305.         else
  306.         {
  307.             *removefood = temp;
  308.         }
  309.     }
  310.  
  311. }
  312.  
  313.  
  314. void addItem(struct foodtype *addshopping,int number_of_entries, float *converted_float)
  315. {
  316.     printf("\n");
  317.     add_food(addshopping, number_of_entries);
  318.     add_weight(addshopping, number_of_entries, converted_float);
  319.     add_unit(addshopping, number_of_entries);
  320.  
  321. }
  322.  
  323. void modifyItem(struct foodtype *modifyshopping, int number_of_entries, float *converted_float)
  324. {
  325.     if(number_of_entries<1)
  326.     {
  327.         printf("\nYour shoppinglist is empty, there is nothing to modify\n");
  328.     }
  329.     else
  330.     {
  331.         int object_to_modify, i, quality_check=0;
  332.         printShoppinglist(modifyshopping, number_of_entries, *converted_float);
  333.         do
  334.         {
  335.             printf("Which entry would you like to modify? ");
  336.             i = scanf("%d", &object_to_modify);
  337.             if(i==1&&object_to_modify>0&&object_to_modify<=number_of_entries)
  338.             {
  339.                 object_to_modify = object_to_modify - 1;
  340.                 while(getchar()!='\n');
  341.                 add_weight(modifyshopping, object_to_modify, converted_float);
  342.                 quality_check = 1;
  343.             }
  344.  
  345.             else
  346.             {
  347.                 printf("That is not an allowed entry, please try again...\n");
  348.                 while(getchar()!='\n');
  349.  
  350.             }
  351.  
  352.         }
  353.         while(quality_check==0);
  354.     }
  355. }
  356.  
  357. int removeItem(struct foodtype *removeshopping, int number_of_entries, float converted_float)
  358. {
  359.  
  360.     int i, entry_to_remove, quality_check=0;
  361.  
  362.     if(number_of_entries<1)
  363.     {
  364.         printf("\nYour shoppinglist is empty, there is nothing to remove\n");
  365.     }
  366.     else
  367.     {
  368.         printShoppinglist(removeshopping, number_of_entries, converted_float);
  369.         do
  370.         {
  371.             if(number_of_entries==0)
  372.             {
  373.                 printf("Your shoppinglist is empty, there is nothing to remove...");
  374.             }
  375.             printf("Which object would you like to remove? ");
  376.             scanf("%d", &entry_to_remove);
  377.             if(entry_to_remove>=1&&entry_to_remove<=number_of_entries)
  378.             {
  379.                 entry_to_remove--;
  380.                 for(i=entry_to_remove; i<number_of_entries; i++)
  381.                 {
  382.                     removeshopping[i]=removeshopping[i+1];
  383.                 }
  384.                 number_of_entries = number_of_entries-1;
  385.                 remove_memory(&removeshopping,number_of_entries);
  386.                 quality_check = 1;
  387.             }
  388.             else
  389.             {
  390.                 printf("Not an allowed entry, please try again.\n");
  391.                 while(getchar()!='\n');
  392.             }
  393.  
  394.         }
  395.         while(quality_check==0);
  396.     }
  397.  
  398.  
  399.     return number_of_entries;
  400. }
  401.  
  402. void saveToFile(struct foodtype *saveshopping, int number_of_entries)
  403. {
  404.  
  405.     FILE *fp;
  406.     char filename[50];
  407.     printf("Name of file to be saved: ");
  408.     gets(filename);
  409.     fp = fopen(filename, "wb");
  410.  
  411.     if(fp!=NULL)
  412.     {
  413.         fwrite(&number_of_entries,sizeof(int),1,fp);
  414.         fwrite(saveshopping,sizeof(struct foodtype),number_of_entries,fp);
  415.         printf("File %s was saved successfully!\n", filename);
  416.     }
  417.  
  418.     else
  419.     {
  420.         printf("File was NOT successfully saved!\n");
  421.     }
  422.  
  423.     fclose(fp);
  424.  
  425. }
  426.  
  427. void loadFromFile(struct foodtype **loadshopping, int *number_of_entries, float *converted_float)
  428. {
  429.     int quality=0;
  430.     FILE *fp;
  431.     char filename[50];
  432.     char y;
  433.     if(*number_of_entries>0){
  434.         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): ");
  435.         scanf(" %c", &y);
  436.         while(getchar()!='\n');
  437.         if(y=='y'){
  438.             saveToFile(*loadshopping,*number_of_entries);
  439.         }
  440.         else{
  441.             printf("File was not saved!\n");
  442.  
  443.         }
  444.  
  445.     }
  446.  
  447.     free(loadshopping);
  448.     *loadshopping = (struct foodtype*)malloc(sizeof(struct foodtype)*1);
  449.     do{
  450.     printf("Enter a filename to open: ");
  451.     gets(filename);
  452.     fp = fopen(filename,"rb");
  453.     if(fp!=NULL)
  454.     {
  455.         fread(number_of_entries,sizeof(int),1,fp);
  456.         //printf("%d\n", number_of_entries);
  457.         *loadshopping = (struct foodtype*)realloc(*loadshopping, sizeof(struct foodtype)*(*number_of_entries));
  458.         fread(*loadshopping, sizeof(struct foodtype), *number_of_entries,fp);
  459.         *converted_float = atof((*loadshopping)->weight_number);
  460.         quality=1;
  461.     }
  462.     else
  463.     {
  464.         printf("File could not be opened, try again..\n");
  465.     }
  466.     fclose(fp);
  467.     }while(quality==0);
  468.  
  469. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top