2015-10-18 93 views
0

我的代码是在这里:http://pastebin.com/zgUef181真值表

table.cpp:

#include <iostream> 
#include "table.h" 
#include <fstream> 
#include <string> 
#include <cstring> 
#include <cstdlib> 
#include <math.h> 

using namespace std; 


Table::Table(){ 
    userFile = ""; 
    tableArray[0][0]; 
    height = 0; 
    width = 0; 
    n = 0; 
} 

Table::~Table(){ 
    // cout<<"Object terminated...forevurrrr"<<endl; 
} 

void Table::readInFile(){ 
    cout<<"Reading file..."<<endl; 
    ifstream readme(userFile.c_str()); 
    string line; 
    int countRow= 0; //this count = the row in the array 
    while(getline(readme, line)){ 
     if(countRow == 0){ 
     n = atoi(line.c_str()); 
     countRow++; 
     } 
     else{ 
     for(int i = 0; i < line.length();++i){ 
      if(line[i]=='0'){ 
       tableArray[countRow-1][i]=0; 
       cout << tableArray[countRow-1][i] << " "; 
      } 
      else if(line[i]=='1'){ 
       tableArray[countRow-1][i]=1; 
       cout << tableArray[countRow-1][i] << " "; 
      } 
      else{ 

      } 
     } 

     if(countRow == height){ 
      cout<<"line: " << line[line.length()-1] << endl; 
      //int wtf = line[line.length()-1]; 
      //cout << "wtf: " << wtf; 
     } 
     else{ 
      cout<<"line: " << line[line.length()-2] << endl; 
     } 
     countRow++; 
     } 
    } 
    Table::printArray(tableArray, height, width); 

} 

void Table::getFileName(){ 
    cout << "Enter file name pls: " << endl; 
    cin >> userFile; 
    if(checkIfExists(userFile)!=true){ 
     cout << "Nope, couldn't find the file. Try again." << endl; 
     Table::getFileName(); 
    } 
    else{ 
     Table::collectInfo(); 
     Table::readInFile(); 
    } 
} 

//check if the file exists 
bool Table::checkIfExists(string file_path){ 
    cout<<"Gotta check if this exists..."<<endl; 
    bool real_file=0; 
    ifstream mystream(file_path.c_str()); 
    string line; 
    try{ 
     while(getline(mystream, line)){ 
     real_file=1; 
     } 
     cout<<"Found da booty"<<endl; 
    } 
    catch(int e){ 
     cout<<"we got an error bruh"<<endl; 
    } 
    mystream.close(); 
    return real_file; 

} 

void Table::collectInfo(){ 
    cout<<"collectInfo"<<endl; 
    ifstream readme(userFile.c_str()); 
    string line; 
    string leHeight; 

    getline(readme, leHeight); 
    height = atoi(leHeight.c_str()); 
    height = pow(2,height); 
    cout<<height<<endl; 


    while(getline(readme, line)){ 
     width = line.length(); 
    } 
    width = (width + 1)/2; 
    cout<<width<<endl; 
    int** tempTable = new int*[width]; 

    for(int i = 0; i < height; ++i){ 
     tempTable[i] = new int[width]; 
    } 
    tableArray = tempTable; 
} 

void Table::printArray(int** array, int h, int w){ 
    cout<<"printArray"<<endl; 
    //string line; 

    for(int i = 0; i < h; i++){ 
     for(int j = 0; j < w; j++){ 
     cout << array[i][j]; 
     if(j==(w-1)) 
      cout << endl; 


     // if(array[i][j]==1){ 
     //  line.append("1 "); 
     // } 
     // else if(array[i][j]==0){ 
     //  line.append("0 "); 
     // } 
     // else{ 
     //  line.append("error"); 
     // } 
     } 
     //cout<<line<<endl; 
     //line=""; 
    } 

} 

table.h:

#include <iostream> 
#include <fstream> 

using namespace std; 
#ifndef TABLETOBOOL 
#define TABLETOBOOL 

class Table{ 
    private: 
     string userFile; 
     int** tableArray; 
     int numOfRow; 
     int numOfCol; 
     int height; 
     int width; 
     int n; 
    public: 
     Table(); 
     ~Table(); 

     void printArray(int**,int,int); 
     void readInFile(); 
     void getFileName(); 
     bool checkIfExists(string file_path); 
     void collectInfo(); 

}; 

#endif 

tableMain.cpp:

#include <iostream> 
#include "table.h" 
#include <fstream> 

using namespace std; 

int main() 
{ 
    Table runTable; 
    runTable.getFileName(); 

    return 0; 
} 

//sample2.txt

4 
0 0 0 0 1 
0 0 0 1 0 
0 0 1 0 1 
0 0 1 1 1 
0 1 0 0 0 
0 1 0 1 1 
0 1 1 0 0 
0 1 1 1 0 
1 0 0 0 1 
1 0 0 1 0 
1 0 1 0 0 
1 0 1 1 1 
1 1 0 0 1 
1 1 0 1 0 
1 1 1 0 1 
1 1 1 1 0 

它具有执行文件,头文件,主要和示例文本输入到程序。

示例文本的第一行是输入的数量,所以2^n(n是输入的数量)是真值表中的行数。到目前为止,我可以读取文件然后输出它,但它看起来不像是由于某种原因实际上保存到数组中。

Here is a screenshot的执行。在“读取文件...”之后,在readInFile函数中打印出真值表。 “Line:”部分向您显示输出,即1或0,这是每行上的最后一个数字。这很重要,因为输出为0的行可以省略,因为我最终尝试创建简化的布尔表达式。我想要做的是使用if语句,如“if(line [line.length() - 1] == 0){//跳过文件中的行}”。我试过这个,它不工作,我不知道为什么。

当我去打印数组时,它打印出一堆我只能假设为内存地址的东西?我不太确定。

感谢您的帮助提前。所有最佳

+0

什么是调试器告诉你的代码流? –

+0

分配是错误的,你不会在输入中跳过空格,所以你会在内存中得到随机垃圾等 –

回答

0

此代码的一个问题是,在开始写入之前,您从不为TableArray分配内存。如果您事先知道widthheight,则可以声明您的存储为std::vector

另一个问题是计算每行宽度的代码真的很奇怪而且很脆弱。一个向量会让你只需push_back提示,直到你用完。

+0

它被分配,但代码流是混乱的。 –

+0

好的,'collectInfo()'试图做到这一点,但你确定它正在阅读正确的文件吗?你永远不会传递文件名。 – Davislor

0

这可能不是解决所有的问题,但是......

你是不是为tempTable分配足够的内存。您有:

int** tempTable = new int*[width]; 

,但它应该是:

int** tempTable = new int*[height];