2016-05-24 9 views
-2

Ich habe Probleme, ein Programm, das Dezimalstellen in Brüche konvertiert Arbeit in C++. Ich habe den folgenden Code eingefügt und bin offen für Vorschläge.Wie man Dezimal zu Fraktion Rechner arbeiten

int main() 
{ 
    char n = 0;//for decimal point 
    char x = 0;//for tenths place 
    char y = 0;//for hundredths place 
    std::cout << "Please enter the decimal to the hundredths place here: "; 
    std::cin >> n; 
    std::cin >> x; 
    std::cin >> y; 
     if (x == 0) 
      std::cout << 0 << y << "/100"; 
     if (y == 0) 
      std::cout << x << "/10"; 

     if (y == 1 || y == 2 || y == 3 || y == 4 || y == 5 || y == 6 || y == 7 || y == 9) 
      std::cout << x << y << "/100"; 

} 
+1

* Ich habe Probleme, ein Programm zu machen das konvertiert Dezimalstellen in Brüche arbeiten * - Bitte stellen Sie eine fokussierte Frage. Was "funktioniert" nicht? Und was ist passiert, wenn man einfach "if (y> = 1 && y <= 9)" anstatt dieser "if" -Anweisung angegeben hat? – PaulMcKenzie

+1

Definieren Sie "Brüche". –

Antwort

1

Sie nehmen die char-Eingabe und vergleichen sie mit int-Werten. Versuchen Sie, das char-Äquivalent der von Ihnen verwendeten int-Werte zu vergleichen (d. H. "1" anstelle von 1). Auch deine letzte schließt die Möglichkeit von 8 aus, was meiner Meinung nach komisch ist.

Es wäre so etwas wie dieses geben:

if (x == '0') 
    std::cout << 0 << y << "/100"; 
if (y == '0') 
     std::cout << x << "/10"; 

    if (y == '1' || y == '2' || y == '3' || y == '4' || y == '5' || y == '6' || y == '7' || y == '8' || y == '9') 
     std::cout << x << y << "/100"; 
0

Neben Bettorun Antwort, würden Sie auch das gleiche zu Ihrer Variablen tun müssen:

#include <iostream> 

using namespace std; 

int main() 
{ 
     char n = '0';//for decimal point 
     char x = '0';//for tenths place 
     char y = '0';//for hundredths place 

     cout << "Please enter the decimal to the hundredths place here: "; 
     cin >> n >> x >> y; 

     if (x == '0') 
      cout << '0' << y << "/100" << endl; 

     if (y == '0') 
      cout << x << "/10" << endl; 

     else if (y == '1' || y == '2' || y == '3' || y == '4' || y == '5' || y == '6' || y == '7' || y == '9') 
     cout << x << y << "/100" << endl; 

     return 0; 
}