2.0、请求某人输入其姓名,然后向这个人发出问候
#include <iostream>
#include <string>
using namespace std;
int main()
{
//请求用户输入姓名
cout << "please enter your first name: ";
//读入用户输入的姓名
string name;
cin >> name;
//构造我们将要输出的信息
const string greeting = "hello, " + name + "!";
//围住问候语的空白个数
const int pad = 1;
//待输出的行数与列数
const int rows = 2 * pad + 3;
const string::size_type cols = greeting.size() + 2 * pad + 2;
//输出一个空白行,将输出和输入分隔开
cout << endl;
//输出row行
//不变式:到目前为止,我们已经输出了r行
for (int r = 0; r < rows; r ++)
{
string::size_type c = 0;
//不变式:到目前为止,我们已经输出了c 个字符
while(c != cols)
{
//是否应该输出问候语了?
if (r == pad + 1 && c == pad + 1)
{
cout << greeting;
c += greeting.size();
}else{
//我们是否位为边界上
if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
cout << "*";
else
cout << ' ';
c ++;
}
}
cout << endl;
}
return 0;
}
运行结果
2.1改成框架没有间隔的问候语
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "please enter your first name: ";
string name;
cin >> name;
const string greeting = "hello, " + name + "!";
const int rows =3;
const string::size_type cols = greeting.size()+ 2;
cout << endl;
for (int r = 0; r < rows; r ++)
{
string::size_type c = 0;
while(c != cols)
{
if (r == 1 && c ==1)
{
cout << greeting;
c += greeting.size();
}else{
cout << "*";
c ++;
}
}
cout << endl;
}
return 0;
}
运行结果
2.3重写框架,让用户自己提供在框架和问候语间的空格个数
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "please enter your first name: ";
string name;
cin >> name;
//用户提供空格个数
cout << endl << "please enter the number of pad you want: ";
int pad;
cin >> pad;
const string greeting = "hello, " + name + "!";
const int rows = 2 * pad + 3;
const string::size_type cols = greeting.size() + 2 * pad + 2;
cout << endl;
for (int r = 0; r < rows; r ++)
{
string::size_type c = 0;
while(c != cols)
{
if (r == pad + 1 && c == pad + 1)
{
cout << greeting;
c += greeting.size();
}else{
if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
cout << "*";
else
cout << ' ';
c ++;
}
}
cout << endl;
}
return 0;
}
运行结果:
2.5编写一个程序,让它输出一系列的"*"字符,程序输出的这些字符将构成一个正方形,一个长方形,一个三角形
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "**" << endl;
cout << "**" << endl;
cout << endl;
cout << "***" << endl;
cout << "***" << endl;
cout << endl;
cout << "*" << endl;
cout << "**" << endl;
return 0;
}
运行结果:
2.7编写一个程序来依次输出从10~-5的整数
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = 10;
while( i >= -5){
cout << i << endl;
i -= 1;
}
return 0;
}
运行结果:
2.8编写一个程序来计算区间[1.10)中的所有数值的乘积。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int sum = 1;
for(int i = 1; i < 10; i ++)
{
sum *= i;
}
cout << "sum = " <<sum;
return 0;
}
运行结果:
2.9编写一个程序,让用户输入两个数值并告知用户在这两个数值中哪一个较大
<pre name="code" class="cpp">#include <iostream>
#include <string>
using namespace std;
int main()
{
int a, b;
cout << "please input two fingers: ";
cin >> a >> b;
if (a >= b)
cout << "the first number is bigger" << endl;
else
cout << "the second number is bigger" << endl;
}
运行结果:
<img src="https://img-blog.youkuaiyun.com/20150303085315513?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamlhbmdqaWVxYXp3c3g=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />