该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//两个串表示数的除法,结果精确到小数点后第n位
char *sdivf(char *u, char *v, int n)
{
char *p, *f, *r, *q ;
unsigned int i, lu, lv, lr, iw, c, h ;
int kh, j ;
lu = strlen(u) ;
lv = strlen(v) ;
f = (char *)malloc((unsigned int)(lu + n + 3) * sizeof(char)) ;
q = (char *)malloc(sizeof(char)) ;
for (i = 0; i
f[i] = '\0' ;
r = (char *)malloc((unsigned int)(lv + 2) * sizeof(char)) ;
for (i = 0; i
r[i] = '\0' ;
for (iw = 0; iw
{
if (iw
{
cdel0(r) ;
lr = strlen(r) ;
r[lr] = u[iw] ;
r[lr + 1] = '\0' ;
}
else if (iw > lu)
{
cdel0(r) ;
q[0] = '0' ;
if (scmp(r, q) == 0)
{
break ;
}
lr = strlen(r) ;
r[lr] = '0' ;
r[lr + 1] = '\0' ;
}
else
{
f[lu] = '.' ;
continue ;
}
kh = 0 ;
while (scmp(r, v) >= 0)
{
p = r ;
r = ssub(p, v) ;
++kh ;
}
f[iw] = kh + '0' ;
}
if (iw == lu + n + 2)
{
if (f[lu + n + 1] >= '5')
{
f[lu + n + 1] = '\0' ;
c = 1 ;
for (j = lu + n; j >= 0; --j)
{
if (c == 0)
{
break ;
}
if (f[j] == '.')
{
continue ;
}
h = f[j] - '0' + c ;
if (h > 9)
{
h = h - 10 ;
c = 1 ;
}
else
c = '\0' ;
f[j] = h + '0' ;
}
}
else
f[lu + n + 1] = '\0' ;
}
free(r) ;
free(p) ;
q = NULL ;
free(q) ;
cdel0(f) ;
return(f) ;
}//end sdivf()
//两个串表示数的除法,结果分别用整商与余数表示
char *sdivkr(char *u, char *v, char **rout)
{
char *f, *r ;
unsigned int i, lu, lv, lr, iw ;
int kh ;
lu = strlen(u) ;
lv = strlen(v) ;
f = (char *)malloc((unsigned int)(lu + 1) * sizeof(char)) ;
for (i = 0; i
f[i] = '\0' ;
r = (char *)malloc((unsigned int)(lv + 2) * sizeof(char)) ;
for (i = 0; i
r[i] = '\0' ;
for (iw = 0; iw
{
cdel0(r) ;
lr = strlen(r) ;
r[lr] = u[iw] ;
r[lr + 1] = '\0' ;
kh = 0 ;
while (scmp(r, v) >= 0)
{
r = ssub(r, v) ;
++kh ;
}
f[iw] = kh + '0' ;
}
cdel0(r) ;
*rout = r ;
cdel0(f) ;
return(f) ;
}//end *sdivkr()
//调用上述函数实现两任意长正整数任意指定精度的算术计算器程序
int main(int argc, char *argv[])
{
char *p, *r ;
int n ;
if (argc != 4)
{
if (argc != 3)
printf("\n>>\"order n1 op n2\" or n ! ") ;
exit(0) ;
}
cdel0(argv[1]);
if (cchkdig(argv[1]) == 0)
{
printf("Input data error, Input again!") ;
exit(0) ;
}
cdel0(argv[3]) ;
if (cchkdig(argv[3]) == 0)
{
printf("Input data error, Input again!") ;
exit(0) ;
}
if (strcmp(argv[2], "+") == 0)
{
printf("%s", p = sadd(argv[1], argv[3])) ;
free(p) ;
}
else if (strcmp(argv[2], "-") == 0)
{
printf("%s", p = ssub(argv[1], argv[3])) ;
free(p) ;
}
else if(strcmp(argv[2], "*") == 0)
{
printf("%s", p=smut(argv[1], argv[3])) ;
free(p) ;
}
else if(argv[2][0] == '/' && strlen(argv[2]) == 1)
{
if (argv[3][0] == '0')
{
printf("error!devided by zero!!\n") ;
exit(0) ;
}
p = sdivkr(argv[1], argv[3], &r) ;
printf("k = %s r = %s", p, r) ;
free(p) ;
free(r) ;
}
else if (argv[2][0] == '/' && strlen(argv[2]) > 1)
{
if (argv[3][0] == '0')
{
printf("error!devided by zero!!\n") ;
exit(0) ;
}
argv[2][0] = '\0' ;
cdel0(argv[2]) ;
if (cchkdig(argv[2]) == 0)
{
printf("Input data error, Input again!") ;
exit (0) ;
}
n = atoi(argv[2]) ;
printf("%s", p = sdivf(argv[1], argv[3], n)) ;
free(p) ;
}
return 0 ;
}