无限输入C 计算器要结束计算,需要再进行一个INT

infinite input c++ calculator to end the calculation need one more int

本文关键字:一个 INT 计算器 输入 结束 计算 无限      更新时间:2023-10-16

我试图制作一个我有点我做的无限输入的计算器,但我有问题...

#include <iostream>
using namespace std;
int main()
{
    double a, b, c, sum = 0.0;
    char op;
    cout << sum;
    while (op != '=') 
    {
        cin >> op >> a;
        switch(op)
        {
            case '+':
                sum =  sum+a;
                cout << sum;
                break;
            case '-':
                sum = sum-a;
                cout << sum;
                break;
            case '/':
                sum = sum/a;
                cout << sum;
                break;
            case '*':
                sum= sum*a;
                cout << sum;
                break;
            case '=':
                cout << sum;
                break;
        }
    }
    return 0;
}

要完成第一个计算,您不仅需要键入操作员,还要键入int。因此看起来像这样:

0 33 = [随机int]总和

我想摆脱[随机int]完成。预先感谢。

@Some程序员花花公子是完全正确的。适当的解析器是唯一可靠的方法。以下是一个脆性的示例,也就是说"很容易破碎",我认为您期望的那种输入。比我更聪明的人可以写得更好。注意代码中所需的错误检查和假设。

我也借此自由来清理您的原始主()一小部分。

希望这能激发您学习Boost :: Spirit或任何出色的C 解析器。

    using namespace std;
    bool
    get_input_from_cin(char & op, double & d)
    {
    static const char * OPERATORS="+-/*=";
    char lc ='';
    char d_buffer[64]; // where we'll store the number
    size_t d_buffer_counter = 0;
    memset(d_buffer,'',sizeof(d_buffer));  // initialize it to nulls
    for (char lc = ' '; lc != 'n'; cin.get(lc))
    { // loop gathering characters until you get a newline.
          if ( strchr(OPERATORS,lc) != nullptr)
          { // if we're expecting an operator and we find it then save it
                op = lc; // what happens if we get more than one operator before a new line?
          }
          else if ( ' ' == lc )
          {
                ; // No Operation : consume spaces with no action
          }
          else
          { // we assume a double, save the digits. Check they are digits using isdigit(..) or some call.
              d_buffer[d_buffer_counter++] = lc;
              if ( d_buffer_counter == sizeof(d_buffer) )
              { // issue a warning in here somehow
                  return false;
              }
          }
     }
    if ( '' == op )
    { // no operator included!
        return false;
    }
    if ( '' == d_buffer[0] )
    { // no number included!
        return false;
    }
    d = atof(d_buffer);
    return true;
    }
    int main()
    {
    double a = 0.0, sum = 0.0; // you're not using b or c, better not to declare them
    char op;
    cout    << sum;
    op = ''; // initialize 
    while (op != '=')
    {
        if (false == get_input_from_cin(op, a))
        {
            if ('=' == op)
            {
                cout << sum;
                return 0;
            }
            cerr << "Input parse error." << endl;
            return -1;
        }
        switch (op)
        {
        case '+':
            sum = sum + a;
            cout << sum;
            break;
        case '-':
            sum = sum - a;
            cout << sum;
            break;
        case '/':
            sum = sum / a;
            cout << sum;
            break;
        case '*':
            sum = sum*a;
            cout << sum;
            break;
        case '=':
            cout << sum;
            break;
        }
    }
    return 0;
}

这就是您将整个行存储在字符串中的方式(您必须#include<string>

string line;
getline(cin, line);
string op;
string a;

然后您可以从字符串中获取数据:

if(string.length > 1)
{
    op = line.substr(0,1);
    a  = line.substr(1,string.length);
}
else op = line.substr(0,1)

然后将OP转换为char:

char chOp = line[0];

将字符串A转换为int:

int intA = std::stoi(a); 
相关文章: