(c++) Taschenrechnerskript/cin in zwei Ausgangsvariablen

The ELSE

Grünschnabel
Hallo, tutorials.de-Community,

ich arbeite derzeit als Newbe in C++ an einem kleinen Taschenrechner. Ich habe zwar einen hingekriegt, aber ich würde ihn gern weiter ausbauen. Als Faktoren sollen dem Benutzer sowohl Zahlen als auch Variablen zur Verfügung stehen.

  • Der Rechner soll "e" als Eulersche Zahl und "pi" als PI benutzen.
  • Der Rechner soll drei eigene Variablen besitzen, die durch etwa "def_x(6.3563)" festgelegt werden soll und statt einer Zahl mit "x" aufgerufen werden kann
  • Die Variable "w" soll das letzte Ergebnis anzeigen
  • e(4) oder pi(4) gibt die ersten 4 Ziffern der Zahl aus
  • Außerdem plane ich, zu den Grundrechenarten noch weitere hinzuzufügen (ich bin allerdings auch kein so großer Mathematiker...)

Hier ein Bedienungsbeispiel mit der KDE-Konsole, wie es nachher aussehen soll:
... ./main
6
+
pi
=
9.141592653589...
-
w
=
0
+
eu(56)

Aktuell sieht mein Code so aus:

Code:
#include <iostream.h>
#include <string.h>

int main()
{
// Folgendes nicht ändern:
char add = '+';
char sub = '-';
char mul = '*';
char div = '/';
char cancel1 = 'C';
char cancel2 = 'c';
std::string pi_short = "pi";
std::string eu_short = "e";
char x_short = 'x';
char y_short = 'y';
char z_short = 'z';
std::string help_short = "help";

std::string t4;

float x = 40;
float y = 44.44;
float z = 2;
float pi = 3.141592653589;
float eu = 2.718281828459;

float t1;
char t2;
float t3;
int g1;
int g2;
int g3;

cin >> g1;
/*cin >> g1,t1;
  if(g1 == short_pi)
    {
    t1 = pi;
    }*/
// So etwa habe ich es versucht, aber der Compiler blockte
if (g1 == x_short)
  {
  t1 = x;
  }
if (g1 == y_short)
  {
  t1 = y;
  }
if (g1 == z_short)
  {
  t1 = z;
  }
if (g1 == pi_short)
  {
  t1 = pi;
  }
if(g1 == eu_short)
  {
  t1 = eu;
  }
if (g1 != x_short && g1 == y_short && g1 == z_short && g1 == pi_short && g1 == eu_short)
{
  t1 = g1;
}
 while(t2 != cancel1 && t2 != cancel2 && t1 != cancel1 && t1 != cancel2 && t3 != cancel1 && t3 != cancel2 && script == short_standard)
  {
  cin >> g3;
  if (g3 == x_short)
    {
    t3 = x;
    }
  if (g3 == y_short)
    {
    t3 = y;
    }
  if (g3 == z_short)
    {
    t3 = z;
    }
if (g3 == pi_short)
  {
  t3 = pi;
  }
if (g3 == eu_short)
  {
  t3 = eu;
  }
if (g3 != x_short && g3 == y_short && g3 == z_short && g3 == pi_short && g3 == eu_short)
{
  t3 = g3;
}

    float erg;
    if (t2 == add)
      {
      erg = t1 + t3;
      }
    if (t2 == sub)
      {
      erg = t1 - t3;
      }
    if (t2 == mul)
      {
      erg = t1 * t3;
      }
    if (t2 == div)
      {
      erg = t1 / t3;
      }
    if (t2 != add && t2 != sub && t2 != mul && t2 != div)
      {
      cout << "Unexpected Error: Operator is no operator!" << endl;
      }
    cout << "=" << endl << erg << endl;
    t1 = erg;
    }
}

Danke im Vorraus,
The ELSE :confused:

#Edit: Ich benutze OpenSUSE 10 und KDevelop.
 
Zuletzt bearbeitet:
Ein paar Sachen, die vielleicht nicht das Grundproblem lösen, aber dich hoffentlich trotzdem weiterbringen:
  1. Soweit ich weiß, kannst du keine float-Zahl mit 56 Stellen Genauigkeit ausgeben (auch nicht mit double oder long double)
  2. Definiere pi und e als globale Konstanten, also außerhalb von main: const float pi=3.141592653589793238462643383;
Wenn mir noch was einfällt, schreibe ich es.

Gruß, the_undertaker
 
Soll eigentlich der Benutzer x, y und z Werte zuweisen, oder sind die schon vorgegeben? Wenn sie schon von Anfang an feststehen, kannst du sie wie pi und e behandeln.
 
Zurück