我怎样才能实现这段代码?
所以我的老师给了我这块代码来实现,基本上这里的总体目标是在将PPM图像写入文件之后制作棋盘图案。将所有文件写入文件对我来说都是非常明显的,问题是我得到的所有数据都是我的数组中的零。我怎样才能实现这段代码?
大块我的导师给了我。
int col = ((w & 0x08) == 0)^((h & 0x08) == 0);
raster[i][j].r = static_cast<float>(col);
raster[i][j].g = static_cast<float>(col & 0x00);
raster[i][j].b = static_cast<float>(col & 0x11);
这是我的。
#include <iostream>
using namespace std;
struct RGB{
float r;
float g;
float b;
};
int main(){
//Declare Variables
int h;
int w;
//User Feedback
cout << "Please input height then width" << endl;
cin >> h >> w;
RGB raster[h][w];
//Assignment loop i = h and j = w
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
int col = ((w & 0x08) == 0)^((h & 0x08) == 0);
raster[i][j].r = static_cast<float>(col);
raster[i][j].g = static_cast<float>(col & 0x00);
raster[i][j].b = static_cast<float>(col & 0x11);
cout << raster[i][j].b << endl;
}
}
return 0;
}
如果有人可以解释我在做什么错我会非常感激。我一直被困在这个问题上,并且迄今为止尝试了很多不同的东西。
我发现了这个问题。我的迭代器具有非常高的价值,它确实是随机的。任何人有任何想法为什么?
您可以通过向其添加print
语句来调试您的代码。例如:
for(int j = 0; j < w; j++){
int col = ((w & 0x08) == 0)^((h & 0x08) == 0);
std::cout << "First part: " << ((w & 0x08) == 0) << '\n';
std::cout << "Second part: " << ((h & 0x08) == 0) << '\n';
std::cout << "col = " << col << '\n';
...
}
你的特定情况下具有col
计算错误 - 它应该使用例如j
而不是w
。
在你的代码中,由老师给出的其他内容看起来很可疑。你可能想问他解释代码的作用和原因。或者(更好)自己编写代码 - 它足够小,可以完全重写。
你永远不会更新列的值。
int col = ((w & 0x08) == 0)^((h & 0x08) == 0);
W和H是恒定的,因此COL将是恒定的。因为(1或0)xor(1或0)只能给出(1或0),所以col必须相等(1或0)。
当设置
raster[i][j].b = static_cast<float>(col & 0x11);
的&
什么也不做,因为你的山坳值只能是1或0
确定山坳变量被设置正确?
它应该只有0或1.最终结果是PPM文件中的检查板模式 – Tykota
我必须将它转换为PPM中使用的0 - 225 – Tykota
那么为什么要和它呢? AND不做任何事情。 – user3853544
你有调试吗?你学到了什么? – Carcigenicate
不幸的是我知道我的IDE非常糟糕,它在调试时崩溃,我不得不重新启动。 – Tykota
您是否尝试用调试器逐步执行代码?另外,_standard_ C++不支持VLAs('RGB raster [h] [w];')。 –