---Resume content start---
part 1 format output function printf() and format input function scanf()
ex2_1.cpp
#include <stdio.h> int main() { int x=1234; float f=123.456; double m=123.456; char ch='a'; char a[]="Hello, world!"; // Define an array a,String constants are stored continuously in the array hello,world! int y=3, z=4; printf("%d %d\n", y, z); printf("y=%d, z=%d\n", y,z); printf("%8d,%2d\n", x,x); printf("%f, %8f, %8.1f, %0.2f, %.2e\n",f,f,f,f,f); printf("%lf\n",m); printf("%3c\n", ch); printf("%s\n%15s\n%10.5s\n%2.5s\n%.3s\n",a,a,a,a,a); return 0; }
ex2_2.cpp
#include <stdio.h> int main() { double x,y; char c1,c2,c3; int a1,a2,a3; scanf("%d%d%d",&a1,&a2,&a3); printf("%d,%d,%d\n",a1,a2,a3); scanf("%c%c%c",&c1,&c2,&c3); printf("%c%c%c\n",c1,c2,c3); scanf("%lf,%lf",&x,&y); printf("%lf,%lf\n",x,y); return 0; }
Add address character in parenthesis after scanf&
The defined data type should be the same as that of scanf and printf
part 2 validation content
#include <stdio.h> int main() { double a,b,c; scanf("%lf %lf %lf", &a, &b, &c); if(a<0 || b<0 || c<0) printf("Can't form a triangle\n"); else if(a+b>c && a+c>b && b+c>a) { if(a==b && a==c) printf("Make an equilateral triangle\n"); else if(a==b || a==c || b==c) printf("Form isosceles triangle\n"); else printf("Form a general triangle\n"); } else printf("Can't form a triangle\n"); return 0; }
line 14~line 19 are changed to
#include <stdio.h> int main() { double a,b,c; scanf("%lf %lf %lf", &a, &b, &c); if(a<0 || b<0 || c<0) printf("Can't form a triangle\n"); else if(a+b>c && a+c>b && b+c>a) { if(a==b || a==c || a==c){ if (a==b && a==c) printf("Make an equilateral triangle\n"); else printf("Form isosceles triangle\n"); } else printf("Form a general triangle\n"); } else printf("Can't form a triangle\n"); return 0; }
Yes.
(2) code before modification
#include <stdio.h> #include <stdlib.h> int main() { char choice; printf("Input 0~9 Select the foreground color scheme of the screen background color: \n"); printf("1-\t Black background green\n"); printf("2-\t Black with white background\n"); printf("3-\t Blue bottom white\n"); printf("Other-\t Black bottom white(default)\n"); printf("\n Please input, Choose the screen color scheme you want to use: "); choice = getchar(); if (choice == '1') { system("color 02"); printf("<This kind of screen color is common in movies and TV series like matrix:)\n"); } else if(choice == '2') { system("color f0"); printf("Most editors default to this color:)\n"); } else if(choice == '3') { system("color 1f"); printf("This is not pleasant when the blue screen of the machine breaks down:)\n"); } else { system("color 0f"); printf("Console program default small black window:)\n"); } printf("programming is fun, just try@_@\n"); return 0; }
The code after modifying int is:
#include <stdio.h> #include <stdlib.h> int main() { int choice; printf("Input 0~9 Select the foreground color scheme of the screen background color: \n"); printf("1-\t Black background green\n"); printf("2-\t Black with white background\n"); printf("3-\t Blue bottom white\n"); printf("Other-\t Black bottom white(default)\n"); printf("\n Please input, Choose the screen color scheme you want to use: "); scanf("%d",&choice); if (choice == 1) { system("color 02"); printf("<This kind of screen color is common in movies and TV series like matrix:)\n"); } else if(choice == 2) { system("color f0"); printf("Most editors default to this color:)\n"); } else if(choice == 3) { system("color 1f"); printf("This is not pleasant when the blue screen of the machine breaks down:)\n"); } else { system("color 0f"); printf("Console program default small black window:)\n"); } printf("programming is fun, just try@_@\n"); return 0; }
part 3 programming exercise
(1) number of palindromes
#include<stdio.h> int main(){ int x,a,b,c; printf ("Please enter a decimal integer in three digits:\n"); scanf ("%d",&x); if(x>0&&x<10){ printf("Calculate its inverse ordinal number...\n"); printf("%d Positive and negative order are the same\n",x); } else if(x>=10&&x<100){ a=x%10; b=x/10; if(x==a*10+b){ printf("Calculate its inverse ordinal number...\n"); printf("%d Positive and negative order are the same\n",x); } else{ printf("Calculate its inverse ordinal number...\n"); printf("%d Positive and negative order are different\n",x); } } else if(x>=100&&x<1000){ c=x/100; b=(x-c*100)/10; a=x%10; if(x==a*100+b*10+c){ printf("Calculate its inverse ordinal number...\n"); printf("%d Positive and negative order are the same\n",x); } else{ printf("Calculate its inverse ordinal number...\n"); printf("%d Positive and negative order are different\n",x); } } else printf("?"); return 0; }
(2) input the year and month from the keyboard to calculate the days of the month.
#include<stdio.h> int main(){ printf("Enter year month:\n"); int year,month,day,leapyear; scanf("%d %d",&year,&month); leapyear=year%4==0&&year%100!=0||year%400==0; if(year<0||month<1||month>12) printf("Wrong data entered!"); else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) day=31; else if(month==4||month==6||month==9||month==11) day=30; else if(month==2) day=28+leapyear; printf("%d year%d The days of the month are:%d\n",year,month,day); return 0; }
Input a percentage system score (integer) from the keyboard, and output the grade: excellent, good, medium, pass, fail.
#include<stdio.h> #include<stdlib.h> int main(){ int x; scanf("%d",&x); printf("Enter score (0~100): %d\n",x); switch(x/10){ case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : printf("Fail,");break; case 6 : printf("pass");break; case 7 : printf("secondary");break; case 8 : printf("good");break; case 9 : printf("excellent");break; case 10 : printf("excellent");break; default:printf("Score is not in valid range");break; } return 0; }
---End of recovery content---