#include "library.h" struct money { int pounds, shillings, pennies, farthings; }; // 4 farthings make a penny // 12 pence make a shilling // 20 shillings make a pound. Up until 1971 anyway. void set(money & x, int l, int s, int d, int f) { x.pounds = l; x.shillings = s; x.pennies = d; x.farthings = f; } double convert(money a) { return a.pounds + a.shillings/20.0 + a.pennies/240.0 + a.farthings/960.0; } void display(money a) { if (a.pennies==0 && a.shillings==0 && a.pounds==0 && a.farthings==0) write_string("0"); if (a.pounds > 0) { write_char(0x20A4); // 0x indicates a number written in base 16, hexadecimal write_string(a.pounds); // 0x20A4 is the unicode character code for the pound sign if (a.shillings > 0 || a.pennies > 0 || a.farthings > 0) write_string(" "); } if (a.shillings > 0) { write_string(a.shillings); write_string("s"); if (a.pennies > 0 || a.farthings > 0) write_string(" "); } if (a.pennies > 0) { write_string(a.pennies); } if (a.farthings>0) { write_char(0x00BB+a.farthings); } // The unicode codes for 1/4, 1/2, 3/4 are 0xBC, 0xBD, 0xBE if (a.pennies > 0 || a.farthings > 0) { write_string("d"); } } void fix(money & a) { if (a.farthings > 3) { a.pennies = a.pennies + a.farthings / 4; a.farthings = a.farthings % 4; } if (a.pennies > 11) { a.shillings = a.shillings + a.pennies / 12; a.pennies = a.pennies % 12; } if (a.shillings > 19) { a.pounds = a.pounds + a.shillings / 20; a.shillings = a.shillings % 20; } } money convert(double pds) { money answer; set(answer, 0, 0, 0, (int)(pds*960)); fix(answer); return answer; } money add(money a, money b) { money answer; set(answer, a.pounds+b.pounds, a.shillings+b.shillings, a.pennies+b.pennies, a.farthings+b.farthings); fix(answer); return answer; } money subtract(money a, money b) { money answer; set(answer, a.pounds-b.pounds, a.shillings-b.shillings, a.pennies-b.pennies, a.farthings-b.farthings); fix(answer); return answer; } money multiply(money a, int b) { money answer; set(answer, a.pounds*b, a.shillings*b, a.pennies*b, a.farthings*b); fix(answer); return answer; } void main() { make_window(600, 500); set_font_size(24); money mine, yours; set(mine, 3, 7, 11, 1); set(yours, 0, 10, 6, 0); move_to(100, 100); display(mine); move_to(100, 200); display(yours); mine = add(mine, yours); move_to(100, 300); display(mine); mine = multiply(mine, 35); move_to(100, 400); display(mine); }