0

C++で簡単な電卓を作成しようとしています。これにより、ユーザーは2つの型付き数値の型と操作を選択できるようになります。したがって、最初に、ユーザーはリストから数値タイプを選択する必要があります(int、double、shortなどのさまざまなタイプが表示されます)。その後、前に選択したタイプの2つの数字を書くことができるはずです。次に、最後に、この数値(+、-、/、*)を使用してどの操作を実行するかを決定する必要があります。私の問題は、それらの入力された数値をメソッドから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();
}

ありがとう!

4

3 に答える 3

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;

これに慣れたら、テンプレートを使用して読み取り関数を作成することを検討することをお勧めします。

于 2013-03-10T15:34:51.673 に答える
1

これをC++で行う1つの方法です。基本的に、main()の変数に回答を格納する必要があり、フラグを使用して、それが変数であるintdouble変数であるかを示すことができます。

#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;
}
于 2013-03-10T16:08:42.570 に答える
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;
}
于 2013-04-26T13:39:28.787 に答える