长除法 1/N 算法使用字符串存储数字,当太长时创建 symbols/letters
Long division 1/N algorithm using string to store digits, creates symbols/letters when too long
我想做一个专门针对1/N的长除法算法。我已经让代码正常工作,但是当存储数字的字符串长度达到 ~>20 位时,数据类型正在做一些“时髦的事情”。
您可以更改 b 变量以测试任何其他分数。
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int a = 1;
int b = 666;
int c = 0;
int d = 1;
int z = 0;
char *decimals = "0.";
size_t len = 2;
while (d>0)
{
printf("a: %d ",a);
printf("b: %d ",b);
printf("c: %d ",c);
printf("d: %d\n",d);
if(a<b)
{
a = a*10;
if(a<b)
{
len = len + 2;
char *temp = calloc(len, sizeof(char));
sprintf(temp, "%s%d",decimals,z);
decimals = temp;
printf("%s\n\n", decimals);
}
else
{
c = a/b;
d = a-b*c;
a = d;
char *temp = calloc(len, sizeof(char));
sprintf(temp, "%s%d",decimals,c);
decimals = temp;
printf("%s\n\n", decimals);
}
}
}
free(decimals);
}
CMD 输出:
是什么导致出现 # 字符?如果 运行 时间更长,会出现更多随机 letter/symbols。
我认为内存分配的方式有问题。您最终在 else 语句中写入超过分配的内存量。长度没有在应该扩展的时候扩展。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a = 1;
int b = 666;
int c = 0;
int d = 1;
int z = 0;
int k = 0;
char * decimals = (char*)malloc(sizeof(char) * 3);
if (!decimals)
{
printf("alloc failed!");
return 1;
}
sprintf(decimals, "0.");
size_t len = 2;
while (d > 0 && k++ < 1000)
{
printf("a: %d ", a);
printf("b: %d ", b);
printf("c: %d ", c);
printf("d: %d\n", d);
if (a < b)
{
a = a * 10;
if (a < b)
{
len += 2;
char *temp = (char *)malloc(sizeof(char) * len);
if (!temp)
{
printf("alloc failed!");
break;
}
snprintf(temp, len, "%s%d", decimals, z);
free(decimals);
decimals = temp;
printf("%s\n\n", decimals);
}
else
{
c = a / b;
d = a - b * c;
a = d;
len += 2;
char *temp = (char *)malloc(sizeof(char) * len);
if (!temp)
{
printf("alloc failed!");
break;
}
snprintf(temp, len, "%s%d", decimals, c);
free(decimals);
decimals = temp;
printf("%s\n\n", decimals);
}
}
}
free(decimals);
}
我想做一个专门针对1/N的长除法算法。我已经让代码正常工作,但是当存储数字的字符串长度达到 ~>20 位时,数据类型正在做一些“时髦的事情”。
您可以更改 b 变量以测试任何其他分数。
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int a = 1;
int b = 666;
int c = 0;
int d = 1;
int z = 0;
char *decimals = "0.";
size_t len = 2;
while (d>0)
{
printf("a: %d ",a);
printf("b: %d ",b);
printf("c: %d ",c);
printf("d: %d\n",d);
if(a<b)
{
a = a*10;
if(a<b)
{
len = len + 2;
char *temp = calloc(len, sizeof(char));
sprintf(temp, "%s%d",decimals,z);
decimals = temp;
printf("%s\n\n", decimals);
}
else
{
c = a/b;
d = a-b*c;
a = d;
char *temp = calloc(len, sizeof(char));
sprintf(temp, "%s%d",decimals,c);
decimals = temp;
printf("%s\n\n", decimals);
}
}
}
free(decimals);
}
CMD 输出:
是什么导致出现 # 字符?如果 运行 时间更长,会出现更多随机 letter/symbols。
我认为内存分配的方式有问题。您最终在 else 语句中写入超过分配的内存量。长度没有在应该扩展的时候扩展。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a = 1;
int b = 666;
int c = 0;
int d = 1;
int z = 0;
int k = 0;
char * decimals = (char*)malloc(sizeof(char) * 3);
if (!decimals)
{
printf("alloc failed!");
return 1;
}
sprintf(decimals, "0.");
size_t len = 2;
while (d > 0 && k++ < 1000)
{
printf("a: %d ", a);
printf("b: %d ", b);
printf("c: %d ", c);
printf("d: %d\n", d);
if (a < b)
{
a = a * 10;
if (a < b)
{
len += 2;
char *temp = (char *)malloc(sizeof(char) * len);
if (!temp)
{
printf("alloc failed!");
break;
}
snprintf(temp, len, "%s%d", decimals, z);
free(decimals);
decimals = temp;
printf("%s\n\n", decimals);
}
else
{
c = a / b;
d = a - b * c;
a = d;
len += 2;
char *temp = (char *)malloc(sizeof(char) * len);
if (!temp)
{
printf("alloc failed!");
break;
}
snprintf(temp, len, "%s%d", decimals, c);
free(decimals);
decimals = temp;
printf("%s\n\n", decimals);
}
}
}
free(decimals);
}