C ++ premier plus the sixth edition of Programming Solutions for Practice (Chapter II)

1. Write a C ++ program to display the name and address

#include<iostream>
int main(void)
{
	using namespace std;
	cout << "name";
	cout << endl;
	cout << "address";
	cout << endl;
	return 0;
}

2. Write a C ++ program, it requires the user to enter a long distance unit, and converts it into the code (a long code at 220)

#include<iostream>
int main(void)
{
	using namespace std;
	int distance1,distance2;
	cout << "请输入以long为单位的距离" << endl;
	cin >> distance1;
	distance2 = 220 * distance1;
	cout << distance1 << "long=" << distance2 << "码" << endl;
	return 0;
}

3. Preparation of a C ++ program that uses three user-defined function (including Mian ()), and generates the following output:

Blind MICE Three
Three Blind MICE
See How They RUN
See How They RUN
wherein a function to be called twice, first two lines of the function generator; also another function is called twice and generates the remaining output.

#include<iostream>
using namespace std;

void mice(void);
void run(void);

int main(void)
{
	mice();
	mice();
	run();
	run();
	return 0;
}
void mice(void)
{
	cout << "Three blind mice" << endl;
}
void run(void)
{
	cout << "See how they run" << endl;
}

4. Write a program that allows users to input their age, and then displays the number of months of age contains.

#include<iostream>
int main()
{
	using namespace std;
	int year,month;
	cout << "Enter your age:";
	cin >> year;
	month=12*year;
	cout << "该年龄包含" << month << "个月"return 0;
}

The preparation of a program in which the main () calls a user-defined function (in degrees Celsius as a reference value, and returns the corresponding value Fahrenheit).

Similar to the fourth question, but need to use a function of knowledge.

#include<iostream>

float change(float celsius);

int main()
{
	using namespace std;
	float celsius,fahrenheit;
	cout << "Please enter a Celsius value: ";
	cin >> celsius;
	fahrenheit = change(celsius);
	cout << celsius << " degrees Celsius is " << fahrenheit <<" degrees Fahrenheit." << endl;
	return 0;
}

float change(float celsius)
{
	float fahrenheit;
	fahrenheit = 1.8 * celsius + 32.0;
	return fahrenheit;
}

6. Develop a program that main () calls a user-defined function (light years value and returns the corresponding value AU)

The fifth question is similar, but larger data, use double variables to store.

#include<iostream>

double change(double years);

int main()
{
	using namespace std;
	double years,units;
	cout << "Enter the number of light years: ";
	cin >> years;
	units = change(years);
	cout << years << " light years =  " << units <<" astronomical units." << endl;
	return 0;
}

double change(double years)
{
	double units;
	units = 63240 * years;
	return units;
}

7. The preparation of a program requires the user to enter the number of hours and minutes. In the main () function, these two values ​​are passed to a void function, which shows the two values ​​takes the following form.

#include<iostream>

using namespace std;

void print(int hour,int minute);

int main()
{
	int hour,minute;
	cout << "Enter the number of hours: ";
	cin >> hour;
	cout << "Enter the number of minutes: ";
	cin >> minute;
	print(hour,minute);
	return 0;
}

void print(int hour,int minute)
{
	cout <<"Time: " << hour << ":" << minute << endl;
}
Published 17 original articles · won praise 10 · views 422

Guess you like

Origin blog.csdn.net/acslsr/article/details/103996618