# Branch programming

### 4.1 cited examples

Input the three sides of the triangle to judge whether it can form a triangle. If so, calculate and output the area of the triangle, otherwise an error prompt will be given.

```#include<stdio.h>
#include<math.h>
int main()
{
int a, b, c;
float area;
double s;
printf("Please enter the three sides of the triangle:");
scanf_s("%d,%d,%d", &a, &b, &c);
s = (a + b + c) / 2.0;
if (a + b <= c || b + c <= a || a + c <= b)
printf("Does not form a triangle\n");
else
{
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("The area of a triangle is: % f\n", area);
}
return 0;
}```

When the length of three sides of the input triangle is: 3,4,5, the result is Note: when the program runs, first input the three sides of the triangle and save them in the three integer variables ab.c respectively, and then judge whether the sum of any two sides is greater than the third side according to the composition conditions of the triangle. If this condition is not satisfied, the three sides cannot form a triangle. If this condition is satisfied, the area of the triangle can be calculated according to the corresponding formula.

Write the program, input the value of x, calculate and output the value of y according to the following piecewise function: ```#include<stdio.h>
int main()
{
int x; float y;
printf("input x Value of:");
scanf_s("%d", &x);
if (x != 0) {
y = 1.0 / x;
}
else {
y = 0;
}
printf("%d,%f", x, y);
return 0;
}```

When the value of input x is 2, the result is When the value of input x is 0, the result is ### 4.2.1 relational operators and expressions

1. Relational operators

Relational operators are used to determine the size relationship between two operands. There are 6 relational operators in C language, as shown in the following table

 operator meaning > greater than >= Greater than or equal to < less than <= Less than or equal to == be equal to != Not equal to

be careful:

(1) Relational operators are binocular operators, and their combination direction is left combination.

(2) Relational operators take precedence over arithmetic operators but over assignment operators.

(3) In relation operators, >, <, > =, < = have the same priority, = == The priority of "> =" is higher than "= =".

2. Relational expression

A formula that connects two expressions with relational operators is called a relational expression. The general form of relational expression is

Expression 1 relational operator expression 2

Function: compare the size of two expressions and return a logical value.

be careful:

(1) Note the distinguishing operators "one" and "= =". "Two" is an assignment operator and "= =" is a relational operator.

(2) Judging the equality of real numbers may not get the correct result. For example, the result of "1.0 / 3 * 3.0 = = 1.0" is 0.

(3) Assignment operators can appear in relational expressions, such as "a > (b = 0)", but cannot be written in the form of "a > b = 0". Because the priority of the relational operator is higher than that of the assignment operator, the expression "a > b = 0" is equivalent to "(a > b) = 0). The left side of the assignment operator is not a variable, and a compilation error will occur.

Relational expressions are mainly used to judge conditions in branch structures. The result of a relational expression is a logical value "true" or "false". Since there is no data of logical type in C language, "1" represents "true" and "0" represents "false". For example, the value of relational expression "(a = 3) > (b = 8)" is 0.

Relational operation example

```#include<stdio.h>
int main()
{
char c = 'm';
int i = 10, j = 20, k = 30;
float x = 13e+5, y = 10.85;
printf("%d,%d,", 'a' + 5 < c, -i - 2 * j >= k + 1);
printf("%d,%d,", 1 < j < 5, x - 5.25 <= x + y);
printf("%d,%d\n", i + j + k == -2 * j, k == j == i + 5);
return 0;
}```

The running result is Note: if we calculate, we will find that the running results are different from those in the book. Our own calculation results are 1,1, 0,1,0,0, because   There are two judgments in the paragraph 1 < J < 5, and C language only judges the previous one   1 < J without calculating the following, so the result is 1

### 4.2.2 logical operators and logical expressions

1. Logical operators

A relational expression can only describe a single condition. For complex composite conditions, such as "x is less than 10 and X is greater than 4", if the relational expression "4 < x < 10" is used to describe it, when x=2, the relational operator is left combined. Therefore, first calculate "4 < x" and its value is 0; then calculate "0 < 10" and its value is 1; that is, when x=2, the relationship "4 < x < 10" is satisfied , it is obviously wrong. At this time, you need to use logical operators to connect thousands of relational expressions to correctly describe the above relationship.

The logical operators are shown in the following table:

 operator ! && || name Logical non Logic and Logical or Associativity Right combination Left combination Left combination first high → low

Note: the and operator & & and or operator 𞓜 are binocular operators, and the non operator! Is a monocular operator.

2. Logical expression

The formula that connects two expressions with logical operators is called logical expression. The general form of logical expression is:

Expression 1 logical operator expression 2

or

Logical operator expression 1

The result of the logical expression is also a logical value "true" or "false", that is, "1" or "0". The truth table of the logical operation is shown in the following table.

 x y !x x&&y x||y 0 0 1 0 0 0 Not 0 1 0 1 Not 0 0 0 0 1 Not 0 Not 0 0 1 1

Description of logical expressions

(1) The data involved in logical operation can be 1 and 0, non-zero value and 0, or any type of data, but in the end, non-0 and 0 are used to judge whether they are "true" or "false".

(2) Assignment operators can also be used in logical expressions, such as "A &. &. (b = 0)", but cannot be written in the form of "A &. &. B = 0". Because the priority of logical operators is higher than that of assignment operators, the expression "A & & B = 0" is equivalent to "(A & & B) = 0". There is no variable on the left of assignment operators, so compilation errors will occur.

(3)C language stipulates that only the minimum number of sub expressions required to determine the value of the whole expression are calculated. That is, in the logical expression composed of several sub expressions, it is calculated from left to right. When the value of one sub expression determines the value of the whole logical expression, the values of the remaining sub expressions on the right will not be calculated thereafter. This is called "short circuit" .

① For logical and (&. &.) operation, if the value of the expression on the left of "&. &." is false, the value of the whole expression can be obtained as false, then the expression on the right of "&. &." will not be evaluated; the value of the expression on the right will be calculated only when the value of the expression on the left of "& &" is true.

② For logical or (|) operation, if the value of the expression on the left of "|" is true, the value of the whole expression can be obtained as true, then the expression on the right of "|" will not be evaluated; the value of the expression on the right will be calculated only when the value of the expression on the left of "|" is false.

Logical operation example

```#include<stdio.h>
#include<math.h>
int main()
{
int expression;
float x, y;
printf("input x Value of:");
scanf_s("%f", &x);
expression = (int)(1 * (x < 2) + 2 * (x >= 2 && x <= 10) + 3 * (x > 10));
switch (expression) {
case 1:
y = 2 * x;
break;
case 2:
y = 7 - 3 * x;
break;
case 3:
y = 1 - cos(x);
break;
}
printf("y=%0.5f\n", y);
return 0;
}``` Write a program, input x, calculate and output the value of the following piecewise function f (x) (two decimal places are reserved). It is required to call sqrt() function to find the square root and pow() function to find the power. Input three real numbers and output them in order from small to large.

```#include<stdio.h>
int main()
{
float a, b, c, t;
scanf_s("%f,%f,%f", &a, &b, &c);
if(a>b)
{
t = a; a = b; b = t;
}
if(a>c)
{
t = a; a = c; c = t;
}
if(b>c)
{
t = b; b = c; c = t;
}
printf("%5.2f,%5.2f,%5.2f\n", a, b, c);
return 0;
}```

Operation results Write a program, input x, calculate and output the value of the following piecewise function f (x) (2 decimal places are reserved), and call pow() function for power ```#include<stdio.h>
#include<math.h>
int main()
{
float x, y;
scanf_s("%f", &x);
if (x > 0) {
y = sqrt(x);
}
else {
y = pow(x, 5) + 2 * x + 1 / x;
}
printf("x=%.2f,y=%.2f\n", x, y);
return 0;
}``` Write a program to judge whether the entered year is a leap year. It is required to use the standard format, nested format and multi branch format of if statement respectively.

1. Standard format implementation

```#include<stdio.h>
int main()
{
int year, leap;
printf("Enter year:");
scanf_s("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
leap = 1;
}
else
{
leap = 0;
}
if (leap)
printf("%d It's a leap year\n", year);
else
printf("%d Not a leap year\n", year);
return 0;
}``` explain:

(1) The variable year indicates the year, and leap, as a "flag variable", is used to indicate whether the input year is a leap year. If it is a leap year, make the value of leap 1. If it is not a leap year, make the value of leap 0. Finally, check the value of leap. If it is 1, it is a leap year, and output the information of "yes country year"; If it is 0, it is not a leap year, and the information "not a leap year" is output.

(2)if(leap) means that if the value of leap is true (1), it can also be written as if(leap!=0).

2. Nested format implementation

```#include<stdio.h>
int main()
{
int year, leap;
printf("Enter year:");
scanf_s("%d", &year);
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
leap = 1;
else
leap = 0;
}
else
leap = 1;
}
else
leap = 0;
if (leap)
printf("%d It's a leap year\n", year);
else
printf("%d Not a leap year\n", year);
return 0;
}``` 3. Implementation of multi branch format

```#include<stdio.h>
int main()
{
int year, leap;
printf("Enter year:");
scanf_s("%d", &year);
if (year % 4 != 0)
leap = 0;
else if (year % 100 != 0)
leap = 1;
else if (year % 400 != 0)
leap = 0;
else
leap = 1;
if (leap)
printf("%d It's a leap year\n", year);
else
printf("%d Not a leap year\n", year);
return 0;
}``` Input a 3-digit integer by the keyboard to judge whether the number is ascending. If the input is not 3 digits, output "Enter error". Ascending refers to the number in which the high-order number is smaller than the other low-order numbers. Enter 359 as ascending order:

```#include<stdio.h>
int main()
{
int n, a, b, c;
scanf_s("%d", &n);
if (n < 100 || n>999)
printf("Input error!\n");
else
{
a = n / 100;
b = n / 10 %10;
c = n % 10;
if (a < b && b < c)
printf("%d Is the ascending ordinal number\n", n);
else
printf("%d Not an ascending number\n", n);
}
return 0;
}```

Enter a three digit integer 234 and the result is: explain:

(1) The expression "n < 100|n > 999" means that the value of variable n is true when it is less than 100 or greater than 999. At this time, n is not 3 digits, which is not in line with the meaning of the question.

(2) The expression "a < b & & b < c" means that when the value of variable a is less than the value of variable b, and the value of variable b is less than the value of variable c, its value is true. At this time, n is an ascending number.

Tags: C

Posted on Tue, 23 Nov 2021 20:46:55 -0500 by py343