March 25, 2022

C++ Primer Plus Ch02

梦回C语言,一开始头文件还是会习惯打成stdio,看完第二章感觉和C还是差不多的,学这本主要是想搞懂C++语法和类对象的概念,不过前几张好像和C都差不多。–3.25

CH02 Code Answer 1:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

//using namespace std;

int main(void)
{
// using std::cout;
std::cout << "P.Z TIME!";

return 0;
}

CH02 Code Answer 2:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main(void)
{
long n;
cin >> n;
cout << "Translate: " << n * 220 << "!";

return 0;
}

CH02 Code Answer 3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

void Print1(void);
void Print2(void);

int main(void)
{
Print1();
Print1();
Print2();
cout << endl;
Print2();

return 0;
}

void Print1(void)
{
cout << "Three blind mice" << endl;
}

void Print2(void)
{
cout << "See how they run";
}

CH02 Code Answer 4:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main(void)
{
int age;
cout << "Enter your age:";
cin >> age;
cout << "Your have been to live " << age * 12 << " months in the world!";

return 0;
}

CH02 Code Answer 5:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

double CelsiusToFahrenheit(double celsius);

int main(void)
{
double celsius;
cout << "Please enter a Celsius value: ";
cin >> celsius;
cout << celsius << " degrees Celsius is " << CelsiusToFahrenheit(celsius) << " degrees Fahrenheit!";

return 0;
}

double CelsiusToFahrenheit(double celsius)
{
return celsius * 1.8 + 32;
}

CH02 Code Answer 6:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

double LightToAstronomical(double light);

int main(void)
{
double light;
cout << "Enter the number of light years: ";
cin >> light;
cout << light << " light years = " << LightToAstronomical(light) << " astronomical units.\n";
cout << "You like this number. Right?";
// See you in galaxy.
return 0;
}

double LightToAstronomical(double light)
{
return light * 63240;
}

CH02 Code Answer 7:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

void ShowTime(int hours, int minutes);

int main(void)
{
int hours, minutes;

cout << "Enter the number of hours: ";
cin >> hours;
cout << "Enter the number of minutes: ";
cin >> minutes;
ShowTime(hours, minutes);

return 0;
}

void ShowTime(int hours, int minutes)
{
cout << "Time: " << hours << ":" << minutes;
}
DASCTF X SU
🍬
HFCTF2022
🍪

About this Post

This post is written by P.Z, licensed under CC BY-NC 4.0.