Search

воскресенье, 8 апреля 2012 г.

Study day part 3: Links of Nodes

Linklists. Very important part of programing. Not as confusing as multiple Inheritance, but still pretty confusing. What's a linklist? It's a Main Manager Object and many smaller ones (Nodes) that run around. I won't use any full examples cuz' it will take A LOT of lines to do so, but I will provide functions that can be used. So, I guess we should start with declaration:
class List;

class Node{
   int _data;
   Node* _next;  Node* _prev;
   Node(int data, Node* next = (Node*)0);
   friend class List;
};

class List{
   Node* _head;
   Node* _tail;
   Node* _cur;
public:
   List();
   ...(functions)
   virtual ~List();
};
Pretty strait forward. Next. Constructors & Destructor:
Node::Node(int data, Node* next, Node* prev){
   _data = data;
   _next = next;
   _prev = prev; 
   etc...
Queue::Queue(){
   _head = (Node*)0;
}
Queue::~Queue(){
   while(!isEmpty()) removeHead();
}
Now. After we've covered basics, we should move to functions themselves, and we should start with the most basic function possible "isEmpty" because it's used in most methods.
bool Queue::isEmpty(){
   return !_head;
Nature of a linklist is that if there is nothing at the head, there is nothing in the entire list. So we use it here.
After that, we should add something to the head of the list:
void Queue::add(int data){
   if(isEmpty()){
     _head = new Node(data);
   }else{
      Node* cur = _head;
      Node* tail = cur;
      while(cur){
      tail = cur;
      cur= cur->_next;
   }
   tail->_next = new Node(data);
   }
}

int Queue::remove(){
   int ret = _head->_data;
   Node* ToDel = _head;
   _head = _head->_next;
   delete ToDel;
   return ret;
}

This should cover at least most of it.
 

 

Study day part 2: Bits and Bites

Bitwise operator (I hope I'm spelling it correctly). They aren't that complicated, and I spend most of the break between semester helping a friend with that., so I got used to it. The only problem I have with them is that... Unless you create a function that displays a value by bits working with them is... theoretical. Best thing you can see is "a number changed" and that's it. But they work, and as far as I know very effectively.
First, Let's look at operators themselves.
Operators in binary are exactly like if statements.
AND
1. & - AND. Basically the chart is this:
//I should be a bit more exotic with my tags, only oop344 and c++ isn't enough x)
0 & 0 = 0  //if(false && false) == false
1 & 0 = 0  //if(true && false) == false 
0 & 1 = 0  //if(false && true) == false
1 & 1 = 1  //if(true && true) ==  true
Example:
   A = "0011" //== 3
&
   B = "0101" //== 5
=
         "0001" //== 1
So:
   A & B = 1

inclusive OR 

2. | -  inclusive OR. Basically the chart is this:
//I should be a bit more exotic with my tags, only oop344 and c++ isn't enough x)
0 | 0 = 0  //if(false || false) == false
1 | 0 = 1  //if(true || false) == true  
0 | 1 = 1  //if(false || true) == true  
1 | 1 = 1  //if(true || true) ==  true 

Example:
   A = "0011" //== 3
|
   B = "0101" //== 5
=
         "0111" //== 7
So:
   A | B = 7



exclusive OR 
3. ^ -  exclusive OR, it's a thing that you won't find in if statements, after previous ones it's not that hard. Basically the chart is this:
//I should be a bit more exotic with my tags, only oop344 and c++ isn't enough x)
0 ^ 0 = 0  //if(false | false) == false
1 ^ 0 = 1  //if(true | false) == true  
0 ^ 1 = 1  //if(false | true) == true  
1 ^ 1 = 0  //if(true | true) ==   false  

Example:
   A = "0011" //== 3
^
   B = "0101" //== 5
=
         "0110" //== 6
So:
   A | B = 6

NOT
4.  ~ - NOT operator. Basically what is does if flips all the bits of a value.Example
3 = "0011"
~3 = "11111111111111111111111111111100"

Now the "fun part". Bit movement!

Right Shift
5. >>X - Right Shift operator.
Moves all the bits to the right X times.
A = "0101"; //3
A>>1="0010"; //2
A>>2="0001"; //1
A>>2 = 1;
Also, Right Shift once == divide by two.

Left Shift
 6. <<X - Left Shift operator.
Moves all the bits to the left X times.
WARNING!Apparently, there is a difference in what kind of value are you shifting. Unsigned or signed. If int is unsigned  it will use 0's to fill in the space, but if it's signed, it will use the last bit (if 1 - 1's, if 0 - 0's) to do that.
unsigned A = "0011"; //3
A<<1="0110"; //6
A<<2="1100"; //12
A<<2 = 1; 12
Also, Left Shift once == Multiply by two.

signed A = "0011"; //3
A<<1="0111"; //7
A<<2="1111"; //15
A<<2 = 15;


Example:
#include <cstdio>
using namespace std;

int main(){
unsigned char A = 0xA3;
unsigned char B = 0xF9;
unsigned char C;
printf("A: %X %d\n", A,A); //163 = "1010 0011"
printf("B: %X %d\n", B,B); //249 = "1111 1001"
printf("======================================\n");
C = A & B;
printf("A&B: %X %d\n", C, C); //161 = "1010 0001"
C = A | B;
printf("A|B: %X %d\n", C, C); //251 = "1111 1011"
C = A ^ B;
printf("A^B: %X %d\n", C, C); //90 = "0101 1010"
C = ~A;
printf("~A : %X %d\n", C, C); //92 = "0101 1100"
C = A << 1;
printf("A<1: %X %d\n", C, C); //70 = "0100 0110"
C = A >> 1;
printf("A>1: %X %d\n", C, C); //81 = "0101 0001"
printf ("(A >> 4) << 4:\n");
C = A >> 4;
printf("A>4: %X %d\n", C, C);//10 = "0000 1010"
C = C << 4;
printf("A<4: %X %d\n", C, C); //160 = "1010 0000"
printf ("(A >> 4) << 4 = %X %d\n", C, C);
return 0;
}

Study day part 1: Copycat

So, what's a copy constructor? I donno. I mean I do, but I forgot. To Wikipediaaaa!!
A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or non-const), which might be followed by parameters of any type (all having default values).
 Examples of these are:
cclass(const cclass& copy) 
{
     this->num = copy.num;
}
The following cases may result in a call to a copy constructor:
1. When an object is returned by value
cclass CC = getClass(num);
2. When an object is passed (to a function) by value as an argument
foo(CC);
3. When an object is thrown
throw CC;
4. When an object is caught
catch (CC)

And, to finish off, I need a full example.
    ////*class*////
Point::Point(const Point& p) {
   x = p.age;
   y = p.num;
}
   ////*Main*////
Point p;       // calls default constructor
Point s = p; // calls copy constructor.
p = s;          // assignment, not copy constructor, need to overload the "=" operator

Study day part 0: Da List!

Ok, so, test is tomorrow. And I know I should sit all night and study, instead of getting a good sleep. So! I'm going to study all day, and prepare my notes.
Cocke, dubstep and notes. Let's do this. I will start with the list of things I need to repeat and/or include in my notes. All next post will be related to each one of these (I hope). Here is the list:
1. Copy constructors. Why do we need them and where are we using them?
2. Template linklists. I NEED a good example of that.
3. Binary. Because they will be on the test.
4. Binary Files. Same here.
5. Multiple inheritance. OF COURSE! (c)
6. Exceptions. Won't hurt.
7. Command line arguments. I know them, but just in case.
Also, this list will be updated with link to related posts, just in case.

понедельник, 20 февраля 2012 г.

Console Resolved

YAAAYYY!! Console is fixed! What went wrong? I donno. Apparently, putty doesn't like cout when I use it in my display function, or something.
Before it was:

     std::cout<<tstr;
 I made it like this:
   console<<tstr;
Which is basically a "putchar" statement. I guess it has something to do with the internal putchar and cout differences, but might be wrong, as far as I know it works, and that's good!
I submitted 0.1, have about a week to make 0.2 are we're gonna split the functions between out group in a few days, so I might as well finish with the wiki and have a few hours of rest, on this last day of the long weekend!

Console

So, it's bern a while, since I bloged about something... (Note to self: Don't forget to upload thing on wiki)
Ok, new year, old (sort of) assignment. Everything should go fine... Not. The fist problem I encountered was right at the beginning.
The assignment starts with writing a "console.cpp", basically, a display and edit function for a string of text, noting major... And I did. And it worked... On Visual Studio. I'm not sure why, but in unix environment my function isn't showing the string and just doing all sots of illogical things with the string. Apparently it has something to do with memory handling in the unix.
So, teacher didn't answered my email about an appointment (I should talk with him about the   procedure of "successfully sending an email") I have my goldfish crackers, some water and a few hours of work!

I guess I should start with taking a regular console tester and checking if it works, next, I'm going to work on display function, until I at least will be able to see the sting... Yeah, that should do it.

воскресенье, 15 января 2012 г.

Happpy new OOP year!

   So... I failed C++ last semester. Why? Well, first of all I was a bit too lazy, and unlucky - all the staff I was preparing for some reason got completely useless on the exam and second test. So yeah, I barely made it through test one, failed test two, I guess I should've expected something like this on the exam.
This semester I'm planning to fix stuff I failed last time, let's see...
- Learn Templates
- Repeat Link lists
- Learn how they can work together
- Get a book (I'll probably work on it this week)
- Repeat files and binary
This should cower... most of the course, I will also try to make appointments with the teacher so I can insure that I know everything... Most of it.
   Ok, this looks right. I hope things will work out for me this time, I'm not sure about the team, but it will be alright. I guess I should go to sleep... Classes at 8 am is a fair "punishment" for my laziness.
Good luck for me and everyone who's reading!

суббота, 17 декабря 2011 г.

Files C++

Ok, before the exam, I started repeating files, and now I want to post some of mine notes.

Basic steps to use files:
1. Declare an ofstream/ ifstream/ftream var.
2. Open a file with it.
3. Do stuff to the file (there are couple of ways.)
4. Close it.

ifstream              - open the file for input (reading)
ofstream             - open the file for output (writing)
fstream               - open the file for input/output/both


file. open ("test.txt", ios::in | ios::out | ios::app)
I got a bit confused here, why do I need open AND constructor? Wouldn't it be a lot easier to have just open, or just a constructor... Any way, I'll research it a bit later

flag value           means
ios::in                   open file for reading
ios::out                 open file for writing
ios::app                open for writing,add to end of file(append).
ios::binary             binary file
ios::nocreate         do not create the file,open only if it exists
ios::noreplace       open and create a new file if the specified file does not exist
ios::trunc              open a file and empty it.(Boom, all the data is gone,if any)
ios::ate                 goes to end of file instead of the beginning

Returns current position, if in the end, return length of file:
file.tellg ( );                       
 
Move courser:
seekg/seekp (pos, ios)               
pos – number of bites to move
ios – move from, beg, end, cur

value      means...
ios::beg    beginning of the stream buffer
ios::cur     current position in the stream buffer
ios::end    end of the stream buffer


file.read (buffer,size);
buffer – where to put text
size – how much, counting from seekg

size = sizeof("LOOOOL");
file.write ("LOOOOL",size);
This puts text inside a "file" document.I still need some time to figure out the way it adds, it should be based on seekp position, but I'll figure it out, I just need to make a few examples, I'm planning on making some kind of "holiday calendar" using a text file tomorrow.

eof()                     returns true if the end of the file has been reached

Other functions for reading:
1.seekg();           //move the read pointer in bytes
2.tellg();              //returns where the read pointer is in bytes

Other functions for writing:
1.seekp();           //move the write pointer in bytes
2.tellp();              //returns where the write pointer is in bytes

вторник, 18 октября 2011 г.

CLabel

Ok, it's been a while! Let's program something again! So, what do we have today? A kind of Idea of what is supposed to be in the end AND that I'm going write something for it... And eventually it will work... Or not... I hope it will.
But what I need to do today is write a bunch of small functions, constructors, and I just finished the destructor. It's funny and interesting how thing work - lot's of people come together and each and every one of them write a specific function, and possibly some of them don't even know what the purpose of their part is, but in the end, if everything is one by the plan - Program Works! It's like a small, micro, model of society... But, I got off the track a bit. What I'm planning to do today:
1. Write/Double-check the most basic functions (1-2 lines)
2. Update my knowledge on difference between "." and "->"
3. Walk though rest of the functions and see if I have any questions and, possibly, write them down, or remember.
So, let's start...


I just Love OOP - "editable() - always return false"
bool CLabel::editable()const {
return false;
The PERFECT function. I guess it's gonna be used in the future but seriously, why not just "return false;". And the best part is - there WILL be a place to use this function.

Ok, so, questions we have for today are:

Description: "CLabel(const char *Str, int Row, int Col, int Len = 0) - passes the Row and Col to the CField constructor and then.... etc"
Line: CField a (trow, tcol);
Problem: I got an Error "object of abstract class type "CFrame" is not allowed", so, how can I pass values to the constructor if it's object's creation is not allowed?


Description: "draw(int fn) - makes a direct call to console.display(), passing _data for the string to be printed and... etc"
Line: temp.display(_data, CFrame::absRow(), CFrame::absCol(), _length);
Problem: Don't know how to target an information where _data points, also, "a direct call" has a bit of a problem, since I can't use Console::draw, and I'm pretty sure, that Creating a temp class for the small task is a bit... Stupid wrong.


Description: "edit() - calls draw"
Line: draw();
Problem: Not sure if I should use "this->draw()", or just "draw()", I guess once tester is out I can check, but the problem still exists.



And that's it so far.

воскресенье, 25 сентября 2011 г.

Edit Function 3 - Finale

*14:00*
I will try to finish it today. I need to make and test those changes:
- Offset setting. 90% (Double check and testing)
- Cursor setting 90%  (Double check and testing)
- Cursor moving 90% (Double check and testing)
- Too many characters 40% (Writing and testing)
This shouldn't take too much time, but I'm pretty sure I'll miss something important and it won't be finished completely, but I tried at least. I also found that bag from backspace case I was talking about last time, so i need to fix it as well.

*15:00*
Everything so working.. except the INSERT part. I need to code it based to the conditions I have... That might take a while...

*16:40*
Aaaannnnddd.... Yes. I think it's done. I mean, I'm pretty sure, there are at least A Lot of thing I didn't test for, and somehow missed while reading the conditions for a few times, but it looks good, I didn't find any bugs and it at least meats Most of the conditions. So yeah... fingers crossed.

*16:47*
Nope! Wait. One last this.
- The user terminates editing cases. 0%
So, I'm going to Save everything into temp variables and then put everything back.

*17:15*
Ok, if the way I do it is the right way, I'm going die of laugh. On one hand it's logical - make separate cases for separate keys, but having same 4 lines in each and every case makes me giggle. I guess it will change in the future but I still have my doubts.
Any way... Fingers crossed... Again.

суббота, 24 сентября 2011 г.

Edit Function 2

*13:40*
Ok, so, where was I last time?
1. Fix DEL case. - 99%
2. Write BACKSPACE case.  - 80% (Needs testing + 1 or 2 small fixes)
3. Fix LEFT case. - 90% (Needs testing)
4. Write RIGHT case. - 99%
5. Write HOME and END cases. - 5%
6.
Work on INSERT mode. - 0%
Yep... Nothing major. Let's start.

*13:55*
Ok, one down, few to go.
BACKSPACE case. - 99%
Ok, so now... I had a problem with the display function... Or my Left function, but I think there is a problem with option "if fieldLen is less then a sting length". But this needs further investigation.

*14:00*
Boom! I'm Smoking today! Probably doing it in the least efficient way possible... But I don't know the other ways, OR not in position to rebuild it (Maybe after I'm done with the Edit function). So yeah... 
Fix LEFT case. - 99%
Ok, now the Home and End cases again... I think at least, even though I totally told the same last time.

*14:10*
Ok there is something wrong... I'll the other way - I'm gonna test every function one by one, removing all the others. The problem is that I'm not sure is the bugs I get are display problems, or Left Right problems... Also, as a last way out I will consider downloading the original code and staring from the beginning, even though I don't wont to do that at all.
 So, even though my print shows that Offset is 0, I stand on "F" and can't move further (what a brilliant metaphor...). I can only assume that this is a Display bug. I'll see what I can do with that...

*14:20*
A few tests later I decided that I will try to recode the display function. This will take some time, but if it will work, it might save me...

*15:00*
I rewrote the display function. And so far everything works fine I hope that it Stays that way. It looks like I'm back on the right track. It might be a mistake, somehow, but I did some progress:
DEL case. - 99%
BACKSPACE case.  - 99% 
LEFT case. - 99% (Needs testing)
RIGHT case. - 99%
HOME and END cases. - 99%
So now the tricky part - Writing case. I need to re-read the info again, and I think I have a good chance of finishing this part and the entire task today.  


*15:45*
YES! I'm Done! Everything Works! There is only one problem - ONCE, Somehow, I managed to bug out the BACKSPACE case (it went deleting all the way into the null), but I couldn't repeat this so I guess it will do. There is also a thing about the string itself- if you write something to re-write this "0" (teacher put it to cover second part of the string) the string shows fully, and I'm not sure if that's the way it's supposed to be.
But hey! I'm done! Yaaaayyyyy!!!

*15:50*
Oh... I forgot that there are additional conditions - offset, length something else... Ehh... And that's what I hate about this profession. But anyway, that might take some time, but I'll be done today.

*16:40*
Ehh... I just spend A Lot of time doing the most basic stuff...  I'll finish it tomorrow, or today at night, I just can't do anything for some time. So, so far, I'm done.

четверг, 22 сентября 2011 г.

Edit Function 1

Ok, so, the Edit function, I have enough information to complete it... Unless I get into some kind of small problem as I did in the display function.
Thing I need to do, possibly today:
1. Fix DEL case.
2. Write BACKSPACE case. 
3. Fix LEFT case.
4. Write RIGHT case.
5. Write HOME and END cases.
6. Work on INSERT mode.

*20:10*
Ok, I just finished fixing DEL case. That was easy - basically one line of code. It's not that hard, once you realize that you need to put a NULL in the right place. Then, the will appear a problem… Ok, not a problem, but a bag – if you push Delete continuously, eventually you will pass the cursor and keep deleting, even though I need it to Stop once it reaches the cursor. This bag was solved in about half an hour, I just needed to experiment a little.
Also, a Good line of code for testing, just put it in Left/Right cases:


display("Cursor"/"Set   ", 20, 0);       
//Displays the way you are moving (cursor or offset)
std::cout<<"                       "<<*curPosition<<"  "<<fieldLength; 
//Displays values you need for testing, can only be use with values that are passed to the Edit function and values in this function


I know there is a better debuggers out there, especially considering the fact that I use Visual Studio, but I’m not doing something too large or difficult to compile, so I’m using those lines, just because I feel more comfortable with them. 
Now I'll start working on a BACKSPACE case...  

*21:20*
Heh, documenting everything takes A Lot of Time.
 So, where was I? Oh yes, BACKSPACE. This is not that hard, the basic thing you need to understand while writing it – backspace is LEFT and DEL cases combined.

*21:40*
Well that was easy… Left and Right commands can be tricky, but it took me about half an hour to make them work the way they are supposed to. Now I just going to make the easiest (I think) ones – HOME and END. Let’s see what I can do.

*21:50*
That wasn’t that easy, but I also found out about a few more bags in the Left function. That might take some time, but that’s why I love this subject so much…

*22:22*
It took a while, but I got rid of one more bag – I tried to make it move a bit further if you hit left and if cursor  position is 0, and apparently without proper If statements it just went TOO far into space… I mean… String.
Ok, I need to wake up tomorrow around 8 am, I guess I’m done for today.

Solved Design

So, it took me a few hours, but I found out what the problem was. And obviously it wasn't compiler's fault.
Basically the error was:
 error LNK2019: unresolved external symbol "public: void __thiscall cio::Console::display(char const *,int,int,int)" (?display@Console@cio@@QAEXPBDHHH@Z)
And the code I wrote was:
void display (const char* str, int row, int col, int fieldLen) 
  After a few hours of looking at the screen I realized what a stupid mistake I made. The right line of the code was supposed to be:
  void Console::display (const char* str, int row, int col, int fieldLen) 
 Now this seems obvious but I spend A Lot of time on this bag, just looking in the wrong direction.
The rest wasn't that hard. I had tree situations:
1.  If fieldLen is 0 or less.
 Just one cout and everything works.

2.  If fieldLen more then sting length.
Make another string, and fill it with spaces and print after the str sting. (I'm also thinking about doing another version of it without creating another string of spaces)
3.  If fieldLen less then sting length.
Make another string, and copy str to it. And the Cut it with Null value, and print it.
I might also take some time and think about other ways  of writing it.

суббота, 17 сентября 2011 г.

Assignment Start

 *14:10*
Ok, so it's Sunday, weather is no the best I've seen, but I don't care since it pretty warm inside and I can start working in the first assignment in this Semester. Coke - here, snacks - here, music - playing. Goals for today:
- Download Console Files
- Double check the requirements
- Write the Display Function
- Check the Display Function
- Submit
- Do the other homeworks/labs so I can rest tomorrow
So, let's start...

*14:55*
Ok, so... I have some problems with the Console code, for some reason it won't create the Console object. I guess it's the problem of the Matrix, so I should download Visual Studio and try there.  The problem is - I've never used Visual Studio... This might take a while...
Or I could also Google and figure out how compile with cmd on Windows... That also might take a while...

*17:00*
Ok, the good new are - I actually got a working Visual Studio... The Problem is... The Error I have still isn't fixed... I need to think about it...

*17:10*
Ok, so I spend around... 3 hours doing almost nothing, there are still no Updates on the Wiki web-site, and the SVN version of the console file tells "Still Incomplete"... I guess I'll give it a rest, write a letter to the professor (I HOPE it will get thought). So yeah. Now I'm gonna check other Blogs and  sites for info, and probably will do other subject's home work... Too bad...

четверг, 8 сентября 2011 г.

First Post

  So, this is my first... "post", I guess. I'm not sure what I'm going to do here, I've never used blogs before but it's not that hard to figure out. Sooo... Yeah. I little test.
My name is Dzmitry Kavalchyk, I am from Belarus,  and I'm a student of Seneca Collage in Toronto. I study Computer Programming, this a very interesting and challenging course. If you're reading this, that means that you are my teacher... or one of my classmates... or you're far in the future where I'm a very famous and rich person and people want to find out how it all started. (I had to re-write this part Twice - "Undo" function doesn't work well, but nothing major)
Any way, welcome, and thank you for spending your time on reading this.
Also, random code: 
#include "main.h"
#include "getPosInt.h"


int main() {
int i;

i = getPosInt(MAX);
cout << "You entered " << i << endl;

I think that went well...