I. experimental purpose
1. Master the programming ideas and methods of sequential structure;
2. Master formatted input and output and data type conversion.
2, Experimental task
1. Input a lowercase letter from the keyboard, convert it into uppercase letters, and output the two uppercase and lowercase letters and their ASCII code values. See sample sheet for input / output format:
Example code:
//Lowercase letters, converting them to uppercase letters #include<iostream> #include<cstdio> #include<string> using namespace std; int main() { char a; char b; int c,d; cout<<"Please enter a lowercase letter:"; cin>>a; c=a; cout<<"The lower case letters entered are:"<<a<<","<<"his ASCII The code is:"<<c<<endl; b=a-32; d=b; cout<<"The corresponding capital letters are:"<<b<<","<<"his ASCII The code is:"<<d<<endl; return 0; }
2. Input two integers from the keyboard (requirements: the two numbers cannot be divided, and the second number is not 0), calculate the quotient of the two numbers, output the results, and understand the difference between integer division and real division. See sample sheet for input / output format:
Example code:
//Division operation #include<iostream> #include<cstdio> #include<string> using namespace std; int main() { int a,b,s; cout<<"Please enter two integers:"; cin>>a>>b; s=a/b; cout<<"Integer division:"<<a<<"/"<<b<<"="<<s<<endl; cout<<"Real division:"<<a<<"/"<<b<<"="<<(float)a/b<<endl; return 0; }
3. Input the radius of the ball from the keyboard, calculate the surface area and volume of the ball, and output the results. π is a symbolic constant, and the value is 3.14159. Formula:. Requirements: keep two decimal places for the surface area of the sphere and four decimal places for the volume of the sphere. See sample sheet for input / output format:
Example code:
//Calculate the volume and area of the sphere #include<iostream> #include<cstdio> #include<string> using namespace std; int main() { double s,v,r; cout<<"Please enter the radius:"; cin>>r; s=4*3.14159*r*r; v=(4*3.14159*r*r*r)/3.0; cout<<"The sphere area is:"; printf("%.2f\n",s ); cout<<"The volume of the sphere is:"; printf("%.4f\n",v ); return 0; }
4. Calculate universal gravitation.
As known from ordinary physics, the universal gravitation F between two objects with masses m1 and m2 is directly proportional to the product of the masses of the two objects and inversely proportional to the square of the distance R between the centroids of the two objects, that is:
Where G is the gravitational constant. If the unit of F is Newton (N), the unit of mass is kilogram (kg), and the unit of distance from the center of mass of the object is meter (m), then. It is known that the mass of the sun is kg, the mass of the earth is kg, and the distance between the sun and the earth is m. please calculate the universal gravitation between the sun and the earth. Requirement: G uses symbolic constants. See sample sheet for output format:
Example code:
//Calculate universal gravitation #include<iostream> #include<cstdio> #include<string> #include<iomanip> using namespace std; int main() { double m1,m2,r,f; const double G=6.67259e-11; m1=1.987e30; m2=5.975e24; r=1.495e11; f=(G*m1*m2)/(r*r); cout<<"The mass of the sun is:"<<fixed<<setprecision(0)<<m1<<endl; cout<<"The mass of the earth is:"<<m2<<endl; cout<<"The distance between the sun and the earth is:"<<r<<endl; cout<<"The gravitational force between the sun and the earth is:"<<f<<endl; return 0; }
5. It is known that a classmate has taken the examinations of advanced mathematics, college physics and College English. Programming: input the student number (11 digits) and the scores of the three subjects from the keyboard, calculate the average score, and output the transcript.
Requirements: (1) as an integer, the student number is beyond the storage range of int variables, so it needs to be stored with double variables, but the student number in integer form is required to be displayed during output.
(2) The names entered from the keyboard are stored in string variables.
(3) The scores of the three subjects in the transcript shall be kept to one decimal place, and the average score shall be kept to two decimal places.
(4) See sample sheet for input / output format:
Example code:
//Output transcript #include<iostream> #include<cstdio> #include<string> #include<iomanip> using namespace std; int main() { double a,b,c,s,n; string name; cout<<"Please enter the student number, name and grade of three courses:"<<endl; cin>>n>>name>>a>>b>>c; cout<<endl; cout<<setw(32)<<setfill('*')<<"school report"<<setw(22)<<"*"<<endl; cout.fill(' '); cout<<setw(8)<<"learn"<<setw(4)<<"number"<<setw(8)<<"surname"<<setw(4)<<"name"; cout<<setw(10)<<"Advanced mathematics"<<setw(10)<<"general physics "<<setw(10)<<"College English"<<endl;//Output the second line; cout<<fixed<<setprecision(0)<<setw(12)<<n<<setw(12)<<name<<setprecision(1)<<setw(10)<<a<<setw(10)<<b<<setw(10)<<c<<endl; s=(a+b+c)/3; cout<<"Average score:"<<setprecision(2)<<s<<endl; cout<<setfill('*')<<setw(54)<<"*"<<endl; return 0; }
Summary:
In this experiment, both question 3 and question 4 realize formatted output, but the method is different. Because it is C + + code, it is recommended to refer to the way of question 4 to format output.