【发布时间】:2016-02-21 14:25:59
【问题描述】:
我正在尝试使用 TTMath 在 C++ 中编写 Tuppers 公式来存储大整数。但是我的输出只是一团糟,而不是它应该显示的图片。我无法理解造成这种情况的原因。
#include <iostream>
#include <fstream>
#include <ttmath/ttmath.h>
int main(int argsm, int** args)
{
const ttmath::Int<1> HEIGHT = 17;
const ttmath::Int<1> WIDTH = 106;
ttmath::Big<300, 300> a, b, c, d, e, f, g, h, i, j, k, l, y;
std::ofstream newFile;
char matrix[107][18];
newFile.open("image.txt");
for (ttmath::UInt<3> x = 0; x < WIDTH; ++x)
{
y = ttmath::Big<1, 200>("9609393799189588849716729621278527547150043396601293066515055192717028023952664246896428421743507 18121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719");
for (ttmath::UInt<2> v = 0; v <= HEIGHT; ++v)
{
k = y;
a = y;
a.Mod(17);
b = a;
c = x;
c.Mul(17);
b.Mul(1);
d = c + b;
d.Pow(2);
e = d;
k.Div(17);
f = k ;
g = f;
g.Div(e);
h = g;
h.Mod(2);
j = h;
j > 0.5 ? matrix[stoi(x.ToString())][stoi(v.ToString())] = '\u2588' : matrix[stoi(x.ToString())][stoi(v.ToString())] = ' ';
y.Add(1);
}
}
for (signed int h = 0; h < std::stoi(HEIGHT.ToString()); ++h)
{
for (signed int w = 0; w < std::stoi(WIDTH.ToString()); ++w)
{
newFile << matrix[w][h];
}
newFile << "\n";
}
newFile.close();
return 0;
}
【问题讨论】:
-
如果你使用一个特殊的库来处理大数(ttmath),你的
std::atoi(HEIGHT.ToString())会不会产生有趣的效果,因为这个数字可能比int h大你的平台能支持吗?换句话说。如果矩阵的大小相当小并且 int 可以很好地对其进行索引,为什么要使用这些类型进行矩阵索引? -
您的矩阵元素的类型也是
char,因此newFile << matrix[w][h]行将在您的文件中添加另一个字符。所以根据值,当然它最终会看起来像一个垃圾字符串。如果要输出数字而不是字符,则必须将其转换为 int,如下所示:newFile << (int)matrix[w][h]。这是因为 iostream 很傻 :) -
您在用于指定 K 的字符串中还有一个空格。
-
@BitTickler 除了最后一个,这些都不重要。我也不想输出一个数字,而是一个字符。它的公式本身我不知何故做错了,但我不知道在哪里。
-
你的内循环条件是
v <= HEIGHT;不应该是v < HEIGHT吗?