c++ - String splitting and multiplication after conversion to double -


i'm trying write function gets string it's argument, searches if there 'p' or 'e' in it, splits onto separate strings, replaces "p" "3.14..." , "e" "2.71...", converts strings onto double , multiplies converted strings. example, when argument "123.45p4e9.2", function splits onto "123.45", "p", "4", "e" , "9.2", replaces characters constants: "123.45", "3.14...", "4", "2.71..." , "9.2", after converts of them onto double , multiplies: 123.45*3.14*4*2.71*9.2 .

the problem when give string number, without 'p' or 'e' (for example "2.4" or "32"), returns 0. however, when give "e", "p", or "ep", returns "2.71...", "3.14..." or "8.53...", in in case works well. problem back, when try give string combination of numbers , characters. when enter "3p", function returns correct result: "9.42...". on other side, when enter "p3", returns "3.14..." although should still "9.42...". looks doesn't work numbers , if there appears 'e' or 'p', doesn't seem discover numbers after first character.

could on code , find out, problem is? i've been trying find hours, on logical side seems 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 function  string liczba; cout << "type 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 = 0; < w.length(); i++){          //string array filled "null"     wartosc[i] = null; }  {          //bracket control lifespawn of liczba , element_wartosc variables string liczba = null;           // there'll built number between e , p int element_wartosc = 0;  for(int = 0; < w.length(); i++){          //searching argument string e , p     switch(w[i]){     case 'p':         if(liczba != null){                   //ends generating number, 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, 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 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 number         }      } } }  double wynik = 0;                              //there result  for(int = 0; < w.length(); i++){     if(wartosc[i] == null){                    //wartosc array filled earlier "null" strings, distinguish 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 returned } 

i did not through code, base on ur description here 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, ' ' );      ( auto & str : strs ) {         if ( str != "" ) {             result *= stod( str );         }     }      return result; } 

basically find 'p' , 'e' in string , multiply them replace them white space, split string , multiply rest.

the split function base on this thread, answer #2. tested little , should give u expected result.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -