标题:画表格
在中文Windows环境下,控制台窗口中也可以用特殊符号拼出漂亮的表格来。
比如:
其实,它是由如下的符号拼接的:
左上 = ┌
上 = ┬
右上 = ┐
左 = ├
中心 = ┼
右 = ┤
左下= └
下 = ┴
右下 = ┘
垂直 = │
水平 = ─
本题目要求编写一个程序,根据用户输入的行、列数画出相应的表格来。
例如用户输入:
3 2
则程序输出:
用户输入:
2 3
则程序输出:
对于编程题目,要求选手给出的解答完全符合ANSI C++标准,不能使用诸如绘图、Win32API、中断调用、硬件操作或与操作系统相关的API。
代码中允许使用STL类库,但不能使用MFC或ATL等非ANSI c++标准的类库。例如,不能使用CString类型(属于MFC类库)。
分析:开始感觉无思路,其实做多这种题就那个了。两种方式:一种把图形存在数组里(麻烦),第二种是直接输出。我选择了第二种,我是一行一行的输出,写在了四个不同的函数里打印不同的一行,方便调用。
CODE:
#include <iostream>
using namespace std;
bool flag;
void out1(int t)
{
cout<<"┌─";
while(t-1)
{
cout<<"┬─";
t--;
}
cout<<"┐"<<endl;
}
void out2(int t)
{
while(t)
{
cout<<"│ ";
t--;
}
cout<<"│"<<endl;
}
void out3(int t)
{
cout<<"├";
while(t-1)
{
cout<<"─┼";
t--;
}
cout<<"─┤"<<endl;
}
void out4(int t)
{
cout<<"└";
while(t-1)
{
cout<<"─┴";
t--;
}
cout<<"─┘"<<endl;
flag=false;
}
int main(int argc, char *argv[])
{
int r,c;
while(cin>>c>>r)
{
flag=true;
out1(r);
while(flag)
{
out2(r);
if(c-1)
{
out3(r);
c--;
}
else
out4(r);
}
}
return 0;
}