The second day of C language review -- Tan Haoqiang Chapter III after class exercises

Posted by theprofession on Wed, 02 Feb 2022 06:28:56 +0100

1. If the annual growth rate of China's gross national product is 9%, calculate the percentage increase of China's gross national product in 10 years. The calculation formula is p = (1 + r) ^ n, r is the annual growth rate, n is the number of years, and P is the multiple compared with now

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
	float p,r;
	int n=10;
	r=0.07;
    p=pow(1+r,10);
	printf("%5.2f\n",p);
system("pause");
return 0;
}
 1.97

The difference between double and float in c language
1.double is a double precision floating-point number, with 8 bytes of memory and 16 significant digits. The representation range is -1.79E+ 308~-1.79E+308.
float is a single precision floating-point number, with 4 bytes of memory and 8 significant digits. The representation range is -3.40E+38~3.40E+38.
2. The processing speed of the two is different. The CPU processes float faster than double. Double has high precision. Double consumes twice as much memory as float.
3. If it is not declared, the decimal is double by default. Strong conversion is required when using float, or add f after the decimal.

2. For the calculation of deposit interest, if you want to deposit 1000 yuan for 5 years, you can deposit it in the following five ways:
1: One time deposit for 5 years;
2: First deposit for 2 years, and then deposit the principal and interest for another 3 years after maturity;
3: Deposit for 3 years first, and deposit the principal and interest for another 2 years after maturity;
4: Deposit for 1 year, and deposit the principal and interest for another 1 year after maturity for 5 consecutive times;
5: Survival deposit. Current interest is settled quarterly.
interest:
1: 1.5%
2: 2.1%
3: 2.75%
4: 3%
5: 0.35% (quarterly interest settlement for demand deposits)
If r is the annual interest rate and n is the number of years of deposit, the formula for calculating the principal and interest is as follows:
Sum of 1-year principal and interest: p = 1000 * (1 + r);
Sum of n-year principal and interest: p = 1000 * (1 + n * r);
Sum of 1-year principal and interest deposited n times: p = 1000 * (1 + r) n (power);
Sum of principal and interest of demand deposit: p = 1000 * (1 + r/4) 4n (power)// The principal and interest of a quarter;

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
	float r1,r2,r3,r0,r5,p1,p2,p3,p4,p5,n;
	r1=0.015;
	r2=0.021;
	r3=0.0275;
	r5=0.03;
	r0=0.0035;
	n=5;
	p1=1000*(1+n*r5);
	p2=1000*(1+2*r2)*(1+3*r3);
	p3=1000*(1+3*r3)*(1+2*r2);
	p4=1000*pow(1+r1,n);
	p5=1000*pow(1+r0/4,4*n);
	printf("%6.3f\n%6.3f\n%6.3f\n%6.3f\n%6.3f\n",p1,p2,p3,p4,p5);
	
system("pause");
return 0;
}
1150.000
1127.965
1127.965
1077.284
1017.646

3. The buyer has borrowed a sum of money d from the bank. The monthly repayment amount is p and the monthly interest rate is r. calculate how many months it can be repaid.
Let d be 300000 yuan, p be 6000 yuan and r be 1%. Take one decimal place for the obtained month, and round the second digit.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
	float d,p,r,m;
	d=300000;
	p=6000;
	r=0.01;
	m=log10(p/(p-d*r))/log10(1+r);
	printf("%5.2f\n",m);

system("pause");
return 0;
}
69.66

Knowledge point c language for logarithmic function

#include<stdio.h>
#include<math.h>
int main(){ 
	printf("%f\n",log(10)); //Logarithmic function based on e 
	printf("%f\n",log10(100)); //Logarithmic function with base 10 
	printf("%f\n",log(8)/log(2)); //Calculate log2^8 and use the bottom changing formula 
	printf("%f\n",exp(1)); //Calculate the natural constant e
	return 0;
}


4. Analyze the following procedures:

#include<stdio.h>
#include<stdlib.h>
int main()
{
	char c1,c2;
	c1=97;
	c2=98;
	printf("c1=%c,c2=%c\n",c1,c2);
	printf("c1=%d,c2=%d\n",c1,c2);

system("pause");
return 0;
}

(1) What information will be output at runtime? Why?

c1=a,c2=b
c1=97,c2=98

(2) If lines 4 and 5 of the program are changed to

c1 = 197;
c2 = 198;
What information will be output at runtime? Why?

After changing the number

c1=?c2=?
c1=-59,c2=-58

(3) If line 3 of the program is changed to

int c1, c2;
What information will be output at runtime? Why?

c1=?c2=?
c1=197,c2=198

Knowledge point scanf input

character string

1. If defined as a string

#include <stdio.h>
int main(void)
{
char str[10];  //str is the abbreviation of string, that is, string
printf("Please enter a string:");
scanf("%s", str);  //The input parameter is the name of the defined character array
printf("Output result:%s\n", str);
return 0;
}

In the first case, no space is entered

Please enter the string: hahaha
 Output result: hahaha
 Please press any key to continue. .

In the second case, there is a space to enter

Please enter a string: I'm so handsome
 Output result: I
 Please press any key to continue. . .

We can see that the system uses spaces as separators between input strings. In other words, as long as you "knock" a space, the system will think that the current string has ended, and then enter the next string, so only the string before the space will be stored in the defined character array.
Then we can define three character arrays at this time

#include <stdio.h>
int main(void)
{
char str1[10], str2[10], str3[10];
printf("Please enter a string:");
scanf("%s%s%s", str1, str2, str3);
printf("Output result:%s %s %s\n", str1, str2, str3);  //%Space should be added between s
return 0;
}
Please enter a string: I'm so handsome
 Output result: I'm so handsome
 Please press any key to continue. . .

decimal system

#include <stdio.h>
int main(void)
{
    int i,j;
	scanf("%d%d", &i, &j);
	/*i: Non output controller*/
	printf("i = %d, j = %d\n", i, j);
return 0;
}
//Use the spacebar
1 2
i = 1, j = 2
 Please press any key to continue. . .
//Use the Enter key
1
2
i = 1, j = 2
 Please press any key to continue. . .
//Use Tab
1       2
i = 1, j = 2
 Please press any key to continue. . .
Other situations

If there is a comma in the statement
(1) English comma

#include <stdio.h>
int main(void)
{
    int i,j;
	scanf("%d,%d", &i,&j);  //English comma
	/*i: Non output controller*/
	printf("i = %d, j = %d\n", i, j);
return 0;
}
1,2   //English comma
i = 1, j = 2
 Please press any key to continue. . .

Comma Chinese input is the same
After going through the knowledge of scanf, let's look at the fifth question after class
5. Input data with scanf to make a=3,b=7,x=8.5,y=71.82,c1 = 'A', c2='a '

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int a,b;
	float x,y;
	char c1,c2;
	scanf("a=%d b=%d",&a,&b);//There should be a space in the middle, otherwise the correct result cannot be output
	scanf("%f%e",&x,&y);
	scanf("%c%c",&c1,&c2);
	printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c",a,b,x,y,c1,c2);
	
system("pause");
return 0;
}
a=3 b=7  //The non output control character is output as is
8.5 71.82Aa
a=3,b=7,x=8.500000,y=71.820000,c1=A,c2=a Please press any key to continue. . .

6. Put "China" "The rule of the password is to replace the original letter with the fourth letter after the original letter. Please write a program to make the values of C1, C2, C3, C4 and C5 be 'C', 'h', 'i', 'n' and 'a' respectively by assigning initial values. After calculation, C1, C2, C3, C4 and C5 become 'G', 'l','m ',' r 'and' e 'respectively and output them.

#include<stdio.h>
#include<stdlib.h>
int main()
{
	char c1,c2,c3,c4,c5;
	printf("Please enter lowercase letters between spaces\n");
	scanf("%c %c %c %c %c",&c1,&c2,&c3,&c4,&c5);
	c1=c1+4-32;
	c2=c2+4;
	c3=c3+4;
	c4=c4+4;
	c5=c5+4;
	printf("The password is:%c%c%c%c%c\n",c1,c2,c3,c4,c5);

	
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
	char c1,c2,c3,c4,c5;
	printf("Please enter lowercase letters between spaces\n");
	scanf("%c %c %c %c %c",&c1,&c2,&c3,&c4,&c5);
	c1=c1+4-32;
	c2=c2+4;
	c3=c3+4;
	c4=c4+4;
	c5=c5+4;
	printf("The password is:");
	putchar(c1);
	putchar(c2);
	putchar(c3);
	putchar(c4);
	putchar(c5);


	
system("pause");
return 0;
}

7. Let the radius of the circle r = 1.5 and the height of the cylinder h = 3. Calculate the circumference, area, surface area, volume and cylinder volume of the circle. Requirements: input data with scanf, taking two digits after the decimal point

#include<stdio.h>
#include<stdlib.h>
#define pi 3.14
int main()
{
	float r,h,l,s,sq,vq,vz;

	printf("Please enter r:");
	scanf("%f",&r);
	printf("Please enter h:");
	scanf("%f",&h);
	l=2*pi*r;
	s=r*r*pi;
	sq=4*pi*r*r;
	vq=3.0/4.0*pi*r*r*r;
	vz=pi*r*r*h;
	printf("Circumference length%6.2f\n",l);
	printf("Circle area is%6.2f\n",s);
	printf("The surface area of the sphere is%6.2f\n",sq);
	printf("The volume of the sphere is%6.2f\n",vq);
	printf("The volume of the cylinder is%6.2f\n",vz);
	
system("pause");
return 0;
}

Please enter r:1.5
 Please enter h:3
 The circumference is 9.42
 The circular area is 7.07
 The surface area of the ball is 28.26
 The volume of the sphere is 7.95
 The volume of the cylinder is 21.19
 Please press any key to continue. . .

Topics: C Back-end