C ++中的计算器可以选择不同的类型和操作

时间:2013-03-10 15:28:36

标签: c++ types numbers switch-statement

我即将用C ++创建简单的计算器。它应该允许用户在两​​个键入的数字上选择类型和操作。所以首先,用户必须从列表中选择数字类型(显示不同的类型,如int,double,short等)。之后,它应该允许您编写之前选择的两个类型的数字。然后在最后你需要决定用这个数字做什么操作(+, - ,/,*)。我的问题是我不知道如何从方法中将那些输入的数字带到main(),所以我可以对它进行操作。

#include <iostream>
using namespace std;

int integer()
{
    int number1;
    int number2;
    cout << "First number: " << endl;
    cin >> number1;
    cout << "Second number: " << endl;
    cin >> number2;
}

double doubl()
{
    double number1;
    double number2;
    cout << "First number: " << endl;
    cin >> number1;
    cout << "Second number: " << endl;
    cin >> number2;
}

int main()
{

cout << "Type to choose:" << endl;
cout << "1. int" << endl;
cout << "2. double" << endl;

int choosed;
cin >> choosed;


switch(choosed) {
    case 1:
        integer();
        break;
    case 2:
        doubl();
        break;
    default:
        cout << "Error" << endl;
        break;
}

cout << "What operation would like to do on this numbers?" << endl;
cout << "1. +" << endl;
cout << "2. -" << endl;
cout << "3. *" << endl;
cout << "4. /" << endl;

int result;

switch(result){ //at this point i don't know how to invoke those numbers from      methods
    case 1:  
}

cin.get();
}

谢谢!

3 个答案:

答案 0 :(得分:3)

您可以从函数中返回std::pair

std::pair<double,double> doubl()
{
  ...
  return std::make_pair(number1,number2);
}

然后使用它

std::pair<double,double> nums = doubl();
double res = nums.first <operation> nums.second;

如果您对此感到满意,我建议您研究使用模板来创建阅读功能。

答案 1 :(得分:1)

这是在C ++中实现它的一种方法。基本上,您必须将答案存储在main()中的变量中,并且您可以使用标志来告诉您它是int还是double变量和操作。

#include <iostream>
using namespace std;

template<class T>
class numbers
{
   private:
   T num1;
   T num2;

   public:
   numbers() : num1(0), num2(0) {}
   void setvalues(T n1, T n2) {num1 = n1; num2 = n2;}
   T add()      {return num1 + num2;}
   T subtract() {return num1 - num2;}
   T multiply() {return num1 * num2;}
   T divide()   {return (num2 != 0) ? num1 / num2 : 0;}
};

void integer(numbers<int>& numo)
{
    int number1;
    int number2;
    cout << "First number: " << endl;
    cin >> number1;
    cout << "Second number: " << endl;
    cin >> number2;
    numo.setvalues(number1, number2);
}

void doubl(numbers<double>& numo)
{
    double number1;
    double number2;
    cout << "First number: " << endl;
    cin >> number1;
    cout << "Second number: " << endl;
    cin >> number2;
    numo.setvalues(number1, number2);
}

int main()
{

cout << "Type to choose:" << endl;
cout << "1. int" << endl;
cout << "2. double" << endl;

int choosed;
cin >> choosed;

numbers<int> num_int;
numbers<double> num_dbl;
int typeOfVal = 0; // 1 for integer, 2 for double

switch(choosed) {
    case 1:
        integer(num_int);
        typeOfVal = 1;
        break;
    case 2:
        doubl(num_dbl);
        typeOfVal = 2;
        break;
    default:
        cout << "Error" << endl;
        return 1;
}


int typeOfOp = 0;
cout << "What operation would like to do on this numbers?" << endl;
cout << "1. +" << endl;
cout << "2. -" << endl;
cout << "3. *" << endl;
cout << "4. /" << endl;
cin >> typeOfOp;

int resint; //result for int
double resdbl; // result for double

switch(typeOfOp){ 
    case 1: 
      if (typeOfVal == 1) resint = num_int.add(); else resdbl = num_dbl.add();
      break;
    case 2: 
      if (typeOfVal == 1) resint = num_int.subtract(); else resdbl = num_dbl.subtract();
      break;
    case 3: 
      if (typeOfVal == 1) resint = num_int.multiply(); else resdbl = num_dbl.multiply();
      break;
    case 4: 
      if (typeOfVal == 1) resint = num_int.divide(); else resdbl = num_dbl.divide();
    default:
      cout << "Error" << endl;
      return 1;
}

cout << "The answer:" << endl;
if (typeOfVal == 1) cout << resint << endl;
else cout << resdbl << endl;

cin.get();
return 0;
}

答案 2 :(得分:1)

如果我是你,我会编写这样的代码:我认为它比其他任何代码更标准。

#include<iostream>
#include<string>
using namespace std;
const string EXIT = "-1";
void add(float num1, float num2)
{
    cout<<"Result is: "<< num1 + num2 <<endl;
}
void subtract(float num1, float num2)
{
    cout<<"Result is: "<< num1 - num2 <<endl;
}
void multiply(float num1, float num2)
{
    cout<<"Result is: "<< num1 * num2 <<endl;
}
void divide(float numerator, float denominator)
{
    if (denominator != 0)
        cout<<"Result is: "<< numerator / denominator <<endl;
    else
        cout<<"You can not divide by denominator\n";
}
void modulus(float num1, float num2)
{
    cout<<"Result is: "<<  (int)num1 % (int)num2 <<endl;
}
int main(void)
{
    string mathematicalOperation;
    float num1, num2;
    char operatorSign;

    cout << "Please enter an arithmetic expression (-1 to Exit)\n";
    cin >> mathematicalOperation;
    //search for operator sign and perform calculation
    while(mathematicalOperation.compare(EXIT))
    {
        int getFirstDigitIndex = mathematicalOperation.find_first_of("0123456789");
        int operatorSignIndex = mathematicalOperation.find_first_of("+-*/%", getFirstDigitIndex);

        if (operatorSignIndex != -1)
        {   
            operatorSign = mathematicalOperation.at(operatorSignIndex);

            string firstNumber = mathematicalOperation.substr(0,operatorSignIndex);
            string secondNumber = mathematicalOperation.substr(operatorSignIndex + 1);
            //convert numbers from string to float
            num1 = (float)atof(firstNumber.c_str());
            num2 = (float)atof(secondNumber.c_str());
            switch(operatorSign)
            {
                case '+':
                    add(num1,num2);
                    break;
                case '-':
                    subtract(num1,num2);
                    break;
                case '*':
                    multiply(num1,num2);
                    break;
                case '/':
                    divide(num1,num2);
                    break;
                case '%':
                    modulus(num1,num2);
                    break;
            }
        }

        cout<<"Please Enter Another Expression or Enter -1 to Exit:\n";
        cin>>mathematicalOperation;
    }
    cout<<"SEE YOU LATER!\n";
    return 0;
}