This article shows the C language cycle structure and time function usage, for C language learning is a very good reference material. Share for everyone to use for reference. Specifically as follows:
The full instance code is as follows:
/********************************************** * "Beginning C 4th Edition" Notes Codes * * Created by Goopand * * Compile R:GCC 4.7.0 ***********************************************//* Simon games:memory Ability Test */#include <stdio.h >/* for printf (), scanf () function */#include <ctype.h>/* for ToLower () function//#include <st dbool.h>/* for bool Type:true, false */#include <time.h>/* for time () function * * #include <st dlib.h>/* for Rand (), Srand () function */int main (void) {char nextgame = ' Y '; /* Go ahead for another game * * bool correct = false; /* guess correctly */int counter = 0; /* Number of sequences guessed correctly/int seq_len = 0; /* Length of sequence */int input_num = 0; /* Stores an input digit * * time_t seed = 0; /* Seed value for random number sequence * *//int i = 0; /* For loop variable */clock_t Start_cloCK = 0; /* Record start clock time (not second) */int time_taken = 0;
/* Time used to game in seconds/printf ("Here's a Simon game for training memory. \ n");
printf ("\nthe screen'll show a number sequence for 1 second,"); printf ("Then the" sequence'll disappear.
You are are suggested to "" "input the same sequence to pass the game.\n"); printf ("\nnotice this:each digit is seperated by a".
\ n "); printf ("\nnow Press Enter").
Good luck!\n "); scanf ("%c", &nextgame);
Waiting for user ' s input do {correct = true; Counter = 0; /* Successful tries * * Seq_len = 2;
/* Initial length of a digit sequence * * Time_taken = clock (); while (correct) {/* on each third successful try, increase the squence length */Seq_len +
= (counter++% 3 = 0); If "= =" expression is true,returns 1; else returns 0/* Time (NULL) rEturns number of seconds since 1970-01-01 * * seed = time (NULL);
/* Generate and display a sequence of random numbers/srand ((unsigned int) seed); Initialize the seed for rand () function for (int i=0; i<seq_len; i++) printf ("%d", Rand ()
% 10);
Output a random digit and a space/* Wait one second * * Start_clock = clock ();
Returns start Clock Time,clock () is a timer for (; clock ()-start_clock < clocks_per_sec;); CLOCKS_PER_SEC is a const defined in Time.h * now overwrite the digit sequence/printf ("\ r")
; Control char "\ r": Locator back to beginning of the line for (int i=0; i<seq_len; i++) pr
Intf ("");
Two spaces to overwrite the sequence printf ("\ r");
/* Check wether the input sequence is correct * * Srand ((unsigned int) seed); Restart the random sequence for (int i=0; i<seq_len; i++) {scanf ("%d", &in Put_num); Read an input number if (Input_num!= rand ()%) {correct = False
;
Break } printf ("%s\n", correct?)
"Correct!": "Wrong!"); }/* Calculate total time to play the game in seconds/time_taken= (Clock ()-Time_taken)/clocks_per_s
EC;
/* Output The total game score/printf ("\n\nyour score is%d",--counter * 100/time_taken); Fflush (stdin);
Empty the input buffer printf ("\ndo you want to play again (y/n)");
scanf ("%c", &nextgame);
while (ToLower (nextgame) = = ' Y ');
return 0;
}
This article is from a foreign website, for a digital game, interested readers can debug run, understand the principle of the code, deepen the C language cycle domain time function of the understanding.