⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pyramid.cpp

📁 Print pyramid composed of characters user entered.
💻 CPP
字号:
//************************************************
//      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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -