Wednesday, April 23, 2008

C language: Process Control Block Queue

This is a basic program that consists of a Process Control Block (PCB), and a Queue (which is a vector). I won't display the whole program, but I will show the structure. First, the PCB class definition. I only need a header file for the PCB class, because the class only needs variables, not methods:


// PCB.h

#ifndef PCB_H
#define PCB_H

Class *PCB {
int ID;
string STATE;
};

#endif


Now, here is the ReadyQ class, that uses a vector. The ReadyQ class's methods manipulate the PCB. I will only list the class source file, and not bother displaying the header:

//*************************************************************************************
// Name: Queue.C
// By: Phillip McCubbin
// Description:
// This file contains the implementation of Queue class which is
// used to store the
data in the order of first in first out (FIFO)
//*************************************************************************************

#include
#include"PCB.h"
#include "ReadyQ.h"

using namespace std;

//*************************************************************************************
// Method: Queue::Queue
// Description:
// Constructor. Initializes the data members to 0 and allocates
// memory to storage.

// No arguments, no returns
//*************************************************************************************
ReadyQ::ReadyQ() {
*PCB = new int
}

//*************************************************************************************
// Method: Queue::~Queue
// Destructor
//*************************************************************************************
ReadyQ::~ReadyQ(){
m_data.clear();
}

//*************************************************************************************
// Method: Queue::isEmpty
// Description:
// Check if the queue is empty
// Returns:
// true - if the queue is empty
// false - otherwise
//*************************************************************************************
bool ReadyQ::isEmpty(){
return m_data.empty();
}

//*************************************************************************************
// Method: Queue::size
// Description:
// Returns the size of the queue
// Returns:
// m_total - size of the queue
//*************************************************************************************
int ReadyQ::size() {
return m_data.size();
}

//*************************************************************************************
// Method: Queue::enqueue_proc
// Description:
// This pushes the data onto the queue
// Arguments:
// value - data to be inserted
//*************************************************************************************
void ReadyQ::enqueue_proc(int value){
m_data.push_back(value);
}

//*************************************************************************************
// Method: Queue::dequeue_proc
// Description:
// Pop an element from the queue
//*************************************************************************************
void ReadyQ::dequeue_proc() {
if ( isEmpty() ){
cout << "Fatal Error - Queue is empty!!" << endl;
return;
}
m_data.erase(m_data.begin());
}

//*************************************************************************************
// Method: Queue::front
// Returns the first/top element of the queue.
// Returns:
// int - top element
//*************************************************************************************
int ReadyQ::front(){
return m_data[0];
}

//*************************************************************************************
// Method: Queue::displayAll
// Description:
// Displays the entire queue to stdout.
//*************************************************************************************
void ReadyQ::displayAll(){
for ( int i = 0; i < m_data.size(); i++ ) {
cout << m_data[i] << " ";
}
cout << endl;
}



So, there it is, now you simply need a main function that calls the ReadyQ methods, and you are good to go!

Tuesday, April 15, 2008

C++: Using pointers

This is a very basic use of C++ pointers, but it is really essential to know how pointers work.

Thursday, April 10, 2008

C++: Find 1st non-repeating character

Here is a function that I enjoyed solving. You have to write a function that takes a string input and finds the first non-repeating character in a string (eg. “ABCA” -> B). This code runs in about O(n). Enjoy!

/*FUNCTION*/ //*FIND 1ST NON-REPEATING****************************************
char find_nonrepeat_char(strLetters) {
//make ascii array to store letter count, then count
{
int countArray[25];
for (int i =0; i < 26; i++) countArray[i] = 0;
for (int i = 0; i < length(strLetters); i++) {
countArray[(int)strLetters[i]-65]++;
}
for (i = 0; i < length(strLetters); i++) {
if (countArray[(int)strLetters[i]-65)] == 1) return strLetters[i];
}
}

Saturday, April 5, 2008

CORRECTION php: Cleaning user input text

After reviewing my last post, I came to the conclusion that code should not be bunched together as I did. Sure, it may make the code look sharp and concise, but clarity and user understandability is our second priority (efficiency is our first). So, here is my revision of the code in my last post. I have made it a function, so you can simply call it each time you need to clean a string. Put it in a common_functions.php (or whatever you want to name it) file in directory named "include".

//*function:CLEAN STRING*******************************************************
function clean_string($text) {
$text =
ereg_replace("[,]"," ",$text); //replace , with space
$text = strtolower($text); //lower case
$text = eregi_replace("[^a-z0-9 ']", "", $text); //keep a-z0-9 space '
$text = preg_replace('/[\s\s+]/', ' ', $text); //multiple spaces -> 1
$text = trim($text); //trim
$text_arr = explode(' ',$text); //make into an array
return $text_arr;
}

Thursday, April 3, 2008

php: Cleaning user input text (some text manipulation)

There are many times you will want to ask a user for text input. One of the more common usages is in a search. And usually you will take the word and use it to make a query. But for now, it is important that the code be "cleaned" so that frivolous mistakes go unnoticed by the system (e.g. the user puts a space between words). Also, we are going to take words separated by spaces and make them their own words by putting them in an array.

This snippet does the following:
1) ereg_replace - replaces all the commas with spaces in the $text variable
2) strtolower - makes the whole string lowercase,
3) eregi_replace - replaces anything that isn't a number, letter, space, or apostrophe
4) preg_replace - replaces multiple spaces with 1 space
5) trim - trims the spaces from the sides
6) explode - "explodes" the string into an array delimited by spaces


//*MANIPULATE STRING****************************************************
$cleaned_text = explode(' ',trim(preg_replace('/[\s\s+]/', ' ', eregi_replace("[^a-z0-9 ']", "", strtolower(ereg_replace("[,]"," ",$text))))));