Month: February 2010

C++ code

String to Int function (C++)

This little piece of code can convert a string of decimal characters into an integer value.  If you seek to convert a string into a double, see this post. /* * A function to convert a string into an integer */ #include <string> int StringToInt (std::string str) { int total = 0; int length = str.length(); // the length of the string int x = 1; // this is our multiplier, used to convert each digit into tens, units, etc for (int i = 1; i < str.length(); i++) x *= 10; // initialise x correctly for (int i = […]

C++ code

A Pretend Radio Class (C++)

This C++ class is a good demonstration of Object Orientation in code. Download the  entire code: RadioClass.zip // definition file for class Radio #ifndef RADIO_H #define RADIO_H #include <iostream> using namespace std; const bool FM = true; const bool AM = false; const bool ON = true; const bool OFF = false; const int MIN_VOLUME = 0; const int MAX_VOLUME = 11; // it goes to eleven! class Radio { public: // constructors: Radio(); Radio(bool b,bool p,double fm,double am,int v); // accessors: bool getBand(); bool getPower(); double getFMStation(); double getAMStation(); int getVolume(); // mutators: bool setBand(bool b); bool setPower(bool p); […]

Visual Basic

A Calculator Program Made in Visual Basic

This simple calculator is a great example for beginners to examine to understand the basics of Visual Basic.  I must warn you that this isn’t perfect, but perfecting it would be an excellent exercise for a novice Visual Basic programmer. –           Download: VBcalc.zip – This download is a Microsoft Visual Studio Solution file, Visual studio is required to open it.  A free edition of the Visual studio is available from Microsoft at http://www.microsoft.com/express/Downloads/

C++ code

Doubly linked Stack Class (C++)

Definition file, stack.h #ifndef STACK_H #define STACK_H #include <string> using namespace std; typedef string ItemType; // Change the datatype of the stack here //————————————————————————- // Class: Stack // Class implements a doubly linked list of string items. //————————————————————————- class Stack { private: class Node; typedef Node* NodePtr; //————————————————————————- // Class: Node // A doubly linked list node. //————————————————————————- class Node { public: ItemType item; NodePtr previous; NodePtr next; Node(const ItemType& Item, const NodePtr Previous, const NodePtr Next) { item = Item; previous = Previous; next = Next; } }; NodePtr head; // Deletes all the nodes on the list. Head […]

C++ code

Greatest Common Divisor (Euclidean Algorithm C++)

This piece of code can determine the GCD of two numbers by applying the euclidean algorithm recursively. // euclid.cpp : main project file. #include <iostream> #include <string> #include <fstream> #include <iomanip> //required to use setw() using namespace std; void setOrder(int num1, int num2); int GCD(int num1, int num2); int main(void) { int num1 = 27; int num2 = 18; setOrder(num1,num2); cout << GCD(num1,num2)<<endl; system(“pause”); return 0; } void setOrder(int num1, int num2) { if(num1>num2) { int tempswap = num1; num1 = num2; num2 = tempswap; } } int GCD(int num1, int num2) { if(num1%num2 ==0) { return num2; } return (GCD(num2,(num1%num2))); […]

C++ code

Reverse an Integer Recursively (C++)

// numreverse.cpp : main project file. #include <iostream> #include <string> #include <fstream> #include <iomanip> //required to use setw() #include <cstdio> using namespace System; using namespace std; void NumReverse(int reverseme); int main(array<system ::String ^> ^args) { NumReverse(12345678); return 0; } void NumReverse(int reverseme) { //base case: 1 digit. if((reverseme/10) < 1) { cout << reverseme << “…is the mirrored number.” << endl; system(“pause”); system(“cls”); return ; } //recursive step: cout << (reverseme%10); NumReverse(reverseme/10); }

C++ code

A linked list Class (C++)

Class Definition: #include <iostream> using namespace std; class linklist { private: struct node { int data; node *link; }*p; public: linklist(); void append( int num ); void add_as_first( int num ); void addafter( int c, int num ); void del( int num ); void display(); int count(); ~linklist(); }; Implementation of the class: linklist::linklist() { p=NULL; } void linklist::append(int num) { node *q,*t; if( p == NULL ) { p = new node; p->data = num; p->link = NULL; } else { q = p; while( q->link != NULL ) q = q->link; t = new node; t->data = num; […]

C++ code

A C++ Menu

#include using namespace std; void do_again(),direction(int ans); void option_1(),option_2(),option_3(),option_4(),option_5(),option_6(); int main() { do_again();//call do_again function, say goodbye to main until everything else is done running return 0; }//end function void do_again() { int ans;//declare ans variable, this will be used OVER AND OVER do { cout < < "What would you like to do?n"; cout

C++ code

String to float function (C++)

/* * A function to convert a string into a float * Author: Danny Battison * Contact: gabehabe@hotmail.com */ #include float StringToFloat (std::string str) { float total = 0; // the total to return int length = str.length(); // the length of the string int prefixLength=0; // the length of the number BEFORE the decimal int suffixLength=0; // the length of the number AFTER the decimal bool decimalFound = false; // use this to decide whether to increment prefix or suffix for (unsigned int i = 0; i < str.length(); i++) { // loop through the string if (str[i] == […]

C++ code

String to Int Conversion (C++)

/* * A function to convert a string into an integer * Author: Danny Battison * Author: Kyle Turley http://www.kturley.com */ #include int StringToInt (std::string str) { int total = 0; int length = str.length(); // the length of the string int x = 1; // this is our multiplier, used to convert each digit into tens, units, etc for (int i = 1; i < str.length(); i++) x *= 10; // initialise x correctly for (int i = 0; i < length; i++) { // loop through the string // 48 is the base value (ASCII) // multiply it […]