C++ g++;说函数不';即使包含正确的标题,也不存在
所以我正在为我的大学做一些家庭作业,需要将字符串转换成浮点。无论出于什么原因,g++都在抱怨“stof”函数不存在。尽管我已经包含了所需的标题。这是我的代码,错误就在上面 holder=stof(x.substr(0,数量的末尾))C++ g++;说函数不';即使包含正确的标题,也不存在,c++,string,g++,C++,String,G++,所以我正在为我的大学做一些家庭作业,需要将字符串转换成浮点。无论出于什么原因,g++都在抱怨“stof”函数不存在。尽管我已经包含了所需的标题。这是我的代码,错误就在上面 holder=stof(x.substr(0,数量的末尾)) #包括 #包括 #包括 使用名称空间std; 浮点进程_func(字符串x); bool-isPartOfNum(charx); int main(){ 字符串x; while(true){ Cuth< P>编译通过使用C++ 11 解决了C++标准11(如果您使用
#包括
#包括
#包括
使用名称空间std;
浮点进程_func(字符串x);
bool-isPartOfNum(charx);
int main(){
字符串x;
while(true){
Cuth< P>编译通过使用C++ 11 解决了C++标准11(如果您使用的是更老的GCC版本)但是,我不能,其他一些人,你可以用代码< > '0' < /C>和<代码> '9' 分别替换那些神奇的数字48和57。这使得代码更加清晰,使注释附加得更冗长。没有,我没有启用C++ 11,我怎么能做到呢?
#include <iostream>
#include <string>
#include <list>
using namespace std;
float process_func(string x);
bool isPartOfNum(char x);
int main() {
string x;
while (true) {
cout << "input a string" << endl;
getline(cin, x);
cout << process_func(x);
}
return 0;
}
float process_func(string x) {
int end_of_num =0;// used to find last index from num
int negMult = 1; //used to multiply value at end if there was a negative
bool onNum = false; //used to
list <float> numList;
list <char> operList;
if ((x.at(0) < 48 || x.at(0) > 57) && x.at(0) != '-') //check if start of string doesnt have a number or negative symbol
return -1;
if (x.at(0) != '-')
negMult = -1;
float holder;// temp holder for floats
int i = 0;
while (i<x.length()) {
if (isPartOfNum(x.at(i))) {
end_of_num++;
onNum = true;
}
else if (onNum) {
holder = stof(x.substr(0, end_of_num));
numList.push_back(holder); //adds num as float to list
x.erase(0, end_of_num + 1); //+1 removes the space after the number before the operator
end_of_num = 0;
onNum = false;
}
if (x.at(i) == '+' || x.at(i) == '-' || x.at(i) == '*' || x.at(i) == '/') {
operList.push_back(x.at(i));
}
} //at this point both lists should be full of all needed pieces of info
int answer = 0;
int temp;
bool firstOper=true; // used to hold first operation
while (numList.size() >=2) { //requires at least 2 entries for last operation
while (!operList.empty()) {
temp = numList.front();
numList.pop_front();
if (operList.front() == '+') {
if (firstOper) {
answer = temp + numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer += temp;
}
}
else if (operList.front() == '-') {
if (firstOper) {
answer = temp - numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer -= temp;
}
}
else if (operList.front() == '*') {
if (firstOper) {
answer = temp * numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer *= temp;
}
}
else if (operList.front() == '/') {
if (firstOper) {
answer = temp / numList.front();
numList.pop_front();
firstOper = false;
}
else {
answer /= temp;
}
}
operList.pop_front();
}
}
return answer;
}
bool isPartOfNum(char x) {
if ((x >= 48 && x <= 57) || (x == '-' || x == '.'))
return true;
return false;
}