Better examples of books
4.7 handle menu commands with switch. In many applications, a menu is used to control the process. For example, entering a 'a' or 'a' character from the keyboard will execute operation a, and entering a 'B' or 'B' character will execute operation B
#include<stdio.h> #include<stdlib.h> int main() { void action1(int,int),action2(int,int); char ch; int a=12,b=89; ch=getchar(); switch(ch) { case'a': case'A':action1(a,b);break; case'b': case'B':action2(a,b);break; } system("pause"); return 0; } void action1(int x,int y) { printf("x+y=%d\n",x+y); } void action2(int x,int y) { printf("x*y=%d\n",x*y); }
4.8 judge whether it is a leap year
#include<stdio.h> #include<stdlib.h> int main() { int year,leap; scanf("%d",&year); if((year%4==0&&year%100!=0)||(year%400==0)) leap=1; else leap=0; if(leap) printf("%d It's a leap year",year); else printf("%d It's not a leap year",year); system("pause"); return 0; }
4.9
I have checked this code many times, but I don't know why it looks like random code after entering the number. UUS can check it by themselves
#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { double a,b,c,disc,x1,x2,realpart,imagpart; scanf("%lf,%lf,%lf",&a,&b,&c); printf("The equation"); if(fabs(a)<=1e-6)//absolute value printf("is not a equation"); else { disc=b*b-4*a*c; if(fabs(disc)<=1e-6) printf("equal:%8.4f\n",-b/(2*a)); else { if(disc>=1e-6) { x1=-b+sqrt(disc)/(2*a); x2=-b-sqrt(disc)/(2*a); printf("distinct:%8.4f and %8.4f",x1,x2); } else { realpart=-b/(2*a); imagpart=sqrt(-disc)/(2*a); printf("complex\n"); printf("%8.4f+%8.4fi\n",realpart,imagpart); printf("%8.4f-%8.4fi\n",realpart,imagpart); } } } system("pause"); return 0; }
Disc is a real number. There will be small errors in the calculation and storage of real numbers, so the following judgment cannot be made: if(disc==0)..., which may cause the value of 0 to be judged as not equal to 0 due to the above error, so 1e-6 is introduced, which is equivalent to the negative 6th power of 10.
After class exercises
4. There are three integers a, B and C, which are input by the keyboard and output the largest number
//Less than sign discrimination #include<stdio.h> #include<stdlib.h> int main() { int a,b,c; printf("Please enter three numbers separated by spaces\n"); scanf("%d%d%d",&a,&b,&c); if(a<b) { if(b<c) printf("the max is %d",c); else printf("the max is %d",b); } else if(a<c) printf("the max is %d",c); else printf("the max is %d",a); system("pause"); return 0; }
//Greater than sign discrimination #include<stdio.h> #include<stdlib.h> int main() { int a,b,c; printf("Please enter three numbers separated by spaces\n"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(c>a) printf("the max is %d",c); else printf("the max is %d",a); } else if(c>b) printf("the max is %d",c); else printf("the max is %d",b); system("pause"); return 0; }
#include<stdio.h> #include<stdlib.h> int main() { int a,b,c,temp,max; printf("Please enter three integers:\n"); scanf("%d%d%d",&a,&b,&c); temp=(a>b)?a:b; //Store the greater of a and b in temp max=(temp>c)?temp:c; //Compare the larger one with c and take the largest one printf("the max is %d",max); system("pause"); return 0; }
5. Input a positive number less than 1000 from the keyboard and output its square root (if the square root is not an integer, output the integer part). It is required to check whether the data is a positive number less than 1000 after entering it. If not, re-enter is required.
#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { int a,b; printf("Please enter a positive number less than 1000:\n"); scanf("%d",&a); if(a>1000) { printf("Does not meet the requirements, please re-enter:"); scanf("%d",&a); } b=sqrt(1.0*a); //Specified as double type printf("%d The integer part of its square root is:%d\n",a,b); system("pause"); return 0; }
Knowledge point sqrt() function
#include<math. h> / / required header file double sqrt(double x); //Function prototype
#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { printf("%d\n",sqrt(25)); //Return value should be of type double system("pause"); return 0; }
1>d:\c++try\c\c\c.cpp(6): error C2668: “ sqrt": Ambiguous call to overloaded function 1> d:\baidunetdisk\vc\include\math.h(589): Maybe“ long double sqrt(long double)" 1> d:\baidunetdisk\vc\include\math.h(541): or “ float sqrt(float)" 1> d:\baidunetdisk\vc\include\math.h(127): or "double sqrt(double)" 1> Try matching parameter list“(int)"Time
#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { printf("%f\n",sqrt(25.0)); system("pause"); return 0; } //It can be output correctly
6. There is a function
y={ x x<1
| 2x-1 1<=x<10
{ 3x-11 x>=10
Write a program, input x and output y
#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { float x,y; printf("Please enter x Value of:\n"); scanf("%f",&x); if(x<1) { y=x; printf("y=%3.1f",y); } else if(x<10) { y=2*x-1; printf("y=%3.1f",y); } else { y=3*x-11; printf("y=%3.1f",y); } system("pause"); return 0; }
8. Give a hundred mark system score, and output grade A, B, C, D and E.
Above 90 points are A, 80 to 89 points are B, 70 to 79 points are C, 60 to 69 points are D, and below 60 points are E.
#include<stdio.h> #include<stdlib.h> int main() { int score,a; char c; printf("Please enter 0~100 Achievements:\n"); scanf("%d",&score); if(score<0||score>100) printf("Does not meet the specification, please re-enter"); a=int(score/10); switch(a) { case 10: case 9:c='A';break; case 8:c='B';break; case 7:c='C';break; case 6:c='D';break; case 5: case 4: case 3: case 2: case 1: case 0:c='E';break; } printf("The result is%d,The grade is%c\n",score,c); system("pause"); return 0; }
In fact, the most important thing to write here is your whole logical thinking. If you make your thinking clear, it's not very difficult to write this kind of code.
Let's look at the following questions, which are mainly related to mathematics.
9. For a positive integer with no more than 5 digits, requirements: ① find out how many digits it is; ② output each digit respectively; ③ output each digit in reverse order. For example, if the original number is 123, 321 should be output
#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { int num,indiv,ten,hundreds,thousand,ten_thousand,place; printf("Please enter a positive integer with no more than five digits\n"); scanf("%d",&num); if(num>9999) place=5; else if(num>999) place=4; else if(num>99) place=3; else if(num>9) place=2; else place=1; printf("The number of digits you entered is%d\n",place); printf("Each number is"); ten_thousand=num/10000; thousand=int((num-ten_thousand*10000)/1000); hundreds=int((num-ten_thousand*10000-thousand*1000)/100); ten=int((num-ten_thousand*10000-thousand*1000-hundreds*100)/10); indiv=int(num-ten_thousand*10000-thousand*1000-hundreds*100-ten*10); switch(place) { case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundreds,ten,indiv); printf("\n The number in reverse order is"); printf("%d,%d,%d,%d,%d",indiv,ten,hundreds,thousand,ten_thousand); break; case 4:printf("%d,%d,%d,%d",thousand,hundreds,ten,indiv); printf("\n The number in reverse order is"); printf("%d,%d,%d,%d",indiv,ten,hundreds,thousand); break; case 3:printf("%d,%d,%d",hundreds,ten,indiv); printf("\n The number in reverse order is"); printf("%d,%d,%d",indiv,ten,hundreds); break; case 2:printf("%d,%d",ten,indiv); printf("\n The number in reverse order is"); printf("%d,%d",indiv,ten); break; case 1:printf("%d",indiv); printf("\n The number in reverse order is"); printf("%d",indiv); break; } system("pause"); return 0; }
10. When the profit I is less than or equal to 100000 yuan, the bonus can be increased by 10%;
When the profit is higher than 100000 yuan and lower than 200000 yuan (10000 < I ≤ 200000), the Commission of 100000 yuan is 10%, and the part higher than 100000 yuan can be 7.5%; Between 2000000000 and 400000 yuan, 200000 yuan shall still be deducted according to the above methods (the same below), and the part higher than 200000 yuan shall be deducted by 5%; Between 400000 and 600000 yuan, the part higher than 400000 yuan will be deducted by 3%;
Between 600000 and 10000000, the part higher than 600000 will be deducted by 1.5%;
1> In case of 1000000, 1% commission will be charged for the part exceeding 1 million.
Enter the profit I of the current month from the keyboard to calculate the total amount of bonus to be paid.
#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { double i,bonus,bonus1,bonus2,bonus4,bonus6,bonus10; bonus1 = 100000*0.1; bonus2 = bonus1 + 100000*0.075; bonus4 = bonus2 + 200000*0.05; bonus6 = bonus4 + 200000*0.03; bonus10 = bonus6 + 400000*0.015; while(1) { printf("Please enter the profit of the current month:"); scanf("%lf",&i); int branch=(int)(i/100000); switch(branch) { case 0: bonus = i * 0.1; break; case 1: bonus = bonus1 + (i-100000)*0.075; break; case 2: case 3: bonus = bonus2 + (i-200000)*0.05; break; case 4: case 5: bonus = bonus4 + (i-400000)*0.03; break; case 6: case 7: case 8: case 9: bonus = bonus6 + (i-600000)*0.015; break; default: bonus = bonus10 + (i-1000000)*0.01; break; } printf("What is the bonus due:%.2lf\n", bonus); } system("pause"); return 0; }
11 input four integers and output them from small to large
int a,b,c,d,t; printf("Please enter four integers:"); scanf("%d,%d,%d,%d",&a,&b,&c,&d); if(a>b) { t=a;a=b;b=t;} if(a>c) { t=a;a=c;c=t;} if(a>d) { t=a;a=d;d=t;} if(b>c) { t=b;b=c;c=t;} if(b>d) { t=b;b=d;d=t;} if(c>d) { t=c;c=d;d=t;} printf("%d,%d,%d,%d",a,b,c,d);
12. There are four round towers with centers of (2,2), (- 2,2), (- 2, - 2), (2, - 2) and a radius of 1. The height of the four towers is 10m and there are no buildings outside the towers. Now enter the coordinates of any point to calculate the building height of the point (the height outside the tower is zero).
#include <stdio.h> #include <math.h> int main() { int x, y, h; double p1, p2, p3, p4; //Enter coordinates printf("Please enter coordinate: "); scanf("%d %d", &x, &y); //Coordinate equations of four circular towers p1 = pow(x-2, 2) + pow(y-2, 2); p2 = pow(x-2, 2) + pow(y+2, 2); p3 = pow(x+2, 2) + pow(y-2, 2); p4 = pow(x+2, 2) + pow(y+2, 2); //Judge whether the coordinates are in the circular tower and output the results (p1<=1 || p2<=1 || p3<=1 || p4<=1) ? h = 10 : h = 0; printf("The building height on this point is %d\n", h); return 0; }