-
Notifications
You must be signed in to change notification settings - Fork 0
Description
import std.stdio, std.format, std.checkedint;
struct Country {
string name;
int population;
};
// https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population
immutable Country[] countries = [
Country("China", 1_412_600_000),
Country("India", 1_375_586_000),
Country("United States", 333_299_945),
Country("Indonesia", 275_773_800),
Country("Pakistan", 235_825_000),
Country("Nigeria", 218_541_000),
Country("Brazil", 215_424_390),
Country("Bangladesh", 165_158_616),
Country("Russia", 145_100_000),
Country("Mexico", 128_533_664),
];
void main() {
int total;
bool overflow;
foreach (country; countries) {
writeln(country.name, " ", format("%,3?d", '', country.population));
total = opChecked!"+"(total, country.population, overflow);
if (overflow) {
writeln("FAILED!");
return;
}
}
writeln("total ", format("%,3?d", '', total));
}
// https://dlang.org/phobos/std_checkedint.html
// https://dlang.org/phobos/core_checkedint.html