无限输入C 计算器要结束计算,需要再进行一个INT
infinite input c++ calculator to end the calculation need one more int
我试图制作一个我有点我做的无限输入的计算器,但我有问题...
#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);
相关文章:
- 包装一个对象并假装它是一个 int
- 无法一次从二进制文件中读取一个 int
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- 为什么我不能使用与"常量字符*"相同的"const int*"创建一个 int 数组?
- 如何将私有整数从A类传递到C 类B类的另一个INT变量
- C :用sizeof()operator在for for loop中增加一个int变量,只能工作一次
- 结构C++可以用 0 初始化,但不能用另一个 int 初始化
- 将一个int []转换为字符串,而无需明确的c 循环
- 如何通过另一个int变量递增int变量
- C++根据输入返回一个字符串或一个int
- 如何在另一个结构 arrya 中的一个结构中初始化一个 int 数组
- 两个 UShorts 之间的操作在 VC2015 中给出了一个 int
- turbo 如何在 C++ 的系统() 函数中放入一个 int varriable
- 无限输入C 计算器要结束计算,需要再进行一个INT
- 带有两个INT列表的结构将减少为一个INT
- 在c#中构建一个int数组,每个int调用c++或在c++中构建并传递给c#
- 对于具有 3 个无符号字符和一个 int 的结构来说,对于unordered_map来说,什么是好的哈希函数?
- 当我创建一个int指针并实例化一个数组时,数组在内存中发生了什么
- 如何在C 中的结构内设置一个int数组
- 为什么在我的测试程序中,从字符缓冲区读取一个int时,"std::copy"5x(!)比"