简单计算器帮助(c++)
Simple Calculator Help (C++)
我用c++写了一个非常简单的计算器程序,它工作得很好!但是有人能解释一下我如何修改这个代码,使它能够一次添加两个以上的数字吗?
例如,我希望用户能够做2 + 2 + 2,而不是2 + 2。但每次我尝试,它只是把前两个"2"加起来,结果是4,不管你在后面输入多少个"+ 2"。
代码如下:
#include <iostream>
using namespace std;
// input function
void Input (float &x, float &y);
float a=1.0, b=1.0, result;
char operation;
int main ()
{
cout << "A simple calculator nn";
cin >> a >> operation >> b;
Input (a,b);
cout << result << endl;
system ("pause");
return 0;
}
void Input (float &x, float &y)
{
a = x;
b = y;
switch (operation)
{
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout << "Improper operation. Please input a correct calculation operation: n";
cin >> a >> operation >> b;
Input (a, b);
}
}
谢谢你们了!
您的代码被认为是这样,所以您要求输入三个元素:2个数字和它们的操作符(表示为字符)。
假设你只射击一次。如果你想使用更多的运算符和数字,你必须开发一个简单的解析器,它应该继续获取输入,直到找到结束标记,可能在你的情况下是行尾。
要做到这一点,我将改变你关于数据采集和计算的方法。你必须有几个简单的选项:
- 在线执行计算:即保留部分结果并在每次输入后更新。
- 将所有输入收集到一个数据结构中,然后执行所有计算。
例如,如果你决定实现第一个选项,这段不完整的代码可以帮助你:
float Input (float &x, float &y, char operation)
{
float result;
switch (operation)
{
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout << "Improper operation. Please input a correct calculation operation: n";
}
return result;
}
...
...
string input;
if(!getline( cin, input ) ) return; //Try to read one line.
istringstream lin( input );
float partial = 0.0;
float a;
char op;
while(lin >> op >> a) partial = Input(partial, a, op);
//At this point, partial should contain your final result
注意:我没有测试用例。比如除以0。等
float operate(float a, char operand, float b)
{
float result=0.0;
switch(operand)
{
case '+':
result = (a+b);
break;
case '*':
result = (a*b);
break;
case '/':
result = (a/b);
break;
default:
cout<<"Unknown operand"<<endl;
break;
}
return result;
}
int main()
{
float result=0.0;
char operand;
float a,b;
cout<<"enter a number followed by an operation, followed by a number"<<endl;
while(cin>>a>>operand>>b)
result+=operate(a,operand,b);
cout<<result;
return 0;
}
我几周前写了一个小解析器,你可以研究我的代码,也许(希望)你会发现一些有用的东西。
它不是c++,而是c,所以你的编译器应该能够编译它。
解析器解析任何可以按照以下规则构建的语句:
<expression> ::= <term>
| <term> "+" <term>
| <term> "-" <term>
<term> ::= <factor>
| <factor> "*" <factor>
| <factor> "/" <factor>
<factor> ::= <number>
| "(" <expression> ")"
<number> ::= ["-"] ["0" .. "9"]*
下面是代码,它几乎是自解释的,因为它遵循上面定义的规则:
#include <stdio.h>
#include <stdlib.h>
char next;
void getNext(){
next=getchar();
}
int getNum(){ //get a number
int num=0;
if((next<'0')||(next>'9')){
printf("error: expected number; found %c",next);
exit(-1);
}
while((next>='0')&&(next<='9')){
num*=10;
num+=(next-'0');
getNext();
}
return num;
}
int add(int x){
return x+term();
}
int sub(int x){
return x-term();
}
int multiply(int x){
return x*factor();
}
int divide(int x){
return x/factor();
}
int factor(){
int f=0;
if(next=='('){
getNext();
f=expression();
if(next=')')
getNext();
else{
printf("error ")" expected; found %c",next);
exit(-1);
}
}else{
f=getNum();
}
return f;
}
int term(){
int val;
val=factor();
while((next=='*')||(next=='/')){
char c=next;
getNext();
switch(c){
case '*': val=multiply(val); break;
case '/': val=divide(val); break;
default: printf("error: (*, /) expected; found %c",next);
exit(-1);
}
}
return val;
}
int expression(){
int sign=1;
int val;
if(next=='-'){
sign=-1;
getNext();
}
val=term()*sign;
while((next=='+')||(next=='-')){
char c=next;
getNext();
switch(c){
case '+': val=add(val); break;
case '-': val=sub(val); break;
default: printf("error: (+, -) expected; found %c",next);
exit(-1);
}
}
return val;
}
int main(void) {
getNext();
printf("%dn", expression());
return 0;
}
的例子:
$ gcc main.c -o calc
$ ./calc
1+2+3
6
$ ./calc
(1+2+3)/2
3
$ ./calc
3*(1+2)
9
$ ./calc
-10+20
10
$
相关文章:
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 有人可以帮助我处理正则表达式吗?
- C++调用具有 *this 属性的单个帮助程序函数
- C++:需要帮助了解运算符重载错误
- 需要以下代码的帮助,下面的代码有什么问题
- CS1 项目帮助C++
- 用于检查值是否为其任何参数的帮助程序函数
- 需要有关此 if 语句的帮助
- 类型限定宏帮助程序
- CoreCLR 中的检测探查器 - 将帮助程序程序集加载到 dotnet 进程的方法
- NS3 插槽混淆(需要帮助理解)
- 设计帮助 - 为不同类型的消息处理通用接口的设计模式
- 需要帮助查找内存泄漏
- 有人可以帮助我理解这些参数/参数吗?