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.

/*不明白为什么作者要求是3个函数,个人感觉下面的两个函数实现起来更好*/

#include <iostream>

void printFirstGroup();
void printSecondGroup();

int main()
{
printFirstGroup();
printSecondGroup();
return 0;
}

void printFirstGroup()
{
for(int i=0;i<2;i++)
{
std::cout << "Three blind mice" << std::endl;
}
return;
}

void printSecondGroup()
{
for(int i=0;i<2;i++)
{
std::cout << "See how they run" << std::endl;
}
return;
}

3.

#include <iostream>

#include <string>

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");
printStringRepeat(times, 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;
}

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;
}

猜你喜欢

转载自www.cnblogs.com/f00345709/p/8948408.html