1 Digital
从键盘上输入一个介于[10~99]的整数,按下图所示的数码形式在控制台上用星号(*) 显示该
整数。每个数字的数码自上而下划分为5段。具体要求如下:
1.对输入 的整数进行有效性检查;
2.两个数字的列宽相同,可以根据需要自行调整(常量) ; 3.第2和4段各占3行(例如,数字2的两个竖直段) ;
4.两个数字之间的间隔为1列;
5.各段的绘制不得硬编码,要求采用循环的形式;函数原型依次为: segment_ one(int digit)、segment_ two(int digit)、segment _three(int digit)、 segment, four(int digit)、 segment_ fve(int digit), 其中,参数digit为数字(0~9);
6.主程序的文件名为digital.cpp;辅助函数的名字自行命名。
为了保证输出的数在同一行,所以要一行一行的输出
#include<iostream>
using namespace std;
void row(){
cout<<"********";
}
void columnRight(){
cout<<" *";
}
void columnLeft(){
cout<<"* ";
}
void columnDouble(){
cout<<"* *";
}
/*void segment_one(){
for(int i = 0;i <= 8;i++){
columnRight();
}
}*/
void segment_zero(int digital){
if(digital == 0 || digital == 4 || digital == 8){row();}
else
{
columnDouble();
}
}
void segment_one(int digital){
columnRight();
}
void segment_two(int digital){
if(digital == 0 || digital == 4 || digital == 8){row();}
if(digital == 1 || digital == 2 || digital == 3){columnRight();}
if(digital == 5 || digital == 6 || digital == 7){columnLeft();}
}
void segment_three(int digital){
if(digital == 0 || digital == 4 || digital == 8){row();}
if(digital == 1 || digital == 2 || digital == 3 || digital == 5 || digital == 6 || digital == 7){columnRight();}
}
void segment_four(int digital){
if(digital == 4){row();}
if(digital == 5 || digital == 6 || digital == 7 || digital == 8){columnRight();}
if(digital == 0 || digital == 1 || digital == 2 || digital == 3){columnDouble();}
}
void segment_five(int digital){
if(digital == 0 || digital == 4 || digital == 8){row();}
if(digital == 5 || digital == 6 || digital == 7){columnRight();}
if(digital == 1 || digital == 2 || digital == 3){columnLeft();}
}
void segment_six(int digital){
if(digital == 0 || digital == 4 || digital == 8){row();}
if(digital == 1 || digital == 2 || digital == 3){columnLeft();}
if(digital == 5 || digital == 6 || digital == 7){columnDouble();}
}
void segment_seven(int digital){
if (digital == 0)
{
row();
}else
{
columnRight();
}
}
void segment_eight(int digital){
if(digital == 0 || digital == 4 || digital == 8){row();}
else
{
columnDouble();
}
}
void segment_nine(int digital){
if(digital == 0 || digital == 4 || digital == 8){row();}
if(digital == 1 || digital == 2 || digital == 3){columnDouble();}
if(digital == 5 || digital == 6 || digital == 7){columnRight();}
}
void switchNum(int k,int d){ //k为要输出的数,d为当前的行数
switch (k)
{
case 0:
segment_zero(d);
break;
case 1:
segment_one(d);
break;
case 2:
segment_two(d);
break;
case 3:
segment_three(d);
break;
case 4:
segment_four(d);
break;
case 5:
segment_five(d);
break;
case 6:
segment_six(d);
break;
case 7:
segment_seven(d);
break;
case 8:
segment_eight(d);
break;
case 9:
segment_nine(d);
break;
default:
break;
}
}
int main(){
int num = 0;
cin>>num;
int tenUnit = num/10%10;
int oneUnit = num%10;
for(int i = 0;i < 9;i++){
switchNum(tenUnit,i);
cout<<" "; //输出空格保证两个数字间有空格
switchNum(oneUnit,i);
cout<<"\n"; //输出换行保证一行输出后换行
}
return 0;
}