C++ primer plus Chapter 2 Exercises

helloworld.cpp

#include <iostream>

//include is iostream not stdio.h in c

int main(void){

using namespace std;//Using the usage of namespace using
cout << "hello, world!";//cout uses, << and >> redirection characters
cout << endl;
return 0;

}


carrots.cpp

#include <iostream>
int main(void)
{
using namespace std;
int carrots;//variable declaration and usage are the same as c
carrots=25;
cout << "i have " << carrots << " carrots."<<endl<<"Crunch,Crunch,now i have "<<carrots-1<<" carrots"<<endl;

return 0;
}

getinfo.cpp

#include <iostream>
int main(void)
{
using namespace std;
int carrots;
cout <<"how many carrots do you have?"<<endl;
cin >> carrots;//Get input, assign variables
cout <<"here are two more"<<endl;
carrots+=2;
cout <<"Now you have "

<<carrots

<<" carrots"

<<endl;//Multi-line splicing, better format.
return 0;
}

sqrt.cpp

#include <iostream>
#include <cmath>//or match.h
int main(void)
{
using namespace std;
double area;
cout << "Enter the floor area ,in square feet,of your home "<<endl;
cin >> area;
double side;
side = sqrt(area);
cout <<"That's the equivalent of a square "
<<side
<<" feet to the side"
<<endl
<<"How fascinating!"
<<endl;
return 0;
}

//ourfunc.cpp --define our own function
#include <iostream>
void simon(int);//Note that the function declaration needs to be added; include does not need to be added;
int main(){
using namespace std;
int count;
cout <<"pick an integer\n";
cin >> count;
simon(count);
cout<<"done";
return 0;
}
void simon(int n){
using namespace std;//This statement can be placed after include, all functions in this file use std, and each function does not need to be repeated
cout << "Simon says touch your toes "<<n<<" times"<<endl;
}


//convert.cpp
#include <iostream>
double stoneToLb(double);
int main(){
using namespace std;
double stone;
cout<<"Enter the weight in stone"<<endl;
cin >> stone;
double pounds;
pounds=stoneToLb(stone);
cout << stone << " stone = " <<pounds<<" pounds"<<endl;
return 0;
}
double stoneToLb(double sts)
{
return sts*14;
}


exercise:

Review questions:

1. What are the modules of a C++ program called?

function

2. What is the purpose of the following preprocessor compilation directives? #include <iostream>

Replace this directive location with the iostream file before compiling.

3. What is the use of the following statement? using namespace std;

Programs can use constants, methods, etc. defined in std.


//program_exercise1.cpp
//1, write a c++ program that displays your name and address
#include <iostream>
using namespace std;
int main(void){
cout <<"my name is hujiawei"
<<endl
<<"my address is nanjing"
<<endl;
return 0;
}


//2. Write a c++ program that asks the user to enter a distance in long, and then converts it to code (1 long= 220 ma);
//program_exercise2.cpp
#include <iostream>
using namespace std;
int main(void){
double along,ma;
cout <<"Enter the distance in long:";
cin >> along;
ma=220*along;
cout <<"the distance is equivalent to "<<ma<<" ma"<<endl;
return 0;
}

/*3, write a C++ program that uses three user-defined functions, (including main()), and produces the following output:
Three blind mice
Three blind mice
See how they run
See how they run
*/
//program_exercise3 .cpp
#include <iostream>
using namespace std;
void blind(void);
void run(void);
int main(void){
blind();
run();
return 0;
}
void blind(void){
cout <<"Three blind mice"
<<endl
<<"Three blind mice"
<<endl;
}
void run(void){
cout <<"See how they run"
<< endl
<<"See how they run"
<<endl;
}

/*4, write a C++ program that allows the user to enter their age, and then displays how many months the age contains, as follows:
Enter your age:29
*/
//program_exercise4.cpp
#include <iostream>
using namespace std;


int main(void){
int age;
cout << "Enter your age:";
cin >> age;
cout << age<<"age = "<< age*12 << " monthes"<<endl;
return 0;
}

/*5, Write a C++ program in which main() calls a user-defined function (with the Celsius temperature value as a parameter, and returns the corresponding Fahrenheit temperature value).
The program requires the user to enter the Celsius temperature value in the following format. And display the result:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
The following is the conversion formula:
Fahrenheit=1.8*Celsius+32.0
*/
//program_exercise5.cpp
#include <iostream>
using namespace std;
double celToFah( double);


int main(void){
cout <<"Please enter a Celsius value:";
double cel,fah;
cin >>cel;
fah = celToFah (cel);
cout << cel
<< " degrees Celsius is "
<<fah
<<" degrees Fahrenheit."
<<endl;
return 0;
}
double celToFah(double cel){
return 1.8 * cel + 32.0;
}


/*6, write a C++ program whose main() calls a user-defined function (with a light-year value as a parameter and returns the value of the corresponding astronomical unit).
The program requires the user to input the light-year value in the following format, and Display result:
Enter the number of light years: 4.2
4.2 light years = 265608 astronomical units.
The astronomical unit is the average distance from the earth to the sun
1 light year = 63240 astronomical units
*/
//program_exercise6.cpp
#include <iostream>
using namespace std;
double lyToAu(double);
int main(void){
double ly;
cout <<"Enter the number of light years:";
cin >>ly;
cout << ly<<" light years = "<<lyToAu(ly)<<" astronomical units."<<endl;
return 0;
}
double lyToAu(double ly){
return 63240.0*ly;
}

/*7, Write a C++ program that requires the user to enter the hours and minutes. In the main() function, give these two values ​​to a void function, which
displays the two values ​​in the following format:
Enter the number of hours:9
Enter the number of minutes:28
Time: 9:28
*/
//program_exercise7.cpp
#include <iostream>
using namespace std;
void showTime(int ,int);
int main(void){
cout <<"Enter the number of hours:";
int h,m;
cin>>h;
cout <<"Enter the number of minutes:";
cin>>m;
showTime(h,m);
return 0;
}
void showTime(int h,int m){
cout<<"Time: "<<h<<":"<<m<<endl;

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325611689&siteId=291194637