我的做法是提前写好数字的模板(因为尺寸不确定,所以要把数字做成模板),存放在一个三维数组中(数字,行,列)。输入尺寸和数字串后,将数字的模板组合起来,然后解析成指定尺寸的数字输出出来。
1 //这是一道模拟题
2 //模拟计算器的形式在控制台中输出数字
3
4 #include <iostream>
5 #include <string.h>
6 using namespace std;
7
8 int a[10][5][3]=
9 {
10 {//0
11 0,2,0,
12 1,3,1,
13 0,3,0,
14 1,3,1,
15 0,2,0
16 }
17 ,{//1
18 0,3,0,
19 0,3,1,
20 0,3,0,
21 0,3,1,
22 0,3,0}
23 ,{//2
24 0,2,0,
25 0,3,1,
26 0,2,0,
27 1,3,0,
28 0,2,0
29 }
30 ,{//3
31 0,2,0,
32 0,3,1,
33 0,2,0,
34 0,3,1,
35 0,2,0
36 }
37 ,{//4
38 0,3,0,
39 1,3,1,
40 0,2,0,
41 0,3,1,
42 0,3,0
43 }
44 ,{//5
45 0,2,0,
46 1,3,0,
47 0,2,0,
48 0,3,1,
49 0,2,0
50 }
51 ,{//6
52 0,2,0,
53 1,3,0,
54 0,2,0,
55 1,3,1,
56 0,2,0
57 }
58 ,{//7
59 0,2,0,
60 0,3,1,
61 0,3,0,
62 0,3,1,
63 0,3,0
64 }
65 ,{//8
66 0,2,0,
67 1,3,1,
68 0,2,0,
69 1,3,1,
70 0,2,0
71 }
72 ,{//9
73 0,2,0,
74 1,3,1,
75 0,2,0,
76 0,3,1,
77 0,2,0
78 }
79 };
80 /*
81 0是一个空格
82 1是size个竖杠
83 2是size个横杠
84 3是size个虚横杠
85 */
86 int b[6][1001];
87 int main()
88 {
89 string l;
90 int size;
91 while(cin>>size){
92 cin>>l;
93 if(size==0 && l=="0") break;
94 int length=l.length();
95 //组合数字模板在数组b中
96 for(int i=1;i<=5;i++){ //循环行
97 int f=1; //组合数组b的第一个开始赋值
98 for(int j=1;j<=length;j++){ //循环数字
99 int num=l[j-1]-'0';
100 for(int k=0;k<3;k++){
101 b[i][f]=a[num][i-1][k];
102 f++;
103 }
104 b[i][f]=0;
105 f++;
106 }
107 }
108 /*输出组合好的数字模板
109 for(int i=1;i<=5;i++){
110 for(int j=1;j<=3*length+length-1;j++)
111 cout<<b[i][j];
112 cout<<endl;
113 }
114 */
115 //将数字模板解析成数字
116 //0输出一个空格
117 //1输出size行竖杠
118 //2输出size个横杠
119 //3输出size行空格
120 int count = 0;
121 for(int i=1;i<=5;i++){//循环行
122 for(int j=1;j<=3*length+length-1;j++){
123 //if(j==3*length+length-1 && b[i][j]==0)
124 // continue;
125 if(i%2!=0){
126 switch(b[i][j]){
127 case 0:
128 cout<<' ';
129 break;
130 case 2:
131 for(int k=0;k<size;k++)
132 cout<<'-';
133 break;
134 case 3:
135 for(int k=0;k<size;k++)
136 cout<<' ';
137 break;
138 }
139 }
140 else{
141 switch(b[i][j]){
142 case 0:
143 cout<<' ';break;
144 case 3:
145 for(int k=0;k<size;k++)
146 cout<<' ';
147 break;
148 case 1:
149 cout<<'|';break;
150 }
151 }
152 }
153 cout<<endl;
154 if(i%2==0){ //如果改行是偶数行,且输出次数再输出一遍
155 ++count; //如果是偶数行,已经输出一遍了,count+1
156 if(count<size)
157 --i;
158 }
159 else
160 count=0;
161 }
162 cout<<endl;
163 }
164 return 0;
165 }