接受多个输入的计算器,例如+,-和数字平方.从文本文件中提取信息 [英] Calculator that accepts multiple inputs like +, -, and number squared. Pulling info from text file

查看:26
本文介绍了接受多个输入的计算器,例如+,-和数字平方.从文本文件中提取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码应该能够使用; 字符在一个文本文件中打印多个不同的方程式,从而导致方程式停止并转到下一行,而 ^ 字符,取数字的平方.但是,该代码仅打印一个方程式,然后停止.有人告诉我,我需要在已经建立的循环上循环.我不知道该怎么办.谢谢你.

My code is supposed to be able to print multiple different equations in one text file with the ; character causing the equation to stop and go to the next line, and the ^ character taking the number squared. However, the code only prints one equation then stops. I was told that I needed a loop over my already established loop. I don't know how I can do this though. Thank you.

输入



    5^ + 5 - 4^;
    2 - 1;
    1 + 5^ + 2;

我的输出



    14

我的密码

#include <iostream> 
#include <math.h>
#include <fstream>
using namespace std;

char op;
int result, operand;


int readnumber();

int readnumber()
{
    int val;
    cin >> val;
    if (cin.peek() == '^')
    { // found ^
        val *= val; 
        cin.ignore(); 
    }

    return val;
}

int main() {

    result = readnumber(); 
    while (cin >> op) {

    if (op == ';') {
        cout << result << endl; 
        cin >> result;
    } 

    operand = readnumber();

    if (op == '+') {
        result += operand;
       }

    if (op == '-') {
        result -= operand;
    }


    }

    return 0;

}

推荐答案

打印结果( cin>> result; )后,您正在尝试读取数字.通过删除该行,我可以添加更多项(尽管结果似乎不正确).

You're trying to read a number after you print the result (cin >> result;). By deleting that line I can add more items (although the results seem incorrect).

这篇关于接受多个输入的计算器,例如+,-和数字平方.从文本文件中提取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆