直接上源代码:
#include <string.h>
#include <stdio.h>
#define N 200
char s[N] = {0}, c[N] = {0};
int sn[N], sc[N], cn[N];
int monitor = 0;
int last = 0;
void cheng(int y, int j) //进行乘法运算
{
int i = j;
while(sn[i-j] != -1)
{
cn[i] = sn[i-j] * y + cn[i];
i++;
}
i = 0;
while(i < N)
{
if(cn[i] >= 10)
{
cn[i+1] = cn[i+1] + cn[i] / 10;
cn[i] = cn[i] % 10;
}
i++;
}
/// printf("cheng{} done!\n");
}
void locat(void) //判断小数位有几位
{
int i;
int monitors, monitorc;
int flags = 0, flagc = 0;
int lens = strlen(s);
int lenc = strlen(c);
i = 0;
while(i < lens)
{
if(s[i] == '.')
{
flags = 1;
monitors = lens - i - 1;
break;
}
i++;
}
i = 0;
while(i < lenc)
{
if(c[i] == '.')
{
flagc = 1;
monitorc = lenc - i - 1;
break;
}
i++;
}
if(flags)
monitor += monitors;
if(flagc)
monitor += monitorc;
// printf("locat{} done!\n");
}
bool doc(void) //判断是否整个数非0
{
int i = N - 1;
while(i >= 0)
{
i--;
if(cn[i] > 0)
return true;
}
return false;
}
void dec(void) //判断是否需要小数位前补0
{
int i = N - 1, lencn;
while(cn[i] == 0 && i >= 0)
{
i--;
}
lencn = i + 1;
// printf("%d\n", lencn);
if(lencn-1 < monitor )
{
printf("0.");
lencn++;
}
while(lencn <= monitor)
{
printf("0");
lencn++;
}
// printf("dec{} done!\n");
}
void lastzero() //判断小数点后末几位是否有0,有则删除
{
int i = 0;
if(monitor > 0)
{
while(cn[i] == 0 && i + 1 <= monitor)
{
i++;
last++;
}
}
// printf("lastzero{} done!\n");
}
int main()
{
int i, j;
int flag;
// freopen("data11.in","r",stdin);
// freopen("data11(2).out","w",stdout);
while(scanf("%s%s", &s, &c) == 2)
{
flag = 0;
for(i = 0; i < N; i++)
{
sn[i] = -1;
sc[i] = -1;
cn[i] = 0;
}
int i;
int beicheng = 1;
int lens = strlen(s);
int lenc = strlen(c);
i = lens - 1;
j = 0;
while(i >= 0)
{
if(s[i] == '.')
{
i--;
continue;
}
sn[j] = (int)s[i] - 48;
i--;
j++;
}
i = lenc - 1;
j = 0;
while(i >= 0)
{
if(c[i] == '.')
{
i--;
continue;
}
sc[j] = (int)c[i] - 48;
i--;
j++;
}
i = 0;
while(sc[i] >= 0)
{
beicheng = sc[i];
cheng(beicheng, i);
i++;
}
// printf("%d\n", beicheng);
if(!doc())
{
printf("0\n");
}
else
{
locat();
lastzero();
dec();
for(i = N - 1; i >= last; i--)
{
if(cn[i] > 0)
{
while(i >= last)
{
printf("%d", cn[i]);
if(monitor > 0 && i == monitor && i - last > 0)
printf(".");
i--;
}
}
}
printf("\n");
}
/*
for(i = N - 1; i >= 0; i--)
{
printf("%d", cn[i]);
}
printf("\n");
/*
for(i = N - 1; i >= 0; i--)
{
if(sc[i] != -1)
{
printf("%d", sc[i]);
}
}
printf("\n");
*/
memset(s,0,N);
memset(c,0,N);
monitor = 0;
last = 0;
}
return 0;
}