额外篇:快速绘图
啊,也带大家刷了那么久的纪中OJ了,相信大家一定已经烦躁了吧。那么今天,我就要教大家:
画!图!
(渣渣辉:画图还要你教吗,傻子都会了……)
嘿!叨什么呢!今天我要教的是:用电脑自动画图!
(不是单纯的一个图案)
首先,我先教画一个矩形:
矩形分实心和空心两种,实心的自然很简单:
//部分代码
cin>>m>>n; //行数和列数
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
cout<<ch; //输出固定字符
}
cout<<endl; //换行
}
但空心的,由于第一行和最后一行所有地方都要输出,为特殊行,所以给他单出来输出:
//部分代码
cin>>m>>n; //行数和列数
for(int i=1;i<=n;i++)cout<<ch; //第一行输出固定字符
cout<<endl; //换行
for(int i=3;i<=m;i++)
{
cout<<ch; //输出前边框字符
for(int j=2;j<=n-1;j++)
cout<<" "; //中间空心
cout<<ch; //输出后边框字符
cout<<endl; //换行
}
for(int i=1;i<=n;i++)cout<<ch; //最后一行输出固定字符
接着讲三角形。实心的也好说:
//部分代码
cin>>m>>n;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=i;j++)cout<<ch; //同上
cout<<endl; //同上
}
但空心的,由于第一行和最后一行所有地方都要输出,为特殊行,且三角形的空格要递增输出,所以如下:
//部分代码
cin>>m>>n; //行数和列数
cout<<ch; //第一行输出固定字符
cout<<endl; //换行
for(int i=3;i<=m;i++)
{
cout<<ch; //输出前边框字符
for(int j=1;j<=n-3;j++)
cout<<" "; //中间空心
cout<<ch; //输出后边框字符
cout<<endl; //换行
}
for(int i=1;i<=n;i++)cout<<ch; //最后一行输出固定字符
合起来,改变量,整理一下就变成了:
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
int i,j,sk,xz,l=1;
char ch;
ddd: //特殊的“函数,与goto搭配使用”
cout<<"请输入形状:"<<endl;
cout<<"1.方形 2.直角三角形";
cin>>xz;
if(!(xz==1||xz==2))
{
cout<<endl;
cout<<"非法的输入."<<endl;
system("pause");
goto ddd;
}
cout<<"请输入行数和列数: (行数和列数不大于25,如果是直角三角形行列必须相同)";
cin>>i>>j;
cout<<"请输入一个字符:";
cin>>ch;
cout<<"实心还是空心? (1.实心 0.空心)";
cin>>sk;
if(!(sk==1||sk==0))
{
cout<<endl;
cout<<"非法的输入."<<endl;
system("pause");
goto ddd;
}
if(xz==1)
{
if(sk==1)
{
for(int a=1;a<=i;a++)
{
for(int b=1;b<=j;b++)
cout<<ch;
cout<<endl;
}
}
else
{
for(int a=1;a<=j;a++)
cout<<ch;
cout<<endl;
for(int a=3;a<=i;a++)
{
cout<<ch;
for(int b=2;b<=j-1;b++)
cout<<" ";
cout<<ch<<endl;
}
for(int b=1;b<=j;b++)
cout<<ch;
}
}
else if(xz==2)
{
if(sk==1)
{
for(int a=1;a<=i;a++)
{
for(int b=1;b<=a;b++)
cout<<ch;
cout<<endl;
}
}
else
{
cout<<ch<<endl;
for(int a=3;a<=i;a++)
{
cout<<ch;
for(int b=1;b<=a-3;b++)
cout<<" ";
cout<<ch<<endl;
}
for(int b=1;b<=j;b++)
cout<<ch;
}
}
cout<<endl;
system("pause");
return 0;
}
(亲测没问题)
以上就是电脑快速绘图的全部内容了,谢谢观看!