public
anonymous / compiler.cpp
Created

  • Download Gist
compiler.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// Filename: compiler.cpp
 
 
#include <iostream>
#include <cstdlib>
#include <fstream>
#include "compiler.h"
 
using namespace std;
 
 
Compiler::Compiler()
{
current = NULL;
//manynodes = 0;
}
 
 
bool Compiler::search_list(string item)
{
nodeType *p, *first;
p=first; //
do {
if(first->word==item)
{
return true;
}
p=p->link;
}
while (first->link!=NULL);
return false;
}
 
void Compiler::head_insert()
{
}
 
void Compiler::add_to_count()
{
}
 
ostream& operator<< (ostream & outfile, const Compiler & mylist)
{
nodeType* head_ptr = mylist.current;
while (head_ptr != NULL) //outputs the info of each node in list
{
outfile << head_ptr->word<<" ";
head_ptr = head_ptr->link;
}
outfile << endl; //creates nwln for each list
return outfile;
}
compiler.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
// Filename: compiler.h
 
 
#include <iostream>
#include <fstream>
#include <cstdlib>
 
using namespace std;
 
struct nodeType
{
string word;
int count;
nodeType *link;
};
 
class Compiler
{
public:
Compiler();
bool search_list(string item); // Tells the client whether a spcific value is
//in the list.
// Calls a function search
void head_insert(); // Should be a member function
void add_to_count(); // Accesses the "current" pointer (in the private section)
// to update the count field of the node
// if the search is successful
friend ostream & operator<< (ostream &outfile, const Compiler & mylist);
// Overloaded operator to print both the data and the count field for each node
//int size()const; // yields manynodes
//void bubblesort();
protected:
nodeType *search1_list; // Called by the public function search_list
// sets the private member current to the node to be
// updated, or is NULL if the value is not there
//int manynodes;
 
private:
nodeType *current; // Add to the bag class.
// This pointer will be used in the search and again
// used to update the node if the data is already present
};
 
//void list_clear(nodeType*& head_ptr);
//void list_copy(const nodeType*source_ptr, nodeType*& head_ptr, nodeType*& tail_ptr);
input.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
educe
line
educe
primrose
depart
line
thwart
line
grand
cellular
line
educe
depart
manage
depart
headline
empire
torch
headline
line
myth
deposit
balance
cellular
educe
distance
main.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
 
 
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <string>
#include "compiler.h"
 
 
using namespace std;
 
nodeType* CreateList(string);
void outputlist(nodeType* , string);
 
 
 
int main ()
{
ifstream infile;
ofstream outfile;
string inputfile, outputfile;
nodeType* head;
Compiler mylist;
/*infile.open ("input.txt");
if(!infile)
{
cout<<"Failure to open unsorted.txt."<<endl;
system("pause");
return 1;
}
outfile.open ("output.txt");
if(!outfile)
{
cout<<"Failure to open sorted.txt."<<endl;
system("pause");
return 1;
}*/
//getline(infile, mylist);
cout<<"Input file name:"<<endl;
getline(cin,inputfile);
head = CreateList(inputfile);
cout<<"Enter name of output file: ";
getline(cin, outputfile);
outputlist(head,outputfile);
//outfile << mylist;
//outfile <<endl<<endl;
/*mylist.search_list();
 
while (!infile.eof())
{
cout<<mylist<<endl;
infile>>mylist;
}*/
//out4<<hashtable[i]; // out4 is an ofstream object
infile.close();
outfile.close();
return 0;
}
nodeType* CreateList(string filename)
{
ifstream infile;
infile.open(filename.c_str());
nodeType* head = NULL;
nodeType* compiled;
compiled = new nodeType;
while(infile && compiled != NULL)
{
getline(infile, compiled -> word);
compiled->link = head;
head = compiled;
compiled = new nodeType;
}
delete compiled;
infile.close();
return head;
}
 
void outputlist(nodeType* head, string outputfile)
{
ofstream outfile;
outfile.open(outputfile.c_str());
nodeType* compiled;
compiled = head;
while(outfile && compiled !=NULL)
{
outfile << compiled -> word;
outfile << endl;
compiled = compiled -> link;
}
outfile.close();
}
 
 
/*void load()
{
ifstream infile("input.txt");
struct Compiler *temp, *head, *link;
if(infile.is_open())
{
string line, name;
while (infile.good()) {
{
temp = (Compiler*)malloc(sizeof(Compiler));
temp->link = head;
head = temp;
infile.getline(temp->name,1024);
//infile>>temp->compiling;
//getline(infile,line);
//infile.getline(temp->
}
}
infile.close();
}
else cout<<"Unable to open file.";
}*/
output.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 
distance
educe
cellular
balance
deposit
myth
line
headline
torch
empire
headline
depart
manage
depart
educe
line
cellular
grand
line
thwart
line
depart
primrose
educe
line
educe

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.