C++ Primer Plus第六版编程练习---第2章 开始学习C++
1.
#include <iostream>
int main()
{
std::cout << "名字:草丛里的蚂蚱 " << std::endl << "地址:http://www.cnblogs.com/f00345709/" <<std::endl;
return 0;
}
2.
/*
码 是英美制的长度单位,英文名称是yard,简称yd. 1码(yd) = 3英尺(ft) = 36英寸(in) = 0.9144米(m)
long 是什么还真没搜到,不过这个不影响编程
*/
#include <iostream>
int main()
{
double distance = 0.0;
std::cout << "Please input a distance, unit is long." << std::endl;
std::cin >> distance;
double yard = distance*220;
std::cout << "It is " << yard << " yard " << std::endl;
return 0;
}
3.
#include <iostream>
#include <string>
void printString(std::string& toPrintStr);
void printStringRepeat(int times, std::string& toPrintStr);
int main()
{
int times = 2;
std::string srcStr1("Three blind mice");
std::string srcStr2("See how they run");
printString(srcStr1);
printString(srcStr1);
printStringRepeat(times, srcStr2);
return 0;
}
void printStringRepeat(int times, std::string& toPrintStr)
{
for(int i=0;i<times;i++)
{
std::cout << toPrintStr << std::endl;
}
return;
}
void printString(std::string& toPrintStr)
{
std::cout << toPrintStr << std::endl;
return;
}
4.
#include <iostream>
int transAgeToMonthes(int age);
int main(int argc,char* argv[])
{
int age = 0;
std::cout << "Please input your age." << std::endl;
std::cin >> age;
if(age <= 0)
{
std::cout << "Are you kidding me? Please input your age again, I'm serious." << std::endl;
std::cin >> age;
}
std::cout << "It is " << transAgeToMonthes(age) << " month that you have already alive in this word, enjoy it."<< std::endl;
return 0;
}
int transAgeToMonthes(int age)
{
return age*12;
}
5.
#include <iostream>
double transCelsiusToFahrenheit(double celsiusDegrees);
int main(int argc,char* argv[])
{
double celsiusDegrees = 0;
std::cout << "Please enter a Celsius value: ";
std::cin >> celsiusDegrees;
std::cout << celsiusDegrees << " degrees Celsius is " << transCelsiusToFahrenheit(celsiusDegrees) << " degrees Fahrenheit."<< std::endl;
return 0;
}
double transCelsiusToFahrenheit(double celsiusDegrees)
{
return (1.8*celsiusDegrees + 32.0);
}
6.
#include <iostream>
double transLightYearsToAstronomicalUnits(double lightYears);
int main(int argc,char* argv[])
{
double lightYears = 0;
std::cout << "Enter the number of light years: ";
std::cin >> lightYears;
std::cout << lightYears << " light years = " << transLightYearsToAstronomicalUnits(lightYears) << " astronomical units."<< std::endl;
return 0;
}
double transLightYearsToAstronomicalUnits(double lightYears)
{
return (63240*lightYears);
}
7.
#include <iostream>
void printTime(int hours, int minutes);
int main(int argc,char* argv[])
{
int hours = 0;
int minutes = 0;
std::cout << "Enter the number of hours: ";
std::cin >> hours;
std::cout << "Enter the number of minutes: ";
std::cin >> minutes;
printTime(hours, minutes);
return 0;
}
void printTime(int hours, int minutes)
{
if(0 > hours || 0 > minutes || 24 < hours || 60 < minutes)
{
std::cout << "Format of hours or minutes is not OK."<< std::endl;
return;
}
std::cout << "Time: " << hours << ":" << minutes << std::endl;
}
浙公网安备 33010602011771号