Archive | Uncategorized RSS feed for this section

crunch time

10 Apr

I missed a class last week plus I’ve been doing some catching up the last few days in preparation for the test/exam. I just did a template-stack, and i think it’s pretty easy to follow so I’m just going to post it quickly because I did it pretty quick so I’m proud of myself ūüôā …even though it looks like a carbon copy of the ones from the notes

#include <iostream>
#include <cstdlib>
using namespace std;
template <class t>

class stack;

template <class t>

class node{
    t data;
    node<t>* next;
    node(t data, node<t>* next=(node<t>*)0);
    friend class stack<t>;
};

template <class t>
class stack{
    node<t>* top;
    public:
    stack();
    virtual ~stack();
    void push(t data);
    t pop();
    bool isEmpty();
};

template <class t>

node<t>::node(t data, node<t>* next){
    this->data=data;
    this->next=next;    
}    

template <class t>

stack<t>::stack(){
    this->top=0;
}
template <class t>
void stack<t>::push(t data){

        node<t>*temp=new node<t>(data,top);
        top=temp;    
}
template <class t>
t stack<t>::pop(){
node<t>* temp=top;
t ret=top->data;
top=top->next;
delete temp;
return ret;    
}
template <class t>
bool stack<t>::isEmpty(){
    if (top)return false;
    return true;
}

template<class t>
stack<t>::~stack(){
    while (!isEmpty()){
    cout<<top->data<<endl;
    pop();
    }
    
}

 

#include “nodetemp.h”
using namespace std;
int main(){
    stack<int> x;
    
    for (int i=0;i<100;i++){
        x.push(i);
    }
}

 

 

 

More linked list stuff and bitwise operations

24 Mar

#include <cstdio>
using namespace std;

void printBits(unsigned int val){
  unsigned int m = 1 << sizeof(val) * 8 Р1;
  while(m){
¬†¬†¬† printf(“%d”, !!(val&m));
    m = m >> 1;
  }
¬† printf(“\n”);
}
// challenge:
void setBit(unsigned int& V, int bitNo, bool val){
int g=1<<(bitNo-1);
val==true ? V|=g :V&=~g;
}
//The following is courtesy of Nick Kemp, because I couldn’t get it in a timely matter and he already had it done
void copyBits(unsigned int& num, int bitFrom, int length, unsigned int mask){
  unsigned int pos = bitFrom Рlength + 1;//sets the position the the rightmost bit that needs to be copied
  //101010000101100100100100010010100
  //                 |______________|
  //                 ^                           ^
¬† //¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† |<‚ÄĒ‚ÄĒlength=16‚ÄĒ>|
  //       bitFrom=25     startPos=10
  unsigned int mask2 = 1 << (pos Р1); //sets mask2’s bit @ pos to true
  for(pos;pos<=(unsigned)bitFrom;pos++){
    setBit(num, pos, !!(mask2&mask));
    mask2= mask2<<1;
  }
}

//

 

basic doubly linked list…I did this like 2 weeks ago and forgot to blog it so forgive me for not quite remembering too much about it all I know is it passes all Fardad’s tests, and it does not allow for insertion into an empty list…easily fixed but I’m already late getting it up

 

#include “dll.h”
Node::Node(int data, Node* prev, Node* next){
  _data = data;
  _prev = prev;
  _next = next;
}

DLL::DLL(){
  _head = _tail = _curr = 0;
}
DLL::~DLL(){
  while(del());
}
void DLL::copy(DLL& D){
  int curpos;
  for(curpos=0;D.goPrev();curpos++); // findout where is current
  if(!D.isEmpty()){
    do{
      this->append(D.visit());
    }while(D.goNext());
  }
¬† for(D.goHead(), this->goHead();curpos;D.goNext(), this->goNext(),curpos–);
}
DLL::DLL(DLL& D){
  _head = _tail = _curr = 0;
  copy(D);
}
DLL& DLL::operator=(DLL& D){
  while(del());
  copy(D);
  return *this;
}

void DLL::append(int data){
  Node* newnode = new Node(data);
  if(_tail){  // ! empty
    _tail->_next = newnode;
    newnode->_prev = _tail;
    _tail = _curr = newnode;
  }
  else{
    _tail = _curr = _head = newnode;
  }

}
int DLL::remove(){
  int data = visit();
  del();
  return data;
}
bool DLL::del(){
  bool ok = false;
  if(_curr){
    ok = true;
    Node* todel = _curr;
    if(_curr->_next){
      _curr->_next->_prev = _curr->_prev;
    }
    else{
      _tail = _tail->_prev;
    }
    if(_curr->_prev){
      _curr->_prev->_next = _curr->_next;
    }
    else{
      _head = _head->_next;
    }
    _curr = _curr->_next;
    delete todel;
  }
  return ok;
}
void DLL::insert(int data){
    //conditionals to check for head,tail,minimum required nodes etc.
Node* newNode=new Node(data);
newNode->_prev=_curr->_prev;
newNode->_next=_curr;
_curr->_prev->_next=newNode;
_curr->_prev=newNode;
//_curr=newNode;  debug statement
}
int DLL::visit(){               // retruns data of current
return _curr->_data;
}
bool DLL::goHead(){
if (isEmpty())return false;
    
while (_curr->_prev)_curr=_curr->_prev;
return true;
}
bool DLL::goTail(){
if (isEmpty())return false;
    
while (_curr->_next)_curr=_curr->_next;
return true;

}
bool DLL::goNext(){
if(_curr->_next){
_curr=_curr->_next;
return true;
}
else{

return false;
}
}
bool DLL::goPrev(){
if(_curr->_prev){
_curr=_curr->_prev;
return true;
}
else{

return false;
}
}
bool DLL::isEmpty(){
if(!_head)    
return true;
}

 

 

 

Image

Intro to linked lists

16 Mar

Got working on linked lists this week for the first time since my Java class in high school. Much easier to follow in C++ versus Java, having pointers and literal arrows ‘->’ makes the concept much easier to grasp, although challenging nonetheless. As Fardad has stated several times, when mapping these things out, having a multi colored pen is beneficial. I did’nt have one, but I had 2 markers, a black pen, a pencil, and an imagination. here is the diagram I used when programming my doubly linked list:

 

TV-fTFEi0ZvaqsmiB8uxJLrKeAvVww62I22n8F6xyHc

 

I intend to optimize it over the weekend and I will post my code, here is my queue single linked list add function : (big up to Nick Kemp for help with this one)

 

 

void Queue::add(int data){

Node*t=_head;
if(_head==0){

_head=new Node(data);
}
else{
while (t->_next){
t=t->_next;
}
t->_next=new Node(data);

}

}

 

 

Connecting to MySQL shell users in windows

9 Feb

So I’ve been using SQL server 2008 for all my database needs, and SQL lite for testing (because when using Visual studio or WebMatrix it does pretty much all the work for you). For INT322 we are using MySQL -presumably because it’s open source and very development friendly, and apparently absurdly popular. So in order to do my homework at home I need MySQL on my laptop…which is Windows and not linux. Not willing to switch to Linux over Windows for my main computer and thinking there shouldn’t be much of a difference any ways I went ahead and installed MySQL for windows. It loaded, I set it all up and thought great I’d get to my labs. It’s nice it has a shell type command line interface which I thought would make the fact that I do not have linux bearable. Turns out it’s not the case. So after fumbling for 20 or 30 minutes I finally figured out that in order to log on as a different user you have to go from the WINDOWS command prompt, access MySQL from there, THEN enter your commands, as the MySQL shell only applies to the current user (so root until you change it) so instead of simply typing mysql -u username -p ->enter password, you have to go to command line enter

cd C:\Program Files\MySQL\MySQL Server 5.6\bin\

then enter your MySQL command. Bit of a nuisance but hey it’s the price I pay for insisting on using windows. Having basic features of certain programs originally designed for unix-type-systems being hard to access is something I’ve become somewhat used to.

wow!

3 Feb

Well this has been a doozy of a project, done all initial tests moving on to the last 2 sections. code is far from optimized but at least if I don’t get any more work done it is ready for submission (worst case scenario) I’m happy I waited till yesterday to start…nothing gets me going like a deadline!

Working Away

2 Feb

Just been spending the entire day working on this edit() function…I found it incredibly useful to have the display function work as a debugging tool, so you can view all the indexes you are working with dynamically

e.g- itoa(*curPosition,t,10);
display(t, 20,17, 10);

just puts the current position in a random spot on the console but it has been of tremendous help, especially for more complex indexing.

 

10:43 pm: Done pretty much all the dirty work, will finish up indentation and the like Sunday…my God I love programming ūüôā

Console Display in one line

28 Jan

Okay here’s my attempt, seems to produce the same results as the class case? I’m pretty sure it qualifies as a one-liner apart from declarations.

void Console::display(const char* str, int row, int col, int fieldLen){
unsigned int i=0;
const char *t=str;
setPos(row, col);
while((int)i<(fieldLen>0?fieldLen:strlen(str)))i++ <strlen(str) ? putChar(*t++) : putChar(‘ ‘);

another way I suppose…

void Console::display(const char* str, int row, int col, int fieldLen){
unsigned int i=0;
const char *t=str;
setPos(row, col);
for(;(int)i<(fieldLen>0?fieldLen:strlen(str));i++) *t!=0 ? putChar(*t++) : putChar(‘ ‘);

}

 

 

there’s gotta be a way to do it without a counter variable…