c++ 三维数组 赋值

来源:百度知道 编辑:UC知道 时间:2024/06/05 10:23:49
#include <cstdlib>
#include <fstream>
#include <ctime>
#include <cstring>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>

using namespace std;
const int M=128;

void max_2d(int MS[M][M], int ii )
{
int kk=-1;
int sit=0,i=0,j=0;
stringstream stream;
string ri;
string name="baoheshui";
stream << ii;
stream >> ri;
ri = name + ri+ ".txt" ;
stream.clear();
ifstream file(ri.c_str());
while (!file.eof())
{
char ch;
int i=0,j=0;

file.get(ch);

if (int(ch) == 48 ||int(ch) == 49)
{
++kk;
i=kk/(M);
j=kk%(M);
if (int (ch) == 48)
{
MS[i][j]= 0;
}
else if(int (ch) == 49)
{

首先,如果这段代码只是要完成你说的这些操作,那这些头文件都是多余的:
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
#include <stdio.h>
这五个都可以去掉的。

接下来是max_2d函数里面,
1、sit变量定义了却又没有用到。
2、if (int(ch) == 48 ||int(ch) == 49)这一句可以直接写成 if (ch == '0' || ch == '1')。后面的 if (int (ch) == 48)也是一样。
3、ifstream file(ri.c_str());这一句之后你并没有判断文件打开是否成功。应该加上 if(!ifstream) { 提示用户读文件时出错了。 }
4、函数结尾文件读完了一定要关闭。ifstream.close(); 这样才行。

main函数里面
for (int i = 0; i !=1; i++) 不是要建立128*128*70么?是不是应该 i!=70 ?

我觉得可以改进的就这些了。程序运行出错不知道是为什么。可能是读文件时有问题,比如文件不存在,文件名不正确等等。