tag:blogger.com,1999:blog-70485684654363303772024-01-01T01:05:41.465-08:00C/C++ ProgrammingFrankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-7048568465436330377.post-10469663129364849712009-11-29T14:07:00.000-08:002009-11-29T14:12:45.377-08:00Use Real Syntax of Main to write something into a file#include<stdio.h> <br />#include<stdlib.h> <br /><br />int main(int argc, char* argv[]){ <br /> FILE* fp; <br /> char ch; <br /> if(argc!=2){ <br /> printf("you forgot to enter the filenname.\n"); <br /> exit(1); <br /> } <br /> if((fp=fopen(argv[1],"w"))==NULL){ <br /> printf("cannot open file.\n"); <br /> exit(1); <br /> } <br /> do{ <br /> ch=getchar(); <br /> putc(ch,fp); <br /> }while(ch!='$'); <br /> fclose(fp); <br /> return 0; <br />} <br />After you compile it, you can run it on command prompt. And you can type in anything except you enter "$" and press enter key.Frankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.com0tag:blogger.com,1999:blog-7048568465436330377.post-10876548457673754102009-11-29T10:02:00.000-08:002009-11-29T13:36:07.157-08:00IO_Menu and IO_CheckListIn <a href="https://cs.senecac.on.ca/~fardad.soleimanloo/">Fardad's</a> <a href="http://zenit.senecac.on.ca/wiki/index.php/OOP344_Assignment_Two#IO_Menu">IO_Menu</a> and <a href="http://zenit.senecac.on.ca/wiki/index.php/OOP344_Assignment_Two#IO_CheckList">IO_CheckList</a>, 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.<br />For example in Vertical Menu:<br />case DOWN_KEY:<br /> if(_dir == Vertical){<br /> if(x < _len-1){<br /> x++;<br /> }<br /> else {<br /> x=0;<br /> }<br /> }<br /> else{<br /> done = true;<br /> }<br /> break;<br /> case UP_KEY:<br /> if(_dir == Vertical){<br /> if(x > 0){<br /> x--;<br /> }<br /> else {<br /> x=_len-1;<br /> }<br /> }<br />Then, do the same thing to <a href="http://zenit.senecac.on.ca/wiki/index.php/OOP344_Assignment_Two#IO_CheckList">IO_CheckList</a>.Frankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.com0tag:blogger.com,1999:blog-7048568465436330377.post-58756848648500104042009-11-02T17:52:00.000-08:002009-11-02T17:58:59.074-08:00Copy constructor and assignment operator for DsStack class1. copy constructor<br /> 1.1 Stack::Stack(const DString data){<br /> this->top=(SNode*)0;<br /> Push(data);<br /> }<br /> 1.2 Stack::Stack(const Stack& t){<br /> this->top=(SNode*)0;<br /> *this = t;<br /> }<br /><br />2. assignment operator<br /> Stack& Stack::operator=(const Stack& t){<br /> while(!IsEmpty()){<br /> Pop();<br /> }<br /> SNode* temp=t.top;<br /> while(temp){<br /> Push(temp->data);<br /> temp=temp->next;<br /> }<br /> delete temp;<br /> SNode* tem=this->top;<br /> this->top=(SNode*)0;<br /> while(tem){<br /> Push(tem->data);<br /> tem=tem->next;<br /> }<br /> delete tem;<br /> return *this;<br /> }Frankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.com1tag:blogger.com,1999:blog-7048568465436330377.post-21011832330460283362009-10-29T13:04:00.000-07:002009-10-29T13:19:59.243-07:00IRC classesWe took our <a href='http://download.cnet.com/mIRC/3000-2150_4-10001733.html?part=dl-50355&subj=dl&tag=button'>IRC</a> class this Tuesday, and got a lot of information about how to start our <a href='http://zenit.senecac.on.ca/wiki/index.php/OOP344_Assignment_Two'>project</a>. It’s really a good idea to talk about <a href='http://zenit.senecac.on.ca/wiki/index.php/OOP344_Assignment_Two'>project</a> on-line, because when <a href='https://cs.senecac.on.ca/~fardad.soleimanloo/'>fardad</a> said something, we could do it on our computer immediately. Actually, it’s the same to lab class, but unfortunately, we didn’t have lab class. In addition, we can have <a href='http://zenit.senecac.on.ca/wiki/index.php/OOP344_Teams#IRC_Meeting_Schedule'>our meeting</a> a little bit later than regular schedule, our team (<a href='http://zenit.senecac.on.ca/wiki/index.php/OOP344_BINGO'>BINGO</a>) will meet on <a href='http://download.cnet.com/mIRC/3000-2150_4-10001733.html?part=dl-50355&subj=dl&tag=button'>IRC</a> at 9:00pm on both Tuesday and Friday, in order to ensure everyone can join <a href='http://zenit.senecac.on.ca/wiki/index.php/OOP344_Teams#IRC_Meeting_Schedule'>our meeting</a>. And also, we got a lot of extra time to prepare for the meeting, such as installed <a href='http://tortoisesvn.tigris.org/'>SVN</a>. However, the only imperfection is that wasting lots of time on typing. Anyway, I really enjoy <a href='http://download.cnet.com/mIRC/3000-2150_4-10001733.html?part=dl-50355&subj=dl&tag=button'>IRC</a> class.Frankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.com0tag:blogger.com,1999:blog-7048568465436330377.post-40909877381268949002009-10-10T13:47:00.000-07:002009-10-10T14:15:16.629-07:00Problem on as1tester.c, Maybe BUGI just tested my code by using <a href='https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/AS1/as1tester.c'>as1tester.c</a>, but I found some problem with test 9.23. It said "Test 9.23: io_edit() Escape when <a href='http://zenit.senecac.on.ca/wiki/index.php/OOP344_Assignment_One#IsTextEditor:'>IsTextEditor</a> = 0, hit HOME key, then 'a', 'b', 'c' and then ESCAPE to test!", but it would be always failed because it required everything equals default value, I mean, nothing was allowed to change. So if you hit HOME KEY and insert three letters, that's impossible to pass the test. Therefore, I think there is a bug here.Frankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.com2tag:blogger.com,1999:blog-7048568465436330377.post-15962219409949251902009-09-27T20:08:00.000-07:002009-09-27T20:34:55.880-07:00challenge 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: <br />void io_display(const char* str, int row, int col, int len){<br />for(io_move(row,col), len<=0 ? io_putstr(str):0;0<len;*str?io_putch(*(str++)):io_putch(' '), len--);<br />}<br /><br />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 <a href='https://cs.senecac.on.ca/~ipc144/ipcweb_html/unit4.html'>loop</a> work. I think it's shorter than <a href='http://franksun123.blogspot.com/2009/09/challenge-of-void-iodisplayconst-char.html'>my previous code</a> because it's only <b>ONE</b> line.Frankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.com0tag:blogger.com,1999:blog-7048568465436330377.post-9655080942294769402009-09-19T13:36:00.000-07:002009-09-19T16:03:57.222-07:00challenge 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).<br /><br />void io_display(const char* str, int row, int col, int len){<br /> int i;<br /> io_move(row, col);<br /> len<=0 ? io_putstr(str) : 0;<br /> for(i=0;i<len;i++){<br /> str[i] ? io_putch(str[i]) : io_putch(' ');<br /> }<br />}<br /><br />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 <a href='https://cs.senecac.on.ca/~fardad.soleimanloo/'> Fardad</a>'s <a href='https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/C/02-Sep17/ciol.c'>note</a>.Frankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.com0tag:blogger.com,1999:blog-7048568465436330377.post-34685390001455808772009-09-17T21:15:00.000-07:002009-09-17T21:55:12.444-07:00challenge of GetInt(char* str, int num)<p>void GetInt(char *str, int num){ </p><p>int n=num; </p><p>int count=0; </p><p>while(n>0){ </p><p>n/=10; </p><p>count++; </p><p>} </p><p>*(str+count)='\0'; </p><p>for(;count>0;count--){ </p><p>*(str+count-1)=num%10+'0'; </p><p>num/=10;</p><p>} </p><p>}</p>Here is all my code for this challenge. It's different from <a href="https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/C/02-Sep17/keycode.c"> the notes </a> 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.Frankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.com0tag:blogger.com,1999:blog-7048568465436330377.post-76106146249074401552009-09-17T06:13:00.000-07:002009-09-17T09:57:52.110-07:00Review of C1. 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.<br /> Here is the website links to example :<br /> <a href="https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/oplazy1.c">https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/oplazy1.c</a><br /><br />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.<br /> Example :<br /> <a href="https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/whatever.what">https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/whatever.what</a><br /> <a href="https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/foo.faa">https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/foo.faa</a><br /> <a href="https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/include.c">https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/include.c</a><br /><br />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.<br /> Example :<br /> <a href="https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/define.c">https://cs.senecac.on.ca/~fardad.soleimanloo/oop344/notes/02-Sep10/define.c</a>Frankhttp://www.blogger.com/profile/06408763071839631262noreply@blogger.com0