### Arithemetic PROBLEMS

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 //This program asks the user to input two rational number and then does arithmetic //operations on them. class Rational_Num { public: int nemor; int denom; int fraction; int fraction2; string tostring(); string multi(); void validateInput(string); int GCD(int nemor, int denom); int Multiply(int nemor, int denom, int nemor2, int denom2); }; string Rational_Num::tostring() { string Result; int numberToConvert = Rational_Num::nemor; int number2ToConvert = Rational_Num::denom; Result = ConvertToString(numberToConvert) + "/" + ConvertToString(number2ToConvert); return Result; } int Multiply(int nemor, int denom, int nemor2, int denom2) { if(nemor == 1) { return nemor2; } if(nemor2 == 1) { return nemor; } if(denom == 1) { return denom2; } } string ConvertToString(int inputInt) { int Number = inputInt; // number to be converted to a string string Result; // string which will contain the result ostringstream convert; // stream used for the conversion convert << Number; // insert the textual representation of 'Number' in the characters in the stream Result = convert.str(); return Result; } int GCD(int nemor, int denom) { if(nemor > denom) { if(nemor % denom == 0) { return denom; } else { denom = nemor % denom; GCD(nemor, denom); } } else { if(nemor == 0) { return 0; } else if(denom % nemor == 0) { return nemor; } else { nemor = denom % nemor; GCD(nemor, denom); } } } int main() { Rational_Num rash; Rational_Num rash2; Rational_Num fration; do { cout << "This program performs Arithmetic operations on two rational numbers."; cout << endl; do { cout << endl; cout << "Enter a rational number : "; fflush(stdin); error = true; getline(cin, input); if(input.empty()) { error = false; cout << "Nothing was entered."; } int num_slashes = 0; for(int k = 0; k < input.size(); k++) { if(input.at(k) == '/') { num_slashes++; } } if(num_slashes > 1) { error = false; cout << endl; cout << "Too many slashes. Unacceptable"; //cout << "Error! Re-Enter in a rational number : "; continue; } int num_minus = 0; if(error == true) { for(int n = 0; n < input.size(); n++) { if(input.at(n) == '-') { num_minus++; } } if(num_minus > 2) { error = false; cout << endl; cout << "Too many minus signs. Unacceptable!"; //cout << "Error! Re-Enter in a rational number : "; continue; } } //if(error == true) if(error == true) { for(int k = 0; k < input.size(); k++) { if(!(isdigit(input.at(k))) && input.at(k) != '/' && input.at(k) != '-') { error = false; cout << endl; cout << "ERROR! Only integers are allowed."; continue; } } } if (error == false) { continue; } int foundYou; foundYou = input.find('/'); int numerator = atoi(input.substr(0, foundYou).c_str()); int denominator = atoi(input.substr(foundYou + 1).c_str()); int fraction; int fraction3; rash.nemor = numerator; rash.denom = denominator; if(GCD(rash.nemor, rash.denom) == 0) { fraction3 = 0; cout << endl; cout << ""; cout << endl; } else { fraction3 = (rash.nemor / GCD(rash.nemor, rash.denom) ) / (rash.denom / GCD(rash.nemor, rash.denom)); cout << endl; cout << ""; cout << endl; } cout << fixed << showpoint << setprecision(2); }while(error == false); do { cout << endl; cout << "Enter a rational number : "; fflush(stdin); error = true; getline(cin, input); if(input.empty()) { error = false; cout << "Nothing was entered."; } int num_slashes = 0; for(int k = 0; k < input.size(); k++) { if(input.at(k) == '/') { num_slashes++; } } if(num_slashes > 1) { error = false; cout << endl; cout << "Too many slashes. Unacceptable"; : "; continue; } int num_minus = 0; for(int n = 0; n < input.size(); n++) { if(input.at(n) == '-') { num_minus++; } } if(num_minus > 2) { error = false; cout << endl; cout << "Too many minus signs. Unacceptable!"; continue; } if(error == true) { for(int k = 0; k < input.size(); k++) { if(!(isdigit(input.at(k))) && input.at(k) != '/' && input.at(k) != '-') { error = false; cout << endl; cout << "ERROR! Only integers are allowed."; continue; } } } if (error == false) { continue; } int foundYou; foundYou = input.find('/'); int numerator2 = atoi(input.substr(0, foundYou).c_str()); int denominator2 = atoi(input.substr(foundYou + 1).c_str()); int fraction; int fraction2; int fraction3; rash2.nemor = numerator2; rash2.denom = denominator2; if(GCD(rash2.nemor, rash2.denom) == 0) { fraction3 = 0; cout << endl; cout << ""; cout << endl; } else { fraction3 = (rash2.nemor / GCD(rash2.nemor, rash2.denom) ) / (rash2.denom / GCD(rash2.nemor, rash2.denom)); cout << endl; cout << ""; cout << endl; } cout << fixed << showpoint << setprecision(2); cout << endl; cout << ""; cout << endl; cout << fixed << showpoint << setprecision(2); //fraction2 = rash2.nemor / GCD(rash2.nemor, rash2.denom) << '/' << rash2.denom / GCD(rash2.nemor, rash2.denom); }while (error == false);
So what i need help with is how to do the arithmetic operations on the two rational numbers entered at runtime and dealing with negative numbers and division by zero. Im trying to separate the different operations into different functions but ive been struggling
Topic archived. No new replies allowed.