# Introduction to C + + (practice + code) - 05

1, Experimental purpose

1. Understand the programming ideas and methods of "processing one or some operations that need to be conditionally repeated";

2. Master the program design of loop structure and the use of while, do while and for statements.

1. A monkey picked N peaches. On the first day, he ate half and one more. On the second day, he ate the remaining half and one more. When he was ready to eat on the tenth day, he found another one. Program to calculate and output how many peaches the monkey picked. See sample sheet for input and output format: (one of two samples)

Example code:

//Number of peaches
#include<iostream>
using namespace std;
int main()
{
double s=1;
int i;
cout<<"Please enter the number of days you have eaten a peach when there is one peach left:";
cin>>i;
for(i;i>0;i--)
{
s=(s+1)*2;
cout<<"The first"<<i<<"The total number of peaches in the sky is"<<s<<endl;
if(i==1)
{
cout<<endl;
cout<<"The original number of peaches is"<<s<<"individual"<<endl;
}

}
return 0;
}

2. Program to realize the conversion between decimal integer and octal integer (i.e. convert octal number to octal number, or convert octal number to octal number). See sample sheet for input and output format: (one of two samples)

Example code:

//Binary conversion
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int f1(int x)
{
int wq=1,k,r=0;
while(x!=0)
{
k=x%10;
r=r+k*wq;
wq=wq*8;
x=x/10;
}
return r;
int f2(int x)
{
int wq=1,k,r=0;
while(x!=0)
{
k=x%8;
r=r+k*wq;
wq=wq*10;
x=x/8;
}
return r;
int main()
{
int n,b;
char a;
a='y';
cout.fill('*');
while(a=='y')
{
cout<<setw(21)<<"*"<<endl;
cout<<"    Binary conversion system"<<endl;
cout<<"1, Octal to decimal"<<endl;
cout<<"2, Convert decimal to octal"<<endl;
cout<<setw(21)<<"*"<<endl;
cout<<"Please select serial number 1 or 2:";
cin>>b;
cout<<"Please enter the data to be converted:";
cin>>n;
if(b==1)
{
cout<<"Convert to:"<<f1(n)<<endl;
}
if(b==2)
{
cout<<"Convert to:"<<f2(n)<<endl;
}
cout<<"Continue conversion? y/n: ";
cin>>a;
if(a=='n')
{
cout<<"Thank you for using! bye!"<<endl;
}

}
return 0;
}

3. Calculate the maximum common divisor and minimum common multiple of the two numbers entered by the keyboard. See sample sheet for input / output format:

Example code:

//Maximum common multiple and minimum common divisor
#include<iostream>
using namespace std;
int f(int x,int y)
{
int r=1;
if(x<y)
{
x=x+y;
y=x-y;
x=x-y;
}
while(r!=0)
{
r=x%y;
x=y;
y=r;
if(x<y)
{
x=x+y;
y=x-y;
x=x-y;
}
}
return x;
}
int main()
{
int a,b,s;
cin>>a>>b;
cout<<a<<"and"<<b<<"The maximum common divisor of is:"<<f(a,b)<<endl;
cout<<a<<"and"<<b<<"The least common multiple of is:"<<(a*b)/f(a,b)<<endl;
return 0;
}

4. Input an angle value y from the keyboard (the angle value needs to be converted into radian value during calculation: x=y*PI/180), and the value of PI is 3.14159265. To find the approximate value of sin(x), the truncation error is required to be less than 10-7, that is, the calculation shall be stopped when the value of the general term formula is less than 10-7. The approximate calculation formula is as follows:

See sample sheet for input / output format:

Note: when you enter 180 or 360, the program running result is not 0. The calculation results will be different with the accuracy of PI, but it must be a number very close to 0. The data in the figure is calculated when = 3.14159265.

Tip:

This approximate calculation can be regarded as an accumulation process, and the key is to determine the number of accumulation items. If item saves item n, the method to launch item n+1 is: item=item*x*x/((2*n)*(2*n+1))

Example code:

//sin Value calculation
#include<iostream>
using namespace std;
int main()
{
const long double PI=3.14159265;
double x,y,item,sum;
int n=1,s=1;
cin>>x;
y=x*PI/180;
item=y,sum=0;
do
{
sum=sum+item*s;
item=item*y*y/((2*n)*(2*n+1));
s=-s;
n++;
}while(item>=1e-7);
cout<<"sin("<<x<<")="<<sum<<endl;
return 0;
}

5. Input the salary income of several employees (ending with a negative number), output the salary income of each employee, calculate and output the tax paid by each employee, and count and output the total number of employees and total tax paid.

Tax calculation method: (see experiment 3-5: according to the national tax policy, the monthly taxable threshold is 5000 yuan; if it does not exceed 3000 yuan, the excess part will pay personal income tax at 3%; if it exceeds 3000-12000 yuan, the excess part will pay personal income tax at 10%; if the monthly taxable income exceeds 12000-25000 yuan, the excess part will pay personal income tax at 20%. 2 See sample sheet for input / output format of 5000 (not considered)

Example code:

//Tax value calculation
#include<iostream>
#include<iomanip>
using namespace std;
double f(double n)
{
double s;
if(0<=n&&n<=5000)
{
s=0;
}
if(n<=8000&&n>5000)
{
s=(n-5000)*0.03;
}
if(n>8000&&n<=17000)
{
s=((n-8000)*0.10)+90;
}
if(n<=30000&&n>17000)
{
s=((n-17000)*0.20)+90+900;
}
return s;
}
int main()
{
int i=1,t=0;
double n,s=0;
cout.fill('*');
do
{
cout<<"Please enter page"<<i<<"Employee's salary (negative end):";
cin>>n;
if(n<0)
{
cout<<endl;
cout<<"The total number of employees is:"<<i-1<<",The total tax paid is:"<<s<<endl;
t=-1;
}
else
{
s+=f(n);
cout<<"The first"<<i<<"The salary of each employee is:"<<n-f(n)<<"，The tax paid is:"<<f(n)<<endl;
i++;
cout<<setw(40)<<"*"<<endl;

}

}
while(t>=0);
return 0;
}

Added by space1 on Fri, 04 Feb 2022 05:04:33 +0200