Re: Gibt es einen logischen Fehler in diesem Code?
Verfasst: Do Sep 10, 2020 8:52 pm
Wenn sich jemand da durchwurscheltn will, würde es mich freuen: https://pastebin.com/g1dfEPCu
Programmieren lernen von Anfang an
https://www.proggen.org/forum/
Code: Alles auswählen
#include <stdio.h>
#include <stdlib.h> // EXIT_SUCCESS
#include <math.h>
#include <string.h>
#include <time.h>
float oilPriceChange[34] = { 1.1f, 1.7f, 1.8f, 1.8f, 1.6f, 1.6f, 1.1f, 1.7f, 1.1f, 1.6f, 1.4f, 0.7f, 1.2f, 1.6f, 1.7f, 1.8f, 1.2f, 0.5f, 0.2f, 0.8f, 1.3f, 1.4f, 1.9f, 1.9f, 2.0f, 2.0f, 2.0f, 1.3f, 1.2f, 0.7f, 0.1f, 0.1f, 0.4f, 0.5f };
int main(void) {
int i;
int amountOfOilToSell, amountOfPumps, amountOfTankWagons;
int amountOfSellableOil = 0;
int amountOfTankWagonCapacity;
int playerMoney = 0;
float oilPrice = oilPriceChange[0];
srand((unsigned) time(NULL));
amountOfPumps = rand() % 10 + 1;
amountOfTankWagons = rand() % 10 + 1;
amountOfSellableOil += amountOfPumps * 8000;
amountOfTankWagonCapacity = amountOfTankWagons * 7000;
printf("Rafinerieabnahmepreis $ %.1f\n", oilPrice);
printf("%d\n", amountOfPumps);
printf("%d\n", amountOfSellableOil);
printf("%d\n", amountOfTankWagons);
printf("%d\n", amountOfTankWagonCapacity);
printf("%d\n", playerMoney);
scanf("%d", &amountOfOilToSell);
if (amountOfOilToSell < 0)
amountOfOilToSell = 0;
if (amountOfOilToSell > amountOfSellableOil)
amountOfOilToSell = amountOfSellableOil;
if (amountOfOilToSell > amountOfTankWagonCapacity)
amountOfOilToSell = amountOfTankWagonCapacity;
amountOfSellableOil -= amountOfOilToSell;
playerMoney += amountOfOilToSell * oilPrice;
printf("%d\n", playerMoney);
return EXIT_SUCCESS;
}
Es ist gute Tradition solche Werte als integer zu halten und die Einheit anzupassen. Statt Euro halt Cent. Die Ausgabe erfolgt dann über eine modifizierte Stringumwandlung, die einfach vor die letzten beiden Stellen einen Punkt einfügt. Intern 1000ct, die Ausgabe sagt 10.00 Euro.Orioner hat geschrieben: ↑Sa Sep 12, 2020 6:46 pmEine Alternative dazu, oilPrice und oilPriceChange[] als float zu behandeln, wäre, die Werte mit 10 zu multiplizieren, als int zu speichern, damit zu rechnen und da, wo es nötig ist, den Wert wieder durch 10 zu dividieren und ggf. als float zu speichern. Was ist besser? Was meint ihr?