Thursday, December 3, 2009

review quiz 3


#include < stdio.h >

int main (int argc, char* argv[], char* e[]){
int i;
printf("%d\n", argc);

printf("PRINT ARGUMENTS\n");
for(i=0; i < argc; i++) {
printf("%d : %s\n", i, argv[i]);
}

printf("\nPRINT ENVIRONMENT\n\n");
for(i=0; e[i]; i++) {
printf("%d : %s\n", i, e[i]);
}
return 0;
}

Tuesday, December 1, 2009

write copy file template


=========================================
TEMPLATE CLASS
=========================================

template
class CopyFormattedFile {
ifstream *fin;
ofstream *fout;
public:
CopyFormattedFile(const char *fromFileName, const char *toFileName); // constructor
~CopyFormattedFile(); // destructor
int copy(); // copy file, return 0 when success
};


template
CopyFormattedFile::CopyFormattedFile(const char *fromFileName, const char *toFileName)
{
fin = new ifstream(fromFileName, ios::in|ios::binary);
fout = new ofstream(toFileName, ios::out|ios::binary);
}

template
CopyFormattedFile::~CopyFormattedFile()
{
delete fin;
delete fout;
}

template
int CopyFormattedFile::copy()
{
Type rec;

if(!fin->is_open() || !fout->is_open()) {
return -1;
}

while(!fin->fail() && !fout->fail()) {
fin->read((char *)&rec, sizeof(Type));

if(fin->gcout() == sizeof(Type)) { // gcount() function return the last read bytes
fout->write((char *)&rec, sizeof(Type));
}
else {
return -1; // file format error
}
}

return fin->feof() ? 0 : -1; // return 0 only when having copied all content
}


================================
TEMPLATE FUNCTION:
================================


template
int copy(const char *fromFileName, const char *toFileName)
{
ifstream *fin = new ifstream(fromFileName, ios::in|ios::binary);
ofstream *fout = new ofstream(toFileName, ios::out|ios::binary);
Type rec;

if(!fin->is_open() || !fout->is_open()) {
return -1;
}

while(!fin->fail() && !fout->fail()) {
fin->read((char *)&rec, sizeof(Type));

if(fin->gcout() == sizeof(Type)) { // gcount() function return the last read bytes
fout->write((char *)&rec, sizeof(Type));
}
else {
return -1; // file format error
}
}

int ret = fin->feof() ? 0 : -1; // return 0 only when having copied all content

delete fin;
delete fout;

return ret;
}

Saturday, November 28, 2009

group work conflict

1. As a group work, it was hard to balance work load for everyone. Some of the group members complained they did not have enough work to do.

2. Some of the group members took too many courses and they didn't have time to work for this project every day. But for a project, we needed time to merge work and fix bugs.

3. Some people liked to finish work as soon as possible and others did not take it as a high priority.

4. After we merged our works together and started testing, we found many bugs. First we notified the class owner to fix them. Later we found it is inefficient to wait for their fixes. Therefore, we decided anybody could change the code even it was written by others. However, new problem raised: person A changed person B's code without completely understanding, and later person B changed them back. There were lots of repeat work and conflicts.

study notes for fstream (2)


#include
#include
using namespace std;

int copy (const char* A, const char* B){
ifstream fin (A, ios::in | ios::binary);
ofstream fout (B, ios::out | ios:: binary);
if (!fin.is_open() || !fout.is_open()){
return -1;
}

char buf [512];
while (!fin.eof() && !fin.fail() && !fout.fail()){
fin.read(buf, 512);
int n = fin.gcount();
if ( n > 0 ){
fout.write(buf, n);
}
}

return fin.eof() ? 0 : -1;
}

int main(){

int ret = copy ("E:\\sxz\\1.docx" , "E:\\sxz\\B.docx");
cout << "ret=" << ret << endl;
return 0;
}

Sunday, November 22, 2009

write template for linkedlist


#ifndef __DLL_H__
#define __DLL_H__
template < class T >
class DLinkedList;
template < class Type >
class DNode{
Type data;
DNode < Type >* next;
DNode < Type >* prev;
DNode(Type data, DNode < Type > * prev = (DNode < Type > *)0, DNode < Type > * next = (DNode < Type > *)0);
friend class DLinkedList < Type >;
};
template < class T >
class DLinkedList{
DNode < T >* head;
DNode < T >* cur;
DNode < T >* tail;
public:
DLinkedList(void);
~DLinkedList(void);
void Add(T data);
bool IsEmpty(void);
T Remove(void);
bool GoNext(void);
bool GoPrev(void);
bool GoHead(void);
bool GoTail(void);
T Visit(void);
T& operator[](T index);
};
#endif

#include "LL.h"
template < class T >
DNode < T > ::DNode(T data, DNode < T > * prev, DNode < T > * next){
this->data = data;
this->prev = prev;
this->next = next;
}

template < class T >
DLinkedList < T > ::DLinkedList(void){
head = tail = cur = (DNode < T > *)0;
}
template < class T >
DLinkedList < T > ::~DLinkedList(void){
while(!IsEmpty()) Remove();
}
template < class T >
void DLinkedList < T > ::Add(T data){
DNode < T > * temp = new DNode < T > (data, tail);
if(tail){
tail->next = temp;
tail = temp;
}
else{
head = tail = cur = temp;
}
}
template < class T >
bool DLinkedList < T > ::IsEmpty(void){
return !cur;
}
template < class T >
int DLinkedList < T > ::Remove(void){
T data = 0;
return data;
}
template < class T >
bool DLinkedList < T > ::GoNext(void){
bool res = false;
if(cur != tail){
cur = cur->next;
res = true;
}
return res;
}
template < class T >
bool DLinkedList < T > ::GoPrev(void){
bool res = false;
if(cur != head){
cur = cur->prev;
res = true;
}
return res;
}
template < class T >
bool DLinkedList < T > ::GoHead(void){
bool res = false;
if(cur){
cur = head;
res = true;
}
return res;
}
template < class T >
bool DLinkedList < T > ::GoTail(void){
bool res = false;
if(cur){
cur = tail;
res = true;
}
return res;
}
template < class T >
T DLinkedList < T > ::Visit(void){
return cur->data;
}
template < class T >
T& DLinkedList < T > ::operator[](T index){
Node < T > * temp = head;
if (cur == head){
GoNext();
}
head = head -> next;
head -> prev = (Node < T > *) 0;
T d = temp -> data;
delete temp;
return d;
}

Saturday, November 21, 2009

study notes for fstream

print a file backward


int main(){
Student S;
fin.seekg(0); //back to the first
int i=1;
while(!fin.fail()){
fin.seekg(sizeof(Student*(-1)), ios::end);
if(!fin.fail()){
fin.read((char*)&S, sizeof(Student));
cout << s << endl;
i++;
}
}
}

Thursday, November 19, 2009

a bug in IO_Checklist

There was a bug in IO_Checklist.

To fix it, you need to change "for(int i=0;i<_len-1;i++)" to "for(int i=0;i<_len;i++)".


int IO_CheckList::edit(void){
int key;
bool done = false;
int x = 0;
int stat = 0;
while(!done){
stat = (int)_status[x];
key =io_flag(_format, IO_Field::getRow()+1+x, IO_Field::getCol()+1,&stat,_mode);
_status[x] = !!stat;
switch(key){
case DOWN_KEY:
if(x < _len-1){
x++;
}
else{
done = true;
}
break;
case UP_KEY:
if(x > 0){
x--;
}
else{
done = true;
}
break;
case ' ':

if(_mode == Radio){
for(int i=0;i<_len;i++){
if(i!=x && _status[i]){
_status[i] = false;
io_displayflag(_format, IO_Field::getRow()+1+i,
IO_Field::getCol()+1,0);
}
}
}
break;
default:
done = true;
}
}
return key;
}

Sunday, November 15, 2009

study notes for operator =; +; +=

I reviewed my notes and added operator +

DString& DString :: operator = (const Dstring& s){

delete[ ] this ->data;
this -> data = (char*)0;
this -> size = s.size;
this -> data = new char[this->data+1];
strcpy(this->data, s.data);
teturn *this;

}

DString& DString :: operator += (const DString& s){

char* temp = new char[this->size+s.size+1];
strcpy(temp, this->data);
strcat(temp, s.data);
this->size += s.size;
delete [ ] this -> data;
this ->data = temp;
return * this;

}

DString& DString :: operator + (const DString& s){

char* temp = new char[this->size+s.size+1];
strcpy(temp, this->data);
strcat(temp, s.data);
DString ds(temp);
delete[ ] temp;
return ds;

}

Thursday, October 29, 2009

online meeting

I do like the online meeting because my group members are in different sections and it is hard to find a convenient time to set up a face-to-face meeting. We can discuss our work and know each others' progress. Online meeting can be easily recorded for later reference.

However, it is annoying when I experienced a time delay. The chat window was not refreshed in real time. Sometimes the window was either frozen or scrolled up half the screen. Hopefully it would be better next time.

Overall, the online meeting is very good. It’s convenient, flexible and productive.

MEETING MINUTES OF TEAM BINGO (TEAM#3)

The second meeting of our team was held at #Seneca through IRC

Date: Oct. 27, 2009
Time: 9 to 10 pm
Present: Fardad, Keyan, [Frank]Dong, ChunXia, Feihong, David, [Lucy]Xiaozhe

Prof. Fardad was a chair and organized the meeting.

What we have done during the meeting?

1. Frank was chosen to be the organizer for our group, to be able to initiate the project source.
He added his ciol.h and ciol.c to the PRJ directory.

2. A general headerfile io_def.h was created and added to the PRJ dir.

3. A prjmain.cpp file was created. There were one int main() funciton and 8 "sub-main" functions. Everyone can share and at the same time run his or her own test.

What we need to do next?

1. We can modify our own main and start developing.

David ___________IO_edit
Keyan __________ IO_checklist
[Frank]Dong _____ IO_label
Feihong _________ IO_menu
Chunxia_________ IO_ frame
An______________ IO_Radio
Zhaolong_________IO_Form
[Lucy]Xiaozhe_____ IO_field

2. For each class in the project we should create a headerfile and a cpp file with the same name of the class. ie: io_label.h and io_label.cpp.

3. Each of us MUST add a txt file with Seneca id to the docs directory.

4. Whenever we are committing something make sure at top of the file we add what we have done.

5. Fardad said: “NEVER COMMIT A CODE THAT IS NOT BUILDABLE ”

Saturday, October 24, 2009

first meeting minutes

MEETING MINUTES OF TEAM#3

The first meeting of our team was held at #OOP344Team#3 chart room through IRC

Date: Oct. 23, 2009
Time: 9 to 10 pm
Present: Keyan, [Frank]Dong, ChunXia, Feihong, David, [Lucy]Xiaozhe

Actions:
1. Every member will take a ownership of different classes in assignment 2

David ____________IO_edit
Keyan ___________ IO_checklist
[Frank]Dong ______ IO_label
Feihong __________ IO_menu
Chunxia__________ IO_ frame
An______________ IO_Radio
Zhaolong__________IO_Form
[Lucy]Xiaozhe______ IO_field

There are two classes left IO_MenuBar and IO_TextEdit. Frank, the strongest technical person in our group, is volunteered both of them. We all believe that those exercises can help Frank promote his mark from A+ to A++.

2. Next two meetings will be held at the same place on Tuesday (Oct. 27) 9:00pm and Friday (Oct. 30) 9:00pm. We need more discussing about assignment 2. Please prepare your questions and proposals (solutions) in advance.

3. Team name will be changed to BINGO. Feihong will be responsible for updating the group web pages accordingly.

Thursday, October 22, 2009

Challenge question: DsStack copy constructor


Stack::Stack(const Stack& stack) {
SNode *p = stack.top;
top = (SNode*)0;

// get a reversed stack
while(p) {
Push(p->data);
p = p->next;
}

// reverse it again
p = top;
top = (SNode*)0;
SNode* del;

while(p) {
Push(p->data);
del = p;
p = p->next;
delete del;
}
}

Friday, October 16, 2009

study notes

After I wrote assignment1, I felt that the io_display function written by Fardad is a really good study example, because when I first wrote displayflag, I wrote more than 30 lines of code to handle spaces and try to move the cursor around. After I walked though io_display function, I found Fardad used only two for loops with a few lines and did what my 30 lines did. So, I changed my displayflag function and called io_display inside.



This is my displayflag function final copy,

void io_displayflag(const char *format, int row, int col, int status) {

char format2[4];

strcpy (format2, format);

if (status == 0){

//display unchecked

format2[1] = ' ';

}

io_display(format2, row, col, 4);

io_move(row, col+1);

}


This is the display function written by Fardad,

void io_display(const char *str, int row, int col, int len){

io_move(row, col);

if(len <= 0){

io_putstr(str);

}

else{

int i;

for(i=0;i < len && str[i];i++){

io_putch(str[i]);

}

for(;i < len;i++){

io_putch(' ');

}

Sunday, October 11, 2009

study pointer and array

The variable 'ar' is being used without being initialized

int _tmain(int argc, _TCHAR* argv[])
{
char* ar;

*ar = 'a';
printf ("%c\n", *ar);

return 0;
}

fix 1,

int _tmain(int argc, _TCHAR* argv[])
{
char* ar;
char p;

ar = &p;

*ar = 'a';
printf ("%c\n", *ar);

return 0;
}

Output:
a

fix 2,

int _tmain(int argc, _TCHAR* argv[])
{
char* ar;
ar = new char [8];

*ar = 'a';
printf ("%c\n", *ar);

return 0;
}

Output:
a

Sunday, October 4, 2009

A Good Book to Read - The C++ Standard Library: A Tutorial and Reference

There is a C++ book that you can find in Seneca College ebook library.

http://lcweb.senecac.on.ca:2063/0201379260

The C++ Standard Library: A Tutorial and Reference
by Nicolai M. Josuttis

Nicolai Josuttis's The C++ Standard Library provides one of the best available guides to using the built-in features of C++ effectively. It reflects the newest elements of the C++ standard library incorporated into the full ANSI/ISO C++ language standard. In particular, the text focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms.

Sunday, September 27, 2009

Modify io_display function

Challenge: Modify io_display function to the shortest code possible.

void io_display(const char *str, int row, int col, int len){
io_move(row, col);
(len <= 0) ? io_putstr(str) : for(int i=0; i < len; i++)
io_putch(i < strlen(str) ? str[i] : ' ');
}

Thursday, September 17, 2009

new solution of GetInt(char *strint, int val)

There are some improvements in Cong Wang's solution of the first challenge.

1. it does not handle negative integer
2. it does not handle integer zero

Cong Wang's solution
==============================================
void GetInt(char *strint, int val){

char rev[100];

int length=0;

while (val > 0)
{
int a = val % 10;
rev[length++] = a | '0';
val /= 10;
}
rev[length]='\0';


length--;
int rev = 0;
while (length >= 0)
{
strint[rev++] = rev[length--];
}

strint[rev] = '\0';
}
==============================================


my new solution
==============================================


void GetInt(char *strint, int val) {
int len = 0;
bool negative = false;

if(val < 0) { // if negative
val = -val;
negative = true;
} else if (val == 0) { // if 0
strint[len++] = '0';
}

while (val > 0) {
strint[len++] = (val % 10) + '0';
val /= 10;
}

if(negative) {
strint[len++] = '-';
}

// reverse this string
for(int i=0; i < len /2; i++) {
char a = strint[i];
strint[i] = strint[len-1-i];
strint[len-1-i] = a;
}

strint[len] = '\0';
}

Wednesday, September 16, 2009