accelerated c++习题2-5

/*************************************************
//程序说明:用"*"构建正方形,长方形,三角形
//程序来源:accelerated c++ 习题2-5
//作者:ying
//时间:2014.9.12
**************************************************/

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::string;

int main()
{
	//程序选择输出 1正方形 2长方形 3正三角形
	int choose,out = 0;
	cout<<"1正方形 2长方形 3正三角形,请选择:"<<endl;
	cin>>choose;
	switch(choose)
	{
	case 1:
		out = 1;
		break;
	case 2:
		out = 2;
		break;
	case 3:
		out = 3;
		break;
	default:
		cout<<"请选择数1,2,3"<<endl;
	}

	//正方形
	if(out==1)
	{
		int a;  //边长
		cout<<"输入正方形边长:";
		cin>>a;

		//输出
		for (int i=0;i<a;++i)
		{
			for (int j=0;j<a;++j)
			{
				if(i==0 || i==a-1 || j==0 || j==a-1)
					cout<<"* ";
				else cout<<"  ";
				if(j == a-1)
					cout<<endl;
			}
		}
	}

	//长方形
	if (out==2)
	{
		int wight,high;
		cout<<"长:"<<endl;
		cin>>wight;
		cout<<"宽:"<<endl;
		cin>>high;

		//输出
		for (int i=0;i<wight;++i)
		{
			for (int j=0;j<high;++j)
			{
				if(i==0 || i==wight-1 || j==0 || j==high-1)
					cout<<"* ";
				else cout<<"  ";
				if(j == high-1)
					cout<<endl;
			}
		}
	}

	//正三角形
	if (out==3)
	{
		int d; //d表示三角形的底
		cout<<"请选择底边长d为奇数:";
		cin>>d;
		int high = ((d-1)/2);
		int count = (d-1)/2;  //count表计数
		for (int i=0;i<=high;++i)
		{
			for (int j=0;j<d;++j)
			{
				if(i == high)
					cout<<"* ";
				if (i < high)
				{
					if (j==d-1-count || j==count)
						cout<<"* ";
					else cout<<"  ";
				}
				if(j == d-1)
					cout<<endl;
			}
			--count;	
		}
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值