一个3位数的数字转换小程序
发布网友
发布时间:2022-04-20 04:13
我来回答
共3个回答
热心网友
时间:2023-09-11 10:59
合并版本的代码:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
static char cha = 'o';
static char chb = 'x';
static char chc = 'U';
static char chd = 'D';
static char chx = 'o';
static char chy = 'x';
int isSSZ(int x, int y) // y在x的顺时针方向,距离*1到4
{
switch(x)
{
case '0': case '1': case '2':
case '3': case '4': case '5': return (y>=x&&y<=x+4)?1:0;
case '6': return (y=='7'||y=='8'||y=='9'||y=='0')?1:0;
case '7': return (y=='8'||y=='9'||y=='0'||y=='1')?1:0;
case '8': return (y=='9'||y=='0'||y=='1'||y=='2')?1:0;
case '9': return (y=='0'||y=='1'||y=='2'||y=='3')?1:0;
default : return 0;
}
}
void main(int argc, char* argv[])
{
if(argc<3)
{
printf("Error.\nno input file or output file.\n");
system("pause");
exit(-2);
}
FILE *fpi,*fpo;
char num[50];
char bin[][5] = { "000", "000", "000", "000", "000" };
char loop[][5] = { "000", "000", "000", "000", "000" };
char tmp[4] = { '\0', '\0', '\0', '\0' };
fpi=fopen(argv[1],"rt");
fpo=fopen(argv[2],"wt");
int count = 0, ixx = 0;
if(!fpi||!fpo)
{
printf("Error open input file or output file.");
system("pause");
exit(-1);
}
while(!feof(fpi))
{
if( fgets( num, 50, fpi ) != NULL)
{
count++;
if(count%30==1) fprintf(fpo,"num,bin01,bin02,bin03,bin04,bin05,loop1,loop2,loop3,loop4,loop5\n");
num[3]=loop[0][3]=loop[1][3]=loop[2][3]=loop[3][3]=loop[4][3]='\0';
num[3]=bin[0][3]=bin[1][3]=bin[2][3]=bin[3][3]=bin[4][3]='\0';
bin[0][0]=(num[0]=='0')?chx:((num[0]=='7')?chy:((num[0]=='1'||num[0]=='3'||num[0]=='5'||num[0]=='9')?'1':'2'));
bin[0][1]=(num[1]=='0')?chx:((num[1]=='7')?chy:((num[1]=='1'||num[1]=='3'||num[1]=='5'||num[1]=='9')?'1':'2'));
bin[0][2]=(num[2]=='0')?chx:((num[2]=='7')?chy:((num[2]=='1'||num[2]=='3'||num[2]=='5'||num[2]=='9')?'1':'2'));
bin[1][0]=(num[0]=='1')?chx:((num[0]=='8')?chy:((num[0]=='0'||num[0]=='3'||num[0]=='5'||num[0]=='9')?'1':'2'));
bin[1][1]=(num[1]=='1')?chx:((num[1]=='8')?chy:((num[1]=='0'||num[1]=='3'||num[1]=='5'||num[1]=='9')?'1':'2'));
bin[1][2]=(num[2]=='1')?chx:((num[2]=='8')?chy:((num[2]=='0'||num[2]=='3'||num[2]=='5'||num[2]=='9')?'1':'2'));
bin[2][0]=(num[0]=='2')?chx:((num[0]=='9')?chy:((num[0]=='0'||num[0]=='3'||num[0]=='5'||num[0]=='8')?'1':'2'));
bin[2][1]=(num[1]=='2')?chx:((num[1]=='9')?chy:((num[1]=='0'||num[1]=='3'||num[1]=='5'||num[1]=='8')?'1':'2'));
bin[2][2]=(num[2]=='2')?chx:((num[2]=='9')?chy:((num[2]=='0'||num[2]=='3'||num[2]=='5'||num[2]=='8')?'1':'2'));
bin[3][0]=(num[0]=='3')?chx:((num[0]=='4')?chy:((num[0]=='1'||num[0]=='2'||num[0]=='5'||num[0]=='8')?'1':'2'));
bin[3][1]=(num[1]=='3')?chx:((num[1]=='4')?chy:((num[1]=='1'||num[1]=='2'||num[1]=='5'||num[1]=='8')?'1':'2'));
bin[3][2]=(num[2]=='3')?chx:((num[2]=='4')?chy:((num[2]=='1'||num[2]=='2'||num[2]=='5'||num[2]=='8')?'1':'2'));
bin[4][0]=(num[0]=='5')?chx:((num[0]=='6')?chy:((num[0]=='1'||num[0]=='2'||num[0]=='3'||num[0]=='8')?'1':'2'));
bin[4][1]=(num[1]=='5')?chx:((num[1]=='6')?chy:((num[1]=='1'||num[1]=='2'||num[1]=='3'||num[1]=='8')?'1':'2'));
bin[4][2]=(num[2]=='5')?chx:((num[2]=='6')?chy:((num[2]=='1'||num[2]=='2'||num[2]=='3'||num[2]=='8')?'1':'2'));
fprintf( fpo, "%s", num );
fprintf( fpo, "," );
fprintf( fpo, "%s,%s,%s,%s,%s", bin[0], bin[1], bin[2], bin[3], bin[4] );
if(count==1)
{
fprintf( fpo, "\n" );
tmp[0]=num[0];tmp[1]=num[1];tmp[2]=num[2];
continue;
}
ixx=(num[0]-tmp[0])%10; ixx=(ixx>=0)?ixx:(10+ixx);
loop[0][0]=(ixx==0)?cha:((ixx==5)?chb:(isSSZ(num[0],tmp[0])?chc:chd));
loop[1][0]=(ixx==1)?cha:((ixx==6)?chb:(isSSZ(num[0],tmp[0])?chc:chd));
loop[2][0]=(ixx==2)?cha:((ixx==7)?chb:(isSSZ(num[0],tmp[0])?chc:chd));
loop[3][0]=(ixx==3)?cha:((ixx==8)?chb:(isSSZ(num[0],tmp[0])?chc:chd));
loop[4][0]=(ixx==4)?cha:((ixx==9)?chb:(isSSZ(num[0],tmp[0])?chc:chd));
ixx=(num[1]-tmp[1])%10; ixx=(ixx>=0)?ixx:(10+ixx);
loop[0][1]=(ixx==0)?cha:((ixx==5)?chb:(isSSZ(num[1],tmp[1])?chc:chd));
loop[1][1]=(ixx==1)?cha:((ixx==6)?chb:(isSSZ(num[1],tmp[1])?chc:chd));
loop[2][1]=(ixx==2)?cha:((ixx==7)?chb:(isSSZ(num[1],tmp[1])?chc:chd));
loop[3][1]=(ixx==3)?cha:((ixx==8)?chb:(isSSZ(num[1],tmp[1])?chc:chd));
loop[4][1]=(ixx==4)?cha:((ixx==9)?chb:(isSSZ(num[1],tmp[1])?chc:chd));
ixx=(num[2]-tmp[2])%10; ixx=(ixx>=0)?ixx:(10+ixx);
loop[0][2]=(ixx==0)?cha:((ixx==5)?chb:(isSSZ(num[2],tmp[2])?chc:chd));
loop[1][2]=(ixx==1)?cha:((ixx==6)?chb:(isSSZ(num[2],tmp[2])?chc:chd));
loop[2][2]=(ixx==2)?cha:((ixx==7)?chb:(isSSZ(num[2],tmp[2])?chc:chd));
loop[3][2]=(ixx==3)?cha:((ixx==8)?chb:(isSSZ(num[2],tmp[2])?chc:chd));
loop[4][2]=(ixx==4)?cha:((ixx==9)?chb:(isSSZ(num[2],tmp[2])?chc:chd));
fprintf( fpo, "," );
fprintf( fpo, "%s,%s,%s,%s,%s", loop[0], loop[1], loop[2], loop[3], loop[4] );
fprintf( fpo, "\n" );
tmp[0]=num[0];tmp[1]=num[1];tmp[2]=num[2];
} else
printf( "done. %d line(s) processed.\n", count);
}
fclose(fpi);
fclose(fpo);
system("pause");
}
热心网友
时间:2023-09-11 10:59
x,x+5 x-1,x-2,x-3,x-4 x+1,x+2,x+3,x+4 loop1
(注意loop1第一行没有,loop1中第二行生成的数据是通过080与596进行比较而形成的
方法为:X,X+5 因为080百位数0与596中百位数5比较刚好从|0-5|=5就用●表示
(如果两个位相等那就用○表示),在看十位,他们分别为8和9 是从9变到8也就等于x-1所以用↑表示,其他就这样的方法推出来。)
注意:百位和百位比较,个位与各位比较
不过他的方法是通过下一行与上一行进行比较来实现数字的转换的
热心网友
时间:2023-09-11 11:00
),在看十位,他们分别为8和9 是从9变到8也就等于x-1所以用↑表示,其他就这样的方法推出来。)
注意:百位和百位比较,个位与各位比较
不过他的方法是通过下一行与上一行进行比较来实现数字的转换的