/* * This code/software is licensed under the * CC-GNU GPL version 2.0 or later. * * see: http://creativecommons.org/licenses/GPL/2.0/ * * Author: Florian Streibelt * C-Kurs der Freitagsrunde, TU-Berlin September 2010 * */ #include #include #include typedef struct product_t { char *name; enum _punit { UNDEF,Pieces, KGrams, Liters} punit; union { struct{ int count; double price_p_piece; } ppiece; struct{ double weight; double price_p_kilo; } pkilo; struct { double liter; double price_p_liter; } pliter; }; } product; //-----------[begin: listen.tex] typedef struct bondetail_t{ product *prod; double pricesum; } bondetail; typedef struct bonrow_t{ bondetail* data; struct bonrow_t* next; }bonrow; //---------[ende: listen.tex] double calc_price(product *foo){ double sum=-1; switch (foo->punit){ default: printf ("ERROR.\n"); break; case KGrams: sum=(foo->pkilo.weight * foo->pkilo.price_p_kilo)/1000; break; case Pieces: sum=foo->ppiece.count * foo->ppiece.price_p_piece; break; case Liters: sum=foo->pliter.liter * foo->pliter.price_p_liter; break; } return sum; } //---------[begin: listen.tex] bondetail* newPiecesP(int amount, char* name, double price) { product *prod = (product*) calloc(1,sizeof(product)); prod->name=strdup(name); prod->punit=Pieces; prod->ppiece.count=amount; prod->ppiece.price_p_piece=price; bondetail *item = (bondetail*) calloc(1,sizeof(bondetail)); item->prod=prod; return item; } bondetail* newKilosP(double amount, char* name, double price) { product *prod = (product*) calloc(1,sizeof(product)); prod->name=strdup(name); prod->punit=KGrams; prod->pkilo.weight=amount; prod->pkilo.price_p_kilo=price; bondetail *item = (bondetail*) calloc(1,sizeof(bondetail)); item->prod=prod; return item; } bondetail* newLiterP(double amount, char* name, double price) { product *prod = (product*) calloc(1,sizeof(product)); prod->name=strdup(name); prod->punit=Liters; prod->pliter.liter=amount; prod->pliter.price_p_liter=price; bondetail *item = (bondetail*) calloc(1,sizeof(bondetail)); item->prod=prod; return item; } //---------[end: listen.tex] double calc_sum(bonrow* firstelement){ bonrow* current_row = firstelement; double sum=0; while(current_row!=NULL){ double price = calc_price(current_row->data->prod); current_row->data->pricesum=price; sum+=price; current_row=current_row->next; } return sum; } void dump_bonhead(){ printf("%20s CKURS 2010\n"," "); printf("%5s Wir lieben C! Immer in Ihrer Nähe.\n\n"," "); printf(" %s %-25s %s %s \n", "Menge","Produkt","EP","GP"); } void dump_bonsum(double sum){ double mwst=0.17*sum; printf(" %30s: %6.2f \n", "MwSt (17%)",mwst); printf(" %30s: %6.2f \n", "GESAMTPREIS",sum); } void dump_bonelement(bondetail* elem){ switch (elem->prod->punit){ default: printf ("\n"); break; case KGrams: printf(" %4.0fg %-25s %6.2f %6.2f \n", elem->prod->pkilo.weight, elem->prod->name, elem->prod->pkilo.price_p_kilo, elem->pricesum); break; case Pieces: printf(" %4.0i %-25s %6.2f %6.2f \n", elem->prod->ppiece.count, elem->prod->name, elem->prod->ppiece.price_p_piece, elem->pricesum); break; case Liters: printf(" %4.0fL %-25s %6.2f %6.2f \n", elem->prod->pliter.liter, elem->prod->name, elem->prod->pliter.price_p_liter, elem->pricesum); break; } } void print_bon(bonrow* firstelement,double sum){ dump_bonhead(); bonrow* current_row = firstelement; while(current_row!=NULL){ dump_bonelement(current_row->data); current_row=current_row->next; } dump_bonsum(sum); } int main(int argc, char** argv){ //---------[begin: listen.tex] bonrow *k_head, *k_tail; //first element k_tail = calloc(1,sizeof(bonrow)); k_head = k_tail; k_tail->next = NULL; k_tail->data = newPiecesP(5,"Club Mate",0.75); //next element k_tail->next=calloc(1,sizeof(bonrow)); k_tail=k_tail->next; k_tail->next=NULL; k_tail->data= newKilosP(750,"Wiener",4.90); //next element k_tail->next=calloc(1,sizeof(bonrow)); k_tail=k_tail->next; k_tail->next=NULL; k_tail->data= newLiterP(75,"Vollmilch 3.9%",1.90); double sum = calc_sum(k_head); print_bon(k_head,sum); //---------[end: listen.tex] return 0; }