Because I want to become a big man, I want to record the growth process of learning programming, so I choose to record in the Nuggets. I will record my most basic daily learning content and experience, as well as how to solve the problems I have encountered. When I was in high school, my head teacher often said, There is no such thing as success for no reason. This record is not only to witness my own growth, but also to provide programming help to more people. (Will always be updated)
I’ll start with the simplest HELLO C PRODRAM, which IS dev-C ++, and I’ll update it all the time.
— — — — — — — — — — — — — — — — — — — — — — — — 2021/2/19 18:46 — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –
The first chapter:
< 1.>
# output control note:
The output control characters are as follows:
%d—— int
%ld—— long int
%c—— char
%f—— float
%lf—— double
%x(or %x or %#x)—— int or long int or short int
Any integer output in hexadecimal.
% o — — — — — – same as above
An integer output in octal
% s — — — — — – a string
< 2.>
Here is the corresponding code:
/ *
2020/12/23 17:05 Function: Read data from a file on a hard disk and output it in another file. Purpose: Test the usage of %x %x %#x %#x
* /
include <stdio.h>
int main()
{
int i = 47; //47 is decimal printf("%d\n", I); Printf ("%x\n", I); // The output structure is 2f printf("%X\n", I); Printf ("%#x\n", I); 0x2f (0 is a number, not a letter) printf("%#X\n", I); 0X2F return 0;Copy the code
}
/ * output results as follows: in the DEV - c + + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 47 2 f 2 0 x2f 0 f x2f -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- don't have to run in the future when you look at, be clear at a glance. (The results may vary from software to software.) Note: The second important point is that %#x and %#x are generally recommended, because your program is not just for you to see 0f output hexadecimal, more straightforward than the other output. Otherwise, it's just a number and a letter. Note: The normative nature of typing requires that symbols be paired! First (later); Move the cursor between the double brackets and type. Sometimes you can't find the missing () error when you have too much code. * /Copy the code
/ *
2020/12/23 inquire
Objective:
Understand the meaning of output control characters by testingCopy the code
* /
include <stdio.h>
int main(void)
{
int i = 33; Printf (" I = %d\n", I); Printf (" I = %#X\n", I); printf(" I = %#X\n", I); Printf (" I = %o\n", I); printf(" I = %o\n", I); // use the output control %o to return 0 in octal;Copy the code
}
/ *
In dev-C ++ the result is: |
---|
i = 33 |
i = 0X21 |
i = 41 |
———————- |
No result |
* / |
< 3. >
Input tool scanf:
The use of the Scanf
[Input data into variables via keyboard]
Two usages:
- Scanf (” Input control character “, input parameter);
Function: the character input from the keyboard into the control character of the specified format of data, and then stored in the input parameter value as the address of the variable;
- Scanf (” non-input controller input controller “, input parameter);
Function: the character input from the keyboard into the control character of the specified format of data, and then stored in the input parameter value as the address of the variable;
Non-input control characters must be entered intact;Copy the code
< 4.>
Corresponding code:
/ *
2020/12/24 16:44
Objective: To test precautions for non-input control characters in scanf
* /
include <stdio.h>
int main()
{
int i; scanf("m%d", &i); Printf (" I = %d\n", I); printf(" I = %d\n", I); // I Returns 0;Copy the code
}
/ *
The output of this program in dev-C ++ is: |
---|
m123 |
i = 123 |
123 i = 0
m123n
i = 123 |
---|
* / |
1. M %d = 1. M %d = 1. M %d = 1
< 5.>
Note on the division and mod operators:
The result of the/(division) operation depends on the data type of the operation result. For example, if both numbers are int, the quotient is int. If the quotient has a decimal, the decimal part is cut off and only the integer part is kept.
As long as one or both of the dividend and divisor are floating-point data, the quotient is also floating-point data and does not truncate the decimal part.
Eg: 16/5 = 3; 16/5.0 = 3.20000; -13/4 = -3;
The % (mod) operand must be an integer. The result is the remainder of a divisible number with the same sign as the dividend. (Dividend/divisor = quotient)
Eg: 13%3 == 1; 13 % -1 == 1; -13%3 == -1;
-13 % -23 == -13; 3%5 == 3;
👇
Notice that this is not a negative that makes a positive! The sign of the quotient depends only on the dividend.
< 6.>
Corresponding code:
/ *
2021/01/10 12:27 Function: Tests results of the mod operatorCopy the code
* /
include <stdio.h>
int main(void)
{
printf("%d %d %d %d %d %d %d", 13 % 3, 13 % -3, -13 % 3, -13 % -3, 3 % 5, -3 % 5, -3 % -5);
return 0;
Copy the code
}
/ *
In DEV - is the result of the output in c + + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1 1-1-1, 3-3-3 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- note: take over the object data type must be an integer types; Note: the symbol of the remainder of the quotient is only the same as the symbol of the dividend, and similar: 3%5 =3(the remainder of the quotient is 3, so the remainder is 3);Copy the code
* /
< 7.> Logical terms used:
/ *
2020/01/14 14:12 testing logic expressions && (and), | | (or) usage;Copy the code
* /
#include <stdio.h>
int main(void)
{
int i = 10; int k = 4; int h = 3; int g = 7; int m,n,p; m = (3>2) && (k = 8); /* If the left side of && is true, we must execute the following expression, assigning 8 to K is true, so execute m = 1, K = 8; */ n = (1>2) && (h = 5); /* If && left is false, the result is false regardless of whether the right side is true or false. */ p = (3>2) || (g = 9); / * when | | on the left is true, "one vote to determine" whether the right side is true or not, the results must be true, so is not performed to the right of expression (the program directly output definition of g value) : p = 1, g = 7; When | | on the left is false, will be determined by right is real or fake p of true and false, so you have to execute the right of expression; */ printf("m = %d, k = %d, n = %d, h = %d, p = %d, g = %d",m, k, n, h, p, g); return 0;Copy the code
}
/ *
In dev-C ++ the result isCopy the code
m = 1, k = 8, n = 0, h = 3, p = 1, g = 7
Result:
Depending on whether the expression on the left side of the logical operator is true or false, that is, "one vote down", "one vote yes" and all non-zero values are "true" and zero is "false"Copy the code
* /
The second chapter:
< 1.>
Use of if notes:
If is easiest to use.
Format:
If (expression)
Statements;
Function: If the expression is true, the statement is executed. If the expression is false, the statement is not executed;Copy the code
The scope of if.
Format 1: if (expression)
Statements 1;
Statements 2;
If the expression is true, then statement 1 is executed, not statement 2.
Format 2: If (expression)
{statement 1; Statements 2; }Copy the code
If the expression is true, then statements 1 and 2 are executed.
If can only control whether a statement is executed.
If you want to control whether multiple statements are executed, enclose them in {};Copy the code
If,,,, else if,,, else
Format:
If (expression 1)
Statements 1; Else if (expression 2) statement 2; Else if (expression 3) statement 3; Else statement 4;Copy the code
If expression 1 is not true and expression 2 is true, statement 2 is executed.
If neither expression 1 nor 2 is true and expression 3 is true, statement 3 is executed. If expressions 1, 2 and 3 are not true, statement 4 is executed.Copy the code
If loop “cast”
First, understand that neither floats nor doubles are guaranteed to store a single piece of data in its entirety.
Eg: float zero; It doesn’t have to be 0; May be a floating point number near zero, 0.00000000000000000000000001 will be saved as 0;
< 2.>
Corresponding code:
< A.>
/ *
Function: Test the key points in the simple use of if.Copy the code
* /
include <stdio.h>
int main(void)
{
if (3) printf("AAAA\n"); If (0) printf("BBBB\n"); If (0==0) printf("CCCC\n"); // Return 0;Copy the code
}
/ *
The output in dev-C ++ is: |
---|
AAAA |
CCCC |
————————— |
Comment: a non-zero number is true, zero is false, 0==0 is correct, so the statement controlled by if can be printed.
* /
< B.>
/ *
Function: Tests the range of if controlCopy the code
* /
include <stdio.h>
int main(void)
{
if (1>2) printf("AAAA\n"); Printf ("BBBB\n"); // Return 0;Copy the code
}
/ *
The output in dev-C ++ is:
BBBB
Comment: if expression is false, if control statement cannot output; The output is BBBB, indicating that there is only one statement controlled by if. Note: If the statement does not belong to the previous statement, the statement should be aligned with the beginning of the statement.
* /
< C.>
/ *
Bank against 2021/1/27Copy the code
Function:
The scope controlled by if can be enclosed in {};Copy the code
* /
include <stdio.h>
int main(void)
{
if (1>2)
{
printf("AAAA\n");
printf("BBBB\n");
}
printf("CCCC\n");
return 0;
Copy the code
}
/* In dev-C ++, the output is:
CCCC |
---|
Tip: When you want to control more than one statement, you need to use {}; Also, statements that cannot be controlled by if need to be indented.
* /
< D.>
/ *
2021/1/27 20:59
Copy the code
Function: Tests the use of if,,, else,,, and.
* /
include <stdio.h>
int main(void)
{
double delta ; Printf (" Please input delta value %lf\n", delta); scanf("%lf", &delta); If (delta > 0) printf(" There are two different solutions of a binary equation "); Else if (delta == 0) printf(" Binary equation of one degree has and only one solution "); Else printf(" Binary first order equation has no solution "); return 0;Copy the code
}
/ *
In dev-C ++, the output is:
Please enter the value of delta 0.000000
5
There are two different solutions to a binary first order equation
If (expression 1) printf (statement 1); Else if (expression 2) printf (statement 2); Else if (expression 3) printf (statement 3); Else printf (statement 4);
If,,,, else,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, when you run the file will not run, the reason is the suffix, what to add, for example, c file name is:, c, and then, else,,, so you cannot run the C file. If necessary, add a, C to save. * /
< E.>
/ *
2021/1/30 capacityCopy the code
Function: Test if for the simple judgment of results;
* /
include <stdio.h>
int main(void)
{
float score; Printf (" Please enter your grade :"); scanf("%f", &score); If (score >100) printf(" ); Else if (score >= 90 && score <=100) // 90 <= score <=100; Otherwise the output is always 1, the expression is true, and the output is 1; Printf (" Excellent grades!" ); Else if (score >= 80 && score <90) printf(" score is good! ); Else if (score >= 60 && score <80) printf(" score! ); Else if (score >= 0 && score <60) printf(score = 0 && score <60) ); Else printf (" Wrong result, please re-enter!" ); return 0;Copy the code
}
/ *
In dev-C ++ the result is: |
---|
Please enter your score: |
45 |
Failing the grade, keep up the good work! |
————————— |
Experience: expression writing method can not directly in accordance with the mathematical habit to write programming such as: 90 <= score <= 100;
* /
< F.>
/ *
2021/1/30 20:24
Copy the code
Function: Swap values with if
* /
include <stdio.h>
int main(void)
{
int i,j; int t; // Define a temporary variable; // Lines 7 and 8 cannot interchange I and j; i=3; j=4;Copy the code
// i=j; // I =4 j=4 // j= I; // I = 4; // I = 4
// Correct interchange of I and j; t = i; i = j; j = t;
printf("i = %d j = %d\n", i, j);
return 0;
Copy the code
}
/ *
Running in dev-C ++ results in:
i = 4 j = 3
Note: Define a temporary value t when swapping two values and remember the order: t = I I = j j = t Examples are a bottle of Coke, a bottle of Sprite, and an empty bottle;
* /
< G.>
/ *
2021/2/2 wentestCopy the code
Function: Tests if to sort any three numbers
* /
include <stdio.h>
int main(void)
{
int a, b, c; Printf (" Enter three numbers (separated by Spaces) : ", a, b, c); scanf("%d %d %d", &a, &b, &c); // select a as the minimum value, b as the middle value, c as the maximum value; 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(" input three numbers in descending order: %d %d %d\n", a, b, c); return 0;Copy the code
}
/ *
Running in dev-C ++ results in:
Enter three digits (separated by Spaces) : 4 8 5 The entered three digits are in descending order: 8 5 4
Note: When sorting multiple numbers or letters, you can compare and then swap values and swap positions (bubble method)
* /
The third chapter:
< 1.>
/ *
2021/2/13 likewiseCopy the code
Function: Tests simple use of nested for loops
* /
include <stdio.h>
int main(void)
{
int i,j; for (i=0; i<3; ++i) for (j=2; j<5; ++j) printf(" ha ha \n"); Printf (" hee hee \ n "); return 0;Copy the code
}
/ *
In dev-C ++ the result is:
Ha ha
Ha ha
Ha ha
Ha ha
Ha ha
Ha ha
Ha ha
Ha ha
Ha ha
Hee hee
Result:
Pay attention to the order in which you run. Such as:
for(1; 2; 3) { for(4; 5; 6) printf("7"); } the sequence is 1,2 (if 2 meets the condition), 4,5 (if the condition is met), 6, until the end of the second for loop 3,2,4,5,6,5,6......Copy the code
* /
< 2.>
/ *
2021/2/13 didCopy the code
Function: Tests complex use of nested for loops
* /
include <stdio.h>
int main(void)
{
int i,j; for (i=0; i<3; ++ I) {printf(" hey hey \n"); for(j=2; j<5; ++j) {printf(" ha ha \n"); Printf (" hee hee \ n "); } printf(" woo \n"); } return 0;Copy the code
}
/ *
In dev-C ++ the result is:
Hey hey
Ha ha
Hee hee
Ha ha
Hee hee
Ha ha
Hee hee
whoops
Hey hey
Ha ha
Hee hee
Ha ha
Hee hee
Ha ha
Hee hee
whoops
Hey hey
Ha ha
Hee hee
Ha ha
Hee hee
Ha ha
Hee hee
whoops
Result:
For complex uses, pay more attention to the order. D:/ desktop /c program and self-study/program and result/for loop nesting use -1
* /
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — I’m line — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
If there are any comments about for, I will directly send the files I recorded to you because there are a lot of problems involved.
Such as:
For loop cast
For for the sum of odd numbers
For is the sum of integers that are divisible by 3
For for the number of odd numbers
For for the average of odd numbers
For takes the sum of odd and even numbers
The procedure of this kind of problem I no longer enumerate one by one, use a procedure also can, separate say can good analysis, if have need small partner private letter I. Thanks for your support!
—————————- come on, go on! ———————————–
< 3. >
/ *
2021/2/14 9:19
Copy the code
Function: Use while to judge palindrome number (drag algorithm)
* /
include <stdio.h>
int main(void)
{
int val; int m; int sum = 0; Printf (" Please enter an arbitrary value: \n"); scanf("%d",&val); m = val; while(m) { sum = sum*10+m%10; m = m/10; } if(sum==val) printf("YES! \n"); else printf("NO! \n"); return 0;Copy the code
}
/ *
In dev-C ++ the result is: |
---|
Please enter an arbitrary value: |
12321
YES! |
---|
Explanation: A palindrome number is a number that is both forward and backward.
Note: If you can’t figure out how to write this program, then understand the algorithm of others to remember.
Consider: Why can’t we just substitute a val into a while? Because after val=val/10, the value of val is not the value that you entered, so if sum =val is incorrect.
* /
< 4.>
include <stdio.h>
int main(void)
{
int a1 = 1, a2 = 2, a3 = 3; int n, i; Printf (" Please enter the fipolacci sequence you want to start: \n"); scanf("%d", &n); if(n==1) a1 = 1; else if(n==2) a2 = 2; else if(n==3) a3 = 3; else { for(i=3; i<=3; ++i) { a(i+1) = a(i) + a(i-1); printf("a(i+1)\n"); } } return 0;Copy the code
}
< 5.>
/ *
2021/2/15 andCopy the code
Function: Use the for loop to find the Fibonacci sequence.
* /
include <stdio.h>
int main(void)
{
int n; int f1, f2, f3; int i; f1 = 1; f2 = 2; Printf (" Please enter the sequence you want: \n"); scanf("%d", &n); if(1==n) f3 = 1; else if(2==n) f3 = 2; else { for(i=3; i<=n; ++i) { f3 = f1 + f2; f1 = f2; f2 = f3; } } printf("%d\n", f3); return 0;Copy the code
}
/ *
In dev-C ++ the result is:
Please enter the sequence you need:
July 21
Note: click several times to understand the program.
* /
< 6.>
include <stdio.h>
Include <math.h> // call <math.h> SQRT (root)
int main(void)
{
double a, b, c; double delta; double x1, x2; char ch; Do {printf(" Please enter the three coefficients of the unary quadratic equation: \n"); printf("a = "); scanf("%lf", &a); printf("b = "); scanf("%lf", &b); printf("c = "); scanf("%lf", &c); delta = b*b - 4*a*c; if(delta>0) { x1 = (-b + sqrt(delta))/(2*a); x2 = (-b - sqrt(delta))/(2*a); Printf (" This quadratic equation has two solutions: x1 = %lf, x2 = %lf\n", x1, x2); } else if(delta=0) { x1 = x2 = (-b)/(2*a); Printf (" This quadratic equation has one and only one solution: x1 = x2 =%lf\n", x1, x2); } else printf(" No real solution! \n"); Printf (" You want to continue please press (y), otherwise press any key. \n"); scanf(" %c", &ch); / / c % in front of the blank space} while (= = ch 'y' | | 'y' = = ch); return 0;Copy the code
}
/ *
In dev-C ++ the result is:
Please enter the three coefficients of the unary quadratic equation:
a = 1
b = 2
c = 2
No real solution!
If you want to continue, press (y), otherwise press any key.
y
Please enter the three coefficients of the unary quadratic equation:
a = 3
b = 2
c = 1
No real solution!
If you want to continue, press (y), otherwise press any key.
y
Please enter the three coefficients of the unary quadratic equation:
a = 4
b = 6
c = 1
The quadratic equation with one variable has two solutions: x1 = -0.190983, x2 = -1.309017
If you want to continue, press (y), otherwise press any key.
e
Note: the %c of “%c” must be preceded by a space.
* /
— — — — — — — — — — — — — — — — — — — — 2021/2/19 update (switch and break the continue) — — — — — — — — — — — — — — — — — — — — — — — — — — — —
< 7.>
/ *
2021/2/17 20:28
Copy the code
Function: Tests whether break can be used directly in an if statement.
* /
include <stdio.h>
int main(void)
{
int i; for(i=0; i<3; ++i) { if(3>2) break; // Break cannot be used directly in if statements, unless if is a substatement of a loop. Also, we know from the result that break terminates the external for loop. Printf (" ha ha! \n"); } return 0;Copy the code
}
/ *
In dev-C ++ the result is: |
---|
Process exited after 4.493 seconds with return value 0
Please press any key to continue.. |
---|
The absence of an output statement indicates that break terminates the external for loop, not the if statement.
* /
The < 8. >
/ *
2021/2/17 21:00
Copy the code
Function: Tests which break is used when a break is nested in multiple layers.
* /
include <stdio.h>
int main(void)
{
int i,j; for(i=0; i<3; ++i) { for(j=2; j<=4; ++j) break; Printf (" hey hey! \n"); // Run three times, indicating only the first loop is executed. } return 0;Copy the code
}
/ *
In dev-C ++ the result is:
Hey hey! Hey hey! Hey hey!
Process exited after 4.56 seconds with return value 0 Press any key to continue..
Note: When break is used for loop multi-level nesting, break terminates only the loop closest to it.
* /
The < 9. >
include <stdio.h>
int main(void)
{
int x=1, y=0, a=0, b=0; switch(x) { case 1: switch(y) { case 0: a++; break; case 1: b++; break; } b=100; break; case 2: a++; b++; break; } printf(" %d %d", a, b); return 0;Copy the code
}
/ *
In dev-C ++ the result is:
Output the values of a and B: 1, 100
Process exited after 4.619 seconds with return value 0 Press any key to continue..
* /
The < 10. >
/ *
2021/2/19 oCopy the code
Function: Test the use of break in switch multi-layer nesting.
* /
include <stdio.h>
int main(void)
{
int x=1, y=0, a=0, b=0; switch(x) { case 1: switch(y) { case 0: a++; break; case 1: b++; break; } b=100; break; case 2: a++; b++; break; } printf(" %d %d", a, b); return 0;Copy the code
}
/ *
In dev-C ++ the result is:
Output the values of a and B: 1, 100
Process exited after 4.619 seconds with return value 0 Press any key to continue..
Note In switch nesting, break only terminates the nearest switch.
* /
/ *
2021/2/19"Copy the code
Function: Tests simple use of continue.
* /
include <stdio.h>
int main(void)
{
int i,j; char ch; scanf("%d",&i); printf("i = %d\n", i); while((ch=getchar()) ! ='\n') // Line 7 assigns a value to ch that does not equal a newline character and continues until you press the newline key to terminate. { continue; // Line 8} scanf("%d",&j); printf("j = %d\n", j); return 0;Copy the code
}
/ *
Background: It is required that an invalid input value of I does not affect the assignment of j.
Without line 7, line 8, if I = 1 ajDASjnj is entered illegally then output I = 1, j = (a string of garbage values). So instead of having you type in the value of j, you automatically assign ajDASjnj to j.
Input test value by yourself!
* /
— — — — — — — — — — — — — — — — — — — — — — — add notes: — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Break and continue
1. Break:
Break, if used for a loop, terminates the loop.Copy the code
Break Terminates the switch if used for switch (select statement).
Break cannot be used for if statements unless if is a substatement of a loop. Example: for (I =0; i<3; ++ I) {If(3>2) break; Printf (" ha ha! \ n ");Copy the code
}
Although break is an if statement, it terminates the external for loop. So printf(” Ha ha! \ n “); Cannot be executed. In a multi-level loop nesting, break terminates only the loop closest to it.
For example: (I = 0; i<3; ++i) { for(i=2; i<=4; ++i) break; Printf (" hey hey! \ n ");Copy the code
}
The result is: hey hey! Hey hey! Hey hey!
Break terminates only the nearest loop, and only the first loop runs.
When used in a multi-level nesting of a switch, break also terminates the switch closest to it.
Example:Copy the code
int x=1, y=0, a=0, b=0;
switch(x) { case 1: switch(y) { case 0: a++; break; case 1: b++; break; } b=100; break; case 2: a++; b++; break; } printf(" %d %d", a, b); return 0;Copy the code
}
Output the values of a and B: 1, 100
The above examples show that break terminates the nearest loop or selection, whether in loop nesting or selection nesting.
2.continue:
The remainder of the loop is skipped to determine whether to execute the next loop.Copy the code
Example:
a.
for(1; 2; 3)
{
A;
B;
continue;
C;
D;
Copy the code
}
Skip C and D after continue. Execute 3 directly, that is, C and D are not executed.
B. While (expression) {A; B; continue; C; D;Copy the code
}
Skip C and D after continue. The expression is executed directly, that is, C and D are not executed.
Note: When for is interchanged with while, the 3 statement for precedes the continue! Otherwise 3 will be skipped by continue!