C++ program help!!! Write a program that reads a report ( running text ) and pro
ID: 3570501 • Letter: C
Question
C++ program help!!!
Write a program that reads a report ( running text ) and produces a statistical summary with the following items:
Total number of words in the report
Number of unique words
Average word length
Average sentence length
An index (alphabetical listing) of all the unique words (see next page for specific format)
Input (From a file)
The report to be analyzed
Output
Write the following information to a file:
The name of the file
The statistical summary of the report ( see above )
Data Structures
A list (BST) of unique words in the report, created as the file is read.
Definitions:
Word: Sequence of letters ending in a blank, a period, an exclamation point, a question mark, a colon, a comma, a single quote, or a semicolon. You may assume that numbers do not appear in the words; they may be ignored.
Unique word: Words that are spelled the same, ignoring uppercase and lowercase distinctions.
Sentence: Words between end of markers.
SAMPLE OUTPUT
Name of file: whatever.txt
Total number of words : 1,450.
Number of unique words: 1,105.
Average word length: 6 characters.
Average sentence length: 8 words.
INDEX:
A
and
all
around
at
Explanation / Answer
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
int compare(string str1, string str2){
int size = 0;
if (str1.size() > str2.size()){
size = str2.size();
}
else{
size = str1.size();
}
for (int i = 0; i < size; ++i){
if (tolower(str1[i]) < tolower(str2[i])){
return -1;
}
else if (tolower(str1[i]) > tolower(str2[i])){
return 1;
}
}
if (str1.size() < str2.size()){
return -1;
}
else if (str1.size() > str2.size()){
return 1;
}
else{
return 0;
}
return 0;
}
struct node{
string str;
node *left;
node *right;
};
class Bst{
private:
int count;
int total;
node *root;
char ch;
public:
Bst();
void add(string str);
void add(node *&temp, string str);
int size();
double avglength();
void print();
void print(node *temp);
};
Bst::Bst(){
count = 0;
total = 0;
ch = 'a';
root = NULL;
}
void Bst::add(node *&temp, string str){
if (temp == NULL){
node *newone = new node;
newone->str = str;
newone->left = NULL;
newone->right = NULL;
temp = newone;
total += str.size();
count++;
return;
}
else{
if (compare(temp->str, str) == -1){
add(temp->right, str);
}
else if (compare(temp->str, str) == 1){
add(temp->left, str);
}
else{
return;
}
}
}
void Bst::add(string str){
if (root == NULL){
node *newone = new node;
newone->str = str;
newone->left = NULL;
newone->right = NULL;
root = newone;
total += str.size();
count++;
}
else{
add(root, str);
}
}
int Bst::size(){
return count;
}
double Bst::avglength(){
return (double)total / count;
}
void Bst::print(node *temp){
if (temp == NULL){
return;
}
else{
print(temp->left);
if (tolower(temp->str[0]) == (int)ch){
cout << temp->str << endl;
}
else{
do{
ch = (char)(((int)ch) + 1);
} while (tolower(temp->str[0]) != (int)ch);
cout << "..." << endl << endl;
cout << (char)toupper(ch) << endl << endl;
cout << temp->str << endl;
}
print(temp->right);
}
}
void Bst::print(){
cout << "A" << endl;
print(root);
cout << "..." << endl;
}
int main(){
ifstream file;
int scount = 0, count = 0, total = 0;
Bst tree;
string line, fname;
cout << "Name of the file: ";
cin >> fname;
file.open(fname.c_str());
if (file.is_open()){
while (file >> line){
string temp = "";
for (int i = 0; i < line.size(); ++i){
if (((int)line[i] > 64 && (int)line[i] < 91) || ((int)line[i] > 96 && (int)line[i] < 123)){
temp = temp + line[i];
if (i == line.size() - 1){
if (temp != ""){
count++;
total += temp.size();
tree.add(temp);
temp = "";
}
}
}
else if (line[i] == '.'){
scount++;
if (temp != ""){
count++;
total += temp.size();
tree.add(temp);
temp = "";
}
}
else{
if (temp != ""){
tree.add(temp);
count++;
total += temp.size();
temp = "";
}
}
}
}
}
else{
cout << "can not open the file" << endl;
}
cout << "Total Number of words: " << count << endl;
cout << "Number of unique words: " << tree.size() << endl;
cout << "Average world length: " << total / (double)count << endl;
cout << "Average sentence length: " << count / (double)scount << endl;
cout << "INDEX: " << endl;
tree.print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.