制作一个纸牌是指操作员C

Make a charcter mean an operator c++

本文关键字:操作员 一个      更新时间:2023-10-16

我正在尝试编写一个程序,该程序允许用户输入A,S,M或D。基本上,我要做的是使角色的含义增加,减法,乘法和划分。问题是我不确定如何做。这是我到目前为止的代码。

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double num1;
double num2;
char operation;
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<"What would you like to do with the numbers? a-addition, s=subtraction, m=multiplacation, d=division";
cin>>operation;

检查开关语句:

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double num1;
double num2;
char operation;
cout<<"Enter the first number: ";
cin>>num1;
cout<<"Enter the second number: ";
cin>>num2;
cout<<"What would you like to do with the numbers? a-addition, s=subtraction, m=multiplacation, d=division";
cin>>operation;
switch(operation)
{
    case 'a':
        ... // Addition code
        break;
    case 's':
        ... //Substraction code
        break;
    ...
}

您也可以只使用每种类型的操作。

另外,作为提示,请考虑验证您的输入数据(尝试将更多字符键入程序或无效的选项键入)。

您使用开关:

switch (operation) {
  case 'a': // addition
    break;
  case 's': // subtraction
    break;
  // ...
  default: // none of these
    break;
}