Skip to content

Instantly share code, notes, and snippets.

@MggMuggins
Last active January 16, 2017 16:46
Show Gist options
  • Save MggMuggins/f2e68e7aff1e12ce0b6e8c720c0a4279 to your computer and use it in GitHub Desktop.
Save MggMuggins/f2e68e7aff1e12ce0b6e8c720c0a4279 to your computer and use it in GitHub Desktop.
A little program written in D to calculate the price range of items in LOTR mod trading from the average price.
import std.stdio;
float readValue (string text) {
float output;
write(text);
readf(" %s", &output);
return output;
}
void printOut (float large, float small) {
writeln("Range: ", small, "-", large);
}
int main() {
float itemPrice, diff, large, small, oddAverage;
while (1 == 1) {
try {
itemPrice = readValue("Item Price? ");
} catch (std.conv.ConvException) {
return 0;
}
oddAverage = itemPrice / 2 + 0.5;
if (itemPrice % 2 == 0) {
if (itemPrice % 4 == 0) {
diff = itemPrice / 4;
large = itemPrice + diff;
small = itemPrice - diff;
printOut(large, small);
} else {
diff = itemPrice / 4 + 0.5;
large = itemPrice + diff;
small = (itemPrice - diff) + 1;
if (itemPrice == 2) {
small = small - 1;
}
printOut(large, small);
}
} else {
if (oddAverage % 2 != 0) {
diff = (oddAverage / 2) + 0.5;
large = itemPrice + diff;
diff = (oddAverage / 2) + 0.5;
small = itemPrice - diff;
printOut(large, small);
} else {
diff = oddAverage / 2;
large = itemPrice + diff;
small = itemPrice - diff;
printOut(large, small);
}
}
}
}
@Campinator
Copy link

Yep. Amazingly, 'r' is not a price :)

@MggMuggins
Copy link
Author

Yeah, pretty much the point of that. There would be cleaner ways to do that, such as reading to an intermediary type (Such as char or string) and then converting that, and depending on what it is, the program would behave differently. However, this makes more sense for a little program like this. It's easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment