In: Computer Science
I haves code on bottom.
what do i need to edit?
Create a subdirectory called proj1.
For this project you need to create at least two files: proj1.cpp, and makefile. Both files should be placed in the proj1 directory.
The file proj1.cpp should contain the main function, int main(). In the main() function, the program should read the input until it reaches the end, counting the number of times each word, number, and character is used. A word is defined as a sequence of letters ('a'..'z' or 'A'..'Z'). Words are case insensitive ("AA", "Aa", "aA", and "aa" are the same). A number is defined as a sequence of digits ('0'..'9'). Note that both words and numbers can be of length of 1, that is, contain one letter or one digit, respectively. Different sequences represent different numbers. For example, number "001" is different from number "1". Words are separated by numbers or other non-letter and non-digit characters. Numbers are separated by words or other non-letter and non-digit characters. Your program should record the number of times each word, number, and character happens (note that characters are case sensitive). The program should then output the ten most used characters (case sensitive), the ten most used numbers, and the ten most used words (case insensitive) as well as the number of times these characters/numbers/words are used. Since words are case insensitive, the program only outputs lower case words. The characters, numbers and words should be outputted in the descending order based on the number of times they are used. When two characters happen in the same number of times, the character with a smaller ASCII value should be considered as being used more frequently. When two words (numbers) happen in the same number of times, the word (number) that occurs earlier in the input should be considered as being used more frequently.
An example executable code of the program proj1.x is provided to you. In proj1.x, the output related to the display of characters, words, and numbers is aligned in the following manner: the width of the column of the characters, words, and numbers is the length of the longest words and numbers to be displayed, plus five (5). You should make the outputs of your program the same as those of 'proj1.x'. When printing characters, use '\t' for tab and '\n' for newline. All other characters should be outputted normally.
Write a makefile for your project that compiles an executable called proj1.x
You are encouraged to use any C++ STL containers and algorithms. You should also use C++ string class instead of the built-in string type.
Your program must be able to compile and run on linprog.
I have some code. What do I need to do to satisfy the
requirements from my code
include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;
int main() {
string s = "aaa";
bool g;
char str[100];
char str1[100];
char str2 [100];
int charCount = 0;
int c[100];
int num[100];
int numCount = 0;
int p = 0;
for (int i = 0; i < s.length(); i++)
{
if (isalpha(s[i]))
{
str[i] = s[i];
for(int j = 0; j < s.length();j++)
{
// checks to see how many times the char appears
if(str[i] == s[j])
{
p++;
}
c[i] = p;
p = 0;
}
cout << "Alpha: " << str[i] <<
" and shows " << c[i] << " times" << endl;
}
else if (isdigit(s[i]))
{
str1[i] = s[i];
cout << "Number: " << str1[i]
<< endl;
}
else
{
str2[i] = s[i];
cout <<"Char: " << str2[i]
<<endl;
}
}
return 0;
}
Answer:-
The below is the required source code for the given problem in C++
Code:-
#include<iostream>
#include<vector>
using namespace std;
class getMost{
private:
//charVect holds the count of each
character in each index corresponding with the ascii value
vector<int> charVect;
//charCount holds the total number
of different ascii values read in
int charCount;
//wordVect[0][0]
will be the total number of different words, wordVect[i][0] will be
the number of instances for each individual word
//wordVect[i][1->n] will hold
each ascii value for the each word.
vector<vector<unsigned
int> > wordVect;
vector<unsigned int>
wordCount;
vector<unsigned int>
wordTemp;
//numVect is made in the same
structure of wordVect
vector<vector<unsigned
int> > numVect;
vector<unsigned int>
numCount;
vector<unsigned int>
numTemp;
//i is the current
ascii value read in
char i;
public:
//getMost constructor sets the
structure of the vectors as explained above
getMost(){
charVect.resize(128);
charCount =
0;
wordVect.resize(1);
wordCount.resize(1);
wordCount[0] =
0;
wordVect[0] =
wordCount;
wordTemp.resize(1);
wordTemp[0]=1;
numVect.resize(1);
numCount.resize(1);
numCount[0] =
0;
numVect[0] =
numCount;
numTemp.resize(1);
numTemp[0] =
1;
}
//reads in each
character, calling a different set of functions dependend on the
ascii type
void parseList(){
while(cin.get(i)){
if((i >= 48) && (i <= 57)){
numList();
charList();
}
else{
charList();
}
}
};
//fills the charVect
with any ascii value and iterates charCount if its the first time
seeing i's ascii value
//calls wordList() if the ascii
value is a letter
void charList(){
int a =
charVect[i];
charVect[int(i)]++;
if(a==0){
charCount++;
}
if(((i >= 65)
&& (i <= 90)) || ((i >= 97) && (i <=
122))){
wordList();
}
};
//fills the
wordVect
void wordList(){
if((i >= 65)
&& (i <= 90)){
i = i+32;
wordTemp.push_back (i);
}
else{
wordTemp.push_back (i);
}
if((cin.peek() < 65) || ((cin.peek() > 90) &&
(cin.peek() < 97)) || (cin.peek() > 122)){
bool match = false;
unsigned int matchIndex;
for(unsigned int a = 1; a < wordVect.size(); a++){
if(wordVect[a].size()==wordTemp.size()){
for(unsigned int b = 1; b < wordTemp.size(); b++){
if(wordTemp[b]==wordVect[a][b]){
if(b==(wordTemp.size()-1)){
match = true;
matchIndex = a;
a = wordVect.size();
}
}
else if(wordTemp[b]!=wordVect[a][b]){
match=false;
b = wordTemp.size();
}
}
}
}
if(match==false){
wordVect[0][0]++;
wordVect.push_back (wordTemp);
wordTemp.clear();
wordTemp.push_back (1);
}
else if(match==true){
wordVect[matchIndex][0]++;
wordTemp.clear();
wordTemp.push_back (1);
}
}
};
void
numList(){
numTemp.push_back (i);
if((cin.peek()
< 48) || (cin.peek() > 57)){
bool match = false;
unsigned int matchIndex;
for(unsigned int a = 1; a < numVect.size();
a++){
if(numVect[a].size() ==
numTemp.size()){
for(unsigned int b = 1; b < numTemp.size(); b++){
if(numTemp[b]==numVect[a][b]){
if(b==(numTemp.size()-1)){
match =
true;
matchIndex
= a;
a =
numVect.size();
}
}
else if(numTemp[b]!=numVect[a][b]){
match=false;
b = numTemp.size();
}
}
}
}
if(match==false){
numVect[0][0]++;
numVect.push_back
(numTemp);
numTemp.clear();
numTemp.push_back (1);
}
else if(match==true){
numVect[matchIndex][0]++;
numTemp.clear();
numTemp.push_back (1);
}
}
};
void
printList(){
unsigned int
largest = 1;
for(unsigned int
i = 1; i < wordVect.size(); i++){
if(wordVect[i].size() > largest){
largest =
wordVect[i].size();
}
}
for(unsigned int
i = 1; i < numVect.size(); i++){
if(numVect[i].size() > largest){
largest =
numVect[i].size();
}
}
largest+=5;
//print
characters
if(charCount>=10){
cout << "Total " << charCount
<< " different characters, 10 most used characters: "
<< endl;
for(int i = 0; i < 10; i++){
int maxValue = i;
int maxCount =
charVect[i];
for(int j = 0; j <
128; j++){
if(charVect[j] > maxCount){
maxValue = j;
maxCount = charVect[j];
}
}
if(maxValue==10){
cout
<< "No. " << i << ": \\n";
for(unsigned int i = 0; i < largest-2; i++){
cout << " ";
}
cout
<< maxCount << endl;
charVect[maxValue]=0;
}
else if(maxValue!=10){
cout
<< "No. " << i << ": " <<
char(maxValue);
for(unsigned int i = 0; i < largest-1; i++){
cout << " ";
}
cout
<< maxCount << endl;
charVect[maxValue]=0;
}
}
}
else
if(charCount<10){
cout << "Total " << charCount << " different
characters, " << charCount << " most used characters: "
<< endl;
for(int i = 0; i < charCount; i++){
int maxValue = i;
int maxCount = charVect[i];
for(int j = 0; j < 128; j++){
if(charVect[j] >= maxCount){
maxValue = j;
maxCount = charVect[j];
}
}
if(maxValue==10){
cout << "No. " << i << ": \\n";
for(unsigned int i = 0; i < largest-2; i++){
cout << " ";
}
cout << maxCount << endl;
charVect[maxValue]=0;
}
else if(maxValue!=10){
cout << "No. " << i << ": " <<
char(maxValue);
for(unsigned int i = 0; i < largest-1; i++){
cout << " ";
}
cout << maxCount << endl;
charVect[maxValue]=0;
}
/*
cout << "No. " << i << ": " <<
char(maxValue);
for(unsigned int i = 0; i
< largest-1){
cout
<< " ";
}
cout << maxCount
<< endl;
charVect[maxValue]=0;
}
*/
}
cout <<
endl;
//print words
if(wordVect[0][0]>=10){
cout << "Total " << wordVect[0][0] << " different
words, 10 most used words: " << endl;
for(unsigned int i = 0; i < 10; i++){
unsigned int maxCount = 0;
unsigned int maxIndex = 1;
for(unsigned int j = 1; j < wordVect.size(); j++){
if(wordVect[j][0] > maxCount){
maxIndex = j;
maxCount = wordVect[j][0];
}
}
cout << "No. " <<
i << ": ";
for(unsigned int k = 1; k < wordVect[maxIndex].size();
k++){
cout << (char)wordVect[maxIndex][k];
}
for(unsigned int i = 0; i
< largest-wordVect[maxIndex].size(); i++){
cout << " ";
}
cout << maxCount
<< endl;
wordVect[maxIndex][0]=0;
}
}
else if(wordVect[0][0]<10){
cout << "Total " << wordVect[0][0]
<< " different words, " << wordVect[0][0] << "
most used words: " << endl;
for(unsigned int i = 0; i < wordVect[0][0];
i++){
unsigned int maxCount =
0;
unsigned int maxIndex = 1;
for(unsigned int j = 1; j < wordVect.size(); j++){
if(wordVect[j][0] > maxCount){
maxIndex = j;
maxCount = wordVect[j][0];
}
}
cout << "No. " << i << ": ";
for(unsigned int k = 1; k < wordVect[maxIndex].size();
k++){
cout << (char)wordVect[maxIndex][k];
}
for(unsigned int i = 0; i
< largest-wordVect[maxIndex].size(); i++){
cout
<< " ";
}
cout << maxCount
<< endl;
wordVect[maxIndex][0]=0;
}
}
cout<<endl;
//print numbers
if(numVect[0][0]>=10){
cout << "Total " << numVect[0][0]
<< " different numbers, 10 most used numbers: " <<
endl;
for(unsigned int i = 0; i < 10; i++){
unsigned int maxCount =
0;
unsigned int maxIndex =
1;
for(unsigned int j = 1; j
< numVect.size(); j++){
if(numVect[j][0] > maxCount){
maxIndex =
j;
maxCount =
numVect[j][0];
}
}
cout << "No. " <<
i << ": ";
for(unsigned int k = 1; k
< numVect[maxIndex].size(); k++){
cout
<< (char)numVect[maxIndex][k];
}
cout << "\t\t" <<
maxCount << endl;
numVect[maxIndex][0]=0;
}
}
else
if(numVect[0][0]<10){
cout << "Total " << numVect[0][0]
<< " different numbers, " << numVect[0][0]<< "
most used numbers: " << endl;
for(unsigned int i = 0; i < numVect[0][0];
i++){
unsigned int maxCount =
0;
unsigned int maxIndex =
1;
for(unsigned int j = 1; j
< numVect.size(); j++){
if(numVect[j][0] >
maxCount){
maxIndex = j;
maxCount = numVect[j][0];
}
}
cout << "No. " <<
i << ": ";
for(unsigned int k = 1; k
< numVect[maxIndex].size(); k++){
cout
<< (char)numVect[maxIndex][k];
}
cout << "\t\t" <<
maxCount << endl;
numVect[maxIndex][0]=0;
}
}
};
};
int main(){
getMost test;
test.parseList();
test.printList();
return 0;
}
If you find any
difficulty with the code, please let know know I will try for any
modification in the code. Hope this answer will helps you. If you
have even any small doubt, please let me know by comments. I am
there to help you. Please give Thumbs Up,Thank You!! All the
best