0

引数として文字列を取得し、「p」または「e」が含まれているかどうかを検索し、それを個別の文字列に分割し、「p」を「3.14...」および「e」に置き換える関数を作成しようとしています。 " 2.71... " を使用すると、すべての文字列が double に変換され、変換されたすべての文字列が乗算されます。たとえば、引数が「123.45p4e9.2」の場合、関数はそれを「123.45」、「p」、「4」、「e」、および「9.2」に分割し、文字を定数「123.45」、「3.14 」に置き換えます。 .."、その後、それらすべてを double に変換して乗算します: 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.単に数字では機能しないようで、「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] == '\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
}
4

1 に答える 1