As an outside person, the time has gone from the school graduation 2.5, has experienced the work, the Postgraduate examination (failed), again to find the work finally decided to a programmer development. Although the recent spread of artificial intelligence, the future to replace a variety of basic jobs (including programmers, of course), but I think as long as more than the study of robotics in the line, if the study is not as good as the robot what the industry is not the same is replaced? Of course, there are a lot of industry functions is the artificial intelligence temporarily difficult to touch, but many for me is not very cold, at least now yes.
Although enrolled in the Java, but not formal classes, the teacher first popularized a C language, a period of two weeks, the first week has ended, the same is in the teacher's advice to open such a blog to record the learning process. The benefits are many, but it's a bit uncomfortable for me that I've never loved to take notes. But what should be done is to do, whether for the consolidation or the completion of the task, if the serious record of daily income will always be a great benefit, just a fee.
Back to the point, has been learning C language 5 days, the blog opened a little late, the 5 days of the important content review summarized as a piece:
Start with a program first
#include <stdio.h> for a standard input and output library in C language;
void Main () {} represents the main program, in which the program is compiled as the main program, generally in all functions () the content represents the function condition, the program that writes the function in {}, each executable program must be, as the end (all symbols in the program are half-width), void represents the function without The return value can also be written as int main (void) {program +return 0;}, representing a return value of zero;
The program can add//+ comment content to represent a single line of comments to illustrate the current meaning of the program;
or use/* Comment content */For multi-line comments,/* To start the comment,/* To end the comment, the content of the comment will not be compiled to execute;
Here are a few of the main learning functions this week:
1.printf ("");
The string used to print out "", which can be used to represent different types of placeholders using%d,%f,%ld,%lf,%c, to refer to data variables in the program, followed by "", and then separately referenced. Cases:
#include <stdio.h>
void Main ()
{
int num=1; int (shaping), float (floating point), char (single-character) refers to a variable that defines the appropriate type
Different types of variables allocate different sizes of memory, and the corresponding range of values can be represented differently
NUM represents the declared variable name variable name can only be composed of letters (case-sensitive), numbers, _, and cannot start with the same keyword as a number
= is an assignment symbol, the value after = is assigned to the variable before =
float numf=1.1;
Char character= ' a ';
printf ("Output%d/t%f/t%c/n", num,numf,character);
}
This procedure is to print out 1 1.1
A
where/T and/n represent tab stops and line breaks, respectively
2.SCANF (",&");
Used to get the input data, for example:
#include <stdio.h>
void Main ()
{
int num; Variable num is assigned a value that assigns a certain address in memory based on the type of assignment
Prinf ("Please enter the assignment you need to assign to num: \ n");
scanf ("%d", &num); %d is still the placeholder representing the int type,& represents the accessor, and &num represents the data entered in the previous input into the address of Num assigned
Content equivalent to num= input
printf ("You have entered%d assigned to num\n", num); The program executes according to the upper and lower order one by one
}
This procedure is to assign the input value to the variable num, and then print;
3.if () {} else{} else if () {}
if () {} is used for conditional judgment, the content in () is a Boolean type, ture or FALSE, if the statement in {} is executed if the condition is true, the statement in {} is not executed if the conditions are false, and if () {} is used with the IF () {} connection, the condition in its () Should be the same as the condition in if (), else{} means that if the preceding if () and else if () conditions are not satisfied then execute the statement in the {} below it
{} can be omitted to execute if () or else if () or else () in the case of a condition that satisfies ();
where if () {} can be used alone, and else if{} and else{} can only be used in pairs with the if () {} on it;
Cases:
#include <stdio.h>
void Main () //a period based on the input of the year, month, and day is calculated as the days of the year of the program {int year,month,day,feb,count;//feb represents February days, and count represents the day of the year
printf ("Please enter the year: \ n"); scanf ("%d",& year); printf ("Please enter month: \ n"); scanf ("%d",&month); printf ("Please enter date: \ n"); scanf ("%d",& day); if ((year%4==0&&year%100!=0) | | | (year%400==0)) The condition in {//() is used to determine whether the current year is a leap years//condition in which the% operator represents the redundancy, the same operator has +,-,*,/etc., and if both the left and right sides are int (short, long), then the budget result is automatically rounded
The = = in the condition and the non-appearing <,<=,>,>= are comparison operators used to compare the values on both sides of the symbol to compare the results to Ture/false
The!=,| |,&& in the condition are logical operators representing non-or, respectively, with ture/false that are used to judge multiple conditions
feb=29; Execute the bank if the condition is true or else the following statement
}else{
Feb=28; }//Calculates the count value according to the different months of the input to get the result if (month==1) {count=Day }else if (month==2) {count=day+31; }else if (month==3) {count=day+31+ Feb; }else if (month==4 ) {count=day+31+feb+31 ; }else if (month==5 ) {count=day+31+feb+31+30 ; }else if (month==6 ) {count=day+31+feb+31+30+31 ; }else if (month==7 ) {count=day+31+feb+31+30+31+30 ; }else if (month==8 ) {count=day+31+feb+31+30+31+30+31 ; }else if (month==9 ) {count=day+31+feb+31+30+31+30+31+31 ; }else if (month==10 {count=day+31+feb+31+30+31+30+31+31+30 ; }else if (month==11 {count=day+31+feb+31+30+31+30+31+31+30+31 ; }else if (month==12 {count=day+31+feb+31+30+31+30+31+31+30+31+30 ;
//Output printf ("%d-%d-month%d-day%d"%d days \ n ", Year,month,day,year,count); }
4.switch (expression) {case Value 1:case value 2: ... default:}
Similar to If...else switch is also a conditional structure, the difference is that the case value of the switch under {} is compared with the expression in (), if the same execution case value: Under the statement, otherwise execute the default under the statement; The following must be a specific value , unlike the if....else if syntax, where the expression value is equal to the value after a case statement, all statements below it are run and the other case is ignored until a break is encountered.
If the value of any case statement is not equal to the value of the expression, run the statement under the optional label default;
Cases:
Use the switch structure to write a program that is the same as above
#include <stdio.h>voidMain () {int year,month,day,feb,count=0; printf ("Please enter year: \ n")); scanf ("%d",&year); printf ("Please enter month: \ n")); scanf ("%d", & month), printf ("Please enter date: \ n" ), scanf ("%d", & day); if (year%4==0&&year% 100!=0) | | (year%400==0 )) {feb=29 ;} else {feb=28 ;} switch (month) {case] : count+=30;//here the + = operator is equivalent to count=count+30//similar and-=,/=,* = usage//There is also a + +,--represented from +1 or since -1 case : count+=31 , Case : count+=30 , Case 9 : count+=31 ; case 8 : count+=31 ;//If a break occurs in between, then the case is skipped and the default 7 : count+=30 ; 6 : Count+=31 , Case 5 : count+=30 , Case 4 : count+=31 , Case 3 : count+= Feb, Case 2
: count+=31
; Case 1 : count+= Day;} printf ("%d-%d-%d-day%d-%d days \ n" , Year,month,day,year,count );}
5.while () {} do{}while ();
While is a looping syntax, the content in () is the condition of the loop execution, and if the condition in () is ture, the program in {} is executed, otherwise the execution is stopped;
Do.. While different with while, the procedure in do{} is executed before entering the condition in while (), if Ture continues to execute the program in do{}, where while as a separate statement requires;
Cases:
#include <stdio.h>void Main () {//print 100~1000 The number of daffodils between all, and count the number of int i=100;//loop variable int count=0, GE, Shi,bai,sum; Do{ ge=i%10;//get the bit shi=i/10%10;//get 10 bits bai=i/100;//Get the Hundred sum=ge*ge*ge+shi*shi*shi+bai*bai* Bai; The IF (sum==i) { //is used to determine whether the I value into the loop is a daffodil number //while,do....while,if, etc. can be embedded with each other in printf ("%d\t", i); count++; Used for counting } i++; The interval of each cycle, the i++ represents the value of I after each cycle of +1}while (i<1000); () in the loop condition if satisfied will always execute do{} in the program printf ("\n100~1000 total of%d daffodils"
Example of usage of loop nesting:
#include <stdio.h>void Main () {//Print positive isosceles triangle// *// ***//*****//******* int i=0, j,k; while (i<4) { //Outer loop control number of rows j=0,k=0; while (j<3-i) { //To find the relationship between the variable and the number of cycles required, printf (""); Use space to position J + +; } while (k<2*i+1) { //print each planetary number printf ("*"); k++;} printf ("\ n"); Wrap the loop at the end of each line i++
6.for (;;) {}
The For loop effect is similar to the while loop, and the program in {} is executed when the condition is satisfied in ()
In the For loop, is not or missing, expression 1, expression 2, expression 3 can be omitted;
for (expression 1; expression 2; expression 3) {//loop-body;}
Execution order: The initialization of the loop variable is the expression 1, the loop condition is the expression 2, the execution loop body is {} Inside the statement, change the bad variable is the expression 3, where the expression 1 initializes the loop variable, only 1 times;
Example 1:
#include <stdio.h> voidMain () {/**************************************************** *************zhaoyi 2017.3.18 test2************ * * * * * * * * According to user input data output the appropriate number of sizes neatly arranged diamond ***************************************************///size for diamond size, row output row number, Colum output column number, Xyij internal and external loop variable intSize,row,column,x,y,i,j; Get user input information printf ("Please enter the diagonal length of the diamond you want to show: \ n")); scanf ("%d",&size); printf ("Please enter the number of lines you want to show the diamond: \ n"); scanf ("%d",&row); printf ("Enter the number of columns you want to show the diamond: \ n"), scanf ("%d",&column); printf ("\ n"); for (x=0;x <column;x++) {//loop number of rows for (i=0;i<size/2+1;i++) {for (y=0;y<row;y++) {//loop diamond number of columns for (j=0;j+i<size/2;j++) {//The upper part of a single diamond is printf ("");} for (j=0;j<2*i+1;j++) {printf ("*"),} for (j=0;j+i<size/2;j++) {printf ("");}} printf ("\ n");} for (i--;i>0;i--) {for (y=0;y<row;y++) {//loop diamond number of columns for (j=0;j+i<size/2+1;j++) {// Single rhombic lower half part printf ("");} for (j=0;j<2*i-1;j++) {printf ("*"),} for (j=0;j+i<size/2+1;j++) {printf ("");}} printf ("\ n");}} printf ("\ n");}
In addition the break and Contine keywords can be inserted in the loop
Break: Jump out of the loop
Continue: Skip this cycle and continue to the next loop
Example 2:
#include <stdio.h>void Main () {/************************************************** print 0,1,2, 3,4 can be composed of a number of different three-digit **********************************************/ int num,ge_5,shi_5,bai_5,count=0; for (num=25;num<125;num++) { ge_5=num%5; Converts a decimal number to a three-digit 5-in (25~125 corresponding 100~1000) shi_5=num/5%5; and separately extracts the digital bai_5=num/5/5%5 of each of you ; if (ge_5==shi_5| | ge_5==bai_5| | Shi_5==bai_5) { //determine if each digit is different, skip the loop continue; } printf ("%d%d%d\t", bai_5,shi_5,ge_5); The output of the resulting five-digit count++; } printf ("\n\n0,1,2,3,4 can consist of a number of different three-digit numbers with%d \ n", Count);}
Write so many still do not have this week teacher professor's all summed up in place, just through this week to learn a few functions interspersed several typical cases to explain the summary, inevitably omissions, not in place or error place, the last two cases are teachers decorate the weekend homework, the teacher has not reviewed, not necessarily the best way to write, But I am quite satisfied with it.
This concludes this week's review.
PS: Just start not to notice the function of inserting code is too lazy to change, the subsequent perfection of the ~
C Language Learning Diary Week1