timer



  • Hallo,
    ich habe versucht einen timer_2 zu progen, welcher nach 5 Sekunden einen Zufallsbuchstaben auf einem Label ausgeben soll.
    Wer kann mir helfen, das ist die letzte Aufgabe in meinem Fernstudium,juchuuuuuu 😉
    Danke!!!!!

    class __Form1_declspec Form1 : public __Form1_Base
    {

    public:       // add your public instance data here
        private:      // add your private instance data here
    
            clock_t                         startZeit, stopZeit;
    
            WULong                          timerTicks,timer2Ticks ;
            WDouble                         warte;
        protected:    // add your protected instance data here
    
    };
    
    // Code added here will be included at the top of the .CPP file
    
    //  Include definitions for resources.
    #include "WRes.h"
    
    Form1::Form1()
    {
    }
    
    Form1::~Form1()
    {
    }
    
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    WBool Form1::Schaltflaeche_1_Click(
        WObject *           source,
        WEventData *        event )
    {
        WString                  sBuchstabe;
        char                     cBuchstabe;
        WDouble                  zufall;
    
        label_3->SetText( "" );
        label_13->SetText( "" );
    
        timerTicks = 0;
        timer_1->Start( 100,0 );
        startZeit = clock();
    
        srand (clock());
        zufall = rand();
        cBuchstabe = ((int) zufall % 26);
        cBuchstabe = cBuchstabe + 'a';
        sBuchstabe.Sprintf("%c", cBuchstabe);
        label_4->SetText( sBuchstabe);
    
        Textfeld_1->SetText( "" );
        Textfeld_1->SetFocus( );
    
        return FALSE;
    }
    
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    WBool Form1::timer_1_Timer(
        WObject *               source,
        WTimerEventData *       event )
    {
         timerTicks++;         // Zaehler bei jedem Timer-Ereignis inkrementieren
        return FALSE;
    }
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    /*                Reagieren auf eine Aenderung des Textfeld-Inhaltes       */
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    WBool Form1::Textfeld_1_Change(
        WObject *           source,
        WEventData *        event )
    {
        WString                         text1, text2;
        WString                         string_diffZeit;
        WDouble                         diffZeit;
    
        text1 = Textfeld_1->GetText();  
        if (text1 == "")                
        {
           return FALSE;               
        }
       text2 = label_4->GetText();      // Entspricht der Inhalt des Textfeldes  
        if (text1 == text2)              // dem im Label 4?
        {                                // Ja!
    
          timer_1->Stop();
          stopZeit = clock();
    
          diffZeit = (stopZeit - startZeit);
          diffZeit = diffZeit / CLOCKS_PER_SEC;
    
          string_diffZeit.Sprintf("%4.2f",diffZeit);
          label_3->SetText( string_diffZeit );
    
          diffZeit = timerTicks / 10.0;
    
          string_diffZeit.Sprintf("%4.2f",diffZeit);
          label_13->SetText( string_diffZeit );
    
        }
        else                             // Entspricht der Inhalt des Textfeldes  
        {                                // dem im Label 4?
            Textfeld_1->SetText( "" );   // Nein: Text löschen
        }
    
        return FALSE;
    }
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    WBool Form1::timer_2_Timer(
    	WObject *        		source,
    	WTimerEventData *		event )
    {
    
        WString                  sBuchstabe;
        char                     cBuchstabe;
        WDouble                  zufall;
     WDouble                         diffZeit;
         timer2Ticks++;
    
        if (timer2Ticks > warte)
        {
        timerTicks = 0;
        timer_1->Start( 100,0 );
        startZeit = clock();
    
        srand (clock());
        zufall = rand();
        cBuchstabe = ((int) zufall % 26);
        cBuchstabe = cBuchstabe + 'a';
        sBuchstabe.Sprintf("%c", cBuchstabe);
    
        diffZeit = timerTicks / 10000000.0;
        label_4->SetText( sBuchstabe);
    
        Textfeld_1->SetText( "" );
        Textfeld_1->SetFocus( );
    
        timer_2->Stop();
        timer2Ticks = 0;
        }
        return FALSE;
    

    }



  • Und? Wo ist nun dein Problem? (außer daß du timer_1 mit einem Intervall von 0 startest?)



  • CStoll schrieb:

    Und? Wo ist nun dein Problem? (außer daß du timer_1 mit einem Intervall von 0 startest?)

    Hallo,

    der Zufallsbuchstabe soll auf Label_4 ausgegeben werden, mit einer Verzögerung von 5 Sekunden und dafür benötige ich den timer_2.
    Was meinst Du "timer_1 mit einem Intervall von 0 startest"
    Ausgegeben wird ja ein Zufallsbuchstabe schon nur jetzt soll er eben mit einer Verzögerung von 5 Sekunden ausgegeben werden.
    Alles Klar!!!!! 😕



  • Renate schrieb:

    der Zufallsbuchstabe soll auf Label_4 ausgegeben werden, mit einer Verzögerung von 5 Sekunden und dafür benötige ich den timer_2.

    Kann sein, daß ich es übersehen habe - aber wer startet eigentlich den timer_2?

    (ich kenne mich mit der Timer-Programmierung deines Systems (btw, was für ein Compiler ist das überhaupt) nicht so gut aus, aber ich tippe darauf, daß timer_2_Timer() aufgerufen wird, wenn er bei 0 ankommt)

    Was meinst Du "timer_1 mit einem Intervall von 0 startest"

    Das hier:

    timer_1->Start( 100,0 );
    

    Du verwendest dort den Komma-Operator (ich vermute, das soll eigentlich ein Dezimalpunkt werden).



  • Hallo danke,

    jetzt funkts, anbei der code jetzt passts

    VIELEN DANK!!!! 👍 😉 🙂

    // Declarations added here will be included at the top of the .HPP file

    class __Form1_declspec Form1 : public __Form1_Base
    {

    public: // add your public instance data here
    private: // add your private instance data here

    clock_t startZeit, stopZeit;

    WULong timerTicks,timer2Ticks ;
    WDouble warte;
    protected: // add your protected instance data here

    };

    // Code added here will be included at the top of the .CPP file

    // Include definitions for resources.
    #include "WRes.h"

    Form1::Form1()
    {
    }

    Form1::~Form1()
    {
    }

    /@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/

    WBool Form1::Schaltflaeche_1_Click(
    WObject * source,
    WEventData * event )
    {
    label_3->SetText( "" );
    label_13->SetText( "" );
    label_4->SetText( "" );
    Textfeld_1->SetText( "" );

    srand (clock());
    warte = int(rand() %40);
    warte = (warte + 10);

    timer_2->Start( 100,0 );

    return FALSE;
    }

    /@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/

    WBool Form1::timer_1_Timer(
    WObject * source,
    WTimerEventData * event )
    {
    timerTicks++; // Zaehler bei jedem Timer-Ereignis inkrementieren
    return FALSE;
    }
    /@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/
    /* Reagieren auf eine Aenderung des Textfeld-Inhaltes /
    /
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

    WBool Form1::Textfeld_1_Change(
    WObject * source,
    WEventData * event )
    {
    WString text1, text2;
    WString string_diffZeit;
    WDouble diffZeit;

    text1 = Textfeld_1->GetText();
    if (text1 == "")
    {
    return FALSE;
    }
    text2 = label_4->GetText(); // Inhalt des Textfeld
    if (text1 == text2) // gleich Label 4?
    { // Ja!
    timer_1->Stop();
    stopZeit = clock();

    diffZeit = (stopZeit - startZeit);
    diffZeit = diffZeit / CLOCKS_PER_SEC;

    string_diffZeit.Sprintf("%4.2f",diffZeit);
    label_3->SetText( string_diffZeit );

    diffZeit = timerTicks / 10.0;

    string_diffZeit.Sprintf("%4.2f",diffZeit);
    label_13->SetText( string_diffZeit );
    }
    else // Inhalt des Textfeld
    { // gleich Label 4?
    Textfeld_1->SetText( "" ); // Nein: Text löschen
    }

    return FALSE;
    }
    /@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/

    WBool Form1::timer_2_Timer(
    WObject * source,
    WTimerEventData * event )
    {

    WString sBuchstabe;
    char cBuchstabe;
    WDouble zufall;

    timer2Ticks++;

    if (timer2Ticks > warte)
    {
    timerTicks = 0;
    timer_1->Start( 100,0 );
    startZeit = clock();

    srand (clock());
    zufall = rand();
    cBuchstabe = ((int) zufall % 26);
    cBuchstabe = cBuchstabe + 'a';
    sBuchstabe.Sprintf("%c", cBuchstabe);
    label_4->SetText( sBuchstabe);

    Textfeld_1->SetText( "" );
    Textfeld_1->SetFocus( );

    timer_2->Stop();
    timer2Ticks = 0;
    }
    return FALSE; }
    /@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/


Anmelden zum Antworten