Sunday, April 15, 2012

Test 2 Q4 walkthrough with comments!


Q4:

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct PhRec{
  char name[60];
  int PhoneNumber;
};
bool operator<(PhRec& left, PhRec& right){
  return strcmp(left.name, right.name) < 0;
}
#include "tqueue.h"

int main(){
  fstream f("phones.bin", ios::in|ios::binary);//just reads the file
  PhRec pr;
  Tqueue<PhRec> Q;//change all Tqueue type into PhRec type
  while(f.good()){//continue in this loop if f.good() is true
    f.read((char*)&pr, sizeof(pr));//reads the sizeof(pr) and store it in &pr as a char*
    if(f.good()){//if the previous read function worked go in here
      Q.append(pr);//append pr (in this case is a character string) to the queue list
    }
  }
  f.close();
  f.open("phones.bin", ios::out|ios::binary);//rewriting the file
  Q.sort(true);//sort the Queue list
  while(!Q.isEmpty()){//continue this loop until the Queue list is empty
    pr = Q.remove();//assign pr with the value of Q.remove();
    f.write((const char*)&pr, sizeof(pr));//write sizeof(pr) bytes from &pr as a char*
  }
  return 0;
}

Test 2 Q3 walkthrough with comments!:
//Q3
#include <iostream>
#include "tqueue.h"
using namespace std;
int main(int argc, char* argv[]){
  bool badArgs = false;
  bool Asc = false;
  Tqueue<int> Q;//changing all "type" in tqueue class as int
  if(argc < 3){//if there are less than 3 argument in argc
    badArgs = true;
  }
  else{
    if(argv[1][0] != '-'){//checks the first character
      badArgs = true;
    }
    else{
      if(argv[1][1] == 'a'){//checks the second character
        Asc = true;
      }
      else if(argv[1][1] != 'd'){
        badArgs = true;
      }
      int arg;
      for(int i =2; !badArgs && i<argc;i++){//i=2; if !badArg is false and i < argc; i++
        if(sscanf(argv[i],"%d", &arg) == 1){//look at argv[i], read it as an integer, store it into &arg, sscanf return
                                            //sscanf returns the number of variable filled (in this case 1)
          Q.append(arg);//append the value of arg at the end of the Qlist
        }
        else{
          badArgs = true;
        }
      }
    }
  }
  if(badArgs){
    cout<<"Incorrect class use as follows:"<<endl<<"tq -[a|d] 99 [99 99 ...]"<<endl;
  }
  else{
    Q.sort(Asc);//see if the bool(Ascending) is true or not in the function sort (bool sort(bool Ascending = true);)
                //if true will sort the Qlist by ascending order or if false will go descending
    while(!Q.isEmpty()){
      cout<<Q.remove()<<" ";
    }
    cout<<endl;
  }
  return 0;
}
Quick look at command line: int main(int argc, char* argv[])
#include <iostream>
using namespace std;

int main(int argc,char* argv[]){//argc holds the number of parameter +1 (-d del name = 4 arguments), argv[] holds each character in the array
    int i;
    if (argv[1][0] != '-'){
        cout << "idk what: " << argv[1][0] << " is..."<< endl;
        return -1;
    }
    else{
        cout << argc << endl;
        cout << "i know what that is" <<endl;
        cout <<"Command executed: " <<endl;
        cout << "-------------------------" <<endl;
        cout << " argv[1][0]:" <<endl;
        cout << argv[1][0] << endl;//must use argv[1][x] because argv[0][0] holds the file path name, command prompt accepts a string in the second row([1][x])
        cout << " argv[1][1]:" <<endl;
        cout << argv[1][1] << endl;//gets the second character of the command line
        cout << " argv[1][2]:" <<endl;
        cout << argv[1][2] << endl;//gets the third character of the command line
        cout << " argv[1][3]:" <<endl;
        cout << argv[1][3] << endl;
        cout << " argv[1][4]:" <<endl;
        cout << argv[1][4] << endl;
        cout << " argv[2]:" <<endl;
        cout << argv[2] << endl;//checks 2nd argument
        cout << " argv[3]:" <<endl;
        cout << argv[3] << endl;//checks 3rd argument
        cout << " argv[4]:" <<endl;
        cout << argv[4] << endl;//checks 4th argument
    }
  return 0;
}

Sunday, April 8, 2012

Break down of the Fstream walkthrough in class:

#include <fstream>
#include <iostream>
using namespace std;
int main(){
  int i,j;
  cout<<sizeof(i)<<endl;   // the output of this line is 4
  fstream f("f.bin",ios::out|ios::in|ios::binary|ios::trunc);
  for(i=10;i<100;i+=10){//write 10,20,30...90
    f.write((const char*)&i, sizeof(int)); //casting &i to char* because you cannot convert a char with a int because char are 1 byte whilst int are 4 bytes
                                          //hence turning it to char* and &i will make both variable to 4 bytes (all pointers are 4 bytes)
                                          //we put sizeof(int) because we want to write 4 bytes because we want to keep the number in the file as int not char hence keeping the right number
  }
  f.seekg((ios::pos_type)20);//going to position 20 in bytes(since in the file each number is an integer (4 bytes) it will shift 5 numbers (20/4)), casting 20 as a pos_type
  f.read((char*)&i, sizeof(int));//reads sizeof(int)(4 bytes) and store it in i
  cout<<i<<endl; // 1 mark //output is 60, even though its on position 50 it reads number after the position not the position itself
  f.seekg(-(ios::off_type)sizeof(int), ios::end);//go back 4 bytes from the end of the file,at position is at 80
  f.read((char*)&i, sizeof(int));//read the next 4 byte and store it in i
  cout<<i<<endl; // 1 mark output: 90.
  f.seekg(-(ios::off_type)sizeof(int)*3, ios::cur);//go back 12(4*3) bytes(3 ints) from the current position(90), at position(60)
  f.read((char*)&i, sizeof(int));
  cout<<i<<endl; // 1 mark output:70
  f.seekp((ios::off_type)sizeof(int)*2, ios::beg);//go up 8 bytes from the beginnning of the file position(20)
  f.write((char*)&i, sizeof(int));//write 4 bytes from &i(holding number 70 from previous read function)
  f.seekp((ios::off_type)0, ios::end);//change the writing position to the end of the file
  cout<<(j=f.tellp())<<endl;  // 1 mark, j=36 because there are 9 integer in the file hence 36 bytes in total (4*9)
  f.seekg(0);//put the reading cursor to position 0
  while(f.tellg() < j){  // 1 mark, read one integer at a time until the f.tellg() is less than j
    cout << "("<<f.tellg() <<")";
    f.read((char*)&i, sizeof(int));
    cout<<i<<"("<<f.tellg() << "), ";
  }
  //output: (0)10(4), (4)20(8), (8)70(12), (12)40(16), (16)50(20), (20)60(24), (24)70(28), (28)80(32), (32)90(36),
  cout<<endl;   
  return 0;
}

Saturday, April 7, 2012

Simple look at casting operator overload:

#include <iostream>
using namespace std;

class fun{
    int _data;
    char _char;
    int* _data2;
    public:
    fun():_data(0){}
    fun(int d):_data(d),_char('w'),_data2(0){}
    fun(int d, char c):_data(d),_char(c),_data2(0){}
    fun(int d, char c,int size):_data(d),_char(c){_data2 = new int[size];}
    void show(){cout << "data is: " << _data << endl;
                cout << "character is: "<< _char << endl;}
    operator int(){cout << "inside int casting operator" << endl;//casting operator int()
                    return _data;}
    operator char(){{cout << "inside char casting operator" << endl;//casting operator char()
                    return _char;}}
    operator int*(){cout << "inside int* casting operator" << _data2<<endl;//casting operator int*()
                    return _data2;}
    ~fun(){
        delete [] _data2;
    }
};

int main (){
    int x=20;
    char c='d';
    int* ptr(0);

    fun a(5);
    a.show();
    x=(int)a;//calling int casting operator of class
    cout << "x: " << x << endl;//output: x: 5
    cout << endl;
    fun stuff(10,'f',3);
    stuff.show();
    c = char(stuff);//calling char casting operator of class
    cout << c << endl;//output: f

    cout << "&ptr: " << ptr << endl;
    cout << "size: " << sizeof(ptr) << endl;
    ptr = (int*)stuff;//calling int* casting operator
    cout << "&ptr: " << ptr << endl;//output &ptr: (same addess as _data2 in fun class)
    cout << "size: " << sizeof(ptr) << endl;

    delete [] ptr;

    return 0;
}
//ps a little thanks to Mr. D'amico for telling me casting is a operator XD
Break down of Fstream example in class

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

class Employee{
  char _name[15];
  char _lastname[30];
  int _empno;
  double _salary;
public:
  Employee(const char* name="", const char* lastname="", int empno=0, double salary=0.0){
     set(name, lastname, empno, salary);
  }
  void set(const char* name="", const char* lastname="", int empno=0, double salary=0.0){
    strcpy(_name, name);
    strcpy(_lastname, lastname);
    _empno = empno;
    _salary = salary;
  }
  ostream& print(ostream& OS)const{
    return OS<<"Name: "<<_name<<" "<<_lastname<<endl<<"EmpNo: "<<_empno
      <<", Salary: "<<_salary;
  }
  virtual ~Employee(){
    //print(cout)<<"is dead!!!!"<<endl;
  }
};
ostream& operator<<(ostream& OS, const Employee& E){
  return E.print(OS);
}

int main(){
  Employee E;
  ifstream file;
  file.open("emp.bin",ios::binary);//open the file emp.bin, consider the stream as Binary instead of text
  file.seekg((ios::off_type)0, ios::end);//set the cursor position from (0), to the end of the file(ios::end)
  int loc = file.tellg();//set loc to the cursor position (tellg())
  cout << sizeof(Employee) << endl;
  while(loc > 0 ){//while loc is greater than zero go in here.
    loc -= sizeof(Employee);//subtract loc by the size of an Employee object (72)
    file.seekg((ios::pos_type)loc);//put the cursor position to loc (first time in is 288 then, 216, 144, 72, 0)
    file.read((char*)&E, sizeof(Employee));//read (sizeof(Employees) = 72) of character from the current position and store it in &E, which is cast to a char*
    cout<<E<<endl;
  }
  return 0;
}

Sunday, April 1, 2012

Modified the stack list in the textbook closer to gamer point of view:

//Queue List
//First in First out FIFO
#include <iostream>
using namespace std;

class monster {
    int damage;

    public:
    monster(int a=0):damage(a){}
    int pop(){
        return damage;
    }
};

struct wave {
    monster data;//creates a monster object when you make a wave object
    wave* _next;//points to the address of a wave object
    wave(const monster& a, wave* next):data(a), _next(next){}//constructor accepts a reference of a monster object and pointer that points to a wave object.
                                                            //will also initiate monster object 'a' and cast it to monster object 'data', will do the same with _next with next
};

class game {
    wave* _start;
    wave* _end;
    public:
    game(): _start(NULL), _end(NULL){}
    ~game (){
        wave* current;
        while (current = _start){
            _start = _start->_next;
            delete current;
        }
    }//~game()
    void add(int d){
        wave* p = new wave(d,0);//second parameter is 0 because in a Queue when you're adding it should be in the end, hence the _next value should be null
        if (_start)
            _end->_next=p;//if there is a _start the _end will have the new address
        else
            _start=p;//if there are no _start meaning this is the first one will assign the _start address to the new wave object
         _end = p;//regardless if there was a value for _start or not, assign the _end to the new object
    }
    monster rem(){//returns a monster object
        monster data;
        if (_start){
            wave* current=_start;
            data = _start->data;//assign the _start's monster object(data, inside wave class) with the new monster object (created in this function)
            _start=_start->_next;//assign the _start address to the _next _start address;
            delete current;
            if (!_start)//if the address of _start is NULL change the _end to NUll as well
                _end=NULL;
        }
        return data;
    }
    bool empty(){return _start == NULL;}
};

int main(){
    game d;//_start and _end has Null address

    //Push Data onto the queue
    d.add(3);
    d.add(30);
    d.add(10);
    d.add(50);

    //remove the first node
    d.rem();

    //pop off data off the queue
    while (!d.empty())
        cout << d.rem().pop() << endl;

    return 0;
}