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;
}