gpt4 book ai didi

C++ 如何将方程式解释为字符串 C++(非 C)

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:36 25 4
gpt4 key购买 nike

<分区>

我正在尝试用 C++ 编写一个程序,用户将在其中输入一个方程式(例如:y = 3x + 6)。我如何确定什么是值,什么是字符,以及它们的位置?

所以从这个例子中,我会知道:

  • 值 3 在位置 stringArray[4]
  • 值 6 在位置 stringArray[9]
  • 字符 x 在位置 stringArray[5]

我该怎么写?

当我在 visual studio 2013 中输入 y = 3x 时,出现错误框:

Unhandled exception at 0x77662EEC in Graphmatica_WhatsInDatInput.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x00FAF94C.

但是当我输入任何不是以字符开头的内容时,没问题(例如 1 + 3x)

到目前为止,这是我的代码:

#include <iostream>
#include <string>

using namespace std;

int main(){

string Equation;
double dNumber;

cout << ": ";
getline(cin, Equation);


for (int i = 0; i < Equation.size(); i++){
if (isdigit(Equation[i])) {
cout << "Number: ";
dNumber = stod(Equation);
cout << dNumber << endl;

}
else {
if (Equation[i] != ' ') {
cout << "Character" << endl;
}
}
}

cout << endl;
system("pause");
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com