我的代碼是在這裏: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){//跳過文件中的行}」。我試過這個,它不工作,我不知道爲什麼。
當我去打印數組時,它打印出一堆我只能假設爲內存地址的東西?我不太確定。
感謝您的幫助提前。所有最佳
什麼是調試器告訴你的代碼流? –
分配是錯誤的,你不會在輸入中跳過空格,所以你會在內存中得到隨機垃圾等 –