// Name // CS201 HW6 // Problem 1 #include using namespace std; // class definition class Counter { public: // constructor Counter(int maximum); // member functions void reset(); void incr1(); void incr10(); void incr100(); void incr1000(); void output(); bool overflow(); private: int count; //data members int max; }; //implemention of constructor & member functions Counter::Counter(int maximum) : max(maximum), count(0) { } void Counter::reset() { count=0; } void Counter::incr1() { count++; overflow(); } void Counter::incr10() { count +=10; overflow(); } void Counter::incr100() { count += 100; overflow(); } void Counter::incr1000() { count = count+1000; overflow(); } void Counter::output() { cout << "The value on the counter is " << count << endl; } bool Counter::overflow() { if (count > max) cout << "Overflow: The counter has exceeded its maximum of " << max << endl; return (count > max); } //driver int main() { char choice; int num=0, i; Counter clicker(9999); do { i = 0; cout << "The current amount is "; clicker.output(); cout << endl; cout << "Press \"a\" to input a number of cents from 1 to 9.\n" << "Press \"s\" to input a number of dimes from 1 to 9.\n" << "Press \"d\" to input a number of dollars from 1 to 9.\n" << "Press \"f\" to input a number of tens of dollars from 1 to 9.\n" << "Press \"o\" to check for overflow.\n" << "Press \"r\" to reset the counter.\n" << "Press \"x\" to exit.\n"; cin >> choice; switch (choice) { case 'a': do { cout << "Enter a number between 1 and 9.\n"; cin >> num; if (num<1 || num>9) cout << "You did not enter a number between 1 and 9.\n"; }while (num<1 || num>9); while (i> num; if (num<1 || num>9) cout << "You did not enter a number between 1 and 9.\n"; }while (num<1 || num>9); while (i> num; if (num<1 || num>9) cout << "You did not enter a number between 1 and 9.\n"; }while (num<1 || num>9); while (i> num; if (num<1 || num>9) cout << "You did not enter a number between 1 and 9.\n"; }while (num<1 || num>9); while (i using namespace std; // class definition class Rational { public: //construtors Rational(); Rational(int numerator); Rational(int numerator, int denominator); //member functions Rational add(Rational rhs); Rational sub(Rational rhs); Rational mul(Rational rhs); Rational div(Rational rhs); Rational neg(); bool less(Rational rhs); void input(istream& in); void output(ostream& out); private: int num; int den; }; // Implementation of member functions Rational::Rational() : num(0), den(1) { } Rational::Rational(int numerator) : num(numerator), den(1) { } Rational::Rational(int numerator, int denominator) :num(numerator), den(denominator) { } Rational Rational::add(Rational rhs) { return Rational((num*rhs.den + den*rhs.num), (den*rhs.den)); } Rational Rational::sub(Rational rhs) { return Rational((num*rhs.den - den*rhs.num), (den*rhs.den)); } Rational Rational::mul(Rational rhs) { return Rational(num*rhs.num, den*rhs.den); } Rational Rational::div(Rational rhs) { return Rational(num*rhs.den, den*rhs.num); } Rational Rational::neg() { return Rational(num*-1, den); } void Rational::input(istream& in) { in >> num >> den; } bool Rational::less(Rational rhs) { double x=num, y=den, a=rhs.num, b=rhs.den; return x/y < a/b; } void Rational::output(ostream& out) { out << num << "/" << den; } // driver int main() { Rational onehalf(1,2); Rational five(5); Rational x=five.mul(onehalf); cout << "Five times one half is "; x.output(cout); cout << endl; cout << "Five divided by one half is "; five.div(onehalf).output(cout); //notice the chained . . operators cout << endl; cout << "One half minus five is "; onehalf.sub(five).output(cout); cout << endl; cout << "(1/2 - (-1/3)) * 6 = "; (Rational(1,2).sub(Rational(1,3).neg())).mul(Rational(6)).output(cout); cout << endl; cout << "Test the \"less\" operator where ab: " << endl; for(int ii=0;ii<3;++ii) { Rational a,b; cout << "Enter two rational numbers "; a.input(cin); b.input(cin); a.output(cout); if (a.less(b)) cout << " is less than "; else cout << " is not less than "; b.output(cout); cout << endl; } return 0; } /* Five times one half is 5/2 Five divided by one half is 10/1 One half minus five is -9/2 (1/2 - (-1/3)) * 6 = 30/6 Test the "less" operator where ab: Enter two rational numbers 1 2 3 4 1/2 is less than 3/4 Enter two rational numbers 5 2 5 3 5/2 is not less than 5/3 Enter two rational numbers 5 5 5 5 5/5 is not less than 5/5 Press any key to continue */