Sunday, November 29, 2009

Use Real Syntax of Main to write something into a file

#include
#include

int main(int argc, char* argv[]){
FILE* fp;
char ch;
if(argc!=2){
printf("you forgot to enter the filenname.\n");
exit(1);
}
if((fp=fopen(argv[1],"w"))==NULL){
printf("cannot open file.\n");
exit(1);
}
do{
ch=getchar();
putc(ch,fp);
}while(ch!='$');
fclose(fp);
return 0;
}
After you compile it, you can run it on command prompt. And you can type in anything except you enter "$" and press enter key.

IO_Menu and IO_CheckList

In Fardad's IO_Menu and IO_CheckList, when you reach the last item in the vertical menu, radio, or checkbox and you press down key, it does nothing and stops there. But it's supposed to go back to the first item. And the up key has the same problem. So I change some codes there to fix the problem.
For example in Vertical Menu:
case DOWN_KEY:
if(_dir == Vertical){
if(x < _len-1){
x++;
}
else {
x=0;
}
}
else{
done = true;
}
break;
case UP_KEY:
if(_dir == Vertical){
if(x > 0){
x--;
}
else {
x=_len-1;
}
}
Then, do the same thing to IO_CheckList.

Monday, November 2, 2009

Copy constructor and assignment operator for DsStack class

1. copy constructor
1.1 Stack::Stack(const DString data){
this->top=(SNode*)0;
Push(data);
}
1.2 Stack::Stack(const Stack& t){
this->top=(SNode*)0;
*this = t;
}

2. assignment operator
Stack& Stack::operator=(const Stack& t){
while(!IsEmpty()){
Pop();
}
SNode* temp=t.top;
while(temp){
Push(temp->data);
temp=temp->next;
}
delete temp;
SNode* tem=this->top;
this->top=(SNode*)0;
while(tem){
Push(tem->data);
tem=tem->next;
}
delete tem;
return *this;
}