简体   繁体   English

我如何接受用户输入的算术表达式,如 5-8+7*4-8+9 或 1+5?

[英]How do i take user input of an Arithmetic Expression like 5-8+7*4-8+9 or 1+5?

I am solving a problem for finding maximum value of an arithmetic expression.我正在解决一个寻找算术表达式最大值的问题。 But I am having problem in taking input for the expression as no of characters are not fixed.但是我在输入表达式时遇到问题,因为没有固定的字符。

Input Format:输入格式:

  • The only line of the input contains a string s of length 2n + 1 for some n, with symbols s0, s1, .输入的唯一行包含一个长度为 2n + 1 的字符串 s,其中包含符号 s0、s1、...。 . . . . , s2n. , s2n。
  • Each symbol at an even position of s is a digit (that is, an integer from 0 to 9) s 的偶数 position 处的每个符号都是一个数字(即从 0 到 9 的 integer)
  • while each symbol at an odd position is one of three operations from {+,-,*}.而奇数 position 上的每个符号是 {+,-,*} 的三个操作之一。

But I want digits and symbols in different arrays(digits in an int array and operations in a char array) for my solution.但我想要不同数组中的数字和符号(int 数组中的数字和 char 数组中的操作)作为我的解决方案。

This is how I implemented originally:这就是我最初实现的方式:

  string s;
  std::cin >> s;

  n=s.size();
  cout<<"n= "<<n<<endl;

  vector<long long> no;
  vector<char> ops;

  for(int i = 0; i <n; i++)
  {
    if(i%2==0)
    {
      no.push_back(s[i]);
    }
    else{
      ops.push_back(s[i]);
    }
  }

But I am not being able to get required input, instead getting this:但我无法获得所需的输入,而是得到这个:

INPUT:
5-8+7*4-8+9

OUTPUT:
n = 11
no[0] = 53
no[1] = 56
no[2] = 55
no[3] = 52
no[4] = 56
no[5] = 57
ops[0] = -
ops[1] = +
ops[2] = *
ops[3] = -
ops[4] = +

I have also tried one more solution:我还尝试了另一种解决方案:

  vector<long long> no;
  vector<char> ops;

  int i=0;
  while(cin)
  {

    cout<<"i= "<<i<<endl;
    if(i%2==0)
    {
      int s;
      cin>>s;
      if(s=='\0')
      {
        exit();
      }
      cout<<"s= "<<s<<endl;
      no.push_back((int)s);
      cout<<"no= "<<no[i/2]<<endl;
    }
    else
    {
      char s;
      cin>>s;
      if(s=='\0')
      {
        exit();
      }
      cout<<"s= "<<s<<endl;
      ops.push_back(s);
      cout<<"ops= "<<ops[(i-1)/2]<<endl;
    }
    i++;
  }

But this goes into infinite loop.但这进入了无限循环。

Please help me out请帮帮我

Your output seems to be correct, but as already mentioned in the comments, your values are read as characters, not as digits, so a conversion needs to be done.您的 output 似乎是正确的,但正如评论中已经提到的,您的值被读取为字符,而不是数字,因此需要进行转换。

In order to do this, it might be helpful to understand that, in ASCII, the digits have following value:为此,了解 ASCII 中的数字具有以下值可能会有所帮助:

Character ASCII-code value
      '0'         48     0
      '1'         49     1
      '2'         50     2
      '3'         51     3
      '4'         52     4
      '5'         53     5
      '6'         54     6
      '7'         55     7
      '8'         56     8
      '9'         57     9

How to get the value out of the character value?如何从字符值中获取值? Easy:简单的:

value(<character>) = ASCII_code(<character>) - ASCII_code('0'), or:
                   = ASCII_code(<character>) - 48

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM