Einige Probleme-Fragen ... Einfaches programmieren



  • Denim schrieb:

    Falls du noch Zeit hast [...]

    Dann reichts aber - mehr geht nicht in 20 Zeilen ...

    #include <stdio.h>
    #include <conio.h> // platform-specific: getche()
    #include <windows.h> // platform-specific: GetStdHandle(), SetConsoleTextAttribute(), GetConsoleScreenBufferInfo(), SetConsoleCursorPosition()
    HANDLE const hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    void print_colorful( int value ) {
    	SetConsoleTextAttribute( hstdout, 0x0c ); printf( "%d\n", value ); SetConsoleTextAttribute( hstdout, 0x07 ); }
    void undo_linebreak( void ) {
    	CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo( hstdout, &csbi );
    	csbi.dwCursorPosition.Y--; SetConsoleCursorPosition( hstdout, csbi.dwCursorPosition ); }
    int main( void ) {
    	char ch = 0; int sum = 0, op = 0, is_first = 1;
    	for( ;; ) {
    		if( ch = getche(), putchar( '\n' ), ch == '1' || ch == '2' ) {
    			if( is_first ) op = ( ch == '1' ? 1 : -1 );
    			else { print_colorful( sum += op + ( ch == '1' ? 1 : - 1 ) ); op = 0; }
    		} else if( ch == '\r' ) { undo_linebreak(); print_colorful( sum += ( is_first ? 0 : op ) );
    		} else continue;
    		is_first = !is_first;
    	}
    }
    


  • So herzlichen Dank!

    Programm ist fast fertig hänge noch an folgendem:

    Wenn man z.B. die Leertaste drückt, wird angezeigt wie viele Zahlen man bereits eingegeben hat (Enter wird nicht mitgezählt, nur die Zahlen), man kann aber sofort weiter danach weiter Zahlen eingeben, also nur eine kleine Statusinformation, die nichts am Programmablauf verändern soll.

    Es können auch ruhig 25 Zeilen sein :).

    Danach ist das Programm endgültig fertig !



  • Denim schrieb:

    Es können auch ruhig 25 Zeilen sein :).

    Sicher ned.

    #include <stdio.h>
    #include <conio.h> // platform-specific: getche()
    #include <windows.h> // platform-specific: GetStdHandle(), SetConsoleTextAttribute(), GetConsoleScreenBufferInfo(), SetConsoleCursorPosition()
    HANDLE const hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    void print_colorful( int value, short unsigned color ) {
    	SetConsoleTextAttribute( hstdout, color ); printf( "%d\n", value ); SetConsoleTextAttribute( hstdout, 0x07 ); }
    void undo_linebreak( void ) {
    	CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo( hstdout, &csbi );
    	--csbi.dwCursorPosition.Y; SetConsoleCursorPosition( hstdout, csbi.dwCursorPosition ); }
    int main( void ) {
    	char ch = 0; int sum = 0, op = 0, is_first = 1, num_ops = 0;
    	for( ;; ) {
    		if( ch = getche(), putchar( '\n' ), ch == '1' || ch == '2' ) {
    			if( is_first ) op = ( ch == '1' ? 1 : -1 );
    			else { print_colorful( sum += op + ( ch == '1' ? 1 : - 1 ), 0x0c ); op = 0; } ++num_ops;
    		} else if( ch == '\r' ) { undo_linebreak(); print_colorful( sum += ( is_first ? 0 : op ), 0x0c );
    		} else if( ch == ' ' ) { printf( "\r" ); undo_linebreak(); print_colorful( num_ops, 0x0a ); continue;
    		} else continue; is_first = !is_first;
    	}
    }
    


  • Danke !!!
    Ein Meisterstück 🙂



  • Ne, wahrscheinlich Trollfutter...



  • Ja, das stimmt, aus Sicht derjenigen, die nicht wissen wozu das Programm benutzt wird, dass alles musste ich bis jetzt im Kopf sehr schnell machen, jetzt macht das das Programm, eine echte Erleichterung :).
    Danke



  • In diesem Falle:

    Swordfish expressly disclaims any warranty for the above code snippet. It is provided 'As Is' without any express or implied warranty of any kind, including but not limited to any warranties of merchantability, noninfringement, or fitness of a particular purpose. Swordfish does not warrant or assume responsibility for the accuracy or completeness of any information, text, or other items contained within. Swordfish makes no warranties respecting any harm that may be caused by the transmission of a computer virus, worm, time bomb, logic bomb, or other such computer program. Swordfish further expressly disclaims any warranty or representation to Authorized Users or to any third party.


  • Mod

    Denim schrieb:

    Also ist wohl die Aufgabe von meinen derzeiten Wissenstand nicht lösbar.

    Es muss ja nicht so elegant sein wie bei Swordfish. Ich dachte an folgendes:

    #include <stdio.h>
    int main()
    {
        int sum = 0;
        char choice;
        while (scanf(" %c", &choice) == 1)
        {
            if (choice == '1')
            {
                sum += 1;
                printf("%d\n", sum);
            }
            if (choice == '2')
            {
                sum -= 1;
                printf("%d\n", sum);
            }
        }
        return 0;
    }
    

    Da dürftest du alles verstehen, oder? Die einzig neue Idee ist vielleicht, das scanf(" %c", &choice) == 1 direkt in die while-Bedingung zu packen, du hättest vermutlich eine Endlosschleife mit break oder änhliches gemacht (oder den Rückgabewert gar nicht geprüft?). Dann wäre ich aber nicht ohne Quetschen auf 20 Zeilen gekommen 🙂 (beachte, dass 8 meiner Zeilen nur aus Klammern bestehen).



  • Und, was lernt uns das nun? 😕 Klar, auch eine unsinnige Aufgabe kann man zum Laufen bringen - auch wenn niemand anderes als der Fragesteller damit etwas anfangen kann. Ich bewundere die Mühe der anderen Poster zur Lösung beigetragen zu haben. Solche Denksportprogramme können möglicherweise das Gehirn trainieren und somit Alzheimer aufschieben - super! 🕶


  • Mod

    Was findet ihr eigentlich alle so unsinnig an der Aufgabe? Sie hat eine klare Formulierung, welche Eingabe, welche Ausgabe erzeugt. Sie ist in wenigen Zeilen zu lösen, benötigt aber mehrere Konstrukte zur Ablaufsteuerung und ein paar Überlegungen zu eben diesem Ablauf. Und es gibt sie, zumindest bis jetzt, nicht schon im Internet gelöst. 😋

    Ich hoffe mal, die Farbe und

    Wenn man z.B. die Leertaste drückt, wird angezeigt wie viele Zahlen man bereits eingegeben hat (Enter wird nicht mitgezählt, nur die Zahlen), man kann aber sofort weiter danach weiter Zahlen eingeben, also nur eine kleine Statusinformation, die nichts am Programmablauf verändern soll.

    waren nur Sachen, die sich der Threadersteller selber ausgedacht hat. Die wären wirklich ziemlich sinnlos.



  • Swordfish, YMMD! 😃



  • np 😉


Anmelden zum Antworten