PROGRAM 1
// FILENAME: TTABLE.H // THIS IS THE DECLARATION FOR THE TRUTH_TABLE CLASS. #include #include #include #include #include #include #include class TRUTH_TABLE { public: // CONSTRUCTER. TRUTH_TABLE (); // DESTRUCTER. ~TRUTH_TABLE (); // OPEN DATA FILE CONTAINING THE TOTAL NUMBER OF ROWS IN // THE TRUTH TABLE AND AN OUT FILE, CONTAINING THE TRUTH TABLE. OPEN_FILES (); // DRAW TRUTH TABLE. DRAW_TTABLE (); // DRAW AN EDGE OF THE TRUTH TABLE. DRAW_EDGE_BOX (int ROW, int COL, int LINE_COUNT); // DRAW THE MIDDLE PART OF THE TRUTH TABLE. DRAW_MIDDLE_BOX (int ROW, int COL); // DRAW A BOX PART. DRAW_BOXPART (int BOX_PART); // DRAW THE CURRENT ROW AT THE LEFT OF THE TRUTH TABLE. DRAW_ROW (int ROW); // DRAW A 0 OR A 1 IN INDIVIDUAL BOXES OF THE TRUTH TABLE. DRAW_01 (int ROW, int COL); // CLOSE DATA FILE AND OUT FILE. CLOSE_FILES (); private: // TOTAL NUMBER OF COLUMNS IN TRUTH TABLE. int TOTAL_COLS; // TOTAL NUMBER OF ROWS IN TRUTH TABLE + 1. int TOTAL_ROWS; // MAXIMUM NUMBER OF BAR LINES. int MAX_LINES; // FILE POINTER TO DATA FILE. ifstream FILEPTR1; // FILE POINTER TO OUT FILE. ofstream FILEPTR2; }; // THIS IS THE IMPLEMENTATION FOR THE TRUTH_TABLE CLASS. // CONSTRUTER. TRUTH_TABLE :: TRUTH_TABLE () { // INITIALIZE TOTAL_COLS, FILEPTR1, AND FILEPTR2 TO 0. TOTAL_COLS = 0; TOTAL_ROWS = 0; MAX_LINES = 0; } // DESTRUCTER. TRUTH_TABLE :: ~TRUTH_TABLE () { } // OPEN DATA FILE CONTAINING THE TOTAL NUMBER OF ROWS IN // THE TRUTH TABLE AND AN OUT FILE, CONTAINING THE TRUTH TABLE. TRUTH_TABLE :: OPEN_FILES () { // DECLARE VARIABLES FOR FILES, ASSIGN THEM NAMES, AND OPEN FILES. char DATA_FILE [25]; char OUT_FILE [25]; strcpy (DATA_FILE, "TTABLE.DAT"); strcpy (OUT_FILE, "TTABLE.OUT"); FILEPTR1.open (DATA_FILE, ios :: in); FILEPTR2.open (OUT_FILE, ios :: out); // IF THERE IS A PROBLEM WITH FILES, PRINT ERROR MESSAGE AND QUIT. if (!FILEPTR1 || !FILEPTR2) { cout << "ERROR OPENING ONE OF THE FILES." << endl; exit (0); } return 0; } // DRAW TRUTH TABLE. TRUTH_TABLE :: DRAW_TTABLE () { // THIS VARIABLE COUNTS THE LINE BARS OF THE TRUTH TABLE. int LINE_COUNT; // SET MAXIMUM NUMBER OF LINES TO 29. MAX_LINES = 28; // THIS VARIABLE HOLDS THE TYPE OF TRUTH TABLE CHARACTER. // EXAMPLE, 1 = 'Ú', 2 = '¿', ETC.. int BOX_PART; // GET TOTAL COLUMNS VALUE FROM THE DATA FILE. FILEPTR1 >> TOTAL_COLS; // SKIP LINE IN OUT FILE. FILEPTR2 << endl; // QUIT LOOP WHEN THERE IS AN END OF FILE MARKER. while (FILEPTR1.eof () != 1) { // TOTAL NUMBER OF ROWS + 1. TOTAL_ROWS = pow (2, TOTAL_COLS); // IF THE TOTAL COLUMNS VALUE IS OUT OF RANGE (1 TO 10), // DISPLAY ERROR MESSAGE AND QUIT. if ((TOTAL_COLS < 2) || (TOTAL_COLS > 10)) { cout << "ERROR, ROWS IS NOT IN THE RANGE 2 TO 12." << endl; exit (0); } // LINE COUNT INITIALIZES TO 0. LINE_COUNT = 0; // THE NEXT THREE FOR LOOPS WITH WRITE THE TRUTH TABLE // TO THE OUT FILE. for (int ROW = 0; ROW <= (TOTAL_ROWS - 1); ROW++) { // ADD ONE TO LINE COUNT. LINE_COUNT++; // WRITE SOME BLANK SPACES TO OUT FILE SO THAT // TRUTH TABLE DOES NOT START TOO CLOSE TO THE // EDGE OF PAPER WHEN IT IS FINALLY PRINTED OUT. FILEPTR2 << " "; // WRITE THE START OR THE MIDDLE LINES OF THE // TRUTH TABLE. for (int COL = 1; COL <= TOTAL_COLS + 1; COL++) { DRAW_EDGE_BOX (ROW, COL, LINE_COUNT); } // SKIP LINE IN OUT FILE. FILEPTR2 << endl; // WRITE THE MIDDLE PORTION OF THE TRUTH TABLE CELL. DRAW_MIDDLE_BOX (ROW, COL); // SKIP LINE IN OUT FILE. FILEPTR2 << endl; // IF LINE COUNT = 29 OR ROW = 2^ TOTAL_COLS THEN // WRITE THE ENDING LINE OF THE TRUTH TABLE. if ((LINE_COUNT == MAX_LINES) || (ROW == (TOTAL_ROWS - 1))) { // SKIP SOME SPACES IN OUT FILE. FILEPTR2 << " "; // ADD ONE TO LINE COUNTER. LINE_COUNT++; // WRITE ENDING LINE TO TRUTH TABLE. for (int COL = 1; COL <= TOTAL_COLS + 1; COL++) { DRAW_EDGE_BOX (TOTAL_ROWS, COL, LINE_COUNT); } // INITIALIZE LINE COUNT TO 0. LINE_COUNT = 0; // WRITE SOME EXTRA LINES TO OUT FILE WITH // A FORM FEED. FILEPTR2 << "\n\f\n\n"; } } // GO TO THE NEXT TRUTH TABLE. FILEPTR1 >> TOTAL_COLS; } return 0; } // DRAW AN EDGE OF THE TRUTH TABLE. TRUTH_TABLE :: DRAW_EDGE_BOX (int ROW, int COL, int LINE_COUNT) { // WRITE ONE OF 9 POSSIBLE LINES OF THE TRUTH TABLE ACCORDING // TO ROW, COL, AND LINE COUNT. // EXAMPLE, ROW = 0, LINE_COUNT = 1, COL = 1, THE IF // STATEMENT WILL WRITE 'ÚÄÄÄ' if ((ROW == 0 || LINE_COUNT == 1) && COL == 1) { DRAW_BOXPART (1); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (2); } else if ((ROW == 0 || LINE_COUNT == 1) && COL == TOTAL_COLS + 1) { DRAW_BOXPART (3); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (4); } else if ((ROW == 0 || LINE_COUNT == 1) && COL != 1 && COL != (TOTAL_COLS + 1)) { DRAW_BOXPART (3); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (2); } else if (ROW == TOTAL_ROWS && COL == 1) { DRAW_BOXPART (8); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (2); } else if (ROW == TOTAL_ROWS && COL == (TOTAL_COLS + 1)) { DRAW_BOXPART (9); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (10); } else if (ROW == TOTAL_ROWS && COL != 1 && COL != (TOTAL_COLS + 1)) { DRAW_BOXPART (9); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (2); } else if (ROW != 0 && ROW != TOTAL_ROWS && COL == 1) { DRAW_BOXPART (5); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (2); } else if (ROW != 0 && ROW != TOTAL_ROWS && COL == (TOTAL_COLS + 1)) { DRAW_BOXPART (6); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (7); } else if (ROW != 0 && ROW != TOTAL_ROWS && COL != 1 && COL != (TOTAL_COLS + 1)) { DRAW_BOXPART (6); DRAW_BOXPART (2); DRAW_BOXPART (2); DRAW_BOXPART (2); } return 0; } // DRAW THE MIDDLE PART OF THE TRUTH TABLE. TRUTH_TABLE :: DRAW_MIDDLE_BOX (int ROW, int COL) { // WRITES THE CURRENT ROW TO THE LEFT OF THE TRUTH TABLE BOX. DRAW_ROW (ROW); // WRITES THE MIDDLE PART OF THE BOX // EXAMPE, ROW = 0, COL = 0, WRITE '³ 0 ³ 0 ³' TO TRUTH TABLE. for (int LINES = 1; LINES <= COL; LINES++) { // DRAW MIDDLE BOX PART. DRAW_BOXPART (11); if (LINES <= (COL - 1)) { // FILL THE MIDDLE PART OF THE TRUTH TABLE WITH // 0, 1, OR A SPACE. for (int SPACES = 1; SPACES <= 3; SPACES++) { // IF SPACE = 2, WRITE 0 OR 1 TO THE // CURRENT CELL OF THE TRUTH TABLE. // IF SPACE = 1 OR 3, WRITE A SPACE TO // THE CURRENT CELL OF THE TRUTH TABLE. if ((SPACES == 2) && (LINES <= TOTAL_COLS)) { DRAW_01 (ROW, LINES); } else { FILEPTR2 << " "; } } } } return 0; } // DRAW A BOX PART OF THE TRUTH TABLE. TRUTH_TABLE :: DRAW_BOXPART (int BOX_PART) { // WRITES 1 OF 11 DIFFERENT BOX PARTS OF THE TRUTH TABLE. // FOR EXAMPLE, WRITE 'Ú' OF TRUTH TABLE IF BOX_PART VALUE = 1. switch (BOX_PART) { case (1) : { FILEPTR2 << "Ú"; break; } case (2) : { FILEPTR2 << "Ä"; break; } case (3) : { FILEPTR2 << "Â"; break; } case (4) : { FILEPTR2 << "¿"; break; } case (5) : { FILEPTR2 << "Ã"; break; } case (6) : { FILEPTR2 << "Å"; break; } case (7) : { FILEPTR2 << "´"; break; } case (8) : { FILEPTR2 << "À"; break; } case (9) : { FILEPTR2 << "Á"; break; } case (10) : { FILEPTR2 << "Ù"; break; } case (11) : { FILEPTR2 << "³"; } } return 0; } // DRAW THE CURRENT ROW TO THE LEFT OF THE TRUTH TABLE. TRUTH_TABLE :: DRAW_ROW (int ROW) { // KEEP THE ROWS PERFECTLY ALIGNED AND WRITE ROW TO LEFT OF // TRUTH TABLE. FILEPTR2.setf (ios :: showpoint); FILEPTR2 << setw (4) << ROW << " "; return 0; } // DRAW 0 OR 1 TO INDIVIDUAL BOXES OF THE TRUTH TABLE. TRUTH_TABLE :: DRAW_01 (int ROW, int COL) { // BITMASK FOR ROW VALUE. int BITMASK; BITMASK = pow (2, TOTAL_COLS - COL); // AND THE ROW & BITMASK TOGETHER. IF THEY ARE GREATER THAN OR EQUAL // TO 1, WRITE A 1 TO THE TRUTH TABLE BOX. IF THEY ARE LESS THAN // 1, WRITE A 0 TO THE TRUTH TABLE BOX. if ((ROW & BITMASK) >= 1) { FILEPTR2 << 1; } else { FILEPTR2 << 0; } return 0; } // CLOSE DATA FILE AND OUT FILE. TRUTH_TABLE :: CLOSE_FILES () { FILEPTR1.close (); FILEPTR2.close (); return 0; } // FILENAME: PROGRAM1.CPP AUTHOR: ANTHONY F. ORTIZ // THIS PROGRAM DRAWS A TRUTH TABLE USING A CLASS CALLED TRUTH_TABLE. int main () { // CLEAR THE SCREEN, OPEN THE DATA FILE AND OUT FILE, DRAW THE // TRUTH TABLE, CLOSE THE DATA FILE AND OUT FILE, WRITE A // QUICK MESSAGE TO THE SCREEN, AND QUIT THE PROGRAM. clrscr (); TRUTH_TABLE TT; TT.OPEN_FILES (); TT.DRAW_TTABLE (); TT.CLOSE_FILES (); cout << "TRUTH TABLE DONE." << endl; return 0; } // DATA FILE: TTABLE.DAT 2 // OUTFILE: TTABLE.OUT ÚÄÄÄÂÄÄÄÂÄÄÄ¿ 0 ³ 0 ³ 0 ³ ³ ÃÄÄÄÅÄÄÄÅÄÄÄ´ 1 ³ 0 ³ 1 ³ ³ ÃÄÄÄÅÄÄÄÅÄÄÄ´ 2 ³ 1 ³ 0 ³ ³ ÃÄÄÄÅÄÄÄÅÄÄÄ´ 3 ³ 1 ³ 1 ³ ³ ÀÄÄÄÁÄÄÄÁÄÄÄÙ
BACK TO CS3430 PAGE.