[College] “C ++ program design” P66 case 2.6 analysis

Example 2.6

Title description

According to the input capital letters, output a “character matrix” consisting of letters and spaces.

Input format

One uppercase letter.

Output format

Corresponding “character matrix”.

Sample input

D

Sample output

 A

B B B
C C C C C
D D D D D D D
C C C C C
B B B
A

Question Analysis

According to the example, we can actually easily find the law of this “character matrix”. Let us first use mathematical expressions to describe the law of letters: the entire “character matrix” can be seen as a diamond, from top to bottom, each line is from A to the input character ch. Let us suppose that there are num letters from A to ch. Then the whole diamond has 2*num-1 rows, in the first num rows, there are 2*num-1 letters in the i-th row; in the last num (or num-1) rows, the i-th row from the bottom also has 2*num- 1 letter.

The law of spaces: easy to get, there is a space between every two letters (this is nonsense). The key lies in the number of spaces in front of each line——

share picture

By observing the “character matrix” output by the sample, we can find that for the first num lines, the number of spaces before the i-th line can be expressed as (num-i)*2 ; For the last num rows, we can also use similar expression calculations.

Next, express the above rules through the program:

AC code

#include

#include

#include

#include


#define put_blank(n) cout</ /output n spaces

using namespace std;

int main()
{
//freopen("test.out","w", stdout);//Test and view output format
char ch;//Definition: ch is the uppercase letter entered
cout<<"Please input a capital letter: "<<endl;
cin
>>ch;
if(ch<'A'||ch>' Z')//< /span>If illegal characters are entered
{
cout
<<"The character is invalid!< span style="color: #800000;">"
<<endl;
system(
"pause");
return 0;
}
int num=int(ch-'A'+< span style="color: #800080;">1);//define num Is the number of letters

// output the first num rows of the diamond-shaped letter array, the width is increasing Arithmetic sequence of
for(int i=1;i<=num;i++)
{
put_blank((num
-i)*2);//< /span>Output the space before the letter string of this line
for(int j=1;j<=2*i-1; j++)
cout
<<char('A'+i-1 )<<' '; //output letter string
cout<//
End the output of this line
}

//After outputting num-1 lines, the width is decreasing, etc. Difference sequence
for(int k=num-1;k>=1;k--)
{
put_blank((num
-k)*2);
for(int l=1;l<=2*k-1< /span>;l++)
cout
<<char('A'+k-1 )<<' ';
cout
<<endl;
}
system(
"pause");
return 0;
}

1, About int num=ch-‘A’+ 1: This kind of expression is actually more common in character processing, and its essence still relies on the idea of ​​forced conversion. num is an integer, so ch-‘A’ is actually calculating the difference between the ASCII codes of the input characters ch and’A’. It is easy to find that this difference plus 1 means that ch is the first letter in the alphabet . Understand this principle, then the following char(‘A’+k-1) is not difficult to understand, and it is only reverse to coerce the int type to the char type.

2. Regarding the output of spaces: In order to avoid using too many loop structures to make the program too cumbersome, the program defines the output of spaces in the macro put_blank(n), put_blank(n) in the program, etc. The price is cout<

3. About for(int k=num-1;k>=1;k–) in the second set of for loops: Let k from The advantage of iterating from num-1 to 1 is that the expressions for the number of spaces and the number of letters in each line are the same as before, and there is no need to spend time calculating new expressions.

D

 A

B B B
C C C C C
D D D D D D D
C C C C C
B B B
A

#include

#include

#include

#include


#define put_blank(n) cout</ /output n spaces

using namespace std;

int main()
{
//freopen("test.out","w", stdout);//Test and view output format
char ch;//Definition: ch is the uppercase letter entered
cout<<"Please input a capital letter: "<<endl;
cin
>>ch;
if(ch<'A'||ch>' Z')//< /span>If illegal characters are entered
{
cout
<<"The character is invalid!< span style="color: #800000;">"
<<endl;
system(
"pause");
return 0;
}
int num=int(ch-'A'+< span style="color: #800080;">1);//define num Is the number of letters

// output the first num rows of the diamond-shaped letter array, the width is increasing Arithmetic sequence of
for(int i=1;i<=num;i++)
{
put_blank((num
-i)*2);//< /span>Output the space before the letter string of this line
for(int j=1;j<=2*i-1; j++)
cout
<<char('A'+i-1 )<<' '; //output letter string
cout<//
End the output of this line
}

//After outputting num-1 lines, the width is decreasing, etc. Difference sequence
for(int k=num-1;k>=1;k--)
{
put_blank((num
-k)*2);
for(int l=1;l<=2*k-1< /span>;l++)
cout
<<char('A'+k-1 )<<' ';
cout
<<endl;
}
system(
"pause");
return 0;
}

Leave a Comment

Your email address will not be published.