用字符生成绘画到txt的简单c语言小程序

作者: qwq 分类: 日常 发布时间: 2020-04-07 00:46

有很多上古时期的文档都是txt格式的,而其中很多内嵌了用字符画出来的图表。

例如 rfc 793 中的TCP状态机:

                         +---------+ ---------\      active OPEN
                              |  CLOSED |            \    -----------
                              +---------+<---------\   \   create TCB
                                |     ^              \   \  snd SYN
                   passive OPEN |     |   CLOSE        \   \
                   ------------ |     | ----------       \   \
                    create TCB  |     | delete TCB         \   \
                                V     |                      \   \
                              +---------+            CLOSE    |    \
                              |  LISTEN |          ---------- |     |
                              +---------+          delete TCB |     |
                   rcv SYN      |     |     SEND              |     |
                  -----------   |     |    -------            |     V
 +---------+      snd SYN,ACK  /       \   snd SYN          +---------+
 |         |<-----------------           ------------------>|         |
 |   SYN   |                    rcv SYN                     |   SYN   |
 |   RCVD  |<-----------------------------------------------|   SENT  |
 |         |                    snd ACK                     |         |
 |         |------------------           -------------------|         |
 +---------+   rcv ACK of SYN  \       /  rcv SYN,ACK       +---------+
   |           --------------   |     |   -----------
   |                  x         |     |     snd ACK
   |                            V     V
   |  CLOSE                   +---------+
   | -------                  |  ESTAB  |
   | snd FIN                  +---------+
   |                   CLOSE    |     |    rcv FIN
   V                  -------   |     |    -------
 +---------+          snd FIN  /       \   snd ACK          +---------+
 |  FIN    |<-----------------           ------------------>|  CLOSE  |
 | WAIT-1  |------------------                              |   WAIT  |
 +---------+          rcv FIN  \                            +---------+
   | rcv ACK of FIN   -------   |                            CLOSE  |
   | --------------   snd ACK   |                           ------- |
   V        x                   V                           snd FIN V
 +---------+                  +---------+                   +---------+
 |FINWAIT-2|                  | CLOSING |                   | LAST-ACK|
 +---------+                  +---------+                   +---------+
   |                rcv ACK of FIN |                 rcv ACK of FIN |
   |  rcv FIN       -------------- |    Timeout=2MSL -------------- |
   |  -------              x       V    ------------        x       V
    \ snd ACK                 +---------+delete TCB         +---------+
     ------------------------>|TIME WAIT|------------------>| CLOSED  |
                              +---------+                   +---------+

                      TCP Connection State Diagram
                               Figure 6.

 

如果这些字符画用记事本来直接绘制则非常麻烦,于是我就用c语言写了个非常简陋小程序来帮助绘制字符画。

用法如下:

i x y d  //在[x][y]点插入字符 ‘d’

h x y1 y2 d // 在[x][y1]到[x][y2]一横行之间插入字符’d’

s x1 y x2 d // 在[x1][y]到[x2][y]一竖行之间插入字符’d’

o  //将字符画保存到out.txt文件中

p //将字符画绘制在控制台上

源代码:

#include<bits/stdc++.h>
using namespace std;
char a[100][100];
int main(){
	for(int i=0;i<50;i++){
		for(int j=0;j<50;j++){
			a[i][j]=' ';
		}
	} 
while(1){
	cout<<"  |";
	for(int i=0;i<50;i++){
		cout<<i;
		if(i<10){
					cout<<" ";
		}
		cout<<"|"	;
	}
	cout<<endl;
	for(int i=0;i<50;i++){
		for(int j=0;j<152;j++){
			cout<<"-";
		}cout<<endl;
		cout<<i;
		if(i<10)cout<<" ";
		cout<<"|";
		for(int j=0;j<50;j++){
		
			cout<<a[i][j];
			cout<<" ";	cout<<"|";
		}cout<<endl;
		
			
	}
	
char c,d;

while(cin>>c){
if(c=='i'){
	int x,y;
	cin>>x>>y>>d;
	a[x][y]=d;
}else if(c=='h'){
	int x,y1,y2;
	cin>>x>>y1>>y2>>d;
	for(int i=y1;i<=y2;i++){
		a[x][i]=d;
	}
} else if (c=='s'){
	int x1,x2,y;
	cin>>x1>>y>>x2>>d;
	for(int i=x1;i<=x2;i++){
		a[i][y]=d;
	}
}else if(c=='o'){
	freopen("out.txt","w",stdout);
	for(int i=0;i<50;i++){
		for(int j=0;j<50;j++){
			cout<<a[i][j];
		}cout<<endl;
	}
	freopen("CON", "w", stdout); 
}	else if(c=='p'){
	break;
}


}



} }

例如

输入

h 7 7 37 -
h 27 7 37 -
s 7 7 27 |
s 7 37 27 |
p

则会画出一个长方形
显示如下:


  |0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|
--------------------------------------------------------------------------------------------------------------------------------------------------------
0 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
1 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
2 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
3 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
4 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
5 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
6 |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
7 |  |  |  |  |  |  |  || |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
8 |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
9 |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
10|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
11|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
12|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
13|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
14|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
15|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
16|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
17|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
18|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
19|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
20|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
21|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
22|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
23|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
24|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
25|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
26|  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
27|  |  |  |  |  |  |  || |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- |- || |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
28|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
29|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
30|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
31|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
32|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
33|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
34|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
35|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
36|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
37|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
38|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
39|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
40|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
41|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
42|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
43|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
44|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
45|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
46|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
47|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
48|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
--------------------------------------------------------------------------------------------------------------------------------------------------------
49|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

再输入o,则会输出out.txt中:



                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
       |-----------------------------|            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |                             |            
       |-----------------------------|            
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  
                                                  

发表评论

电子邮件地址不会被公开。 必填项已用*标注