[C++] Dynamischer-Datentyp?



  • Ich bastle mir grad ein Virtuellen Taschenrechner, nun bin ich mit meinem Latein am ende und weiß nicht wie es weiter gehen soll. 😞

    Folgendes Problem:

    Ich möchte einen Datentyp haben der "automatisch" denn Typ ermittelt wie z.B ob es sich um ein char Datentyp bzw. float Datentyp handelt und dementsprechend konvertiert in dem fall mit cast...

    Ich habe mir schon ein FilterSytem ausgedacht:

    Pseudo-Filtersystem:

    cin>> whatever;
    
    if(whatever != TYPECHAR) // Wenn whatever ein char-type ist... 
    {
    	// whatever in ein float-type umwandeln (mit cast)
    }
    esle
    {
    	// whatever in ein char-type umwandeln (mit cast)
    }
    

    Fragen:

    1. Wenn ich anstelle des PseudoAusdrucks TYPECHAR, char setze beklagt sich der Compiler. Warum?

    2. Existiert überhaupt so ein "dynamischer" Datentyp?
      Habe schon vergeblich gegoogelt...

    3. Falls nicht wie kann ich dann anstelle zwei console-inputs (cin>>) in meinem Programm der eine ermittelt den Rechen-Operator der andere den Zahlenwert was ziemlich umständlich ist, umgehen?

    Danke im Voraus!

    Oh falls ihr meine Source braucht:

    #include<iostream>
    #include<math.h>
    #include<windows.h>
    using namespace std;
    
    int main(void)
    {
        float constantValue = 0, currentValue = 0, x;
        char command;
        const float PI = 4*atan(1);
    
        do
        {
    
        cin>>command;
        cin>>constantValue;
    
            switch(command)
            {
                // main features
    
                case'+':
                currentValue += constantValue;
                break;
    
                case'-':
                currentValue -= constantValue;
                break;
    
                case'*':
                currentValue *= constantValue;
                break;
    
                case'/':
                currentValue /= constantValue;
                break;
    
                case'=':
                currentValue = currentValue;
                break;
    
                case'd':    // destroy
                currentValue = 0;
                constantValue = 0;
    
                case'n': // new screen
                system("cls");
                break;
    
                break;
    
                // add-on trigonometric functions ToDo
    
                case'S': // SINE
                currentValue = ((constantValue*PI)/180);
                currentValue = sin(currentValue);
                break;
    
                case's': // arcus-sine
                currentValue = asin(constantValue);
                currentValue = ((currentValue*180)/PI);
                break;
    
                case'C': // COSINE
                currentValue = ((constantValue*PI)/180);
                currentValue = cos(currentValue);
                break;
    
                case'c': // arcus-cosine
                currentValue = acos(constantValue);
                currentValue = ((currentValue*180)/PI);
                break;
    
                case'T': // TANGENT
                currentValue = ((constantValue*PI)/180);
                currentValue = tan(currentValue);
                break;
    
                case't': // arcus-tangent
                currentValue = atan(constantValue);
                currentValue = ((currentValue*180)/PI);
                break;
    
                // add-on pi constant
    
                case'p':
                currentValue = PI;
                break;
    
                // add-on nth power
    
                case'P':
                currentValue = pow(currentValue, constantValue);
                break;
    
                // add-on radical
    
                case'R':
                currentValue = sqrt(currentValue);
                break;
    
            }
    
            cout<<currentValue<< endl;
    
        }
        while(command != 'a'); // abort
    
    }
    

    PS: Mein aller-erstes C++ Programm... 😃



  • 2+3) Du kannst einen String verwenden:

    #include<iostream>
    #include<string>
    #include<cctype>
    
    int main()
    {
    	std::string calculation("");
    	getline(std::cin, calculation);
    	for( int i=0; i<=calculation.size(); i++ )
    	{
    		if( isdigit(calculation[i]) )	// == wenn calculation[i] eine zahl ist
    		{
    			// ...
    		}
    		else
    		{
    			// ...
    		}
    	}
    }
    

    siehe auch:
    Einmal Zahl nach String und zurück

    EDIT:

    Individual Code schrieb:

    //...
    #include<math.h>
    #include<windows.h> 
    //...
    

    - die windows.h brauchst du hier nicht
    - math.h in cmath umwandeln

    mfg


Anmelden zum Antworten