fork download
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C/C++.
  5. Code, Compile, Run and Debug online from anywhere in world.
  6.  
  7. *******************************************************************************/
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. struct flavor
  13. {
  14. char name[20];
  15. double small_price;
  16. double medium_price;
  17. double large_price;
  18. struct flavor *next;
  19. struct flavor *prev;
  20. };
  21. typedef struct flavor f;
  22. f *head = NULL;
  23. f *tail = NULL;
  24.  
  25. void add_flavor(char name[], double small_price, double medium_price, double large_price)
  26. {
  27. f *new_flavor = (f*) malloc(sizeof(f));
  28. strcpy(new_flavor->name, name);
  29. new_flavor->small_price = small_price;
  30. new_flavor->medium_price = medium_price;
  31. new_flavor->large_price = large_price;
  32. new_flavor->next = NULL;
  33. new_flavor->prev = tail;
  34. if (tail != NULL)
  35. {
  36. tail->next = new_flavor;
  37. }
  38. tail = new_flavor;
  39. if (head == NULL)
  40. {
  41. head = new_flavor;
  42. }
  43. }
  44.  
  45. void display_flavors()
  46. {
  47. f *current = head;
  48. while (current != NULL)
  49. {
  50. printf("%s:\n", current->name);
  51. printf("Small - %.2f INR\n", current->small_price);
  52. printf("Medium - %.2f INR\n", current->medium_price);
  53. printf("Large - %.2f INR\n", current->large_price);
  54. current = current->next;
  55. }
  56. }
  57.  
  58. void purchase_flavor(char name[], char size[])
  59. {
  60. f *current = head;
  61. while (current != NULL)
  62. {
  63. if (strcmp(current->name, name) == 0)
  64. {
  65. double price;
  66. if (strcmp(size, "Small") == 0)
  67. {
  68. price = current->small_price;
  69. }
  70. else if (strcmp(size, "Medium") == 0)
  71. {
  72. price = current->medium_price;
  73. }
  74. else if (strcmp(size, "Large") == 0)
  75. {
  76. price = current->large_price;
  77. }
  78. else
  79. {
  80. printf("Invalid size.\n");
  81. return;
  82. }
  83. printf("You have purchased a %s ice cream, size %s, for %.2f INR.\n", current->name, size, price);
  84. return;
  85. }
  86. current = current->next;
  87. }
  88. printf("Sorry, we don't have %s flavor.\n", name);
  89. }
  90.  
  91. int main()
  92. {
  93. add_flavor("Vanilla", 20.0, 30.0, 40.0);
  94. add_flavor("Chocolate", 25.0, 35.0, 45.0);
  95. add_flavor("Strawberry", 30.0, 40.0, 50.0);
  96. add_flavor("Mango", 35.0, 45.0, 55.0);
  97. add_flavor("Butterscotch", 40.0, 50.0, 60.0);
  98. printf("Welcome to the ice cream vending machine!\n");
  99. printf("Please select a flavor and size:\n");
  100. display_flavors();
  101. char selected_flavor[20];
  102. char selected_size[10];
  103. scanf("%s %s", selected_flavor, selected_size);
  104. purchase_flavor(selected_flavor, selected_size);
  105. return 0;
  106. }
Success #stdin #stdout 0s 5516KB
stdin
Vanilla
Small
stdout
Welcome to the ice cream vending machine!
Please select a flavor and size:
Vanilla:
Small - 20.00 INR
Medium - 30.00 INR
Large - 40.00 INR
Chocolate:
Small - 25.00 INR
Medium - 35.00 INR
Large - 45.00 INR
Strawberry:
Small - 30.00 INR
Medium - 40.00 INR
Large - 50.00 INR
Mango:
Small - 35.00 INR
Medium - 45.00 INR
Large - 55.00 INR
Butterscotch:
Small - 40.00 INR
Medium - 50.00 INR
Large - 60.00 INR
You have purchased a Vanilla ice cream, size Small, for 20.00 INR.