Branch structure programming
Input the three sides of the triangle to judge whether it can form a triangle. If it can form a triangle, calculate the area of the triangle. If not, it cannot form a triangle.
Example [4.1]
#include<stdio.h> #include<math.h> int main() { int a, b, c; float area; double s; printf("Please enter the length of 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 constitute a tertiary:\n"); else { area = sqrt(s * (s - a) * (s - b) * (s - c)); printf("The area of the triangle is: f\n", area); } return 0; }
The operation results are as follows:
Procedure flow chart:
Example [4.2]
Write a program, input the value of x, calculate and output the previous 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; }
Operation results:
flow chart:
Example [4.3]
Relational operator, logical operator, conditional operator
1. Relational operators
Relational operators are used to determine the size relationship between two operands.
2. Relational expression
A formula that connects two expressions with a relational expression is called a relational expression.
#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; }
Operation results:
flow chart:
Example [4.5]
Logical operator
operator ! && ||
name Logical right and wrong Logic and Logical or
Associativity Right combination Left combination Left combination
priority High to low
code:
#include<stdio.h> int main() { int a = 3, b = 3, c = 2, d; printf("%d,", !a && !b && c); printf("%d,", !a || !b || c); printf("%d\n", a >= b && b > c); d = c-- || (b = 6) && a++; printf("a=%d,b=%d,c=%d,d=%d\n", a, b, c, d); return 0; }
Operation results:
flow chart:
Example [4.6]
Maximum value of judgment number
#include<stdio.h> int main() { int x, y; printf("Please enter two integers:"); scanf_s("%d,%d", &x, &y); printf("The maximum number of two integers is:%d\n", x > y ? x : y); return 0; }
Example [4.8]
Calculate the 1 value of y.
y=f(X)=eX X<=1;
X*x-1 X>1;
#include<stdio.h> #include<math.h> int main() { double x, y; printf("input x Value of:"); scanf_s("%lf", &x); if (x <= 1) y = exp(x); else y = pow(x, 2) - 1; printf("f(%f)=%.2f\n", x, y); return 0; }
Example [ 4.9 ]
Write a program to input a real number arbitrarily and output its absolute value.
#include<stdio.h> int main() { float x; scanf_s("%f", &x); if (x < 0); x = -x; printf("%f", x); return 0; }
Example [ 4.10 ]
#include<stdio.h> int main() { int x, y; printf("x="); scanf_s("%d", &x); if (x) y = 1; else y = -1; printf("y=%d", y); return 0; }
Example [ 4.11 ]
if branch statement
#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 > b) { 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; }
Example [ 4.12 ]
Enter 3 arbitrary integers to find the maximum number.
int main() { int a, b, c, max; scanf_s("%d%d%d", &a, &b, &c); if (a > b) if (a > c) max = a; else max = c; else if (b > c) max = b; else max = c; printf("max=%d\n", max); return 0; }
Example [ 4.13 ]
#include<stdio.h> int main() { int x, y; scanf_s("%d", &x); if (x > 0) y = 1; else if (x == 0) y = 0; else y = -1; printf("y=%d\n", y); return 0; }
Example [ 4.14 ]
Calculation function
#include<stdio.h> #include<math.h> int main() { float x, y; printf("input x Value of:"); scanf_s("%f", &x); if (x > 2) y = 2 * x; else if (x <= 10) y = 7 - 3 * x; else y = 1 - cos(x); printf("y=%0.6f\n", y); return 0; }
Example [ 4.15 ]
Convert lowercase letters to uppercase letters
#include<stdio.h> int main() { char c1, c2; printf("Please enter a character:"); c1 = getchar(); if (c1 >= 'a' && c1 <= 'z') c2 = c1 - 32; else if (c1 >= 'A' && c1 <= 'Z') c2 = c1 + 32; else c2 = c1 + 1; putchar(c2); return 0; }
Example [ 4.16 ]
Using switch statement to implement branch structure
Notes on switch statements:
(1) The expression after switch can be any expression, and its value can only be one of integer type, character type and enumeration type.
(2) The values of constant expressions behind each case are different from each other. Otherwise, there will be contradictions.
(3) The order of occurrence of each case and default does not affect the execution result.
(4) Multiple case s can share a set of execution statements.
(4) switch statements allow nested use.
#include<stdio.h> int main() { int month; printf("Enter month:"); scanf_s("%d", &month); switch ((month - 1) / 3) { case 0: printf("%d Month is the first quarter\n", month); break; case 1: printf("%d Month is the second quarter\n", month); break; case 2: printf("%d Month is the third quarter\n", month); break; case 3: printf("%d April is the fourth quarter\n", month); break; default: printf("Input error\n"); break; } return 0; }
Example [ 4.17 ]
Output the days of the year and month according to the year and month entered on the keyboard.
#include<stdio.h> int main() { int year, month, days; printf("Please enter year, month:"); scanf_s("%d,%d", &year, &month); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) days = 29; else days = 28; break; } printf("%d year%d The number of days in the month is%d day\n", year, month, days); return 0; }
Example [ 4.18 ]
switch Statements
#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; }
Example [4.19]
#include<stdio.h> #include<math.h> int main(void) { 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; }
Example [ 4.20 ]
Judge whether the year is a leap year
#include<stdio.h> int main(void) { 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; }
Nested Type
#include<stdio.h> int main(void) { 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; }
Multi branch format type
#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; }
Example [ 4.21 ]
Input a 3-bit integer from the keyboard to judge whether the number is in ascending order. If the input is not 3 digits, output "Enter error". Ascending refers to the number in which the high-order number is successively smaller than its low-order number. For example, 359 is the ascending order number.
#include<stdio.h> int main() { int n, a, b, c; printf("Please enter a 3-bit integer:"); 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; }