Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #define NAME_MAX_LEN 30
- typedef struct{
- char name[NAME_MAX_LEN];
- double quantity, price;
- } Prod;
- Prod *exists(Prod prods[], const char *const name, int uniqueNr) {
- for (int i = 0; i < uniqueNr; i++) {
- if (!strcmp(prods[i].name, name)) {
- return &prods[i];
- }
- }
- return NULL;
- }
- Prod readProd(void) {
- Prod prod;
- scanf("%s %lf %lf", prod.name, &prod.quantity, &prod.price);
- return prod;
- }
- void displayProd(Prod prod) {
- printf("%s %g, %g\n", prod.name, prod.quantity, prod.price);
- }
- void processProd(Prod uniqueProds[], Prod prod, double *const totalPrice, int *const uniqueNr) {
- Prod *target = exists(uniqueProds, prod.name, *uniqueNr);
- if (target) {
- target->quantity += prod.quantity;
- target->price += prod.quantity * prod.price;
- } else {
- Prod aux = {
- .quantity = prod.quantity,
- .price = prod.price * prod.quantity
- };
- strcpy(aux.name, prod.name);
- uniqueProds[*uniqueNr] = aux;
- ++*uniqueNr;
- }
- *totalPrice += prod.quantity * prod.price;//*/
- }//*/
- int main(void) {
- int n, uniqueNr = 0;
- scanf("%d", &n);
- Prod *uniqueProds = (Prod*)malloc(sizeof(Prod) * n);
- double totalPrice = .0;
- for (int i = 0; i < n; i++) {
- Prod prod = readProd();
- processProd(uniqueProds, prod, &totalPrice, &uniqueNr);
- }
- for (int i = 0; i < uniqueNr; i++) {
- displayProd(uniqueProds[i]);
- }
- printf("global price: %g\n", totalPrice);
- free(uniqueProds);//*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement