Chapter IV summary of C language

Keywords: C

 #include<stdio.h>
#include<math.h>
int main()
{
 int a, b, c;
 float area;
 double s;
  printf("please enter 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("area of triangle is:% f\n", area);
 }
 return 0;
}

 

 

#include<stdio.h>
int main()
{
 int x; float y;
  printf("please enter the value of x:");
 scanf_s("%d", &x);
 if (x != 0)
  y = 1.0 / x;
 else
  y = 0;
 printf("%d,%f", x, y);
 return 0;

}

4.2 relational operators, logical operators and conditional operators  

1. Relational operators

be careful:

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

2. The priority of relational operators is lower than arithmetic operators, but higher than 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.

For example, "a + b > = C-D", "x < = 3 / 2", "a '+ 1! = C", "I-5 * J = = K + 1" are all legal relationship expressions

Nesting is allowed in relational expressions, such as "a > (b > C)", "a! = (C = = D)" and so on

be careful:

1. Note that the distinguishing operators "=" and "= =". = "are assignment operators and" = = "are relational operators

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 relational operators is higher than that of assignment operators, the expression "a > b = 0" is equivalent to "(a > b) = 0". The left side of assignment operators is not a variable, and compilation errors will occur

Relational expressions are mainly used to judge conditions in branch structures. The result of relational expressions is a logical value of "true" or "false". Since there is no data of logical type in C language, "1" represents true and "0" represents false.

#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;
}

 

  4.2.2     Logical operators and logical expressions

1. Logical operators

The 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", the value is 0; then calculate "0 < 10", and the value is 1, that is, when X=2, the relationship "4 < x < 10" is satisfied ", which is obviously wrong. Therefore, it is necessary to connect several relational expressions with logical operators to correctly describe the relationship. The logical operators are shown in the table:

 

Note: the and operator & & and or operator 𞓜 are binocular operators, and the non binocular 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

 

#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;
}

 

4.2.3   Conditional operators and conditional expressions  

 

1. Conditional operator

The conditional operator consists of two symbols'? 'and': '. It is used for conditional evaluation. It is a ternary operator and requires three operands. The priority of the conditional operator is lower than that of the logical operator and higher than that of the assignment operator. It is right bound

2. Conditional expression

The formula connecting three expressions by conditional operators is called conditional expression, and its general form is

                    Expression 1? Expression 2: expression 3

#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;
}

#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;
}

 

4.3 implementing branch structure using if else

4.3.1     If else statement

Its general form is as follows

if (expression)

Statement 1;

else

Statement 2;

 

#include<stdio.h>
int main()
{
 int a, b;
 printf("Please enter two integers:");
 scanf_s("%d%d", &a, &b);
 if (a > b) {
  printf("The maximum number of two integers is:%d\n", a);
 }
 else {
  printf("The maximum number of two integers is:%d\n", b);
 }return 0;
}

 

#include<stdio.h>
#include<math.h>
int main()
{
 double x, y;
  printf("value of input x:");
 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;
}#include<stdio.h>
#include<math.h>
int main()
{
 double x, y;
  printf("value of input x:");
 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;
}

#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;
}

 

2. Single branch if statement

Its general form is as follows:

if (expression)

sentence;

 

#include<stdio.h>
int main()
{
 float x;
 scanf_s("%f", &x);
 if (x < 0)
 {
  x = -x;
   printf("%f", x);

 }
 return 0;
}

 

 

#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;

}

#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;
}

 

 

4.3.2   Nesting of if statements

#include<stdio.h>
int main()
{
 int a, b, c, max;
 scanf_s("%d,%d,%d", &a, &b, &c);
 if (a > b) {
  if (a > c)
   max = a;
 }
 else {
  if (b > c)
   max = b;
  else
   max = c;
 }
 printf("max=%d\n", max);
 return 0;

}

 

 

#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;
}

 

 

4.3.3     Multi branch if statement

if (expression 1)

Statement 1;

else if (expression 2)

Statement 2;

.....

else if (expression n-1)

Statement n-1;

else

Statement n;

#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;
}

 

 

#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;

}

 

 

four point four   Using switch statement to implement branch structure

Switch (expression){

case constant expression 1: statement 1; break;

case constant expression 2: Statement 2; break;

....

case constant expression n: statement n; break;

default: statement n+1;break;

}

#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 Month is the fourth quarter\n", month);
  break;
 default:
  printf("Input error\n");
  break;

 }
 return 0;
}

 

 

#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;
}

 

 

#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;
}

 

 

 

#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;
}

 

4.5 example of branch structure programming

 

 

 

 

#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;

}

#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;

}

 

#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;
}

 

 

#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;
}

 

 

Posted by carlos1234 on Mon, 22 Nov 2021 02:45:27 -0800