C++ code

C++ code

Binary Search Tree implemented. C++

This is my personal implementation of a binary search tree. My tree uses iteration instead of recursion for its insertion, deletion and searches for faster execution because function stack overhead is avoided. Recursion is still used for tree deletion because I didn’t want to implement a stack for the traversal of the tree which is required for deletion. // SearchableADT test driver // Copyright (C) 2011 Kyle Turley // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, […]

C++ code Code

A realistic C++ line counter

This program asks the user for a c++ source file. It displays the file’s linecount to the screen. Program exits on invalid input. This sourcefile is 90 lines of code(according to itself). Each of the following counts as one line of code: Preprocessor directives if() while() do..while() switch() non-repeating semicolons #include <iostream> #include <fstream> #include <string> using namespace std; // accepts a valid C++ filename, returns a C++ linecount. Prints a error and exits program on invalid input. int loc(string filename); int main() { string filename; cout << "Enter a cpp filename to linecount " << endl; cin >> filename; […]

C++ code Code

Can’t use a string in a C++ switch statement? Here is a simple alternative.

Many programmers are surprised to find that switch statements aren’t compatible with strings in C/C++. When attempting to compile, you will see an error: Visual Studio 2010 —–  error C2450: switch expression of type ‘std::string’ is illegal Netbeans 6.9.1 —– error: switch quantity not an integer Here is an example of invalid code #include <iostream> #include <string> using namespace std; int main(){ string dayofweek = "wednesday"; switch(dayofweek){ case "monday": cout << "Watch football" << endl; break; case "tuesday": cout << "steal a car"<< endl; break; case "wednesday": cout << "rob a bank" << endl; break; case "thursday": cout << "count […]

C++ code Code

A Lexical Analyizer for C++ tokens

Lexical analysis is step 1 of compiling code down to machine language. The process breaks source code down into a long list of pieces called tokens. This list of tokens is used by a parser algorithm that extracts meaning from the order and arrangement of the tokens. Here is a small example of lex analysis: code: int main(void) { float myvar = 2.5; return 0; } list of tokens: int type main reserved word ( void keyword ) { float keyword myvar identifier = operator 2.5 floating point constant ; end statement return keyword 0 integer constant ; end statemant […]

C++ code

Doubly Linked List (C++)

This code is an implementation and test driver of a doubly linked list. This list is pointer based and uses dynamic memory. Sorted Doubly Linked List with Insertion and Deletion #include <iostream> #include <cstdlib> #include <string> using namespace std; class Dllist { private: typedef struct Node { string name; Node* next; Node* prev; }; Node* head; Node* last; public: Dllist() { head = NULL; last = NULL; } bool empty() const { return head==NULL; } friend ostream& operator<<(ostream& ,const Dllist& ); void Insert(const string& ); void Remove(const string& ); }; void Dllist::Insert(const string& s) { // Insertion into an Empty […]

C++ code

Open Text File the Right Way. (C++)

This code is a simple example of how to safely open a text file in C++. Notice the very important check that the file was opened correctly. This covers a range of errors,  most commonly file not found. // Read integers from file and print sum. #include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; int main() { int sum = 0; string word; ifstream inFile; inFile.open("test.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); // terminate with error } while (inFile >> word) { cout << word << " "; } inFile.close(); return 0; }

C++ code

Press any key to Continue…(C++)

this snippet of code is an alternative to system(“pause”) in MS Visual Studio. /* * File: SystemPause.cpp * Author: B Turley * * This function can be used as a substitute for ‘system("pause")’ * in Microsoft Visual Studio * * Created on November 10, 2009, 1:17 AM */ #include &lt;iostream&gt; #include &lt;limits&gt; using namespace std; void pause() { cout&lt;&lt;"Press any key to Continue…"; cin.sync(); cin.ignore(numeric_limits&lt;streamsize&gt; ::max(), ‘n’); } int main(int argc, char** argv) { pause(); return (0); }

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 &lt;string&gt; 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 &lt; 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); […]

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 […]