Write a C/C++ program that read a source file (input.txt) containing characters
ID: 3571269 • Letter: W
Question
Write a C/C++ program that read a source file (input.txt) containing characters described below (Seperated with single spaces) and
Your program should allocate and build and create and instantiate 4 stacks class (using arrays):
1 for special, 1 for digit and 1 for alphabet, 1 for other which will store each token/character group that was found in the input file.
Size of each stack must be defined dynamically based on the total characters in your input file – if you total characters in your input file is MAX ( such as 200) then
Each stack for special, digit and character or other must be defined dynamically based on on this MAX (such as 200).
Read your file 1 time and find the MAX characters in your file to initialize your integer variable or parameter MAX
While reading the input, your program should push the tokens/character to the right stack (1 of the 4 stacks) and when all the input has been read, your program should pop each stack and count each stack at the end to generate the summary output.
And your program should produce a JUST a count summary report that includes a total of all the tokens/character group that appeared in the input,
Basically, a count of each group (like below): how many times each appeared in the input. You need to write
4 Stacks Report:
Special : 50
Digit : 80
Character : 120
Other : 400
Total : 650
4 character/token groups:
More details:
Your input file will read the characters and push them into 4 stacks: special (9), digit (10) and character (26 lower case, 26 upper case) and other (any other character) characters
The delimiters are single space and newline
The token/character groups that should be recognized are special, digit, alphabet and other (other is anything else)
Your final program must compile and run using a C++ compiler (your choice).
Explanation / Answer
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Stack{
char element;
Stack *stack;
int size;
public:
Stack(){}
Stack(int MAX){
size = 0;
element = 0;
stack = new Stack[MAX];
}
~Stack(){
delete[] stack;
}
int isEmpty(){
return size == 0;
}
int stack_size(){
return size;
}
void push(char value){
this->stack[size++].element = value;
}
int pop(){
return this->stack[--size].element;
}
};
int main(){
Stack *special, *digit, *character, *others;
ifstream fin;
fin.open("filename");
if(!fin){
cout << "There is no such file as filename"<<endl;
return 0;
}
int MAX = 0;
string line;
while(getline(fin, line)){
MAX += line.size();
}
fin.close();
special = new Stack(MAX);
digit = new Stack(MAX);
character = new Stack(MAX);
others = new Stack(MAX);
fin.open("filename");
while(getline(fin, line))
{
for(int i = 0;i < line.size(); ++i){
int num = line[i];
// special characters
if(line[i] == '(' || line[i] == ')' || line[i] == '['
|| line[i] == ']' || line[i] == '+' || line[i] == '-'
|| line[i] == '=' || line[i] == ',' || line[i] ==';'){
special->push(line[i]);
}
// alphabet
else if((num >= 65 && num <= 90) || (num >= 97 && num <= 122)){
character->push(line[i]);
}
// digit
else if(num >= 48 && num <= 57){
digit->push(line[i]);
}else{
others->push(line[i]);
}
}
}
cout << "Stack Report ";
int count, total = 0;
count = 0;
cout << "Special" << ": ";
while(!special->isEmpty()){
count++;
special->pop();
}
cout << count << endl;
total += count;
count = 0;
cout << "Digit" << ": ";
while(!digit->isEmpty()){
count++;
digit->pop();
}
cout << count << endl;
total += count;
count = 0;
cout << "Character" << ": ";
while(!character->isEmpty()){
count++;
character->pop();
}
cout << count << endl;
total += count;
count = 0;
cout << "Others" << ": ";
while(!others->isEmpty()){
count++;
others->pop();
}
cout << count << endl;
total += count;
cout << "Total : "<<total<<endl;
//cout << special->pop() << special->pop() << special->stack_size() << endl;
return 0;
}
i/p
asbbaASGJJBS093249SAAhsbs
uh sdhh 899 )())
_+)_+)--=*&&^~@!oip
o/p
Stack Report
Special: 11
Digit: 9
Character: 28
Others: 12
Total : 60
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.