Sunday, September 27, 2009

challenge 3 --- void io_display(const char* str, int row, int col, int len)

This is my code for challenge 3 which makes the io_display function shorter. Here is my code:
void io_display(const char* str, int row, int col, int len){
for(io_move(row,col), len<=0 ? io_putstr(str):0;0<len;*str?io_putch(*(str++)):io_putch(' '), len--);
}

I put io_move(row, col) in the initial part, and also in the initial part, I check if "len" is less or equal than 0. And I only put 0<len in the condition part, so I check the str at the last. At the very last, I put len-- to make the for loop work. I think it's shorter than my previous code because it's only ONE line.

Saturday, September 19, 2009

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

Here is my code for void io_display(const char* str, int row, int col, int len).

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

First of all, I use io_move(row, col) to put the curse at somewhere I want, and then, I check whether len is less or equal than 0, if it's true, it will do io_putstr(str). However, I put a 0 there meaning do nothing. After that, I do a for loop in order to decide which I should print, str[i] or a space. I think it's shorter than Fardad's note.

Thursday, September 17, 2009

challenge of GetInt(char* str, int num)

void GetInt(char *str, int num){

int n=num;

int count=0;

while(n>0){

n/=10;

count++;

}

*(str+count)='\0';

for(;count>0;count--){

*(str+count-1)=num%10+'0';

num/=10;

}

}

Here is all my code for this challenge. It's different from the notes in class because we cannot use std library, so the first thing came to my mind was use a number plus '0', but at the very first, I need to know how long this number is. And I write while(n>0){ n/=10; count++; } . Then, I change the integer to string from very last to the beginning, so I use the for loop for(;count>0;count--){ *(str+count-1)=num%10+'0'; num/=10; } . Finally, I just want to end the string, and I got *(str+count)='\0'; in the middle. That's all my steps.

Review of C

1. First of all, we can use logical operator (&&) instead of some if statements. The structure of if statement is if( condition) { statement;}, if the condition is true, it will do the statement. But we can also do that by using && . Like condition && statement; , it's the same to if statement, if the condition is false, it will not look at it.
Here is the website links to example :
https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/oplazy1.c

2. # include "file.h". It uses double quote, meaning the file is in the same directory. It will copy the date in the file.h to the current file, and be executed when the program starts. You can divide a program into two parts, and put each of them in a file. Then, write a program just contains two #include "filename" in it. You will get the same result.
Example :
https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/whatever.what
https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/foo.faa
https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/include.c

3. #define a b. It's the same as replace. Simply, all the a will be replaced by b. So be careful with this, #define SUM b+c. When you see SUM*a in the program, it will just do b+c*a.
Example :
https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/define.c