Beginner C language

Source: Internet
Author: User

C language, I take you to get started ...

It doesn't matter if you don't get started ... Do not think that learning C will be a lot of math knowledge!
Most important, you have to understand how fun C is.

Make a < guess number > Game ...
The rules of the game refer to the guesses in the text of the song star:

Enter four in non-repeating numbers (separated by spaces) Press < Enter > after the results are displayed behind,
In the form of "? A? B ", the positions and numbers in the four numbers are correctly A, the numbers are in the same position as B,
When 4 A 0 B is guessed, each inning has 8 chances.

such as: The original four number is 1234, then
1234 4 A 0 B
5678 0 A 0 B not a pair.
1243 2 A 2 B 3,4 although right but position wrong.

Understand the rules, it is easy to start, maybe you have no clue, it's okay, step by step.

1. First understand what your game is going to do?
Four numbers are entered by the player to verify the relationship between the four numbers and the correct answer, providing a "x a x B"
Prompt to the player, and then enter ... Until the player guesses or arrives eight times.

2. What resources does the game need?
This game doesn't need anything special, just a printf, and scanf get input.

So the beginning of the program:

#include <stdio.h>
Add standard input and output without it turbo C doesn't know where printf and scanf come from.

Well, the programming begins ...

Main () {
}

Write this function first, this is called the main function, a program is only one, also known as the program entry function,
That's where your program starts. (Is it a bit of crap?)

(The following operations are all in main ())
Set four variables to store the correct answer: (can also be an initial value)
int a1,a2,a3,a4;
Then set the input of four variables to save the player:
int b1,b2,b3,b4;
Sets the number of times a variable is stored and guesses the initial value:
int n = 0;
Set the number of two variables to be stored as a, A and B:
int CA,CB;

Prepare to complete and set the correct answer:
a1 = 3;
A2 = 5;
A3 = 9;
a4 = 7;
(Of course you will complain that the correct answer is fixed, it does not matter, the following will show you how to produce a random effect, slowly.)

Think about the whole game process ...
This is probably the case:

Loop execution:
Input
Compare the correct answer;
Output hint;
Judge whether to guess right;
is: Show victory and exit the loop, no: ignore;
Number plus 1;
Whether the number of judgments exceeded;
Yes: Display failure and exit loop;
End of cycle;

So, to make it easier to understand, we use a dead loop, but note that there must be statements that make the loop end!
break; This is the statement.

So:

while (1) {
...
Break
...
}

Just write it down. while (XX) {YYYY} works like this:
XX is not 0 (true)
No:
Executive YYYY
Yes: Exit loop
Re-check if XX is not 0
...
But you have to be clear: break; the loop is forced to exit.

OK, that's it:

printf ("\n\t Guess numbers!\n\n"); /* Print a little message */
while (1) {/* Starts the user's input and program output */
printf ("%d:", n+1); /* Print Current is the first several times */
scanf ("%d%d%d%d", &b1,&b2,&b3,&b3);
*/* First don't know why to b1,b2 this before add &,, first think, scanf will be like this * *
ca=0;
cb=0; /* Compare to start, first put the results 0*/

/* Compare to start, we use a bunch of if to do things for us * *
* * Although this is a little cumbersome, but who call us a beginner? But tell you, this expression is the highest efficiency.
if (a1 = = B1) ca++;
if (a2 = = b2) ca++;
if (a3 = = B3) ca++;
if (a4 = = b4) ca++; /* Check to increase the number of a individually.

if (a1 = = B2 | | a1 = = B3 | | a1 = b4) cb++;
if (a2 = = B1 | | a2 = = B3 | | a2 = b4) cb++;
if (a3 = = B1 | | a3 = = B2 | | a3 = b4) cb++;
if (a4 = = B1 | | a4 = = B2 | | a4 = b3) cb++;
/* Poor lift Check to get the number of B */
* * Of course, you must ensure that the player input is not the same, or will make a joke * *
/* Check comparison end */

/* Print Tips */
printf ("\t\t%d A%d b\n", CA,CB);
/* Determine if Victory */
if (CA = = 4) {
printf ("\n\twell done!\n");
Getch (); The function of/*getch () is to enter a character that does not require a carriage return and does not display to the screen */
/* Usually used to pause the program */
Break /* Exit Loop */
}
/* Number plus one */
n++;
/* Number of judgements */
if (n > 7) {
printf ("\nyou lose!\n");
Getch ();
Break
}
}

The entire game process is complete.

Make a clean look:

/*guess.c*/
#include <stdio.h>
Main () {
int a1 = 0,A2 = 0,a3 = 0,a4 = 0;
int B1 = 0,B2 = 0,b3 = 0,B4 = 0;
int n = 0;
int CA,CB;

a1 = 3;
A2 = 5;
A3 = 9;
a4 = 7;

printf ("\n\t Guess numbers!\n\n");
while (1) {
printf ("%d:", n+1);
scanf ("%d%d%d%d", &B1,&B2,&B3,&B4);
ca=0;
cb=0;

if (a1 = = B1) ca++;
if (a2 = = b2) ca++;
if (a3 = = B3) ca++;
if (a4 = = b4) ca++;

if (a1 = = B2 | | a1 = = B3 | | a1 = b4) cb++;
if (a2 = = B1 | | a2 = = B3 | | a2 = b4) cb++;
if (a3 = = B1 | | a3 = = B2 | | a3 = b4) cb++;
if (a4 = = B1 | | a4 = = B2 | | a4 = b3) cb++;

printf ("\t\t%d A%d b\n", CA,CB);

if (CA = = 4) {
printf ("\n\twell done!\n");
Getch ();
Break
}
n++;
if (n > 7) {
printf ("\nyou lose!\n");
Getch ();
Break
}
}
}

Such a simple guessing number game is made.
But you may not be proud of it, because it is not a game to set the right answer, you say?
In addition, we also need to verify that the player's input is not consistent with the specified "non-repetition" and "not out of bounds."
So we're going to give this game a touch of Polish.

(The following section is a bit more C-language knowledge, such as simple arrays)
First: Get random, non-repetitive answers.

There are two predefined functions in the C standard library stdlib:
Randomize () resets the random generator.
the int random (num) generates a random number from 0 to num-1.

All right, do what you say!
But the problem is coming, how to produce it?
We can't guarantee that the four numbers that are randomly generated are different.
But you might think that every time you randomly generate a number until it's different from the one you're already in, isn't it?
This is really a good idea, and it's not a problem to use here.
But this idea does not accord with the idea of program design ... We are not sure when this process will be completed,
Because there may not be "born" in three minutes with other different numbers, although the possibility is almost impossible ...
Also, what if you want to pick 50 million out of 100 million numbers? Obviously this is not feasible ...

The following shows a new algorithm:

1. Create an array of 0,1,2,..., 9. (10 elements)
2. Scatter this number.
3. Take the first four of the array (others can, but fixed)

Scattered arrays can be expressed like this:
Randomly get the two positions of the array and then swap. Of course do one or two times effect is not obvious, we make it do 500 times or more
"It looks very irregular".

Here's the code:

#include <stdlib.h>/* To use the random occurrence function */

In Main ():

int arr[10]={0,1,2,3,4,5,6,7,8,9); /* Declares an array, used for 22 exchange */
int i,index1,index2; /* One for The loop count, index1,2 is used to save the resulting two randomly generated array locations */
int temp; /* Declare temporary variables, you will appreciate the role of temporary variables when exchanging two numbers */

Randomize (); /* Reset the random generator, you can try without this sentence will have what results * *
for (i=0;i<500;i++) {/* do 500 exchanges */
Index1=random (10);
Index2=random (10); /* Randomly generate 0~9, just match the array subscript maximum small value */
TEMP=ARR[INDEX1]; /* Save the value of ARR[INDEX1] to temp */
ARR[INDEX1]=ARR[INDEX2]; /* put ARR[INDEX2] in arr[index1] */
Arr[index2]=temp; /* Remove the value of the original ARR[INDEX1] from temp to [index2]*/
}

A1=ARR[0];
A2=ARR[1];
A3=ARR[2];
A4=ARR[3]; /* Remove the top four as the answer to the question, of course not necessarily the first four */

........
The following addresses the player's "non-compliance" issue.

The player's input is sometimes unpredictable, that is, we have to detect and let the player lose it when there is an error.
If you re-lose, you can go beyond this cycle and proceed directly to the next layer of loop without going down, because we're using a dead loop.
He is not afraid of the cycle will run out, and the number itself will not be changed.

First, give B1~B4 before the player inputs, re-assign the value, make it illegal.
b1=11;
b2=11;
b3=11;
b4=11;

After the player has entered:
(There is also the use of the poor lifting method, of course, there are other ways, I think)

if (b1==b2 | | b1==b3 | | b1==b4 | | b2==b3 | | b2==b4 | | b3==b4) {/* Four number 22 compare six times */
printf ("Input error!\n");
Continue
}
if (b1<0 | | b1>9 | | b2<0 | | b2>9 | | b3<0 | | b3>9 | | b4<0 | | b4>9) {/* Don't bother with such a long if*/
printf ("Input error!\n");
Continue
}

In this case, our code is like this:


/*guess.c*/
#include <stdio.h>
#include <stdlib.h>
Main () {
int a1 = 0,A2 = 0,a3 = 0,a4 = 0;
int B1 = 0,B2 = 0,b3 = 0,B4 = 0;
int n = 0;

int CA,CB;
int arr[10]={0,1,2,3,4,5,6,7,8,9};
int i,index1,index2;
int temp;

Randomize ();
for (i=0;i<500;i++) {
Index1=random (10);
Index2=random (10);
TEMP=ARR[INDEX1];
ARR[INDEX1]=ARR[INDEX2];
Arr[index2]=temp;
}

A1=ARR[0];
A2=ARR[1];
A3=ARR[2];
A4=ARR[3];

printf ("\n\t Guess numbers!\n\n");
while (1) {
printf ("%d:", n+1);

b1=11;
b2=11;
b3=11;
b4=11;

scanf ("%d%d%d%d", &B1,&B2,&B3,&B4);

if (b1==b2 | | b1==b3 | | b1==b4 | | b2==b3 | | b2==b4 | | b3==b4) {
printf ("Input error!\n");
Continue
}
if (b1<0 | | b1>9 | | b2<0 | | b2>9 | | b3<0 | | b3>9 | | b4<0 | | b4>9) {
printf ("Input error!\n");
Continue
}

ca=0;
cb=0;

if (a1 = = B1) ca++;
if (a2 = = b2) ca++;
if (a3 = = B3) ca++;
if (a4 = = b4) ca++;

if (a1 = = B2 | | a1 = = B3 | | a1 = b4) cb++;
if (a2 = = B1 | | a2 = = B3 | | a2 = b4) cb++;
if (a3 = = B1 | | a3 = = B2 | | a3 = b4) cb++;
if (a4 = = B1 | | a4 = = B2 | | a4 = b3) cb++;

printf ("\t\t%d A%d b\n", CA,CB);

if (CA = = 4) {
printf ("\n\twell done!\n");
Getch ();
Break
}
n++;
if (n > 7) {
printf ("\nyou lose!\n");
Getch ();
Break
}
}
}

Beginner C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.