yordanganev

cp2019_zad4

Apr 1st, 2020
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <string.h>
  5.  
  6. #define print(var) cout << #var << ": " << var << endl;
  7. using namespace std;
  8.  
  9. string solve(int a, char o, int b, int r);
  10.  
  11. int main() {
  12.     int a, b, r;
  13.     char op = '+';
  14.     string input;
  15.  
  16.  
  17.     while ( (cin >> input) && input != "") {
  18.  
  19.         //sscanf(sentence, "%s %*s %d", str, &i);
  20.  
  21.         if (input.find('+') != -1) {
  22.             input.at(input.find('+')) = ' ';
  23.             op = '+';
  24.         }
  25.         else if (input.find('-') != -1) {
  26.             input.at(input.find('-')) = ' ';
  27.             op = '-';
  28.         }
  29.         else if (input.find('x') != -1) {
  30.             input.at(input.find('x')) = ' ';
  31.             op = '*';
  32.         }
  33.         else {
  34.             input.at(input.find(':')) = ' ';
  35.             op = ':';
  36.         }
  37.  
  38.         sscanf(input.c_str(), "%d %d=%d", &a, &b, &r);
  39.  
  40.         cout << solve(a, op, b, r) << endl;
  41.     }
  42. }
  43.  
  44. string solve(int a, char o,int b, int r) {
  45.     double result = 0.0;
  46.  
  47.     switch (o)
  48.     {
  49.     case '+':
  50.         result = a + b;
  51.         break;
  52.     case '-':
  53.         result = a - b;
  54.         break;
  55.     case '*':
  56.         result = double(a) * b;
  57.         break;
  58.     case ':':
  59.         result = double(a) / b;
  60.         break;
  61.     }
  62.  
  63.     return result == r ? "Correct" : "Incorrect";
  64. }
Advertisement
Add Comment
Please, Sign In to add comment