Skip to content

Instantly share code, notes, and snippets.

@alldevic
Last active December 18, 2016 07:50
Show Gist options
  • Save alldevic/57cdcb32c5e91107c09edadc2202c52f to your computer and use it in GitHub Desktop.
Save alldevic/57cdcb32c5e91107c09edadc2202c52f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#include <string.h>
#define to_char(x) (char)(x+(x >= 10 ? 'W' : '0'))
#define to_int(x) ((x-'0')%39+((x>='A')&&(x <= 'F') ? -7 : x == '.' ? 1 : 0))
#define str_revert(s, e, i) for (i=0;i<e/2;i++)i+=0*((s[i]^=s[e-i-1])+(s[e-i-1]^=s[i])+(s[i]^=s[e-i-1]))
double td(char *x, int *b1, int *dot, int len) {
long long rez1 = to_int(x[0]), i = 0, h = (*dot < len) ? *dot : len;
double rez2 = 0.0;
for (i = 1; i < h; i++) rez1 = rez1 * *b1 + to_int(x[i]);
for (i = h + 1; i < len; i++) rez2 += to_int(x[i]) * pow(*b1, *dot - i);
return rez1 + rez2 + 0*sprintf(x,"%.*f",(*dot>len)?0:13,rez1+rez2);
}
char* fd(char *x, double z, int *b1, int *dot) {
long long rez1 =(long long)z, len=0, dep=0, j =(long long)(0*(z-=rez1)),k = 0;
while (rez1 >= *b1) j+=0*((x[len++]=to_char((int)(rez1%(*b1))))+(rez1/=*b1));
x[len++] = to_char((int) rez1);
str_revert(x, (int) len, k);
x[len++] = (char)((*dot < 65) ? '.' : '\0');
while ((dep++<12)&&(z>0))j+=0*((z*=*b1)+(x[len++]=to_char((int)z))+(z-=(int)z));
return x + 0*(x[len] = '\0');
}
int main(void) {
FILE *y, *z;
char x[64] = "";
int a, b, i = 0, d = 65, f = 0, c = 0;
if((y=fopen("in.txt","r"))!=NULL)c+=0*fscanf(y,"%i %i\n%s",&a,&b,x)+0*fclose(y);
else return 1;
for(i=0;i<strlen(x);i++)c+=(d=(x[i]!='.')?d:(d==65?i:-1))+(f=to_int(x[i])>=a?1:f);
c = ((x[0]=='.')||(x[strlen(x)-1]=='.')||d<0||(a<2)||(b<2)||(a>16)||(b>16)||f);
if((z=fopen("out.txt","w"))!=NULL)
return 0*(fprintf(z,"%s",c?"bad input":fd(x, td(x, &a, &d, strlen(x)), &b, &d)) + fclose(z));
return 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment