Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <windows.h>
- /*
- +,-,*,/
- Ich garantiere nicht für Fehlerfreie Anwendung und jeder darf das verwenden. Auch wenn es eigentlich voll unnötig ist und Performance lastig. Vielleicht interessiert sich ja der ein oder andere. Kommazahlen und negative zahlen werden nicht unterstützt. Nur das Ergebnis wird als negativ angezeigt beim subtrahieren z.B: 20-50 = -30 oder die Kommazahlen beim dividieren. 5 / 2 = 2,5. Sonst nicht. D.H.: Die Eingabe von Kommazahlen und negativen zahlen ist zu vermeiden.
- <<<Consolen Anwendung>>>
- Man kann Addieren, Subtrahieren, Multiplizieren und Dividieren zwischen 2 zahlen. Zusätzlich gibt es noch kleine extra Funktionen, die ich auch gemacht habe und mir beim programmieren geholfen haben.
- Der Calculator war nur ein kleines Spaß Projekt und hat keinen größeren Sinn außer das man mit sehr großen zahlen rechnen kann. Bedenke aber, dass zum Beispiel beim Multiplizieren schon zahlen im 6 Stelligem Bereich sehr lange brauchen, da ich die Multiplikation mit der Addition verwende um es mir schön einfach zu machen. Vielleicht schau ich mal, wie man das beschleunigen kann und füge noch ein paar andere Funktionen hinzu, wenn ich Lust habe oder einen größeren Nutzen darin sehe.
- Ich Hab den auch nur gemacht, weil mit diese Begrenzung von int und double gestört hat. Zwar ist dieser auch begrenzt, da ich string.length() benutze und dieser halt ein Wert und keinen Text zurückgibt, aber nur auf die Stellenanzahl. Heißt wenn int nur den maximalen Wert von 10 sein kann, kann meiner ein Wert von 10 Stellen sein. int x=10; string y= 1000000000. Denke ich mal. Keine Garantie.
- */
- using std::string;
- using std::cout;
- using std::cin;
- using std::endl;
- char intToChar(int x); //Gibt eine einstellige Zahl als char zurück
- int charToInt(char x); //Gibt ein char als Int aus. Sofern es eine einstellige Zahl ist
- string invert(string); //Gibt den String von rechst nach links zurück
- string nullPruefer(string); //Entfernt alle Nullen von rechts nach links bis zu einer Zahl die nicht 0 ist
- string bigger(string, string); //Gibt den Wert der größeren Zahl(in String Form) zurück
- string move(string, string, char); //Verschiebt ein String um x Stellen nach links mit dem gegebenem Symbol.
- bool kleinerGleich(string, string); //Kleinergleich Vergleich mit Stringzahlen
- bool kleiner(string, string); //Kleiner Vergleich mit Stringzahlen
- bool groesser(string, string); //Größer Vergleich mit Stringzahlen
- string add(string, string); //Addieren x+y
- string sub(string, string); //Subtrahieren x-y
- string mult(string, string); //Multiplizieren x*y
- string div(string, string); //Division x/y
- void SuperStringCalcMenue(); //Gibt ein Menue aus, zur einfachen Eingabe von Zahlen die dann mit dem String Calculator gerechnet werden.
- int main()
- {
- SuperStringCalcMenue();
- return 0;
- }
- //Konverter Start
- char intToChar(int x)
- {
- switch (x)
- {
- case 1:
- return '1';
- break;
- case 2:
- return '2';
- break;
- case 3:
- return '3';
- break;
- case 4:
- return '4';
- break;
- case 5:
- return '5';
- break;
- case 6:
- return '6';
- break;
- case 7:
- return '7';
- break;
- case 8:
- return '8';
- break;
- case 9:
- return '9';
- break;
- case 0:
- return '0';
- break;
- default:
- cout << "Fehler beim convertieren intToChar" << endl;
- cin.ignore();
- return -1;
- }
- }
- int charToInt(char x)
- {
- switch (x)
- {
- case '1':
- return 1;
- break;
- case '2':
- return 2;
- break;
- case '3':
- return 3;
- break;
- case '4':
- return 4;
- break;
- case '5':
- return 5;
- break;
- case '6':
- return 6;
- break;
- case '7':
- return 7;
- break;
- case '8':
- return 8;
- break;
- case '9':
- return 9;
- break;
- case '0':
- return 0;
- break;
- default:
- cout << "Fehler beim convertieren charToInt" << endl;
- cin.ignore();
- return -1;
- }
- }
- //Konverter End
- //Inverter Start
- string invert(string x)
- {
- string tmp;
- for (int i = x.length() - 1; i >= 0; i--)
- {
- tmp += x[i];
- }
- return tmp;
- }
- //Inverter End
- //nullPruefer Start
- string nullPruefer(string x)
- {
- string text;
- int counter = 0;
- for (int i = 0; i < x.length(); i++)
- {
- if (x[i] == '0')
- {
- counter++;
- }
- else
- {
- counter = 0;
- }
- }
- if (counter < 0)
- {
- return x;
- }
- else
- {
- for (int i = 0; i < (x.length() - counter); i++)
- {
- text += x[i];
- }
- return text;
- }
- }
- //nullPruefer End
- //bigger Start
- string bigger(string x, string y)
- {
- char tmp;
- if (x.length() == y.length())
- {
- for (int i = 0; i < x.length(); i++)
- {
- if (charToInt(x[i]) < charToInt(y[i]))
- {
- return y;
- }
- else if( charToInt(y[i]) < charToInt(x[i]) )
- {
- return x;
- }
- else if ( i >= x.length()-1 )
- {
- return x;
- }
- else
- {
- //Nothing
- }
- }
- }
- else if (x.length() < y.length())
- {
- return y;
- }
- else
- {
- return x;
- }
- }
- //bigger End
- //move Start
- string move(string txt, string s, char z)
- {
- string text = invert(txt);
- for (string i = "0"; i != s; i = add(i, "1"))
- {
- text += z;
- }
- return invert(text);
- }
- //move End
- //kleinergleich Start
- bool kleinerGleich(string x, string y) //x<=y
- {
- if (x == bigger(x, y) && y == bigger(x, y))
- {
- return true;
- }
- else if (y == bigger(x, y))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //kleinergleich End
- //kleiner Start
- bool kleiner(string x, string y)
- {
- if (x == bigger(x, y) && y == bigger(x, y))
- {
- return false;
- }
- else if (y == bigger(x, y))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //kleiner End
- //groesser Start
- bool groesser(string x, string y)
- {
- if (x == bigger(x, y) && y == bigger(x, y))
- {
- return false;
- }
- else if (x == bigger(x, y))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //groesser End
- //Calculation Start
- string add(string x, string y)
- {
- int calc = 0;
- int tmp = 0;
- string erg;
- int length_x = x.length();
- int length_y = y.length();
- while (true)
- {
- length_x--;
- length_y--;
- if (length_x < 0 && length_y < 0)
- {
- if (tmp > 0)
- {
- erg += intToChar(tmp);
- }
- return invert(erg);
- }
- else if (length_x < 0)
- {
- calc = charToInt(y[length_y]);
- calc += tmp;
- erg += intToChar(calc%10);
- tmp = calc / 10;
- }
- else if (length_y < 0)
- {
- calc = charToInt(x[length_x]);
- calc += tmp;
- erg += intToChar(calc % 10);
- tmp = calc / 10;
- }
- else
- {
- calc = charToInt(x[length_x]);
- calc += charToInt(y[length_y]);
- calc += tmp;
- erg += intToChar(calc % 10);
- tmp = calc / 10;
- }
- }
- }
- string sub(string x, string y)
- {
- //return "Noch nicht fertig";
- int calc = 0;
- int tmp = 0;
- string erg;
- int length_x = x.length();
- int length_y = y.length();
- while (true)
- {
- length_x--;
- length_y--;
- if (x == bigger(x, y))
- {
- if (length_x < 0 && length_y < 0)
- {
- erg += intToChar(tmp);
- erg = nullPruefer(erg);
- if (erg == "")
- {
- return "0";
- }
- else
- {
- return invert(erg);
- }
- }
- else if (length_y < 0)
- {
- calc = charToInt(x[length_x]);
- calc -= tmp;
- if (calc < 0)
- {
- tmp = 1;
- erg += intToChar(calc + 10);
- }
- else
- {
- tmp = 0;
- erg += intToChar(calc);
- }
- }
- else
- {
- calc = charToInt(x[length_x]);
- calc -= charToInt(y[length_y]);
- calc -= tmp;
- if (calc < 0)
- {
- tmp = 1;
- erg += intToChar(calc + 10);
- }
- else
- {
- tmp = 0;
- erg += intToChar(calc);
- }
- }
- }
- else // y==bigger(x,y)
- {
- if (length_x < 0 && length_y < 0)
- {
- erg += intToChar(tmp);
- erg = nullPruefer(erg);
- erg += "-";
- if (erg == "" || erg=="-")
- {
- return "0";
- }
- else
- {
- return invert(erg);
- }
- }
- else if (length_x < 0)
- {
- calc = charToInt(y[length_y]);
- calc -= tmp;
- if (calc < 0)
- {
- tmp = 1;
- erg += intToChar(calc + 10);
- }
- else
- {
- tmp = 0;
- erg += intToChar(calc);
- }
- }
- else
- {
- calc = charToInt(y[length_y]);
- calc -= charToInt(x[length_x]);
- calc -= tmp;
- if (calc < 0)
- {
- tmp = 1;
- erg += intToChar(calc + 10);
- }
- else
- {
- tmp = 0;
- erg += intToChar(calc);
- }
- }
- }
- }
- }
- string mult(string x, string y)
- {
- string erg = "0";
- if (x == bigger(x, y))
- {
- for (string i = "0"; kleiner(i, y); i = add(i, "1"))
- {
- erg = add(erg, x);
- }
- }
- else
- {
- for (string i = "0"; kleiner(i, x); i = add(i, "1"))
- {
- erg = add(erg, y);
- }
- }
- return erg;
- }
- string div(string x, string y)
- {
- if (y == "0")
- {
- cout << "You can't divide by 0";
- cin.ignore();
- return "Unendlich";
- }
- string erg;
- string counter = "0";
- string i = "0";
- bool komma = false;
- bool kommaHook = true;
- do
- {
- if (x == bigger(x, y))
- {
- while(x == bigger(x, y))
- {
- x = sub(x, y);
- counter = add(counter, "1");
- }
- if (komma && kommaHook)
- {
- erg += ",";
- kommaHook = false;
- }
- }
- erg += counter;
- counter = "0";
- if (groesser(x,"0"))
- {
- x = mult(x, "10");
- komma = true;
- }
- i = add(i, "1");
- } while (kleinerGleich(i, "5") && komma);
- if (erg == "0")
- {
- return "0";
- }
- erg = nullPruefer(erg);
- return erg;
- }
- //Calculation End
- //Menue Start
- void SuperStringCalcMenue()
- {
- string x, y;
- char auswahl, weiter;
- do
- {
- system("cls");
- cout << "<1>Addieren" << endl;
- cout << "<2>Subtrahieren" << endl;
- cout << "<3>Multiplizieren" << endl;
- cout << "<4>Dividieren" << endl;
- cin >> auswahl;
- cin.ignore();
- system("cls");
- switch (auswahl)
- {
- case '1':
- cout << "Erste Zahl eingeben:" << endl;
- cin >> x;
- cin.ignore();
- cout << "Zweite Zahl eingeben:" << endl;
- cin >> y;
- cin.ignore();
- system("cls");
- cout << x << " + " << y << " = " << add(x, y) << endl;
- break;
- case '2':
- cout << "Erste Zahl eingeben:" << endl;
- cin >> x;
- cin.ignore();
- cout << "Zweite Zahl eingeben:" << endl;
- cin >> y;
- cin.ignore();
- system("cls");
- cout << x << " - " << y << " = " << sub(x, y) << endl;
- break;
- case '3':
- cout << "Erste Zahl eingeben:" << endl;
- cin >> x;
- cin.ignore();
- cout << "Zweite Zahl eingeben:" << endl;
- cin >> y;
- cin.ignore();
- system("cls");
- cout << x << " * " << y << " = " << mult(x, y) << endl;
- break;
- case '4':
- cout << "Erste Zahl eingeben:" << endl;
- cin >> x;
- cin.ignore();
- cout << "Zweite Zahl eingeben:" << endl;
- cin >> y;
- cin.ignore();
- system("cls");
- cout << x << " / " << y << " = " << div(x, y) << endl;
- break;
- default:
- cout << "Falsche Eingabe.";
- cin.ignore();
- exit;
- }
- cout << "Weitere Eingabe?(y/n): ";
- cin >> weiter;
- cin.ignore();
- } while (weiter == 'y' || weiter == 'Y');
- }
- //Menue End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement