#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
typedef enum {
ADB_INT,
ADB_STRING, }
adb_type;
typedef enum {
LEFT,
RIGHT,
CENTER,
} just_dir;
typedef struct table table;
typedef struct column column;
struct table {
string name;
vector <column> col;
int col_cnt;
int row_cnt;
};
struct column {
string name;
adb_type type;
vector <int> ints;
vector <string> strings;
};
void justify(string s, just_dir d, int width) {
int length = s.length();
bool is_truncated = false;
int space_cnt = width - length;
if(space_cnt < 0) {
is_truncated = true;
length = width - 3;
}
switch(d) {
case LEFT:
cout << s.substr(0, length);
if(is_truncated) {
cout << "...";
{
}
else {
for(int i = 0; i < space_cnt; i++ ;)/> {
cout << " ";
}
}
break;
case RIGHT:
if(is_truncated) {
cout << "...";
}
else {
for(int i = 0; i < space_cnt; i++) {
cout << " ";
}
}
cout << s.substr(0, length);
break;
case CENTER:
if(is_truncated) {
cout << s.substr(0, length) << "...";
}
else {
int left_space_cnt = space_cnt / 2;
int right_space_cnt = space_cnt - left_space_cnt;
for(int i = 0; i < left_space_cnt; i++) {
cout << " ";
}
cout << s.substr(0, length);
for(int i = 0; i < right_space_cnt; i++) {
cout << " ";
}
}
break;
default:
break;
}
}
void print_table(table *t) {
const int width = 15;
const char head_c = '#';
const char vert_c = '|';
const char horiz_c = '-';
const char cross_c = '+';
cout << "Table: " << t->name << endl;
if(t->col_cnt == 0) {
cout << "...No columns defined" << endl;
}
}
// print header first
// top row of "################" //
for(int i = 0; i < (t->col_cnt * (width + 3)) + 1 ; i++) {
cout << head_c; }
cout << endl;
// fields row "# field1 # field2 #" //
cout << head_c << " ";
for(int i = 0; i < t->col_cnt; i++) {
justify(t->col[i].name, CENTER, width);
cout << " " << head_c << " ";; }
cout << endl;
// bottom row of "################" //
for(int i = 0; i < (t->col_cnt * (width + 3)) + 1 ; i++) {
cout << head_c; }
cout << endl;
// * Print each row //
for(int r = 0; r < t->row_cnt; r++) {
cout << vert_c << " ";
for(int c = 0; c < t->col_cnt; c++) {
stringstream ss;
switch(t->col[c].type) {
case ADB_STRING:
justify(t->col[c].strings[r], LEFT, width);
break;
case ADB_INT:
ss << t->col[c].ints[r];
justify(ss.str(), RIGHT, width);
break;
default:
justify("!! BAD TYPE !!", LEFT, width);
break;
}
cout << " " << vert_c << " "; }
cout << endl;
cout << cross_c;
for(int i = 0; i < t->col_cnt; i++) {
for(int j = 0; j < width + 2; j++) {
cout << horiz_c; }
cout << cross_c; }
cout << endl; }
}
vector<table>tables;
//if(argc!=3)
{
cout<<"Must provide input and output files as command line parameters."<<endl; }*/
ifstream infile;
infile.open("program enrollment.txt");
if(!infile.is_open()) {
cout<<"INVALID FILE";
exit (1); }
int table_count;
infile>>table_count;
for(int i=0;i<table_count;i++) {
table tmp_table;
infile>>tmp_table.name;
infile>>tmp_table.col_cnt;
infile>>tmp_table.row_cnt;
for(int j=0;j<tmp_table.col_cnt;j++) {
column temp_col;
char col_type;
infile>>col_type;
switch(col_type) {
case 'S':
temp_col.type=ADB_STRING;
break;
case 'I':
temp_col.type=ADB_INT;
break; }
tmp_table.col.push_back(temp_col);
infile>>temp_col.name;
print_table(&tmp_table); }
}
infile.close();
system("PAUSE");
Int main() }
table students
students.name "students";
column tmp;
type.name "SID"
temp.type ADB_INT;
tmp.ints.puch_back(1);
tmp.ints.push_back(2);
tmp.ints,push_back(3);
students.col.push.back(tmp);
tmp.name - "name"
tmp.ints.cleat();
tmp.strings.clear();
tmp.race "race"
tmp.type = ADB_STRING;
tmp.strings.push_back;
tmp.strings.Push_back;
tmp.strings.pudh_back;
students.col.push_back(tmp);
void justify(strings; just dir r, int width);
int length_s.length();
bool is_trunacated = false;
int space cnt_width_length;
if(space_cnt<0){
is truncated = true;
legnth_width_3;
return 0;
}
This post has been edited by macosxnerd101: 11 December 2010 - 10:12 AM
Reason for edit:: Please use code tags

New Topic/Question
Reply



MultiQuote







|