Basic C language day4

Posted by techgearfree on Wed, 23 Feb 2022 12:37:15 +0100

review

Conditional operator

Expression 1? Expression 2: expression 3

Expression 1 is true and the result is the result of expression 2; Otherwise, it is the result of expression 3.

sizeof() operator

The memory length of calculation types and variables, in bytes

int 4

char 1

float 4

short 2

double 8

++Self increasing -- self decreasing

The variable itself is + 1 or - 1.

The result of prefix and suffix expressions is different.

Prefix: the result of the expression is the value after the variable + 1 or - 1.

Suffix: the result of the expression is the value before the variable + 1 or - 1.

Short circuit characteristics

&& ||

&&The left is false and the right is not executed.

||The left is true and the right is not executed.

Switch case structure

Switch (expression) / / the result of the expression must be integer or character.

{

case value (constant): / / the value is the possible result of the expression.

You can write countless case s

Default: when no case is matched, default will be executed and can be omitted.

}

When a case is matched, it will be executed from this case until it meets break or switch.

while Loop

While (expression)

{

Circulatory body

}

for loop

For (expression 1; expression 2; expression 3)

Circulatory body

task

The user keeps inputting numbers and prints each number until it is 0

#include <stdio.h>

int main()
{
	int input;
	while(1)
	{
		scanf("%d", &input);
		if(input == 0)
		{
			break;
		}
		printf("%d\n", input);
	}
	return 0;
}

task

Output the following graphics Requirement: output only one character at a time.

*****

#include <stdio.h>

int main()
{
	int i;
	//Loop variables are used to starting from 0
	//We must be clear about the process of code execution. Don't take it for granted
	for(i = 0;i < 5;i++)
	{
		printf("*");
	}
	//When the cycle is completed, five copies have been printed*
	printf("\n");
	return 0;
}

task

Export the following graphics. Requirement: output only one character at a time.

*****

*****

*****

#include <stdio.h>

int main()
{
	int i;
	int line;
	for(line = 0;line < 3;line++)
	{
		//Loop variables are used to starting from 0
		//We must be clear about the process of code execution. Don't take it for granted
		for(i = 0;i < 5;i++)
		{
			printf("*");
		}
		//When the cycle is completed, five copies have been printed*
		printf("\n");
	}
	return 0;
}

task

Export the following graphics. Requirement: output only one character at a time.

*

**

***

****

#include <stdio.h>

int main()
{
	int i;
	int line;
	//int len = 1;
	for(line = 0;line < 4;line++)
	{
		//Loop variables are used to starting from 0
		//We must be clear about the process of code execution. Don't take it for granted
		for(i = 0;i < line+1;i++)
		{
			printf("*");
		}
		//When the cycle is completed, five copies have been printed*
		printf("\n");
		//len++;
	}
	return 0;
}

task

Enter the student's grade, add a cycle in the program, and you can repeatedly enter the grade to view the grade

90-100 points print A

80-89 points print B

70-79 points Print C

60-69 print D

Other failures

#include <stdio.h>

int main()
{
	int score;
	while(1)
	{
		scanf("%d", &score);
		if(score<0 || score>100)
		{
			printf("Input error\n");
		}
		else if(score >= 90)
		{
			printf("A\n");
		}
		else if(score >= 80)
		{
			printf("B\n");
		}
		else if(score >= 70)
		{
			printf("C\n");
		}
		else if(score >= 60)
		{
			printf("D\n");
		}
		else 
		{
			printf("fail,\n");
		}
	}
	return 0;
}

task

Realize the input of a character, and use the cycle to repeatedly input judgment to judge whether the character is a number. If it is not a number, whether it is an uppercase character or a lowercase character

#include <stdio.h>

int main()
{
	char input;
	while(1)
	{
		scanf("%c", &input);
		if(input>='0' && input<='9')
		{
			printf("number\n");
		}
		else if(input>='a' && input<='z')
		{
			printf("a lowercase letter\n");
		}
		else if(input>='A' && input<='Z')
		{
			printf("Capitalize\n");
		}
		else
		{
			printf("Nothing\n");
		}
		getchar();//Clean and enter
	}
	return 0;
}

task

Judge whether the year entered by the user is a leap year?

Once every four years

Never run for a hundred years

Four hundred years and 2000 yes 1900 no

#include <stdio.h>

int main()
{
	int year;
	scanf("%d", &year);
	if(year%4==0&&year%100!=0 || year%400==0)
	{
		printf("yes\n");
	}
	else
	{
		printf("no\n");
	}
	return 0;
}

task

Look for a few daffodils in all three digits. (4)

(371 == 3*3*3 + 7*7*7 + 1*1*1)

num%10 get single digit

num/10%10 get ten digits

num/100 to get hundreds

#include <stdio.h>

int main()
{
	int num;
	//Loop all three digits
	for(num = 100;num < 1000;num++)
	{	
		int a = num%10;//Single digit
		int b = num/10%10;//Ten digits
		int c = num/100;//Hundred digits
		if(a*a*a+b*b*b+c*c*c == num)
		{
			printf("%d\n", num);
		}
	}
	return 0;
}

1, do-while

Format:

do 
{
	Circulatory body. 
}while( condition );

The first loop body is executed unconditionally. After the loop body is executed, judge the condition. If the condition is true, execute the loop body again.

Do while loop; ending.

C language statements must be based on {} or; ending.

if()

{}

switch()

{}

while()

{}

for()

{}

Features: execute first and then judge.

Example:

#include <stdio.h> 

int main()
{
	int i=1; 
	do
	{
		printf("%d",i);
		i++;
	}while(i<=5);
	return 0;
}

practice:

Sum between 1 and 100. Use do while to complete.

#include <stdio.h>

int main()
{
	int i = 1;
	int sum = 0;
	do
	{
		sum += i;
		i++;
	}while(i < 101);
	printf("%d\n", sum);
	return 0;
}

practice:

Please enter a number. If the conditions are met (including 150 and 160 between 150 and 160), output it and end the program.

(requirement: please ensure that this number is within the range. If it is not within the range, please re-enter it.) it is recommended to use do while

#include <stdio.h>

int main()
{
	while(1)
	{
		int input;
		scanf("%d", &input);
		if(input>=150 && input<=160)
		{
			printf("%d\n", input);
			break;
		}
	}
	return 0;
}
#include <stdio.h>

int main()
{
	int input;
	do
	{
		scanf("%d", &input);
	}while(input<150 || input>160);
    printf("%d\n", input);
	return 0;
}

practice:

Export the following graphics. Requirement: output only one character at a time.

​ *

***

*****

#include <stdio.h>

int main()
{
	int i, j;
	//int spaceCount = 2;
	//int starCount = 1;
	for(i = 0;i < 9;i++)
	{
		for(j = 0;j < 8-i;j++)
		{
			printf(" ");
		}

		for(j = 0;j < 2*i+1;j++)
		{
			printf("*");
		}
	//	starCount+=2;
		printf("\n");
	//	spaceCount--;
	}
	return 0;
}

Summary:

Circular statement:

Different circular writing methods are suitable for expressing different logic.

while is suitable for expressing loops that are not conditional on the number of cycles.

For is suitable for expressing a cycle conditional on the number of cycles.

Do while verifies the execution result of the loop body logic. If it does not meet the expectation, the loop will start again.

Loop control statement:

continue ends the loop body of this loop. while will directly judge the cycle conditions; for will directly execute expression 3

break ends the whole cycle.

return ends the current function. It does not belong to the keyword of the loop.

2. One dimensional array (integer type)

1. Why use arrays

A bunch of variables put together is an array.

When we need many variables, we define arrays.

2. How to define

int grade[10];

grade is an array

[10] There are 10 variables in the array. The variables in the array are called the elements of the array.

Int indicates that all 10 variables in the array are of type int.

The variables in the array are of the same type.

When defining an array, the length of the array must be constant!

Array length must be determined!

Element name:

The first element of the grade[0] array, the number in the array element [] is called the angle sign of the array element. (also called subscript)

grade[1]

grade[2]

...

grade[9] the index of the last element of the array must be the length of the array - 1.

Distinction: the difference between int a[5] and a[5]:

int a[5]: is defining an array. 5 represents a total of 5 elements.

a[5]: is the element name of the 6th element in the array.

3. How to assign values

The array cannot be assigned as a whole. It must be assigned to an element separately.

grade[0] = 98;

grade[1] = 77;

grade[2] = 44;

int i;

//Using a loop, assign 88 to all elements in the array

for(i=0; i<10; i++)

​ grade[i] = 88; // The subscript of an array element can be a variable.

#include <stdio.h>

int main()
{
	int grade[10];
	grade[0] = 1;
	grade[1] = 10;
	int i;
	for(i = 3;i < 10;i++)
	{
		grade[i] = i;
	}
	grade[2] = 250;
	//Circular variables are often used as angular markers of arrays
	for(i = 0;i < 10;i++)
	{
		printf("%d ", grade[i]);
	}
	printf("\n");
	return 0;
}

4. Initialization problem

The assignment is called initialization when it is defined! Only when initializing the array can {} be assigned.

Complete initialization: int grade[10] = {1,2,3,4,5,6,7,8,9,10}; The initialized data and array are equal in length.

Partial initialization: int a[10] = {1,2}; a[0]=1 a[1]=2 a[2]~a[9] = 0 the number of initialized data, which is less than the length of the array, and the rest of the array elements are supplemented with 0.

​ int b[10] = {0}; All elements of the array are initialized to 0, B [0] = 0, B [1] ~ B [9] = 0, and the system supplements 0.

​ int c[10] = {1}; c[0]=1 c[1]~c[9] = 0

Default initialization: int array[] = {1,2,3,4,5,0,0,0}; The length of the array is determined by the initialized data.

5. How to traverse

for(i = 0;i < 10;i++)
{
//Traverse each element of the array and print

​ printf("%d\n", grade[i]);

}

6. Type issues

char a[10];

short b[100];

float c[20];

double x[30];

practice:

Please save 100 in this array.

Please print out the values saved in the 50th to 60th variables.

Lowest compilation error

Advanced crash error

The most advanced error has no clue, that is, the result does not meet expectations

#include <stdio.h>

int main()
{
	int a[100];
	int i;
	for(i = 0;i < 100;i++)
	{
		a[i] = i+1;
	}

	for(i = 49;i < 60;i++)
	{
		printf("a[%d] = %d\n", i, a[i]);
	}
	return 0;
}

practice:

Please enter 10 integers, save them in the array, and then accumulate and sum them.

#include <stdio.h>

int main()
{
	int a[10];
	int i;
	for(i = 0;i < 10;i++)
	{
		scanf("%d", &a[i]);
	}
	int sum = 0;
	for(i = 0;i < 10;i++)
	{
		sum += a[i];
	}
	printf("sum = %d\n", sum);
	return 0;
}

practice:

Enter 10 test scores (0 ~ 100 integer, re-enter if out of range) into the array, and then calculate the highest score and lowest score.

#include <stdio.h>

int main()
{
	int a[10];
	int i;
	for(i = 0;i < 10;i++)
	{
		do
		{
			printf("please input %d\n", i);
			scanf("%d", &a[i]);
		}while(a[i]<0 || a[i]>100);
	}

	int max = a[0], min = a[0];
	for(i = 1;i < 10;i++)
	{
		if(max < a[i])
		{
			max = a[i];
		}
		else if(min > a[i])
		{
			min = a[i];
		}
	}
	printf("max = %d min = %d\n", max, min);
	return 0;
}

practice:

Based on the above question, please output the subscripts of the positions of the maximum and minimum values, and exchange the positions of the maximum and minimum values.

12,45,78,9,43,99,32,7,8,23

Assume a[5] maximum and a[7] minimum

After exchange, a[5]=7 a[7]=567

#include <stdio.h>

int main()
{
	int a[10];
	int i;
	for(i = 0;i < 10;i++)
	{
		do
		{
			printf("please input %d\n", i);
			scanf("%d", &a[i]);
		}while(a[i]<0 || a[i]>100);
	}

	//View input
	for(i = 0;i < 10;i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");

	int max = a[0], min = a[0];
	int maxIndex = 0, minIndex = 0;
	for(i = 1;i < 10;i++)
	{
		if(max < a[i])
		{
			max = a[i];
			maxIndex = i;//Save the angle mark of the current maximum value
		}
		else if(min > a[i])
		{
			min = a[i];
			minIndex = i;//Save the corner mark of the current minimum value
		}
	}
	printf("max = %d min = %d\n", max, min);
	printf("maxIndex = %d minIndex = %d\n", maxIndex, minIndex);
	int temp = a[maxIndex];
	a[maxIndex] = a[minIndex];
	a[minIndex] = temp;
	for(i = 0;i < 10;i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
	return 0;
}

Assignment 1

Find a prime of 100 - 200 (Prime: an integer that can only be divided by 1 and himself).

Assignment 2

Please output the first 20 items of Fibonacci series.

The first two items are known: 1. Starting from the third item, each item is the sum of the first two items. 1 1 2 3 5 8 13 21 34 . . . six thousand seven hundred and sixty-five

Homework 3: (choose to do, it's difficult, don't write or talk without ideas)

Find the longest substring in any two strings.

Char a[] = "abcabcdabc";

Char b[] = "abcdefgh";

Longest "abcd"

Topics: C Back-end