using System; public struct coord { public int x; public int y; }
public class test { public static void Main () { coord monster; monster.x = 50; monster.y = 10; Console.WriteLine ("monster x pos = {0}, y pos = {1}", monster.x, monster.y); } }
using System; public struct coord { public int x; public int y; public override string ToString () { return String.Format ("{0}, {1}", x, y); } }
public class test { public static void Main () { coord monster; monster.x = 50; monster.y = 10; Console.WriteLine ("monster at {0}", monster); } }
monster at 50, 10
using System; public struct fract { public int w; // the whole component public int n; // the numerator public int d; // the denominator private bool positive; // is this a positive number? private bool is_simplified; // have we simplified this yet?
public fract (int whole, int num, int dem) { w = whole; n = num; d = dem; positive = true; is_simplified = false; }
public override string ToString () { if (w == 0 && n == 0) return "zero"; if (positive) { if (w == 0) return String.Format ("{0}/{1}", n, d); else { if (n == 0) return String.Format ("{0}", w); return String.Format ("{0} and {1}/{2}", w, n, d); } }
else { if (w == 0) return String.Format ("-{0}/{1}", n, d); else { if (n == 0) return String.Format ("-{0}", w); return String.Format ("-({0} and {1}/{2})", w, n, d); } } } }
public class test { public static void Main () { fract a = new fract (1, 1, 2); Console.WriteLine ("a = {0}", a); } }
a = 1 and 1/2
using System; public struct fract { public int w; public int n; public int d; private bool positive; private bool is_simplified; public fract (int whole, int num, int dem) { w = whole; n = num; d = dem; positive = true; is_simplified = false; }
/* * gcd - Euclid's Greatest Common Denominator algorithm. * pre-condition : x and y are both >0 * post-condition: return the greatest denominator of, * x, and, y. */ public int gcd (int x, int y) { while (x != y) { if (x > y) x -= y; else y -= x; } return x; }
/* * simplify - pre-condition : an initialised fract * post-condition: whole, num, denom are * converted into their * simplist form. */ public void simplify () { int t; if (is_simplified) // if it has been simplified before return return; if (n == 0) // if the numerator is 0 set the demoninator to 0 d = 0;
if ((n != 0) && (d != 0)) // if we have a fraction component { if (n > d) // if the fraction is vulgar { t = n / d; // t is the whole number in the vulgar fraction w += t; // add it to the whole number n = n % d; // find remainder which becomes new numerator }
if ((n != 0) && (d != 0)) { t = gcd (n, d); // t is the largest number which goes if (t > 1) // into n, d { n = n / t; // reduce both numerator and demoninator d = d / t; // by t } } if (n == d) // is the fraction a whole number? { n = 0; d = 0; w++; } } is_simplified = true; // set flag saying it is simplified }
public override string ToString () { if (w == 0 && n == 0) return "zero"; simplify (); /* as before. */
public class test { public static void Main () { fract a = new fract (0, 3, 2); Console.WriteLine ("a = {0}", a); } }
a = 1 and 1/2
This document was produced using groff-1.22.