C ++, this works, but someone can do it better to comply with coding standards / rules

#include <iostream>

using namespace std; 




class Calculator
{
        public: 
          int number1; 
          int number2 ; 

        public: 

        void  setCalcNumbers(int input1 , int input2 )
          {

          number1 = input1; 
          number2 = input2; 
          }
          int addNumber()
          {
              return number1 + number2;
          }
          int subtractNumber()
          {
              return number1 - number2;
          }
          int divideNumber()
          {
              return number1 / number2;
          }
          int multiplyNumber()
          {
              return number1 * number2;
          }

};

int main()
{
    int numberInput1 = 0; 
    cout << "Enter number 1: "; 
    cin >> numberInput1; 


    int numberInput2 = 0;
    cout << "Enter number 2: "; 
    cin >> numberInput2;

    Calculator t; 
    t.setCalcNumbers(numberInput1, numberInput2);

    char userOperationChoice; 
    cout << "which operation would you like to perform? "
         << " , enter M for Multiplication, D for Division, A for addition or S for Subtraction:" << endl;

    cin >> userOperationChoice; 
    char a,d,m,s;
    switch (userOperationChoice) 
    {

    case 'a'  : 
         t.addNumber();
         cout << "the total is: " << t.addNumber() << endl; 
         break; 

    case 's':
         t.subtractNumber(); 
         cout << "the total is: " << t.subtractNumber() << endl; 
         break ; 
    case 'd': 
         t.divideNumber();
         cout << "the total is: " << t.divideNumber() << endl; 
         break; 
    case 'm':
         t.multiplyNumber();
         cout << "the total is: " << t.multiplyNumber() << endl; 
         break; 

    }        



    system("pause"); 
    return 0 ; 
}
+3
source share
5 answers

Not sure if this is homework, so I will give an additional short version. In particular, I would go down the path of the pattern template .

template <typename T> T  add(T n1,T n2){                                        
        return n1 + n2; 
}

and use it:

 int s = add<int> (1,3); 

Considering what you are asking about classes, I would exclude the setNumbers function and make each static function and get numbers that I could work on as parameters. Also, I would make it a template class .

? , . , , , 1.5 3.2? int, 1 3, . ? , . 3 0.5 . ? , : . , template class / function . , integerCalculator ( , ).

 template <typename T> calculator {
      static T add (T n1 ,T n2){
        return n1 + n2;
      }
    };

Integer

class integerCalculator{
  public:
          //other functions

          int divideNumber(){
                 if (number2 == 0) //handle error
                 return number1/number2;
          }
};

:

number1 number2 , , setNumber()? (private apropiate).

class Calculator{
        int n1;
        int n2;

   public:  void setNumbers(int n1,int n2){...}
   //add, divide, multiply, subtract functions
}

, a, d, m, s . ...

+3
  • top of Main function;
  • "default" "switch";

...

0
  • const i.e. int multiplyNumber () const;
  • , .
  • , , 1 public:. private.
  • :

    Calculator (const int n1, const int n2) : number1(n1), number2 (n2) { }

  • , , , , int.

    template <typename T> class Calculator { /* ... */ }

, number1 number2 T.

0
char a,d,m,s;

Switch? / ?

0

, , , ;)

Calculator . . , .

, :

size_t mNumberItems; // number of items in the array
size_t mCapacity;    // maximum number of items in the array
T* mElements;        // array

:

  • mNumberItems <= mCapacity
  • mNumberItems represents the number of elements in an array, it is always in sync
  • mCapacity retypes the maximum capacity of the array, it also syncs

Therefore, I would like to rewrite the code as:

int getNumber(char const* text)
{ 
  int number = 0;
  std::cout << text << std::endl;
  std::cin >> number;
  return number;
}

int main(int argc, char* argv[])
{
  int n1 = getNumber("Please enter the first number:");
  int n2 = getNumber("Please enter the second number:");

  char userOperationChoice; 
  cout << "which operation would you like to perform?\n"
       << "Enter * for Multiplication, / for Division, "
       << "+ for addition or - for Subtraction:" << endl;

  cin >> userOperationChoice;

  int total = 0;
  switch(userOperationChoice)
  {
  case '*': total = n1 * n2; break;
  case '/': total = n1 / n2; break;
  case '+': total = n1 + n2; break;
  case '-': total = n1 - n2; break;
  default:
    std::cout << userOperationChoice
              << " is not in the set of operators allowed" << std::endl;
    return 1;
  }

  std::cout << "Total is: " << total << std::endl;
}

This is not ideal (lack of checking input to numbers), but I factorize a lot.

0
source

Source: https://habr.com/ru/post/1759816/


All Articles