转换为double后的字符串拆分和乘法
String splitting and multiplication after conversion to double
我试图写一个函数,它得到字符串作为它的参数,搜索如果有'p'或'e'在它,分割到单独的字符串,取代"p"与"3.14…"answers"e" with "2.71…",将所有字符串转换为double,然后将所有转换后的字符串相乘。例如,当参数为"123.45p4e9.2"时,函数将其拆分为"123.45", "p", "4", "e"answers"9.2",然后用常量替换字符:"123.45", "3.14…, "4", "2.71…"answers"9.2",然后将它们全部转换为双精度并相乘:123.45*3.14*4*2.71*9.2。
问题是,当我给它一个字符串只有一个数字,没有任何'p'或'e'(例如"2.4"或"32"),它返回0。然而,当我给它"e "、" p ",或" ep ",它返回" 2.71……"、" 3.14……8.53…",所以在这种情况下它工作得很好。问题又回来了,当我试图给它字符串与数字和字符的组合。当我输入"3p",函数返回正确的结果:"9.42…"。另一方面,当我输入"p3"时,它返回"3.14…",尽管它仍然应该是"9.42…"。看起来它根本无法处理数字,如果出现'e'或'p',它似乎无法发现第一个字符之后的数字。
有人能看看代码并找出问题在哪里吗?我已经找了好几个小时了,但从逻辑上讲,一切似乎都没问题。
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <math.h>
#include <windows.h>
#define NULL "null"
using namespace std;
double stale(string w);
int main(){
//Testing the function
string liczba;
cout << "Type the number: ";
cin >> liczba;
double wynik = stale(liczba);
cout << endl << "Result: " << wynik << endl << endl;
system("PAUSE");
}
double stale(string w){
string *wartosc = new string[w.length()]; //Dynamic string array
for(int i = 0; i < w.length(); i++){ //String array is filled with "null"
wartosc[i] = NULL;
}
{ //Bracket only to control lifespawn of liczba and element_wartosc variables
string liczba = NULL; // There'll be built a number between e and p
int element_wartosc = 0;
for(int i = 0; i < w.length(); i++){ //Searching argument string for e and p
switch(w[i]){
case 'p':
if(liczba != NULL){ //Ends generating number, which isn't e or p
wartosc[element_wartosc] = liczba;
liczba = NULL;
element_wartosc++;
wartosc[element_wartosc] = "3.14159265358979323846";
element_wartosc++;
}else{
wartosc[element_wartosc] = "3.14159265358979323846";
element_wartosc++;
}
break;
case 'e':
if(liczba != NULL){ //Ends generating number, which isn't e or p
wartosc[element_wartosc] = liczba;
liczba = NULL;
element_wartosc++;
wartosc[element_wartosc] = "2.71828182845904523536";
element_wartosc++;
}else{
wartosc[element_wartosc] = "2.71828182845904523536";
element_wartosc++;
}
break;
default:
if (liczba == NULL){
liczba = w[i]; //Starts filling liczba variable with first character
}else if(w[i] == ' '){
wartosc[element_wartosc] = liczba; //Ends generating number on argument string end
break;
}else{
liczba = liczba + w[i]; //Builds the number
}
}
}
}
double wynik = 0; //There will be the result
for(int i = 0; i < w.length(); i++){
if(wartosc[i] == NULL){ //wartosc array was filled earlier with "null" strings, to distinguish it from entered numbers
continue;
}else{
double liczba = stod(wartosc[i]); //Converting strings onto doubles
if(wynik == 0){
wynik = liczba; //Starting multiplification
}else{
wynik *= liczba; //Multiplification
}
}
}
delete[] wartosc; //Removing dynamic array
return wynik; //The result is returned
}
我没有看你的代码,但根据你的描述,这里是我要写的
double stale(string w){
double result = 1;
std::replace_if( w.begin(), w.end(),
[&result] ( char & c ) {
if ( c == 'e' ) {
result *= 2.718;
return true;
} else if ( c == 'p' ) {
result *= 3.141;
return true;
} else
return false;
}, ' ' );
auto strs = split( w, ' ' );
for ( auto & str : strs ) {
if ( str != "" ) {
result *= stod( str );
}
}
return result;
}
基本上,它找到字符串中所有的'p'
和'e'
并将它们相乘,然后用空白替换它们,然后我拆分字符串并将其余部分相乘。
split函数是基于这个线程的,回答#2。我测试了一下,它应该给你预期的结果。
- 我还可以添加什么来按空格或空格正确拆分C++字符串?
- 通过空行提升拆分字符串
- 如何按多个括号类型拆分字符串,但仍将括号保留在C++?
- 如何根据字符拆分字符串
- 在C++中拆分字符串的最佳做法
- 如何使用两个不同的分隔符拆分字符串
- 基于 C++ 中的多个字符串分隔符拆分字符串
- 拆分字符串输入并查找字符是否有效
- 使用 boost::split 按分隔符的前 N 次匹配来拆分字符串
- 如何在C 中不带有弦乐和strtok的情况下拆分字符串(提取单词)
- 为什么在黑客兰克的许多问题中都会完成拆分字符串
- C++拆分字符串反馈
- C++:按制表符拆分字符串,但包含空格
- 基于其中的数字子字符串拆分字符串
- 按空格拆分字符串并将列输出到不同的数组中
- C++ STL 在逗号处拆分字符串
- 如何根据某些字符拆分字符串?经度纬度
- 如何在 c++ 中拆分字符串的一部分
- 如何通过分隔符拆分字符串并将每个字符串放入不同的向量中
- 我如何在QT中的更多子字中拆分字符串