catalogue
1, Examples of C language (4 ~ 10)
preface
Here we are. We continued with the remaining seven examples last time. We just updated the content of the last 100 routines. Today, we will continue to talk about the remaining seven examples in 1 ~ 10. In the future, we will release 10 examples at one time. The quantity and quality will also be guaranteed and can be used directly. However, it is better to operate manually by ourselves_ O ha ha ~, OK, let's get to the point.
1, Examples of C language (4 ~ 10)
Procedure 4:
Title: enter a certain day of a certain month in a certain year to judge the day of the year?
Analysis: use switch to judge the month to obtain the basic days, and then add the basic days to the days of the current month. In addition, judge whether it is a leap year or a normal year. In a leap year, add 1 to the total days. Note that the input comma is a comma in English
/* Title: enter a certain day of a certain month in a certain year to judge the day of the year? */ #include "stdio.h" int main() { int year,month,day,sum,leap; printf("Please enter year, month and day in 2021 format,9,4\n"); aaa: scanf("%d,%d,%d",&year,&month,&day); switch(month) { case 1: sum=0;break; case 2: sum=31;break; case 3: sum=59;break; case 4: sum=90;break; case 5: sum=120;break; case 6: sum=151;break; case 7: sum=181;break; case 8: sum=212;break; case 9: sum=243;break; case 10: sum=273;break; case 11: sum=304;break; case 12: sum=334;break; default: printf("Input error, please re-enter\n");goto aaa; break; } sum=sum+day; if(year%400==0||(year%4==0&&year%100!=0)) leap=1; else leap=0; if(leap==1) sum++; printf("This day is the third day of this year%d day\n",sum); }
Procedure 5:
Title: enter three integers x,y,z, please output these three numbers from small to large
Analysis: simple number size comparison, use if to judge and assign values to each other to sort
/* Title: enter three integers x, y and Z. please output these three numbers from small to large. */ #include "stdio.h" int main() { int a,b,c,x; printf("Please enter 3 numbers, format: 12,23,34\n"); scanf("%d,%d,%d",&a,&b,&c); if(a>b) { x=a; a=b; b=x; } if(a>c) { x=a; a=c; c=x; } if(b>c) { x=b; b=c; c=x; } printf("Three numbers from small to large:%d<%d<%d\n",a,b,c); }
Procedure 6:
Title: use * sign to output the pattern of letter A
Analysis: directly use printf to complete, and pay attention to the location
/* Title: use * to output the pattern of letter A. */ #include "stdio.h" int main() { printf(" *\n"); printf(" * *\n"); printf(" *******\n"); printf(" * *\n"); printf(" * *\n"); }
Procedure 7:
Title: output special patterns, please run in c environment
Analysis: you can play freely here. We mainly know that there are 256 characters, but there are only 127 ASCII codes here. After 128 is the extended ASCII code, which generally represents general special letters or symbols in European languages. In China, it is used to spell two such codes into one Chinese character. 128-255 are non printed characters (that is, some Chinese characters that you can't understand will be displayed, except for literary bosses of course).
/* Title: output special patterns, please run in c environment, have a look, Very Beautiful! There are 256 characters in total. Different characters and graphics are different */ #include "stdio.h" int main() { char a=126,b=223; printf("%c%c%c%c%c\n",a,b,a,b,a); printf("%c%c%c%c%c\n",b,b,a,b,b); printf("%c%c%c%c%c\n",a,b,b,b,a); printf("%c%c%c%c%c\n",a,a,a,b,a); }
Procedure 8:
Title: output 9 * 9 formula
Analysis: I believe everyone will be familiar with the 99 multiplication table in primary school, ha ha; Using the for loop to control the output of rows and columns is relatively simple
/* Title: output 9 * 9 formula */ #include "stdio.h" int main() { int i,j,result; printf("\n"); for (i=1;i<10;i++) { for(j=1;j<(i+1);j++) { result=i*j; printf("%d*%d=%-3d",j,i,result);/*-3d Indicates left alignment, accounting for 3 digits*/ } printf("\n");/*Wrap after each line*/ } }
Procedure 9:
Title: request to output the chess board
Analysis: chess board 8 * 8 uses the for loop to judge whether to output the board in even digits. Normally, if ASCII code 219 is used, there will be random code in printing, so it is obvious to directly use "■" and "□" to output, or use printf("%c%c", '\xA1', '\xF6') to output a black grid (those that have been commented out in the code can be debugged by themselves)
/* Title: it is required to output chess board 8 * 8. */ #include "stdio.h" int main() { int i,j; for(i=0;i<8;i++) { for(j=0;j<8;j++) if((i+j)%2==0) printf("■"); // printf("%c%c", '\xA1', '\xF6'); else printf("□"); printf("\n"); } }
Procedure 10:
Title: print the stairs, and print two smiling faces above the stairs at the same time
Analysis: it is also a simple way to set up a loop by using the for loop. Here, we mainly investigate the use and familiarity of the loop structure
/* Title: print the stairs, and print two smiling faces above the stairs at the same time. */ #include "stdio.h" int main() { int i,j; printf("\n"); printf("~^o^~ ~^o^~\n");/*Output two smiling faces*/ for(i=1;i<11;i++) { for(j=1;j<=i;j++) printf("■"); printf("\n"); } }
2, Summary
Ha ha, hurry up and fill in the 7 missing programs, and finish the 100 programs as soon as possible. Come on
The previous chapters mainly focus on the basic application, especially the use of for, if, switch, scanf, printf, and goto I added. You can get familiar with it as much as you like. I hope it can be helpful to you. At the same time, I hope you can also discuss and study together. Although it is a foundation, you must learn it solidly!
If you have any other opinions or ideas on the topic, you are welcome to leave a message in the comment area at any time. Don't forget to praise the blogger. Every time you move your finger, it is the driving force for me to keep moving forward!
Digression:
I like what Peng Yuyan said: "I just don't have talent, so I work hard!"
Learning 32 is hard, but if you stick to it, isn't it cool? Ha ha ha