Advertisement
Guest User

Long arithmetic

a guest
Oct 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "fstream"
  4. #include "string"
  5. using namespace std;
  6.  
  7. bool compareTwoLongNumbers(string first, char sign, string second);
  8. void input(string &first_number, char &sign, string &second_number);
  9. void output(bool result);
  10.  
  11.  
  12. int _tmain(int argc, _TCHAR* argv[])
  13. {
  14.     string first_number, second_number;
  15.     char sign;
  16.     input(first_number, sign, second_number);
  17.    
  18.     output(compareTwoLongNumbers(first_number, sign, second_number));
  19.     system("pause");
  20. }
  21.  
  22. bool compareTwoLongNumbers(string first_number, char sign, string second_number){
  23.     if (first_number == second_number){
  24.         if (sign == '=') { return true; }
  25.         return false;
  26.     }
  27.     else {
  28.         int flag;
  29.         if (first_number.length() > second_number.length()){ flag = true; }
  30.         else if (first_number.length() < second_number.length()) { flag = false; }
  31.         else {
  32.             for (int i = 0; i < (int)first_number.length(); i++){
  33.                 if (first_number[i] > second_number[i]){
  34.                     flag = true;
  35.                     break;
  36.                 }
  37.                 else if (first_number[i] < second_number[i]){
  38.                     flag = false;
  39.                     break;
  40.                 }
  41.             }
  42.         }
  43.         if ((flag && sign == '>') || (!flag && sign == '<')){ return true; }
  44.         return false;
  45.     }
  46. };
  47.  
  48. void input(string &first_number, char &sign, string &second_number){
  49.     string filename;
  50.     cin >> filename;
  51.  
  52.     ifstream input(filename);
  53.     ofstream errors("errors.txt");
  54.    
  55.     if (!input){
  56.         errors << "Can't open the file" << endl;
  57.         cout << "Can't open the file" << endl;
  58.         exit(0);
  59.     }
  60.  
  61.     input >> first_number >> sign >> second_number;
  62.     input.close();
  63. }
  64. void output(bool result){
  65.     ofstream output("output.txt");
  66.     if (result) {
  67.         output << "The expression is true." << endl;
  68.     }
  69.     else {
  70.         output << "The expression is false." << endl;
  71.     }
  72.     output.close();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement