Skip to content

Instantly share code, notes, and snippets.

@MggMuggins
Last active January 16, 2017 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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);
}
}
}
}
@MggMuggins
Copy link
Author

This algorithm seems intensely complicated for just calculating the price range. It needs to be checked anyway to make sure the math works, since it hasn't undergone any significant testing since I re-wrote it from Camp's script.

@Campinator
Copy link

What does the Diff variable do?

@MggMuggins
Copy link
Author

It functions as all of the differences that you used in your original. Because none of those variables of yours were ever used to represent two different numbers at the same time, I could just recycle the variable and use it for the difference for each spot in the if-else logic.

@Campinator
Copy link

Looks good, there just needs to be a special case built in for 1. It has no range (1-1), but the program displays it as 0-2

@Campinator
Copy link

And what's this little code do?

_try {

		itemPrice = readValue("Item Price? ");

	} catch (std.conv.ConvException) {

		return 0;_

@MggMuggins
Copy link
Author

Alright, so when something happens during the program's execution that it does not expect (Such as storing any other type of character as type float, which would be exactly what is happening in this case), then it "Throws" something called an "Exception". Usually exceptions kill your program by default. However, it is possible to "Catch" an exception, and handle it in whatever way you want, rather than just letting it close your program. In this instance, if you try to put anything other than a valid float value (Try inputting "q", or any other char!), then it will catch the exception std.conv.ConvException and return 0 instead of continuing with execution, and instead of printing out a ridiculous number of errors complaining about you putting a "q" instead of a number. Does that make sense?

@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