C Language Logic Flow

Source: Internet
Author: User
1 Introduction

No matter which programming language, has provided three kinds of basic program flow control statements, respectively is the order, the choice and the cycle, these three kinds of program structure can solve every single thread problem in the life, each kind of algorithm foundation also consists of these three kinds of program flow structure.

The sequential Process Control statement means that the program is executed from the first line of code in the Main method. If there is a call to another function in the main function, it goes to the code block of the called function, the code block that executes the function returns, and then executes the code block of the main function until the end of the program. But a lot of things will not be expected so smooth, in the software development process, debugging error time far more than the encoding time.

and C language is through the function (Java called method) to the reality of complex business logic gradually decomposed into multiple functions to achieve a top-down, step-by-step refinement, structured, modular programming.

The following are examples of structured and function calls that are illustrated by the browser's ability to implement automatic search:

The first step: implement open and browser shutdown functions

#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>/

*
    Open Browser
    @param STR-specified URL address
    @author Tony 18601767221@163.com
    @since 20160606 09:03
/void Open_broswer (char *str) { C10/>shellexecutea (0, "open", str,0,0,3);
}
/*
    Close browser, default is Google
    @author Tony 18601767221@163.com
    @since 20160606 09:04/
void Close_ Broswer () {

    system ("taskkill/f/im Chrome.exe");
}

The second step: using keyboard keys to achieve analog user input keyword search page

#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>/
*
    search keywords
    @param Keywords keyword parameter
    @author Tony 18601767221@163.com
    @since 20160606 09:012
/void Search_key (char  KEYWORDS[50] {//

    traversal of the input character parameters for
    (int i = 0; i < 50;i++) {
        //non-null checksum

    if (keywords[i]!= ' ") {

        char Key_words = Keywords[i];
        keybd_event (key_words, 0, 0, 0);/press keyboard
        keybd_event (key_words, 0, 2, 0);//release keyboard
    }
    //Key input complete and then enter
    enter ();

}

Step three: Write the execution process of the program and the call to the function into the main method

#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
void Main () {
    Open_broswer ("www.baidu.com");
    Sleep (3000);
    Char search_keywords[100] = "TAOBAO";
    Search_key (search_keywords);
    Close_broswer ();
    System ("pause");
}

The choice structure maps to life is the condition judgment, for example when the blind date needs to judge the age, the height, the weight as well as the wealth whether assigns the condition, the college Entrance examination admission Total score conforms to the appointed university's admission fraction and so on.

Circular structure mapping to life is to meet a certain condition has been repeated to do something, think of that 2.1 line of life, every day in the repeated do get up, work, go home, sleep ... 2 selection Structure

The choice structure in C language is realized by IF/ELSE statements and switch statements, in which if/else is suitable for interval judgment and switch is suitable for equivalence judgment. 2.1 If/else

There are four forms of if/else: usually nested or multiple branches in development

Branch Structure Way of expression
Single Branch () if (expression) {block of code}
Two-branch (two choice one) if (expression) {code block}else{code block}
Multiple branches (multiple selection) if (expression) {code block}else if (expression) {code block}else{code block}
Nested branches if (expression) {if (expression) {code block}}else{}

Their calculation process is to determine the expression of the calculation results if it is not 0, this executes the code block statements.
If the code's {} is not written, the statement before the end of the first semicolon does not recommend that the {} of each code block must be added to increase the readability of the program.
Else cannot exist alone, matching the nearest if statement

To use an if statement to find the absolute value of an integer

#define _crt_secure_no_warnings
#include <stdio.h>
#include <stdlib.h>/
*
    Calculate the absolute value of the integer entered
    @author Tony 18601767221@163.com
    @since 20160605 12:09
/void Calc_abs () {

    printf ("Please enter an integer \ n") ;
    int input_num = 0;
    scanf ("%d", &input_num);

    if (input_num<0) {
        input_num *=-1;//negative negative positive
    }

    printf ("The integer you entered is%d, which is worth the absolute value of%d\n", Input_num,input_num) ;
}

If statement combined with _bool type of use case

#define _crt_secure_no_warnings
#include <stdio.h>
#include <stdlib.h>
#include < Stdbool.h>
/
    If single branch structure general form
    @author Tony 18601767221@163.com
    @since 20160605 11:03
*
void If_sample () {

    //Boolean expression
    _bool flag = 3 > 2;

    printf ("Flag =%d\n", flag);
    The content of a block of code that executes an if statement when the value of an if (flag) {//variable or the result of an expression operation is 1 (that is, _bool true) is

        printf ("Execute this statement when Flag==1");
    }

The three ways in which an if and a variable exchange are implemented to output the input of three integers from large to small

#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h> * Implement the three integers that will be entered according to the three methods used if and variable Exchange From large to small output @author Tony 18601767221@163.com @since 20160605 11:09/void sort () {printf ("Enter three integers, separated by commas in English!!!, loss
    into completion after carriage return to end \ n ");
    int one = 0, two = 0, three = 0;
    scanf ("%d,%d,%d", &one, &two, &three);


    printf ("The three integers you entered are%d\t%d\t%d\n", one, two, three);
        if (one<two) {//one and two after the interchange One>two//Exchange two variables with intermediate variable implementation int temp = one;
        one = two;
    two = temp;

    printf ("Results after interchange: one =%d\ttwo=%d\n\n", one,two); 
        if (one<three) {//one and three after the interchange one>three at this time to obtain the maximum//use arithmetic operation to exchange the value of two variables (using multiplication and division can also be implemented) one = one + three;
        three = One-three;
    one = One-three;


    printf ("The result of the interchange: one =%d\tthree=%d\n\n", one, three);
        if (two<three) {//two and three are swapped, the value of the exchange of two variables is two>three//by using XOR or operator two = Two^three;
        three = Two^three; two =Two^three;

    printf ("Results after interchange: Two =%d\tthree=%d\n\n", two, three);

printf ("Three integers according to output from large to small one=%d\ttwo=%d\tthree=%d\n", one,two,three); }

Multiple branches and nested If/esle if/else and nested three-mesh operators are the same, in order to enter the smallest number of three integers as an example.

To enter the minimum number of three integers by using if/esle and nested implementations

/*
    Use the If else implementation to get the minimum number of three integers
    @author Tony 18601767221@163.com
    @since 20160605 08:45/

void If_else _get_min () {


    printf ("Please enter three integers, comma separated in English!!!, enter finish after entering) \ n");
    int one=0, two=0, three=0;
    scanf ("%d,%d,%d", &one,&two,&three);

    printf ("You entered three integers, respectively,%d\t%d\t%d", one,two,three);
    if (one>two) {//one is not the smallest number at this time only need to compare two and thre

        if (two>three) {

            printf ("Smallest integer is%d\n", three);
        }
        else {
            printf ("Smallest integer is%d\n", two);
        }

    else if (one<two) {//two is not the smallest number, you only need to compare one and three


        if (one>three) {

            printf (the smallest integer is%d\n, three);
        }
        else {
            printf ("Smallest integer is%d\n", one);}}}

To enter the minimum number of three integers by using a nested implementation of the three-mesh operator

/*
    Use the three-mesh operator to achieve a minimum value of three integers
    @author Tony 18601767221@163.com
    @since 20160605 08:58/
void Ternary_ Operator_get_min () {

    printf ("Please enter three integers, comma separated in English!!!, enter finish after entering) \ n");
    int one = 0, two = 0, three = 0;
    scanf ("%d,%d,%d", &one, &two, &three);
    printf ("The three integers you entered are%d\t%d\t%d", one, two, three);

    One > two? (two>three?printf ("Minimum value is%d\n", three):p rintf ("Minimum value is%d\n", two)): one<three?printf ("Minimum value is%d\n", one):p rintf (" The minimum value is%d ", three);

}

When using multiple-branched if/else if/else, when the first if (expression) is matched, the remaining expressions are not executed ...

#include <stdio.h>/

*
    multiple If/ele if/else judgment
    @author Tony 18601767221@163.com
    @since 20160605 16:57
/void Get_result_str () {

    int age = 45;//is a young middle-aged or elderly person according to their ages
    if (age>24) {

        printf ("Young people \ \ n ");
    }
    else if (age>=40) {

        printf ("Middle-aged \ n");
    }
    else {

        printf ("Old man \ n");
    }

Program main entry
void Main () {
    get_result_str ();
    GetChar ();

}

Calculates the average score of a group (three points entered) and calculates the number of failing grades (72)

#include <stdio.h>/
* Calculates the average scores of three classes
    , and counts the number of failing subjects
    @author Tony 18601767221@163.com
    @since 20160605 17:26
/
void Calc_avg () {
    printf ("Please enter the results of the three subjects, comma-separated!!!, input completed after entering the end \ n");
    Double Chinese = 0, math = 0, 中文版 = 0;
    scanf ("%lf,%lf,%lf", &chinese, &math, &english);
    printf ("The three subjects you entered were%.1f\t%.1f\t%.1f\n", Chinese, Math, 中文版);

    int failurecount = 0; Define variables to save the failed fraction
    if (chinese<72) {

        failurecount++
    }
    if (math<72) {

         failurecount++;
     }
     if (english<72) {
         failurecount++;
     }

     Double Avg_score = (Chinese + math + 中文版)/3;

     printf ("The number of failed subjects is%d, the average is%.1f \ n", Failurecount,avg_score);
}
2.2 Switch

Switch appears to replace multiple If/else If/else (code is prone to bugs after nesting too many), switch can only be used as an integer equivalence, its basic form is as follows:

switch (expression) {
    case  constants_value: Break
        ;
    Case Constants_value: Break
        ;
    Default: Break
        ;
}

Or the result of an expression operation can only be an integer or an integer-compatible type of data such as a character, enumeration), the constant values in the case cannot be equal, the case cannot handle real numbers, variable expressions, and relational expressions, and the break must be added (if not added, each of which will execute). The default statement, equivalent to multiple if/else if/else else statements, is executed until a break is encountered (this enables multiple branches to be chosen), indicating that the current switch statement is out of the way, and if all case mismatches are not matched.

Calculate the size of the dress according to the height of the input

#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h>/* The size of the dress is judged by the incoming height @author Tony.
    18601767221@163.com @since 20160606 10:44/void Get_size_by_height () {//define a mens size enum enum clothessize

    {XS = 160, M = 170, L = 175, XL = 180, Xxml = 185, xxxl = 190};
    int height=0;
    printf ("Please enter your height for example 160 170 175 180 185\n");

    scanf ("%d", &height);
        Switch (height) {case xs:printf ("The size of your dress is xs, height is%d", XS);

    Break
        Case m:printf ("The size of your dress is m, height is%d", m);
    Break
        Case l:printf ("The size of your dress is L, height is%d", l);
    Break
        Case xl:printf ("The size of your dress is XL, height is%d", XL);
    Break
        Case xxml:printf ("The size of your dress is xxml, height is%d", xxml); 
    Break
        Case xxxl:printf ("The size of your dress is XXXL, height is%d", XXXL);
    Break
        default:printf ("The size of your dress is beyond the range of normal people");
    Break }
}

The season in which the month is calculated based on the input

/*
    According to the number of input to judge the season
    @author Tony 18601767221@163.com
    @since 20160606 23:05/
void Get_reason_by_ Number () {

    int number = 0;
    printf ("Please enter a number \ n");
    scanf ("%d", &number);

    Switch (number) {Case 1: Case
    2:
        printf ("It's winter \ n");
        Break A break will jump out of the switch code block case
    3, Case
    4: Case
    5:
        printf ("Now spring \ n");
        break; 
    Case 6:
    Box 7: Case
    8:
        printf ("It's summer \ \ n");
        break;
    Case 9: Case
    :
        printf ("It's winter \ n");
        break;
    Default:
        printf ("You entered the wrong number \ n");
        break;
    
3 Circulation Structure

C language (later c++,java,c# and other programming languages) provides three kinds of looping structures: While,do/while and for Loops, where the while and for loops are commonly used in development. It also provides a loop-interrupted statement break,continue and Goto. The cyclic structure consists of two parts: cyclic condition and loop body, the cyclic condition is usually the number of times to control the loop execution, and the loop body is usually mixed with if/else statements and previously learned operators and expressions. 3.1 While Loop

While loops are often used to determine whether an expression is valid (the result is a 0 o'clock-end loop, and non-0 repeats the contents of the loop body), while using a while loop requires attention to the logic of the program to prevent the occurrence of a dead loop.

Use a while loop to eat memory

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>


*
    dead cycle eat memory
        32-bit system when a process can only occupy 2G of memory space
    @author Tony 18601767221@163.com
    @since 20160606
20:18
/void Eat_memory () {

    while (1)//Loop judgment condition: 0 for false, non 0 for true 
    {
        //Loop body
        malloc (10*1024*1024);//bytes Smallest unit here is 10M Sleep
        (1000);
    }

Open 5 Notepad asynchronously using a while loop

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>/
*
    Loop Open Notepad
    @author Tony 18601767221@163.com
    @since 20160607 20:24
/void Open_notepad () {

    int i = 0;< C25/>while (i++ < 5) {

        system ("Start Notepad");//Open 5 Notepad} asynchronously

Use the while loop to calculate integers between 1-100 and

#inlcude <stdio.h>
    /* find integers within 1-100 and
    @author Tony 18601767221@163.com
    @since 20160607 20:30
*
/void Get_sum () {

    int sum = 0;
    int i = 1;
    while (i<=100)
    {
        sum = i;
        i++;
    }

    printf ("Integers within 1-100 and for%d\n", sum);
}

Using the while loop to compute the result of the N-second side of 2

#include <stdio.h>///
*
    calculates the results of the N-second side of 2
    @author Tony 18601767221@163.com
    @since 20160607 20:35
*/
void calc_2n () {
    int num = 0;//cyclic interrupt condition
    scanf ("%d", &num);
    int result = 1;
    int i = 0; Cyclic initial conditions
    (i<num) {result
        *= 2;//each time multiplied by 2 to know the number of times the input is reached
        i++;
    }

    printf ("The result of%d times of 2 is%d\n", num,result);
}

Using a while loop to implement the Windows function to flirt with QQ
1. Use the Spy tool to obtain QQ window information:

2 programming to realize mobile QQ window

#include <stdlib.h> #include <stdio.h> #include <Windows.h>/* Close QQ @author Tony 18601767221@16
    3.com @since 20160607 21:04 */void Close_qq () {System ("taskkill/f/im QQ.exe");
Sleep (1000); * * Open QQ @author Tony 18601767221@163.com @since 20160607 21:08/void Open_qq () {shellexecutea (0, "Op
En "," \ "C:/Program Files (x86)/tencent/qq/bin/qqsclauncher.exe\" ", 0,0,0); }/* Mobile QQ window @author Tony 1860187221@163.com @since 20160607 21:17/void Move_qq () {HWND win = Findwin DowA ("Txguifoundation", "QQ");
    Get QQ Window object if (Win = NULL) {printf ("QQ window is playing missing"); //Get screen resolution can be entered in cmd desk.cpl get my Computer resolution is 1440*900 int i = 0; 
        Ordinate while (i<900) {SetWindowPos (win, NULL, I * 14/9, I, 400, 400, 0);/Set window size and window position sleep (50);
    i + 10;
    } void Main () {close_qq ();
    Sleep (2000);
    OPEN_QQ ();
    Sleep (2000);
    MOVE_QQ ();
System ("pause"); }

Improve the function of MOVE_QQ, add a if/else judge to realize the QQ move and if the shadow if now

*
    QQ looming
    @author Tony 18601767221@163.com
    @since 20160607 21:22
/void Move_qq () {

    HWND win = Findwindowa ("Txguifoundation", "QQ"); Get QQ Window object

    if (win = NULL) {

        printf ("QQ window is playing missing");
    }

    Get screen resolution  enter desk.cpl in cmd to get  My Computer resolution is 1440*900

    int i = 0;//Ordinate while

    (i<900) {

        SetWindowPos (Win, NULL, I * 14/9, I, 400, 400, 0)//Set the window size and the window sleep
        (50);
        

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.