Dr. monkey class C language learning notes play 3

Select statement

<1> if statement

if(expression)
{
.
.
}
else
{
.
.
}

Condition usage: if so, how about it, otherwise

Look at a programming problem first

Input an integer, if the number is greater than 60, then output "Dr. monkey is handsome";
If the number is no more than 60, the output "Dr. monkey is really handsome".

Let's take a look at the fixed format first

#include<stdio.h>
int main()
{


	return 0;
}

Let's set up some conditions, and pay attention to some points here!
<1> If there is only one statement after if and else, you can leave out the braces
<2> else and if are not followed by semicolons!
<3> Each branch statement needs to be wrapped (\ n)

#include<stdio.h>
int main()
{
	int a;	//Consolidate int (integer)  
	scanf("%d",&a);	//Format of scanf statement 
	if(a>60)	//If there is only one statement after if and else, you can leave out the braces 
	{
		printf("Dr. monkey is so handsome\n");
	}
	else	//if and else without semicolon  
	{
		printf("Dr. monkey is really handsome\n");	//All branches need to be followed by \ n (line feed) 
	}
	return 0;
}

Final output format

#include<stdio.h>
int main()
{
	int a;	//Consolidate int (integer)  
	scanf("%d",&a);	//Format of scanf statement 
	if(a>60)	//If there is only one statement after if and else, you can leave out the braces 
	
		printf("Dr. monkey is so handsome\n");
	
	else	//if and else without semicolon  
	
		printf("Dr. monkey is really handsome\n");	//All branches need to be followed by \ n (line feed) 
	
	return 0;
}

Input an integer, if the number is greater than 60, then output "Dr. monkey Shuai".

#include<stdio.h>
int main()
{
	int a;
	scanf("%d",&a);
	if(a>60)
	printf("Dr. monkey is so handsome\n");
	return 0;
 } 

Let's take another look

#include<stdio.h>
int main()
{
	int x,y;	//Integers define two variables x,y with int
	scanf("%d",&x);	//scanf statement
	if(x<0)
	{				//Braces can be omitted if there is only one statement 
		y=-1;
		 }	 
	return 0; 
 } 

Continue after omission

#include<stdio.h>
int main()
{
	int x,y;	//Integers define two variables x,y with int
	scanf("%d",&x);
	if(x<0)
	y=-1;				//Braces can be omitted if there is only one statement 
	else
	{
		if(x>0)		//Insert another if statement 
		y=1;
		else		//There is no semicolon after the if and else statements!
		y=0;
	 } 
	printf("x=%d,y=%d\n",x,y);	//%d (integer) \ n (line feed) 
	return 0; 
 } 

Let's simplify at last

#include<stdio.h>
int main()
{
	int x,y;
	scanf("%d",&x);
	if(x<0)
	y=-1;				//Braces can be omitted if there is only one statement 
	else		//Let's take a closer look at the else statement. When the if condition is met, only one statement will be executed / / all the parentheses can be removed 
		if(x>0)		//So we can remove the brackets 
		y=1;
		else		 
		y=0;	 
	printf("x=%d,y=%d\n",x,y);	//%d (integer) \ n (line feed) 
	return 0; 
 } 

We can also nest if and else in if

#include<stdio.h>
int main()
{
	int x,y;
	scanf("%d",&x);
	if(x>=0)
	if(x>0)
	y=1;
	else
	y=0;
	else
	y=-1;
	printf("x=%d,y=%d\n",x,y);
	return 0; 
 } 

Another question
Input two real numbers a and b, and output them in the order of numerical value from small to large.

#include<stdio.h>	
int main()		 
{
	double a,b,t;	//Note that the topic is real number. Add another variable here 
	scanf("%lf,%lf",&a,&b);
	if(a>b)		//Condition when a > b (subject requirement a < b) 
	{			//If a=5,b=3 
		t=a;	//t=a t=5
		a=b;	//a=b b=3 a=b=3
		b=t;	//b=t b=5
	}
	printf("%lf,%lf\n",a,b);
	return 0;
}

Input three real numbers a, b and c, and output them in the order of numerical value from small to large.

#include<stdio.h>	
int main()		
{
	double a,b,c,d,t;//Because I'm not sure if there's a number with a decimal point, double is used here 
	scanf("%lf,%lf,%lf,%lf",a,b,c,d);//scanf statement double corresponding to% lf forgotten students, please take a look at the previous one 
	if(a>b)//If a > b, suppose a = 5, B = 1 (easy to understand) 
	{
		t=a;//Let t=a, t=5 
		a=b;//a=b,a=1
		b=t;//b=t, because t=5,b=t, so b=5, so a < B, OK, let's continue, Gan! 
	}
	if(a>c)
	{
		t=a; 
		a=c;
		c=t;
	}
	if(a>d)
	{
		t=a;
		a=d;
		d=t;
	}
	if(b>c)//If a > b, b=2,c=1 
	{
		t=b;//t=2
		b=c;//b=c, assign 2 to c,c=2 
		c=t;//c=t,c=2 
	}
	if(b>d)
	{
		t=b;
		b=d;
		d=t;
	}
	if(c>d)
	{
		t=c;
		c=d;
		d=t;
	}
	printf("%lf,%lf,%lf,%lf",a,b,c,d);
	return 0;
 } 

Common expressions
The relationship between one variable and another number

<1> > greater than
<2> > = greater than or equal to
<3> < less than
<4> < = less than or equal to
<5> = = equal to (use as follows)//
Note: a=2, only the value of a is assigned to this number, = = is equal to

#include<stdio.h>
int main()
{
	int x,y;
	scanf("%d",&x);
	if(x<0)
	y=-1;
	else
	if(x==0)	//y=0 when x=0, otherwise y=-1
	y=0;
	else
	y=-1;
	printf("x=%d,y=%d\n",x,y);
	return 0;
}

<6> ! = not equal to
<7> & & satisfied on both sides (example as follows)
To wash and brush your teeth
Key points: meet the requirements of brushing, washing and wearing masks
<8> Both sides of the scrotum satisfy one (example as follows)
Ci Bai wakes up hungry, orders a takeout and cooks by himself
Key point: meet one of them, either cook or order takeout

Connect the two sentences

If you cook by yourself, you need to brush your teeth, wash your teeth and put on a mask to go out and buy food to cook, or you can order a take away at home

If ((brush and wash your teeth & & put on the mask) | order a take out)

Classic example
Please make a program to judge whether a year is a leap year. (Note: if the current year share is not a multiple of 100 and a multiple of 4, the year is a leap year; if the current year share is a multiple of 100 and a multiple of 400, the year is also a leap year.)

When the current year's share is not a multiple of 100 and a multiple of 4, the current year's share is a multiple of 400 (certainly a multiple of 100)

(year% 100! = 0 & & year% 40) | year% 4000

#include<stdio.h>
int main()
{
	int a;	//Integer variable a
	printf("Please enter the year\n");	
	scanf("%d",a);
	if((a%100!=0&&%4==0)||a%400==0)	
		printf("%d Year is leap year\n",a);
	else
		printf("%d Year is not a leap year\n",a);
	return 0;
}

Example 2 input a character to determine whether it is an uppercase letter. If it is, convert it to a lowercase letter. If it is not, do not convert it. Then output the final character.

#include<stdio.h>
int main()
{
	char echo;	//char for letters, echo for assignment variables
	scanf("%c",a);	//scanf statement letter corresponds to% c
	if(echo>='A'&&echo<='Z')	//If the variable satisfies capital A-Z letters
		echo=echo+32	//+32 to lowercase, similarly - 32 to uppercase
	printf("%c\n",echo);	//Output without lowercase, without else
	return 0;
}

Expression 1? Expression 2: expression 3
Judge expression 1. If expression 1 holds, execute expression 2; otherwise, execute expression 3 (similar to if statement)
for instance:
Do you wear a mask? Wear a mask to go out, only at home

#include<stdio.h>
int main()
{
	char echo;	//char for letters, echo for assignment variables
	scanf("%c",a);	//scanf statement letter corresponds to% c
	echo=(echo>='A'&&echo<='Z')?(echo+32):echo	//If the variable satisfies capital A-Z letters
	return 0;		//Just execute echo+32 and make it lowercase, otherwise it will remain unchanged
}

Input two real numbers a and b, and output them in the order of numerical value from small to large.

#include<stdio.h>
int main()
{
	double a,b;
	scanf("%lf,%lf",&a,&b);
	a>b?printf("%lf,%lf\n",b,a):printf("%lf,%lf\n",a,b);
	return 0;
}

switch Statements

Switch (integer variable or string variable) / / check a variable of integer or character type
{
	case constant 1: statement 1;break; / / if the integer or character type variable is constant 1, execute statement 1
	case constant 2: Statement 2;break; / / if the integer or character type variable is constant 1, execute statement 2
	.
	.
	.
	case constant n: statement n;break; / / if the integer or character type variable is constant one, execute statement n
	default: statement n+1;break; / / if none of them are true, execute statement n+1
 }

Grade A, B, C and D are the four grades of a course. Now we need to convert them to the score segment of the hundred point system. The rules are: Grade A is converted to 85-100, grade B is converted to 70-84, grade C is converted to 60-69, and grade D is converted to < 60. Please make a program, grade input by keyboard, output score segment.

#include<stdio.h>
int main
{
	double echo;
	scanf("%f",echo);
	switch(echo)	//Format switch (the variable we want to judge), the variable is generally a letter or an integer
	{
	case'A':printf("The student's score is 85~100\n");break;//Let's write this sentence, and then add a semicolon; add a break; semicolon
	case'B':printf("The student's score is 70~84\n");break;
	case'C':printf("The student's score is 60~69\n");break;
	case'A':printf("The student's score is<60\n");break;
	default:printf("The grade of this student is wrong!\n");break;
	}
	return 0;
}

The grade of a course was originally a, B, C and D. now we need to turn it into the score segment of the hundred point system. The rule is: Grade A, grade B into 70-100, grade C, grade D into < 70. Please make a program, grade input by keyboard, output score segment.

#include<stdio.h>
int main()
{
	char echo;
	scanf("%c",echo);
	switch(echo)
	{
	case'A':	//If the upper and lower results are the same, the upper ones can not be written (remember to write the lower ones)
	case'B':printf("The student's score is 70~100\n");break;
	case'C':	//Remember to wear a colon!:)
	case'D':printf("The student's score is<70\n");break;
	defaul:printf("The grade of the student is not right!\n");break;
	}
	return 0;
}

The former grade system of A certain course is to convert it into A grade. The rules are: A score above 90, B score 80-89, C score 70-79, D score 60-69 and E score below 60. Please make A program, score input by keyboard, output level.

#include<stdio.h>
int main()
{
	double echo;	//For example, 74.5
	scanf("%lf",echo);	//scanf statement double corresponds to% lf
	switch((int)(echo/10))	//For example, chestnut 74/10=7.4 int rounding 7
	{
		case 10:		//More than 90 points is A, so the first sentence can be left blank
		case 9:printf("The student's grade is A\n");break;
		case 8:printf("The student's grade is B\n");break;
		case 7:printf("The student's grade is C\n");break;
		case 6:printf("The student's grade is D\n");break;
		case 5:	//Similarly, the score below 60 is E, so these sentences can not be written
		case 4:
		case 3:
		case 2:
		case 1:
		case 0:printf("The student's grade is E\n");break;
		default:printf("The score entered does not meet the requirements 23333\n");break; //No 120! So the writing does not meet the requirements
	}
	return 0;
}

Come on! Crabs and crabs

Tags: less Programming

Posted on Wed, 10 Jun 2020 23:02:02 -0400 by art15