Data types and operations

Little Madou has just made his first pot of gold. He is considering whether to deposit the money in the bank or invest in stocks, so we set up a program to help him calculate which way is more cost-effective. (As shown below)

#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
    char a[] = "deposit";
    char b[] = "stock investment";
    printf("deposit or stock investment:\n");
    scanf("%s", &a);
    if (strcmp(a,"deposit")==0)
    {
        float m, y, r, i;
        printf("please enter the amount,length of deposit(year),annual rate in the below:\n");
        scanf("%f %f %f", &m, &y, &r);
        i = m * pow(1 + r, y) - m;
        printf("interest = %.2f\n", i);
        return 0;
    }
    else
    {
        int S = 0, S1 = 0, S2 = 0;//总资产,股票资产,现金 
        int m = 0, p = 0, n = 0;//买入股数,入价,交易天数 
        int k = 0, d = 0;//当天交易量,当天股价
        int i = 0;//循环数 
        printf("please enter the number of shares,stock price,trading days in the below:\n");
        scanf("%d%d%d", &m, &p, &n);
        S1 = m * p;//投入成本 
        for (i = 0; i < n; i++)
        {
            printf("please enter the volume of the day,stock price of the day:\n");
            scanf("%d%d", &k, &d);
            m += k;//持有股数
            S1 = m * d;//股票资产
            S2 += -(k * d);//现金 
        }
        S = S1 + S2;
        printf("%d", S);
        return 0;
    }  
    return 0;
}

In this program, the meanings of the two conversion instructions %d and %.2f in the format string passed to the printf function are as follows.

%d =》》displays an integer of decimal int type;

%.2f=》》Displays a double-type floating-point number with 2 digits after the decimal point.

C language defines some operations commonly used in mathematics as standard library functions. When using these operations, just write the corresponding function name and required parameters in the required location in the program, and the system will automatically calculate the results.

Standard math library functions commonly used in C language
Library Functions               Function Description                       Example
abs(x) Find the absolute value of the integer x abs(-2)=2
fabs(x) Find the absolute value of a real number x fabs(-3.14)=3.14
floor(x) Find the largest integer not greater than x (round down) floor(3.14)=3.000000
ceil(x) Find the smallest integer not less than x (round up) ceil(3.14)=4.000000
log(x) Find the natural logarithm of x log(2)=0.693147
exp(x) Find the natural exponent (ex) of x exp(2)=7.389056
pow(x,y) Calculate the value of xy pow(2,5)=32.000000
rand( ) Generate random integers from 0~RAND_MAX rand( )%900+100 generates a three-digit random integer
sqrt(x) Find the square root of x (

sqrt(36)=6.000000

Before using the above C language standard math library functions, you must add instructions to include the math.h header file in the preprocessing instructions section of the program:

#include <math.h>

 In the above program, we changed the program flow through the "if..else" statement. The value of the half-expression of the if statement. If the result is not 0, the corresponding statement is executed. The () in the brackets is to judge the condition. The expression becomes a control expression. The judgment result of the above program on the control expression strcmp(a,"deposit")==0 is the option to select deposit. Only when "deposit" is selected, Only the statements within {} will be executed; and "else" means "otherwise". When the expression does not select "deposit" but selects "stock investment", the statements within {} will be executed.

In addition to the if statement, the branch statements in C language also include loop statements while/for/do while. In the above example, because Xiao Madou needs to calculate the daily trading volume and stock price of the day within the number of investment days, so the for loop statement.

Expression 1

Expression 1 is the initialization part, used to initialize loop variables.

Expression 2

Expression 2 is the conditional judgment part, which is used to judge the termination of the loop.

Expression 3

Expression 3 is the adjustment part, which is used to adjust the loop conditions.


 

Guess you like

Origin blog.csdn.net/m0_72000264/article/details/127473587