简体   繁体   中英

String splitting and multiplication after conversion to double

I'm trying to write a function which gets string as it's argument, searches if there is 'p' or 'e' in it, splits it onto separate strings, replaces " p " with " 3.14... " and " e " with " 2.71... ", converts all strings onto double and then multiplies all converted strings. For example, when argument is " 123.45p4e9.2 ", function splits it onto " 123.45 ", " p ", " 4 ", " e " and " 9.2 ", then replaces characters with constants: " 123.45 ", " 3.14... ", " 4 ", " 2.71... " and " 9.2 ", after that converts all of them onto double and multiplies: 123.45*3.14*4*2.71*9.2 .

The problem is that when I give it a string with only a number, without any 'p' or 'e' (for example " 2.4 " or " 32 "), it returns 0. However, when I give it " e ", " p ", or " ep ", it returns " 2.71... ", " 3.14... " or " 8.53... ", so in that in this case it works well. The problem is back, when I try to give it string with combination of numbers and characters. When I enter " 3p ", function returns correct result: " 9.42... ". On the other side, when I enter " p3 ", it returns "3.14..." although it should still be " 9.42... ". It looks like it simply doesn't work with numbers and if there appears 'e' or 'p', it doesn't seem to discover numbers after first character.

Could someone look on the code and find out, where the problem is? I've been trying to find it for hours, but on logical side everything seems to be OK.

#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] == '\0'){ 
            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
}

I did not look through your code, but base on ur description here is what I would write

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;
}

Basically it find all 'p' and 'e' in the string and multiply them then replace them with white space, then I split the string and multiply the rest.

the split function is base on this thread, answer #2 . I tested it a little and it should give u expected result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM