C language_branch and loop statements (3)


Preface

:C语言:结构化的程序设计语言 顺序结构、选择结构、循环结构:


提示:以下是本篇文章正文内容,下面案例可供参考

1. Guess the number game

1.1. The computer randomly generates a number (1~100);

1.2. Guess the number:

a> If you guessed it, you will be reminded that you have guessed it and you can continue guessing.
b> If the guess is too small, please remind me to guess if the guess is too small, please continue guessing.
c>Guessed it right, congratulations, guessed it right, the game is over

1.3. If you are not satisfied after playing a game, you can continue playing without exiting the program.

How to generate random numbers---rand The header file included by rand function is

1.4.The connection between rand and srand

  • Call srand before using rand
  • You only need to call srand once. Do not set srand every time you generate random numbers.
  • rand is to generate random numbers, and srand is to set the starting point of random number generation before generating random numbers.

5.Guess the number game source code

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

void menu()
{
    
    
    // 菜单
    printf("*********************************\n");
    printf("********1. play  0. exit*********\n");
    printf("*********************************\n");

}

void game()
{
    
    
    RAND_MAX;
    // 1.生成随机数
    //rand 函数 可以生成随机数
    int ret = rand()%100+1;  //随机数范围是 0 ~ 32767 
    // rand()%100  得到的余数只可能是小于100 ---  0~99
    //rand()%100+1 随机数范围是 1~100
    //printf("%d\n", ret);
    
    // 2.猜数字
    int guess = 0;
    while (1)
    {
    
    
        printf("请猜数字:>");
        scanf("%d", &guess);
        if (guess > ret)
        {
    
    
            printf("猜大了\n");
        }
        else if (guess < ret)
        {
    
    
            printf("猜小了\n");
        }
        else
        {
    
    
            printf("恭喜!猜对了\n");
            break;
        }
    }
}

int main()
{
    
    
    int input = 0;
    srand((unsigned int)time(NULL)); //要给srand 传递一个变化的值,计算机上的时间是时刻发生变化的
    //time 函数可以返回一个时间戳

    do
    {
    
    
        menu();
        
        printf("请选择:>");
        scanf_s("%d", &input); // 1  0 
        switch (input)
        {
    
    
        case 1:
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("选择错误\n");
            break;
        }
        
    } while (input);

    //打印菜单
    //1.玩游戏
    //0.退出游戏
    return 0;
}


2. go to statement

  • C language provides goto statements that can be abused at will
  • Theoretically, the goto statement is not necessary. In practice, the code can be easily written without the goto statement.
  • However, the goto statement is still useful in some situations. The most common usage is to terminate the processing of the program in some deeply nested structures.

2.1. For example: jump out of two or more layers of loops at one time.

In the case of multi-level loops, using break will not achieve the purpose. It can only exit from the innermost loop to the previous level loop.

2.2. The goto statement cannot span functions.

3. Shutdown procedure

3.1. Start the program and shut down the computer within 1 minute.

3.2. If you enter I am a pig, the shutdown will be cancelled.

3.3 String comparison size strcmp header file <string.h>


  • How to write goto


4. Examples - Printing prime numbers and leap years

4.1.char is a character type

The essence of a character is the ASCII code value of the character. The ASCII code value is an integer, so character types can be divided into integer families when classifying.

4.2. Pointer variables are used to store addresses

Address size 32bit / 64bti
variable size 32 bit - 4 bytes / 64bit - 8 bytes

4.3.Switch statement knowledge points

The default clause in the switch statement can be placed anywhere (√)
The expression after the case in the switch statement can only be an integer constant expression (√)
The case clause in the switch statement must precede the default clause (×)
switch Case expressions in statements do not require order

4.4. Exchange of two integer variables

4.5. Print prime numbers – count++ usage – sqrt function

4.6. Print leap year

  • Judgment rules:
    1. It is divisible by four and not divisible by one hundred.
    2. If it is evenly divisible by four hundred, it is a leap year.

Guess you like

Origin blog.csdn.net/Ghr_C99/article/details/132603715