(一)带框架的问候语
1.
题意描述:练习2-4
用一个单独的输出表达式输出上下边框和问候语之间的空白行,而不是每次只输出其中一个字符。
2.
问题分析与解决
书中的程序是用for循环和判断语句,每次输出“*”或“ ”,以达到输出上下边框和问候语之间的空白行的目的,从空白行可以得知,每个空白行都是相同的,而且空白行只有首尾才是“*”,于是就可以根据这点构造空白行,而后用一个单独的输出表达式输出这个空白行。而构造空白行时,可以先简单调用string的构造函数构造空白的字符,然后用重载的“+”构造空白行即可。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::getline;
// 用一个单独的输出表达式输出上下边框和问候语之间的空白行,而不是每次只输出其中一个字符。
int main()
{
//ask for the person's name
cout << "please enter your name: ";
//read the name
std::string name;
getline(cin, name);
//build the message that intend to write
const std::string greeting = "Hello," + name + "!";
//the number of blanks surrounding the greeting
const int pad = 1;
//the number of rows and columns to write
const int rows = pad * 2 + 3;
std::string::size_type cols = greeting.size() + pad * 2 + 2;
//define blank_line
std::string spaces(cols-2, ' ');
std::string blank_line = "*" + spaces + "*";
cout << endl;
for (int r = 0; r != rows; ++r) {
std::string::size_type c = 0;
while (c != cols) {
// is it time to write the greeting?
if (r == pad + 1 && c == pad + 1) {
cout << greeting;
c += greeting.size();
}
else {
// are we on the border
if (r == 1 || r == rows - 2) {
cout << blank_line;
c += blank_line.size();
}
else if (r == 0 || r == rows - 1 || \
c == 0 || c == cols -1) {
cout << "*";
++c;
}
else {
cout << " ";
++c;
}
}
}
cout << endl;
}
system("PAUSE");
return 0;
}
(二)改进的带框架的问候语
1.
题意描述:改写第2章的程序,由用户指定问候语与框架之间的空白符数目(上下左右共4个数目,并可能各不相同)。
2.
问题分析与解决
这个程序需要与用户交互,设定问候语与框架之间的空白符数目。这只需设置四个变量控制,利用这四个变量构造空白行,更改行数和列数,即可简单解决问题。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
// 由用户指定问候语与框架之间的空白符数目(上下左右共4个数目,并可能各不相同)。
void set_num_of_blank_spaces(int &up, int &down, int &left, int &right)
{
cout << "please enter natural number num of blank spaces up: ";
cin >> up;
cout << "please enter natural number num of blank spaces down: ";
cin >> down;
cout << "please enter natural number num of blank spaces left: ";
cin >> left;
cout << "please enter natural number num of blank spaces right: ";
cin >> right;
}
int main()
{
//ask for the person's name
cout << "please enter your first name: ";
//read the name
std::string name;
cin >> name;
//build the message that intend to write
const std::string greeting = "Hello," + name + "!";
int up, down, left, right;
set_num_of_blank_spaces(up, down, left, right);
//the number of rows and columns to write
const int rows = up + down + 3;
std::string::size_type cols = greeting.size() + left + right + 2;
//define blank_line
std::string spaces(cols-2, ' ');
std::string blank_line = "*" + spaces + "*";
cout << endl;
for (int r = 0; r != rows; ++r) {
std::string::size_type c = 0;
while (c != cols) {
// is it time to write the greeting?
if (r == up + 1 && c == left + 1) {
cout << greeting;
c += greeting.size();
}
else {
// are we on the border
if ((r >= 1 && r <= up) || \
(r >= up + 2 && r <= rows - 2)) {
cout << blank_line;
c += blank_line.size();
}
else if (r == 0 || r == rows - 1 || \
c == 0 || c == cols -1) {
cout << "*";
++c;
}
else {
cout << " ";
++c;
}
}
}
cout << endl;
}
return 0;
}
(三)字符‘*’作图
1.
题意描述:输出一个“*”字符集,并使它们分别构成一个正方形,长方形,汉字的“王”字。由用户指定正方形的边长,长方形的长和宽,“王”字上中下三横分别的长度及中间一竖的高度。
2.
问题分析与解决
正方形和长方形都属于矩形,分别构造矩形和“王”的结构体,保存它们的相关数据。
矩形相关的有长和宽,上下部和中间的string,而“王”只需描述“王”字上中下三横分别的长度及中间一竖的高度。由于没有学到类,只好创建相关的函数构造矩形和“王”。
构造矩形,只需要根据长和宽得出上下部和中间的string,再输出上下部的string,并在其间循环输出中间的string。
构造“王”字,就更加简单了,但是为了美观,我把上中下三横长度中非奇数的变成比用户输入大的最小奇数。找出这三者中最大的长度,根据数学的简单计算,决定先输出多少个空格,再输出该长度的“*”,在两两之间输出一个“*”及其前面的若干空格(用三者中最大的长度换算而来)。
// rectangle.h
#ifndef GUARD_rectangle_h
#define GUARD_rectangle_h
#include <string>
struct Rectangle {
int length, width;
std::string topAndBottom, mid;
};
void makeRectangle(Rectangle&);
#endif
// rectangle.cpp
#include "rectangle.h"
#include <string>
#include <iostream>
using namespace std;
const string subs = " *";
void makeRectangle(Rectangle& rec)
{
rec.topAndBottom = "*";
for (int i = 0; i < rec.length - 1; ++i) {
rec.topAndBottom.append(subs);
}
rec.mid = "*" + string(rec.length * 2 - 3, ' ') + "*";
cout << rec.topAndBottom << endl;
for (int i = 0; i < rec.width - 2; ++i)
cout << rec.mid << endl;
if (rec.width >= 2)
cout << rec.topAndBottom << endl;
}
// wang.h
#ifndef GUARD_wang_h
#define GUARD_wang_h
#include <string>
struct Wang {
int topLengthofWang,
midLengthofWang,
bottomLengthofWang,
highLengthofWang;
};
void makeWang(Wang&);
void fixWang(Wang&);
#endif
// wang.cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "wang.h"
using namespace std;
void fixWang(Wang& wang)
{
if (wang.topLengthofWang % 2 == 0) {
cout << "warning: your top length of Wang isn't odd number" << endl
<< "it will be fixed to be the least odd number bigger than yours:" << ++wang.topLengthofWang << endl;
}
if (wang.midLengthofWang % 2 == 0) {
cout << "warning: your mid length of Wang isn't odd number" << endl
<< "it will be fixed to be the least odd number bigger than yours:" << ++wang.midLengthofWang << endl;
}
if (wang.bottomLengthofWang % 2 == 0) {
cout << "warning: your bottom length of Wang isn't odd number" << endl
<< "it will be fixed to be the least odd number bigger than yours:" << ++wang.bottomLengthofWang << endl;
}
if (wang.highLengthofWang % 2 == 0) {
cout << "warning: your high length of Wang isn't odd number" << endl
<< "it will be fixed to be the least odd number bigger than yours:" << ++wang.highLengthofWang << endl;
}
}
void makeWang(Wang& wang)
{
int maxlen;
maxlen = max(wang.topLengthofWang, wang.midLengthofWang);
maxlen = max(maxlen, wang.bottomLengthofWang);
cout << string(maxlen/2 - wang.topLengthofWang/2, ' ')
<< string(wang.topLengthofWang, '*') << endl;
for (int i = 1; i < wang.highLengthofWang - 1; ++i) {
if (i == wang.highLengthofWang/2) {
cout << string(maxlen/2 - wang.midLengthofWang/2, ' ')
<< string(wang.midLengthofWang, '*') << endl;
} else {
cout << string(maxlen/2, ' ') << "*" << endl;
}
}
cout << string(maxlen/2 - wang.bottomLengthofWang/2, ' ')
<< string(wang.bottomLengthofWang, '*') << endl;
}
// 作图.cpp
#include <iostream>
#include <string>
#include <algorithm>
#include "wang.h"
#include "rectangle.h"
using namespace std;
void makePicture(int which)
{
Rectangle targeRec;
Wang targeWang;
switch(which) {
case 1:
cout << "Please enter the length of side(integer):";
cin >> targeRec.length;
targeRec.width = targeRec.length;
makeRectangle(targeRec);
break;
case 2:
cout << "Please enter the length of length and width(integer):";
cin >> targeRec.length >> targeRec.width;
makeRectangle(targeRec);
break;
case 3:
cout << "Please enter the top, mid and the bottom length of Wang(integer):";
cin >> targeWang.topLengthofWang >> targeWang.midLengthofWang
>> targeWang.bottomLengthofWang;
cout << "Please enter the high of Wang(integer):";
cin >> targeWang.highLengthofWang;
fixWang(targeWang);
makeWang(targeWang);
break;
default:
cerr << "Sorry, you enter a wrong number!" << endl;
break;
}
}
// 输出一个“*”字符集,并使它们分别构成一个正方形,长方形,汉字的“王”字。由用户指定正方形的边长,长方形的长和宽,“王”字上中下三横分别的长度及中间一竖的高度。
int main()
{
int whichPicture;
bool retry = true;
string yOrN;
while (retry) {
cout << "Please enter the number of which picture you need to draw:" << endl
<< "1、square; 2、rectangle; 3、Chinese character Wang:";
cin >> whichPicture;
makePicture(whichPicture);
cout << "Draw another one(Y or N):";
cin >> yOrN;
if (yOrN == "Y" || yOrN == "y")
retry = true;
else if (yOrN == "N" || yOrN == "n")
retry = false;
else {
cerr << "Sorry, you enter wrong" << endl;
exit(1);
}
}
return 0;
}
(四)字符串替换
1.
题意描述:
提示用户从键盘输入一句话(其中可能包含空格,以回车符作为结束),将其中的大写字母换成对应的小写字母,小写字母换成对应的大写字母。为避免不雅字眼,还需要将其中的“bullshit”(不论大小写)替换为“bush”(全部小写)。最后将替换前后的两句话分别显示在屏幕上。 要求使用string对象和应用于string对象的函数。
2.
问题分析与解决
先查找“bullshit”(不论大小写)将它替换成“BUSH”(全部大写),减少后面替换的次数。然后将其中的大写字母换成对应的小写字母,小写字母换成对应的大写字母。就能完成用户需求。
查找“bullshit”(不论大小写),用search函数,自己写个BinPred
p,以满足查找对大小写字母不敏感,其中的替换用replace。替换用BinaryFunction f,自己写个UnaryFunction f用于满足大小写转换。
#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;
inline bool caseInsCharCmp(char a, char b)
{
return (toupper(a) == toupper(b));
}
char ChangeUpperLower(char ch)
{
if ( islower(ch) ) {
return toupper(ch);
} else if ( isupper(ch) ) {
return tolower(ch);
} else {
return ch;
}
}
int main()
{
string sentenceNeedToBeChanged;
string bullshit = "bullshit";
string bush = "BUSH";
cout << "Please enter a sentence need to changed, end with the enter" << endl;
getline(cin, sentenceNeedToBeChanged);
string::const_iterator ithOfBullshit,
begin = sentenceNeedToBeChanged.begin();
// 查找“bullshit”(不论大小写)
while ( (ithOfBullshit = search(sentenceNeedToBeChanged.begin(), sentenceNeedToBeChanged.end(),
bullshit.begin(), bullshit.end(), caseInsCharCmp)) != sentenceNeedToBeChanged.end())
{
// 将其中的“bullshit”(不论大小写)替换为“bush”(全部小写)
sentenceNeedToBeChanged.replace(ithOfBullshit - begin, bullshit.size(), bush.c_str(), bush.size());
}
// 将其中的大写字母换成对应的小写字母,小写字母换成对应的大写字母
transform(sentenceNeedToBeChanged.begin(), sentenceNeedToBeChanged.end(), sentenceNeedToBeChanged.begin(), ChangeUpperLower);
cout << sentenceNeedToBeChanged << endl;
system("PAUSE");
return 0;
}
/*
12bullShit 1 2 sdfj4buLLShit54234@#%asDJOklaBUlLShIt6%&*(%
*/
文章评论(0条评论)
登录后参与讨论