1. #1
    Avatar von DMW007
    Registriert seit
    15.11.2011
    Beiträge
    6.080
    Thanked 9.118 Times in 2.995 Posts
    Blog Entries
    5

    Standard Einfacher Taschenrechner

    Schwierigkeit:
    Aufgabenstellung:
    Es soll ein kleiner konsolenbasierter Taschenrechner für die Grundrechenarten +, -, * und / programmiert werden.
    Fürs dividieren sollen zwei Operatoren zur Verfügung stehen:
    / Teilt die Zahl mit dezimalem Ergebnis (Beispiel: 7 / 2 = 3,5)
    : Teilt die Zahl mit Ausgabe des Restes (Beispiel: 7 : 2 = 3 Rest 1)

    Lösung (C++)

    Spoiler:


    #include <iostream>
    #include <conio>

    using namespace std;

    void main() {
    cout << "Erste Zahl eingeben: ";
    float Operand1;
    cin >> Operand1;

    cout << "Operator eingeben (G" << char(129) << "ltig: +, -, *, /, :): ";
    char Operator;
    cin >> Operator;

    cout << "Zweite Zahl eingeben: ";
    float Operand2;
    cin >> Operand2;

    switch(Operator) {
    case '+':
    cout << Operand1 + Operand2;
    break;

    case '-':
    cout << Operand1 - Operand2;
    break;

    case '*':
    cout << Operand1 * Operand2;
    break;

    case '/':
    if(Operand2 == 0) {
    cout << "Division durch 0 nicht m" << char(148) << "glich!";
    getch();
    return;
    }
    else {
    cout << Operand1 / Operand2;
    }
    break;

    case ':':
    if(Operand2 == 0){
    cout << "Division durch 0 nicht m" << char(148) << "glich!";
    getch();
    return;
    }
    else if((int)Operand1 != Operand1 || (int)Operand2 != Operand2) {
    cout << "Nur ganzzahlige Operanden m" << char(148) << "glich!";
    getch();
    return;
    }
    else {
    cout << (int)(Operand1 / Operand2) << " Rest " << (int)Operand1 % (int)Operand2;
    }
    break;

    default:
    cout << "Ung" << char(129) << "ltiger Operator!" << Operator;
    getch();
    return;
    break;
    }

    getch();
    }



  2. #2
    Avatar von Snees
    Registriert seit
    18.11.2011
    Beiträge
    1.001
    Thanked 590 Times in 319 Posts

    Standard AW: Einfacher Taschenrechner

    C#-Klasse;
    Spoiler:


    using System;

    namespace EinfacherTaschenrechner
    {
    class Rechner
    {
    private float Operand1, Operand2;
    private char Operator;

    public Rechner()
    {
    Rechne();
    Console.ReadKey();
    }

    private void Rechne()
    {
    Console.WriteLine("Erste Zahl eingeben:");
    try { Operand1 = float.Parse(Console.ReadLine()); }
    catch { Rechne(); }
    Console.WriteLine("Operator eingeben (+, -, *, /, :)");
    try { Operator = char.Parse(Console.ReadLine()); }
    catch { Rechne(); }
    Console.WriteLine("Zweite Zahl eingeben:");
    try { Operand2 = float.Parse(Console.ReadLine()); }
    catch { Rechne(); }
    switch (Operator.ToString())
    {
    case "+":
    Console.WriteLine(Operand1 + Operand2);
    break;
    case "-":
    Console.WriteLine(Operand1 - Operand2);
    break;
    case "*":
    Console.WriteLine(Operand1 * Operand2);
    break;
    case "/":
    if (Operand2 == 0)
    {
    Console.WriteLine("Division durch 0 nicht möglich!");
    return;
    }
    else
    {
    Console.WriteLine(Operand1 / Operand2);
    break;
    }
    case ":":
    if (Operand2 == 0)
    {
    Console.WriteLine("Division durch 0 nicht möglich!");
    return;
    }
    else if (Math.Floor(Operand1) != Operand1 || Math.Floor(Operand2) != Operand2)
    {
    Console.WriteLine("Nur ganzzahlige Operanden möglich.");
    return;
    }
    else
    {
    Console.WriteLine(Math.Floor(Operand1 / Operand2) + " Rest " + (Operand1 % Operand2));
    }
    break;
    default:
    Console.WriteLine("Ungültiger Operator!");
    return;
    }
    }
    }
    }


  3. #3
    Avatar von Imperativ
    Registriert seit
    08.08.2013
    Beiträge
    30
    Thanked 10 Times in 9 Posts

    Standard AW: Einfacher Taschenrechner

    Python-Code:


    Spoiler:
    PHP-Code:
    # Taschenrechner

    import math

    def main
    ():
       
        
    cPlus '+'
        
    cMinus '-'
        
    cMulti '*'
        
    cDivide '/'
        
    cModulo ':'
        
    cSign ''
        
        
    fOperand1 0.0
        fOperand2 
    0.0

        
    try:
            print(
    "Erste Zahl eingeben: ")
            
    fOperand1 float(input())

            print(
    "Operator eingeben [+, -, *, /, :]")
            
    cSign input()

            print(
    "Zweite Zahl eingeben: ")
            
    fOperand2 float(input())
        
    except:
            print(
    "Eingabefehler!")
            
        if 
    cSign == cPlus:
            print(
    fOperand1 fOperand2)
        
    elif cSign == cMinus:
            print(
    fOperand1 fOperand2)
        
    elif cSign == cMulti:
            print(
    fOperand1 fOperand2)
        
    elif cSign == cDivide:
            try:
                print(
    fOperand1 fOperand2)
            
    except:
                print(
    "Divisionsfehler!")
                
        
    elif cSign == cModulo:
            
    iRoundDivided fOperand1 fOperand2
            iRoundModulo 
    fOperand1 fOperand2
            
    print(math.floor(iRoundDivided), "Rest: "math.floor(iRoundModulo))
            
        else:
            print(
    "Programm wird nun beendet...")

    main() 






    Gruß

    Imperativ
    Geändert von Imperativ (12.08.2013 um 15:41 Uhr)

  4. #4
    Gelöschter Benutzer
    Gast

    Standard AW: Einfacher Taschenrechner

    @Snees, anstatt float.Parse() zu nutzen, würde ichfloat.TryParse() nutzen, dann musst du das Ganze nicht in einen Try-Block packen.

    Beispiel:


    float TestFloat;

    Console.WriteLine("Geben Sie eine gültige Kommazahl ein:");
    float.TryParse(Console.ReadLine(), out TestFloat);


    Nun steht in "TestFloat" entweder die gültige Eingabe, oder eine Null, wenn ein nicht-float eingegeben wurden ist (z.B. "asdf").

Ähnliche Themen

  1. Taschenrechner
    Von Sky.NET im Forum Showroom
    Antworten: 0
    Letzter Beitrag: 18.02.2012, 01:26
  2. [C++] Einfacher Taschenrechner
    Von DMW007 im Forum C++
    Antworten: 2
    Letzter Beitrag: 11.01.2012, 01:56
  3. [C#] Taschenrechner-Konsole
    Von Snees im Forum .Net
    Antworten: 2
    Letzter Beitrag: 17.12.2011, 12:58
  4. [PS] Einfacher Rand
    Von Devon im Forum Tutorials
    Antworten: 0
    Letzter Beitrag: 01.12.2011, 13:02
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.