Skip to content

Instantly share code, notes, and snippets.

@ldclakmal
Created January 24, 2015 16:02
Show Gist options
  • Save ldclakmal/3dfde3c9583fce032778 to your computer and use it in GitHub Desktop.
Save ldclakmal/3dfde3c9583fce032778 to your computer and use it in GitHub Desktop.
Dictionary
// Dictionary.cpp : Defines the entry point for the console application.
// L.D.C.Lakmal
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
template <class E> class Node {
private:
string value;
int count;
Node<E>* next;
public:
Node<E>(){};
string Node<E>::getValue();
void Node<E>::setValue(E val);
int Node<E>::getCount();
void Node<E>::setCount(int count);
Node<E>* Node<E>::getNext();
void Node<E>::setNext(Node<E>* newNode);
};
template<class E> string Node<E>::getValue(){
return this->value;
}
template<class E> void Node<E>::setValue(E val){
this->value = val;
}
template<class E> int Node<E>::getCount(){
return this->count;
}
template<class E> void Node<E>::setCount(int count){
this->count = count;
}
template<class E> Node<E>* Node<E>::getNext(){
return this->next;
}
template<class E> void Node<E>::setNext(Node<E>* newNode){
this->next = newNode;
}
//---------------------------------------------------------------------------------------------------
template <class E> class LinkedList {
private:
Node<E>* head;
int length;
public:
void LinkedList<E>::initList();
Node<E>* LinkedList<E>::search(E value);
bool LinkedList<E>::insert(E value);
int LinkedList<E>::listLength();
void LinkedList<E>::displayList();
};
template<class E> void LinkedList<E>::initList(){
head = NULL;
length = 0;
}
template<class E> Node<E>* LinkedList<E>::search(E value){
Node<E>* temp = head;
while(temp != NULL){
if(temp->Node<E>::getValue() == value){
//cout << "Node found !" << endl;
return temp;
}
temp = temp->Node<E>::getNext();
}
//cout << "Node not found !" << endl;
return NULL;
}
template<class E> bool LinkedList<E>::insert(E value){
Node<E>* newNode = new Node<E>();
newNode->Node<E>::setValue(value);
newNode->Node<E>::setCount(1);
newNode->Node<E>::setNext(NULL);
if(listLength() != 0){
newNode->Node<E>::setNext(head);
}
head = newNode;
length++;
return true;
}
template<class E> int LinkedList<E>::listLength(){
return this->length;
}
template<class E> void LinkedList<E>::displayList(){
int i = 0;
Node<E>* temp = head;
while(i != listLength()){
cout << temp->Node<E>::getValue() << " - " << temp->Node<E>::getCount() << endl;
temp = temp->Node<E>::getNext();
i++;
}
cout << endl;
}
//---------------------------------------------------------------------------------------------------
string replaceChar(string str, char ch1, char ch2) {
for (int i = 0; i < str.length(); ++i) {
if (str[i] == ch1)
str[i] = ch2;
}
return str;
}
string* read_input_array(char* fileName){
string line;
int count=0;
string data;
string* arr=NULL;
ifstream myFile = (ifstream)fileName;
if (myFile.is_open()){
while ( getline (myFile, line) ){
data += line + " ";
}
data = replaceChar(data, '.', ' ');
data = replaceChar(data, ',', ' ');
data = replaceChar(data, '\n', ' ');
arr = new string[2250];
count = 0;
istringstream ss(data);
string token;
while(getline(ss, token, ' ')) {
string s = token.c_str();
if(s == ""){
continue;
}
arr[count] = s;
count++;
}
myFile.close();
}else{
cout << "Unable to open file \n";
}
return arr;
}
void read_array(string* array){
for(int i=0; i < 2250; i++){
cout << array[i] << endl;
}
cout << "\n";
}
void main(int argc, char* argv[]){
char* myfile("input.txt");
string* arr = read_input_array(myfile);
//read_array(arr);
LinkedList<string> dict;
dict.initList();
for(int i=0; i < 2250; i++){
if(arr[i]==" "){
continue;
}
if(dict.search(arr[i])==NULL){
dict.insert(arr[i]);
}else{
dict.search(arr[i])->setCount(dict.search(arr[i])->getCount()+1);
}
}
dict.displayList();
cout << "----------------------------------------------------------------" << endl;
cout << "Length of input.txt = 2244" << endl;
cout << "Length of dictionary = " << dict.listLength() << endl;
cout << "----------------------------------------------------------------" << endl;
system("PAUSE");
}
//---------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment