Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_FOOD_NAME 100
  4. #define MAX_TYPE_NAME 100
  5. #define MAX_LINE 100
  6. void readType(char * type);
  7. void readDrinks(char * drinks);
  8.  
  9. int main() {
  10. int noOfFoods, noOfDrinks,toInt;
  11. printf("Please input the number of food types\n>");
  12. scanf("%d",&noOfFoods);
  13. printf("Please input food types (each on a new line, may contain spaces)\n>");
  14. char ** foods;
  15. foods = (char**)malloc(noOfFoods* sizeof(char*));
  16. for(int i=0;i<noOfFoods;i++) {
  17. foods[i] = (char*)malloc(MAX_FOOD_NAME* sizeof(char));
  18. scanf("%s",foods[i]);
  19. if(i!=noOfFoods-1)printf(">");
  20. }
  21. int * noOfTypes = (int*)malloc(noOfFoods * sizeof(int));
  22. char *** types = (char***)malloc(noOfFoods * sizeof(char**));
  23. double ** prices = (double**)malloc(noOfFoods* sizeof(double*));
  24. for(int i=0; i<noOfFoods; i++) {
  25.  
  26. printf("Please input no of speciffic foods for food \"%s\"\n>",foods[i]);
  27. scanf("%d",&noOfTypes[i]);
  28. getchar();
  29.  
  30.  
  31. types[i] = (char**)malloc(noOfTypes[i]* sizeof(char*));
  32. prices[i] = (double*)malloc(noOfTypes[i]* sizeof(double));
  33.  
  34. printf("Please input \"%s\" speciffic foods &prices: each line in the format <speciffic food> (price):\n>",foods[i]);
  35. for(int j=0;j<noOfTypes[i];j++){
  36. types[i][j] = (char*)malloc(MAX_TYPE_NAME* sizeof(char));
  37. readType(types[i][j]);
  38. char line[MAX_LINE];
  39. gets(line);
  40. sscanf(line, "%lf", &prices[i][j]);
  41. if(j!=noOfTypes[i]-1)printf(">");
  42. }
  43. }
  44. printf("Please input no of drinks\n>");
  45. scanf("%d",&noOfDrinks);
  46. char ** drinks = (char**)malloc(noOfDrinks * sizeof(char*));
  47. double * drinksPrices = (double*)malloc(noOfDrinks* sizeof(double));
  48. printf("Please input the drinks: each line in the format <drink> (price):\n>");
  49. for(int j=0;j<noOfDrinks;j++){
  50. drinks[j] = (char*)malloc(noOfDrinks* sizeof(char));
  51. drinksPrices[j] = *(double*)malloc(noOfDrinks* sizeof(double));
  52. readDrinks(drinks[j]);
  53. char line[MAX_LINE];
  54. gets(line);
  55. sscanf(line, "%1lf", &drinksPrices[j]);
  56. printf(">");
  57. }
  58.  
  59.  
  60. printf("The food data is:\n");
  61. for(int i=0;i<noOfFoods;i++) {
  62. // display brand
  63. printf("%s: ", foods[i]);
  64. for(int j=0;j<noOfTypes[i];j++) {
  65. printf("(%s - %.2lf ) ",types[i][j],prices[i][j]);
  66. }
  67. printf("\n");
  68. }
  69. printf("The drinks data is:\ndrinks:");
  70. for(int i=0;i<noOfDrinks;i++){
  71. printf("%s", drinks[i]);
  72. if(i!=noOfDrinks-1)printf(", ");}
  73. printf("\n");
  74. printf("prices:");
  75. for(int i=0;i<noOfDrinks;i++){
  76. toInt=(int)drinksPrices[i];
  77. printf("%d",toInt);
  78. if(i!=noOfDrinks-1)printf(", ");}
  79. for(int i=0;i<noOfFoods;i++) {
  80. for(int j=0;j<noOfFoods;j++) {
  81. free(types[i][j]);
  82. }
  83. free(types[i]);
  84. free(prices[i]);
  85. free(foods[i]);
  86. free(drinks[i]);
  87. }
  88. free(types);
  89. free(prices);
  90. free(foods);
  91. free(noOfTypes);
  92. free(drinks);
  93. return 0;
  94. }
  95. void readType(char * type) {
  96. char c = getchar();
  97. int i=0;
  98. while(c!='(') {
  99. type[i] = c;
  100. c = getchar();
  101. i++;
  102. }
  103. type[i] = '\0';
  104. }
  105. void readDrinks(char * drinks) {
  106. char c = getchar();
  107. int i=0;
  108. while(c!='(') {
  109. drinks[i] = c;
  110. c = getchar();
  111. i++;
  112. }
  113. drinks[i] = '\0';
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement