2-5.cpp

来自「Accelerated C++ 课后练习题 本人自己完成、可供参考」· C++ 代码 · 共 77 行

CPP
77
字号
#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
	{
		cout<<"Please enter the longth of the square: ";
		int longth;
		cin>>longth;
		const string cheng((longth-2)*2+1,' ');
		const string ning((longth-2)*2+1,'*');
		for(int i=0;i<longth;++i)
		{
			if(i==0||i==longth-1)
				cout<<"*"<<ning<<"*"<<endl;
			else 
				cout<<"*"<<cheng<<"*"<<endl;		
		}
	}


	{
		cout<<"Please enter the longth of the oblong: ";
		int longth;
		cin>>longth;
		cout<<"Please enter the breadth of the oblong: ";
		int breadth;
		cin>>breadth;
		const string cheng((longth-2)*2+1,' ');
		const string ning((longth-2)*2+1,'*');
		for(int i=0;i<breadth;++i)
		{
			if(i==0||i==breadth-1)
				cout<<"*"<<ning<<"*"<<endl;
			else 
				cout<<"*"<<cheng<<"*"<<endl;
		}
	}

	{
		cout<<"Please enter the border of the triangle: ";
		int border;
		cin>>border;

		int mid=15;
		for(int i=0;i<border;++i)
		{
			
			const string cheng((mid-i)*2,' ');
			if(i==0)
				cout<<cheng<<"*"<<endl;
			else
			{
				if(i==border-1)
				{
					const string ning(3+(i-1)*4,'*');
					cout<<cheng<<"*"<<ning<<"*"<<endl;
				}
				else
				{
					const string ning(3+(i-1)*4,' ');
					cout<<cheng<<"*"<<ning<<"*"<<endl;
				}
				
			}
		}

	}




	return 0;
}

⌨️ 快捷键说明

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