pyramid.cpp

来自「Print pyramid composed of characters use」· C++ 代码 · 共 53 行

CPP
53
字号
//************************************************
//      Filename: pyramid.cpp
// Print pyramid composed of characters user entered.
//Programming Problems No.3 on P347 of textbook. 
//************************************************
#include<iostream>
#include<iomanip>    //For setw()

using namespace std;

void PrintPyramid(char);

int main()
{
	char inChar;
	//Get and test data
	do
	{
		cout<<"Enter a uppercase letter and I will print a pyramid:"
			<<endl;
		cin >>inChar;
		if(inChar<'A' || inChar>'Z')
			cout<<"The letter you input is not valid."<<endl;
	}while(inChar<'A' || inChar>'Z');
	//Print pyramid
	PrintPyramid(inChar);

	return 0;
}

void PrintPyramid(char inChar)
//This function prints a pyramid according to the character inChar
{
	char middleChar;   //The character in the middle of a certain line
	char ch;           //The character being printing
	int blankNum;      //The number of blanks in a certain line

	for(middleChar='A' ; middleChar<=inChar ; middleChar++)
	{
		blankNum=inChar-middleChar;
		cout<<setw(blankNum)<<"";

		for(ch='A' ; ch<=middleChar ; ch++)
			cout<<ch;  // output the left part of pyramid
		for(ch=middleChar-1 ; ch>='A' ; ch--)
			cout<<ch;  // output the right part of pyramid

		cout<<endl;
	}
	cout<<endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?