Thursday 28 November 2013

Bit pattern manipulation and bit operations

Oh yay, finally adding a new post...

Well I thought I might as well add some of the c++ programs and codes I made while practicing bit patterns,
see my GitHub repository i.e. PandoraBox, don't worry the only thing one can find in here is Elpis, which I think some of you may find useful for OOP344, and may he guide you well ( I mean in this in a good and kindly way dear friends!).

https://github.com/ragu-S/PandoraBox/tree/master/BitPatternManipulation

P.S. read up on your Greek mythology if you do not know what I am referring too!

Thursday 10 October 2013

Stacked Link Lists

Here is my first attempt at working with simple, stacked link lists
//Copy Constructor
Stack::Stack(const Stack & S)
{
    _top = (Node *) 0;
    Node *newTemp;
    newTemp = S._top;
    Node *nex;
    nex = S._top;
    int count = 0;
    int newCount = 0;
    while (newTemp) {             //Count number of nodes
newTemp = newTemp->_next;
count++;
    }
    while (count) {
while (newCount < count - 1) {     //Move from first node to target node
   nex = nex->_next;
   newCount++;             //Nodes up to this point
}
count--;                             //reduce nodes counted
if (nex->_data) {
   push(nex->_data);             //Copy the respective node's content over to new Stack
}
nex = S._top;                     //Reset to first node
newCount = 0;                     //reset Nodes up to this point counter to zero
    }
}

//Method to reverse contents of a simple stack node
 void Stack::reverse()
{
    Node *nodeTemp;                            
    Node *newNode;
    int newCount = 0;
    if (this->_top)
newNode = this->_top;
    int count = 0;
    if (this->_top)
nodeTemp = this->_top;
    while (nodeTemp) {                             //Count number of nodes
nodeTemp = nodeTemp->_next;
count++;
    }
    while (count--) {
nodeTemp = this->_top;                              //reset to first Node
int temp = newNode->_data;                      //backup first node value
while (newCount++ < count) {
   nodeTemp = nodeTemp->_next;
}
if (newNode->_data == nodeTemp->_data)  //If nodes are equal at this point, means values have                                                                                      // been shifted fully
   count = 0;
else {
   newNode->_data = nodeTemp->_data; //copy over last node value
   nodeTemp->_data = temp;                         //copy over first node value
   newNode = newNode->_next;                 //Move to next node
}
newCount = 0;
    }
}


//Method to determine the number of nodes
unsigned int Stack::depth()
{
    int dept = 0;
    Node *new_top = this->_top;
    while (new_top) {
if (new_top->_data)
   new_top = new_top->_next;      //Jump to next node
dept++;                      //Increase counter value
    }
    return dept;
}

Sunday 22 September 2013

Modification to Zhen's Basic Math program

//Link to the original code see http://zenoop344.blogspot.ca/2013/09/include-include-include-int-mainint.html

#include <cstring>
#include <cstdlib>
#include <stdio.h>

int main(int argc, char** argv){
   if(!(argc==4)){      //check number of parameters
      printf("bm <number> <+-x/> <number><ENTER>\n");
      return 0;
   }
   //check if operator is valid 
   if((strcmp(argv[2], "+")!=0) && (strcmp(argv[2], "-")!=0) && (strcmp(argv[2], "/")!=0) && (strcmp(argv[2],         "x")!=0)){
      printf("bm <number> <+-x/> <number><ENTER>\n");
      return 0;
   }
   
   double left;
   double right;
   int i;
   int j;
   int dcounter;

   for(i=1; i<4; i+=2){
      dcounter=0;
//Added this code so as to accept parentheses      
//it just removes the parentheses before entering Zhen's checks,
// rather than output error message because of parentheses     
 if((argv[i][0] == 40) && (argv[i][strlen(argv[i])-1] == 41)){
      for(j=0; j<strlen(argv[i]); j++){            
          argv[i][j] = argv[i][j+1];     
      }    
      if(argv[i][(strlen(argv[i]))-1] == 41)
          argv[i][(strlen(argv[i]))-1] = '\0';
 }
      //check 1st element for invalid input, '-' is ok in the beginning for negative numbers
      if(((argv[i][0] < '0') || (argv[i][0] > '9')) && (argv[i][0] != '-')){
         printf("bm <number> <+-x/> <number><ENTER>\n");
         return 0;
      }
      (argv[i][0] == '.') && (dcounter+=1);         //check 1st element for decimal
      for(j=1; j<strlen(argv[i]); j++){                //check the rest of the array for invalid input
         (argv[i][j] == '.') && (dcounter+=1);
         if(dcounter>1){                                        //if more than 1 decimal
            printf("bm <number> <+-x/> <number><ENTER>\n");
            return 0;
         }
         if((argv[i][j] < '0' || argv[i][j] > '9') && argv[i][j] != '.'){ 
            printf("bm <number> <+-x/> <number><ENTER>\n");
            return 0;
         }
      }
   }
   left=atof(argv[1]);
   right=atof(argv[3]);
   (argv[2][0]=='+') && (printf("%g\n", left+right));
   (argv[2][0]=='-') && (printf("%g\n", left-right));
   (argv[2][0]=='x') && (printf("%g\n", left*right));
   (argv[2][0]=='/') && (printf("%g\n", left/right));
   return 0;
}