/*************************************************
//程序说明:用"*"构建正方形,长方形,三角形
//程序来源: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;
}accelerated c++习题2-5
最新推荐文章于 2025-08-19 12:05:01 发布
本文介绍了一个程序,通过输入选择构建正方形、长方形或正三角形,并使用'*'符号绘制几何图形。
366

被折叠的 条评论
为什么被折叠?



