Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_FOOD_NAME 100
- #define MAX_TYPE_NAME 100
- #define MAX_LINE 100
- void readType(char * type);
- void readDrinks(char * drinks);
- int main() {
- int noOfFoods, noOfDrinks,toInt;
- printf("Please input the number of food types\n>");
- scanf("%d",&noOfFoods);
- printf("Please input food types (each on a new line, may contain spaces)\n>");
- char ** foods;
- foods = (char**)malloc(noOfFoods* sizeof(char*));
- for(int i=0;i<noOfFoods;i++) {
- foods[i] = (char*)malloc(MAX_FOOD_NAME* sizeof(char));
- scanf("%s",foods[i]);
- if(i!=noOfFoods-1)printf(">");
- }
- int * noOfTypes = (int*)malloc(noOfFoods * sizeof(int));
- char *** types = (char***)malloc(noOfFoods * sizeof(char**));
- double ** prices = (double**)malloc(noOfFoods* sizeof(double*));
- for(int i=0; i<noOfFoods; i++) {
- printf("Please input no of speciffic foods for food \"%s\"\n>",foods[i]);
- scanf("%d",&noOfTypes[i]);
- getchar();
- types[i] = (char**)malloc(noOfTypes[i]* sizeof(char*));
- prices[i] = (double*)malloc(noOfTypes[i]* sizeof(double));
- printf("Please input \"%s\" speciffic foods &prices: each line in the format <speciffic food> (price):\n>",foods[i]);
- for(int j=0;j<noOfTypes[i];j++){
- types[i][j] = (char*)malloc(MAX_TYPE_NAME* sizeof(char));
- readType(types[i][j]);
- char line[MAX_LINE];
- gets(line);
- sscanf(line, "%lf", &prices[i][j]);
- if(j!=noOfTypes[i]-1)printf(">");
- }
- }
- printf("Please input no of drinks\n>");
- scanf("%d",&noOfDrinks);
- char ** drinks = (char**)malloc(noOfDrinks * sizeof(char*));
- double * drinksPrices = (double*)malloc(noOfDrinks* sizeof(double));
- printf("Please input the drinks: each line in the format <drink> (price):\n>");
- for(int j=0;j<noOfDrinks;j++){
- drinks[j] = (char*)malloc(noOfDrinks* sizeof(char));
- drinksPrices[j] = *(double*)malloc(noOfDrinks* sizeof(double));
- readDrinks(drinks[j]);
- char line[MAX_LINE];
- gets(line);
- sscanf(line, "%1lf", &drinksPrices[j]);
- printf(">");
- }
- printf("The food data is:\n");
- for(int i=0;i<noOfFoods;i++) {
- // display brand
- printf("%s: ", foods[i]);
- for(int j=0;j<noOfTypes[i];j++) {
- printf("(%s - %.2lf ) ",types[i][j],prices[i][j]);
- }
- printf("\n");
- }
- printf("The drinks data is:\ndrinks:");
- for(int i=0;i<noOfDrinks;i++){
- printf("%s", drinks[i]);
- if(i!=noOfDrinks-1)printf(", ");}
- printf("\n");
- printf("prices:");
- for(int i=0;i<noOfDrinks;i++){
- toInt=(int)drinksPrices[i];
- printf("%d",toInt);
- if(i!=noOfDrinks-1)printf(", ");}
- for(int i=0;i<noOfFoods;i++) {
- for(int j=0;j<noOfFoods;j++) {
- free(types[i][j]);
- }
- free(types[i]);
- free(prices[i]);
- free(foods[i]);
- free(drinks[i]);
- }
- free(types);
- free(prices);
- free(foods);
- free(noOfTypes);
- free(drinks);
- return 0;
- }
- void readType(char * type) {
- char c = getchar();
- int i=0;
- while(c!='(') {
- type[i] = c;
- c = getchar();
- i++;
- }
- type[i] = '\0';
- }
- void readDrinks(char * drinks) {
- char c = getchar();
- int i=0;
- while(c!='(') {
- drinks[i] = c;
- c = getchar();
- i++;
- }
- drinks[i] = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement