brentech
11-19-2004, 08:40 PM
As promised to Shamus the other night, here is what I've been spending all of my last 3 days working on. My 'Unix, C, & the Internet' final.
It's a pretty big c++ program, it takes a textfile 'editable.txt' and retrieves data on it.
Specifically, the program will print:
1. Chart displaying the number of times each letter of the alphabet was found in the document.
2. Chart displaying the number of times a word was repeated.
3. Number of 1 letter, 2 letter, 3 letter...(and so on), words were used.
Basically, it was a big pain in the ass that really does no good for human kind. :meh: After having half the semester to do the program, but waiting to do it until the last 3 days before it was due...I believe my monitor has been burnt into my eyes. So here it is kids, feel free to steal it for your own!
To easier view the code: CLICK HERE! (http://www.btl-tech.net/temp/MyProgram.txt)
#include <iostream.h>
#include <assert.h>
#include <ctype.h>
#include <cctype>
#include <stdio.h>
#include <string>
#include <cstdio>
#include <cstdlib>
const char FILE_NAME[] = "editable.txt"; /* Actual file used */
std::FILE *input; /* Input file variable */
int alpha[26] = {0}; /* Global variable for RETURN purposes */
void disclaimer()
{
cout << "*****************************************" << endl;
cout << "** **" << endl;
cout << "** You'll want to resize your window **" << endl;
cout << "** so you have more viewing height. ;) **" << endl;
cout << "** **" << endl;
cout << "** This program will be reading from **" << endl;
cout << "** the file 'editable.txt'. **" << endl;
cout << "** Please ensure the file is in the **" << endl;
cout << "** same folder as the program. **" << endl;
cout << "** **" << endl;
cout << "*****************************************" << endl;
}
void pause()
{
cout << "Press ENTER to continue." << endl;
getchar (); /* wait for user to press ENTER */
}
void check_valid()
{
input = fopen(FILE_NAME, "rb"); /* Open routine */
if (input == NULL) /* Function checks file exists/no error/does not start with EOF */
{
cerr << "Can not open " << FILE_NAME << '\n'; /* creates a C error */
exit(8);
}
}
int letter_count(string word_array[], int x)
{
string temp;
int length;
char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
for(int i = 0; i < x; ++i) /* Loop will run once for each word */
{
temp = word_array[i];
length = temp.length();
for(int j = 0; j < length; ++j)
{
length = word_array[i].length();
for(int k = 0; k < 26; ++k)
{
if(temp[j] == alphabet[k])
++alpha[k];
}
}
}
return(alpha[26]); /* function returns our occurrence count */
}
void chart_alpha(int occur[26])
{
char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /* Assign one letter to each array element */
/* Print chart of */
cout << "------------------------------" << endl;
cout << " Characters | Occurrences" << endl;
for(int k = 0; k < 26; ++k)
{
cout << " " << alphabet[k] << " | " << occur[k] << endl;
}
cout << "------------------------------" << endl;
}
void word_occur(string word_array[], int x)
{
string temp;
int word_dupes[500] = {0};
string printed[500];
/* Count how many times a word occurs */
word_dupes[0] = 1;
for(int i = 0; i < x; ++i)
{
temp = word_array[i];
for(int j = 1; j < x; ++j)
{
if(temp == word_array[j])
{
++word_dupes[i];
}
}
}
/* Print chart of word occurrences */
cout << "------------------------------" << endl;
cout << " Characters | Occurrences" << endl;
cout << " " << word_array[0] << " | " << word_dupes[0] << endl;
int k = 1;
while(word_array[k] != "\0")
{
if(printed[k] != word_array[k])
cout << " " << word_array[k] << " | " << word_dupes[k] << endl;
++k;
}
cout << "------------------------------" << endl;
}
void length_counter(string word_array[], int x)
{
string temp;
int length;
int length_array[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,2 0};
int final[20] = {0};
for(int i = 0; i < x; ++i)
{
length = word_array[i].length(); /* Get length of word to test */
for(int j = 0; j < 20; ++j)
{
if(length == length_array[j])
++final[j];
}
}
/* Print chart of word-length occurrences */
cout << "------------------------------" << endl;
cout << " Words | Occurrences" << endl;
for(int k = 0; k < 20; ++k)
{
cout << " " << length_array[k] << "-letter word | " << final[k] << endl;
}
cout << "------------------------------" << endl;
}
int main()
{
disclaimer();
pause();
int ch; int i=0;
string new_word;
string words[500]; /* Current 500 word max */
check_valid(); /* function to check for file validity */
input = fopen(FILE_NAME, "rb"); /* Open routine */
while(true) {
/* Scan past any spaces or returns */
while(true) {
ch = getc(input);
if(isalpha(ch) || (ch == EOF))
break;
}
if(ch == EOF)
break;
new_word = ch;
while(true) {
ch = getc(input);
if(!isalpha(ch)) /* If current character is not a letter, discard */
break;
new_word += ch; /* Add the current character to the current word */
}
words[i] = new_word; /* Add our new word to the words array */
++i; /* The expression (i+1) now carries the number of words in the document */
}
fclose(input);
/* Make all letters lowercase for simplification */
string temper;
for(int j = 0; j < i; ++j)
{
int length;
temper = words[j];
length = temper.length();
for(int k = 0; k < length; ++k)
temper[k] = tolower(temper[k]);
words[j] = temper;
}
alpha[26] = letter_count(words,i); /* Return function to count the alpha characters */
chart_alpha(alpha); /* Create chart of alpha characters */
pause();
word_occur(words,i); /* Find and chart all words and their occurrences */
pause();
length_counter(words,i); /* Find and chart the word-length occurrences */
return(0);
}
It's a pretty big c++ program, it takes a textfile 'editable.txt' and retrieves data on it.
Specifically, the program will print:
1. Chart displaying the number of times each letter of the alphabet was found in the document.
2. Chart displaying the number of times a word was repeated.
3. Number of 1 letter, 2 letter, 3 letter...(and so on), words were used.
Basically, it was a big pain in the ass that really does no good for human kind. :meh: After having half the semester to do the program, but waiting to do it until the last 3 days before it was due...I believe my monitor has been burnt into my eyes. So here it is kids, feel free to steal it for your own!
To easier view the code: CLICK HERE! (http://www.btl-tech.net/temp/MyProgram.txt)
#include <iostream.h>
#include <assert.h>
#include <ctype.h>
#include <cctype>
#include <stdio.h>
#include <string>
#include <cstdio>
#include <cstdlib>
const char FILE_NAME[] = "editable.txt"; /* Actual file used */
std::FILE *input; /* Input file variable */
int alpha[26] = {0}; /* Global variable for RETURN purposes */
void disclaimer()
{
cout << "*****************************************" << endl;
cout << "** **" << endl;
cout << "** You'll want to resize your window **" << endl;
cout << "** so you have more viewing height. ;) **" << endl;
cout << "** **" << endl;
cout << "** This program will be reading from **" << endl;
cout << "** the file 'editable.txt'. **" << endl;
cout << "** Please ensure the file is in the **" << endl;
cout << "** same folder as the program. **" << endl;
cout << "** **" << endl;
cout << "*****************************************" << endl;
}
void pause()
{
cout << "Press ENTER to continue." << endl;
getchar (); /* wait for user to press ENTER */
}
void check_valid()
{
input = fopen(FILE_NAME, "rb"); /* Open routine */
if (input == NULL) /* Function checks file exists/no error/does not start with EOF */
{
cerr << "Can not open " << FILE_NAME << '\n'; /* creates a C error */
exit(8);
}
}
int letter_count(string word_array[], int x)
{
string temp;
int length;
char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
for(int i = 0; i < x; ++i) /* Loop will run once for each word */
{
temp = word_array[i];
length = temp.length();
for(int j = 0; j < length; ++j)
{
length = word_array[i].length();
for(int k = 0; k < 26; ++k)
{
if(temp[j] == alphabet[k])
++alpha[k];
}
}
}
return(alpha[26]); /* function returns our occurrence count */
}
void chart_alpha(int occur[26])
{
char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /* Assign one letter to each array element */
/* Print chart of */
cout << "------------------------------" << endl;
cout << " Characters | Occurrences" << endl;
for(int k = 0; k < 26; ++k)
{
cout << " " << alphabet[k] << " | " << occur[k] << endl;
}
cout << "------------------------------" << endl;
}
void word_occur(string word_array[], int x)
{
string temp;
int word_dupes[500] = {0};
string printed[500];
/* Count how many times a word occurs */
word_dupes[0] = 1;
for(int i = 0; i < x; ++i)
{
temp = word_array[i];
for(int j = 1; j < x; ++j)
{
if(temp == word_array[j])
{
++word_dupes[i];
}
}
}
/* Print chart of word occurrences */
cout << "------------------------------" << endl;
cout << " Characters | Occurrences" << endl;
cout << " " << word_array[0] << " | " << word_dupes[0] << endl;
int k = 1;
while(word_array[k] != "\0")
{
if(printed[k] != word_array[k])
cout << " " << word_array[k] << " | " << word_dupes[k] << endl;
++k;
}
cout << "------------------------------" << endl;
}
void length_counter(string word_array[], int x)
{
string temp;
int length;
int length_array[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,2 0};
int final[20] = {0};
for(int i = 0; i < x; ++i)
{
length = word_array[i].length(); /* Get length of word to test */
for(int j = 0; j < 20; ++j)
{
if(length == length_array[j])
++final[j];
}
}
/* Print chart of word-length occurrences */
cout << "------------------------------" << endl;
cout << " Words | Occurrences" << endl;
for(int k = 0; k < 20; ++k)
{
cout << " " << length_array[k] << "-letter word | " << final[k] << endl;
}
cout << "------------------------------" << endl;
}
int main()
{
disclaimer();
pause();
int ch; int i=0;
string new_word;
string words[500]; /* Current 500 word max */
check_valid(); /* function to check for file validity */
input = fopen(FILE_NAME, "rb"); /* Open routine */
while(true) {
/* Scan past any spaces or returns */
while(true) {
ch = getc(input);
if(isalpha(ch) || (ch == EOF))
break;
}
if(ch == EOF)
break;
new_word = ch;
while(true) {
ch = getc(input);
if(!isalpha(ch)) /* If current character is not a letter, discard */
break;
new_word += ch; /* Add the current character to the current word */
}
words[i] = new_word; /* Add our new word to the words array */
++i; /* The expression (i+1) now carries the number of words in the document */
}
fclose(input);
/* Make all letters lowercase for simplification */
string temper;
for(int j = 0; j < i; ++j)
{
int length;
temper = words[j];
length = temper.length();
for(int k = 0; k < length; ++k)
temper[k] = tolower(temper[k]);
words[j] = temper;
}
alpha[26] = letter_count(words,i); /* Return function to count the alpha characters */
chart_alpha(alpha); /* Create chart of alpha characters */
pause();
word_occur(words,i); /* Find and chart all words and their occurrences */
pause();
length_counter(words,i); /* Find and chart the word-length occurrences */
return(0);
}