Формула для печати результата умножения с несколькими членами (C) - PullRequest
0 голосов
/ 29 мая 2020

Итак, мне нужно умножить общую цену продукта на количество раз, когда он был куплен, а затем суммировать его с другим продуктом (если пользователь решит ввести другой продукт), умноженный на количество раз, когда он был куплен. Я пробовал использовать эту формулу, но она работает, только если я ввожу два продукта, каждый из которых купил несколько раз. Какую формулу мне следует использовать, чтобы она работала, даже если я ввожу более двух продуктов (купленных один или несколько раз)?

формула inventory->priceTot = (inventory->price *inventory->num) + (inventory->price *inventory->num);

#define INVENTORY 10

 typedef struct p{  
    char name[20];
    char productName[20];
    int id;
    float price;
    float weight;
    float priceTot; //total price
    int num;
}product; //


product inventory[INVENTORY];
product p;
product *P = &p;

int create_inventory() {
    int i;
    int choice, choiceNR;
    inventory->num2 = inventory->num;
    while(1){
        printf("\nEnter your name: ");
        scanf("%19s", &(*P).name);
            for(i=0;i<inventory;i++){
                point:
                printf("\nEnter the name of the product            : ");
                scanf("%19s",&inventory[count].productName);
                printf("Enter the ID of the product        : ");
                scanf("%9d", &inventory[count].id);
                printf("Enter the price of the product(In euro) : ");
                scanf("%9f", &inventory[count].price);
                printf("Inserisci il peso del prodotto(In KG)     : ");
                scanf("%9f", &inventory[count].weight);        
                printf("\Buy the product %s multiple times", inventory[count].productName);               
                printf("\n -1- Yes");
                printf("\n -2- No\n");                
                scanf("%d", &choiceNR);
                    if(choiceNR == 1){
                        printf("\nEnter a value: \n"); // value is the nmumber of times you want to buy the same product
                        scanf("%d", &inventory[count].num);
                    } else if (choiceNR == 2){
                        //empty
                    }
                printf("\nDo you want to buy another product?");
                printf("\n-1- Yes");
                printf("\n-2- No\n");
                scanf("%d", &choice);
                count++;
                if(choice == 1){
                    goto point;
                } else if (choice == 2){
                    inventory->priceTot = (inventory->price *inventory->num) + (inventory->price *inventory->num); // formula
                    printf("%0.2f", inventory->priceTot);   //print total price     
                    return;
                }
            break;    
        }

    }
}

Если кто-то может помочь, Я был бы признателен!

Заранее спасибо!

...