Advertisement
193030

NBU 2019 04. Avtomatichna proverka (calculator)

Apr 1st, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. // Rabotqt i dvata testa v hacker rank - https://www.hackerrank.com/contests/fourth-interuniversity-nbu-programming-contest/challenges/challenge-1894
  2. // tochki ot 100 - ???
  3. #include <iostream>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <sstream>      // std::stringstream
  7.  
  8. using namespace std;
  9.  
  10. #define ADDITION 1
  11. #define SUBSTRACTION 2
  12. #define MULTIPLICATION 3
  13. #define DIVISION 4
  14.  
  15.  
  16. int state = 0;
  17. int signPosition =0;
  18. int equalSignPosition = 1;
  19.  
  20.  
  21. long sstoi(const char *s) // custom stoi function
  22. {
  23.     long i;
  24.     i = 0;
  25.     while(*s >= '0' && *s <= '9')
  26.     {
  27.         i = i * 10 + (*s - '0');
  28.         s++;
  29.     }
  30.     return i;
  31. }
  32.  
  33. int getFirstNumber(string input, int signPos)
  34. {
  35.     string newString = input.substr(0,signPos);
  36.     int firstNum = sstoi(newString.c_str());
  37.     return firstNum;
  38.     cout << "Return first num: " << firstNum << endl;
  39. }
  40.  
  41. int getSecondNumber(string input, int signPos)
  42. {
  43.     string newString = input.substr(signPos+1, equalSignPosition-signPosition-1);
  44.     int secondNum = sstoi(newString.c_str());
  45.     return secondNum;
  46. }
  47.  
  48. int getThirdNumber(string input, int equalSignPos)
  49. {
  50.     string newString = input.substr(equalSignPos+1);
  51.     int thirdNumber = sstoi(newString.c_str());
  52.     return thirdNumber;
  53. }
  54. double solve(double firstNum, double secondNum, double thirdNum)
  55. {
  56.     double trueOrNot = 0;
  57.     switch(state)
  58.     {
  59.      case ADDITION:
  60.      if(firstNum+secondNum==thirdNum)
  61.         trueOrNot = 1;
  62.      break;
  63.  
  64.      case SUBSTRACTION:
  65.      if(firstNum-secondNum==thirdNum)
  66.         trueOrNot =1;
  67.      break;
  68.  
  69.      case MULTIPLICATION:
  70.      if(firstNum*secondNum==thirdNum)
  71.         trueOrNot = 1;
  72.      break;
  73.  
  74.      case DIVISION:
  75.      if(firstNum/secondNum==thirdNum)
  76.         trueOrNot =1;
  77.      break;
  78.  
  79.     }
  80.  
  81.  return trueOrNot;
  82.  
  83.  
  84. }
  85.  
  86. int main() {
  87.  
  88. string input;
  89. string word;
  90.  
  91.     while(getline(cin, input)){
  92.    // cin.ignore();
  93.     if(input.empty())
  94.         break;
  95.  
  96.     equalSignPosition = input.find('=');
  97.     if(input.find('+')!=-1)
  98.     {
  99.         state = ADDITION;
  100.         signPosition = input.find('+');
  101.     }
  102.  
  103.     if(input.find('-')!=-1)
  104.     {
  105.         state = SUBSTRACTION;
  106.         signPosition = input.find('-');
  107.     }
  108.  
  109.     if(input.find('x')!=-1)
  110.     {
  111.        state = MULTIPLICATION;
  112.        signPosition = input.find('x');
  113.     }
  114.  
  115.     if(input.find(':')!=-1)
  116.     {
  117.        signPosition = input.find(':');
  118.        state = DIVISION;
  119.     }
  120.  
  121.  
  122.     int firstNum = getFirstNumber(input, signPosition);
  123.     int secondNum = getSecondNumber(input,signPosition);
  124.     int thirdNumber = getThirdNumber(input,equalSignPosition);
  125.     double result = solve(firstNum,secondNum,thirdNumber);
  126.     if(result)
  127.         cout <<"Correct" << endl;
  128.     else
  129.         cout <<"Incorrect" << endl;
  130.  
  131.     }
  132.  
  133.  
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement