{}
run-icon
main.c
// C Program: Räkna ut moms för upp till 20 varor // © 2024, Electric Gauntz / [email protected] #include <limits.h> #include <stdio.h> #include <math.h> struct Row { float price; int amount; float total; }; struct Row rows[20]; void one_row(float vat_percent) { static int index = 0; static char buffer[20][200]; // Max 20 lines, max 200 characters per line const int ROW_MAX = 20; if(index >= ROW_MAX) { return; } float price; int amount; // ---- User input ---- printf("Rad %i Pris: ", index + 1); scanf("%f", &price); if(index > 0) { while (price == 0) { printf("Rad %i raderad\n\n", index); index--; // Delete last row printf("Rad %i Pris: ", index + 1); scanf("%f", &price); } } printf("Rad %i Antal: ", index + 1); scanf("%i", &amount); // --------------------- rows[index].price = price; rows[index].amount = amount; rows[index].total = price * amount; // printf("\n\tProdukt / Tjänst \tPris\tAntal\tTotalt\n"); float vat_value_sum = 0; float net_sum = 0; printf("\n"); for(int i = 0; i < index + 1; i++) { printf("%i)\t", i + 1); printf("Benämning\t"); printf("%.2f\t", rows[i].price); printf("%i\t", rows[i].amount); printf("%.2f\n", rows[i].total); net_sum += rows[i].total; // Add all lines } vat_value_sum = net_sum * vat_percent; // Add all lines printf("\n"); printf("\tNetto:\t%.2f\n", net_sum); printf("\tMoms 25% (beräknad på %.2f):\t%.2f\n", net_sum, vat_value_sum); printf("\n"); printf("\tSumma att betala:\t%.2f Kr\n", round(net_sum + vat_value_sum)); printf("\n"); index++; } int main() { float vat_percent = 0.25; while(1) { one_row(vat_percent); } return 0; }
Output