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
|
#include <iostream>
#include <cstring>
#include <ctime>
#define SIZE 25
char stck[SIZE];
int pos=-1;
using namespace std;
struct Node{
char data;
struct Node* next;
} *top;
void addlist(char data){
struct Node* temp;
temp = new Node();
if (temp==NULL)
exit(1);
temp->data=data;
temp->next=top;
top = temp;
}
void seelist(Node* temp){
if (top==NULL)
exit(1);
else{
struct Node* temp=top;
for (; temp != NULL; temp=temp->next)
cout << temp->data << ' ';
}
cout << endl;
}
void addarray(char a){
pos++;
stck[pos]=a;
}
void seearray(){
if (pos==0)
return;
else{
for (int i=pos; i>-1; --i)
cout << stck[i] << ' ';
cout << endl;
}
}
void count(int n, int k, int m) {
cout << "Vowels: " <<n<< endl;
cout << "Consonant letters: " <<k<< endl;
cout << "Other characters: " <<m<< endl;
if (n>k){
if (m>n) cout << "other characters the most" << endl;
if (m==n) cout << "other characters and vowels the most" << endl;
if (m<n) cout << "vowels the most" << endl;
}
else {
if (n < k){
if (m>k) cout << "other characters the most" << endl;
if (m==k) cout << "other characters and consonants the most" << endl;
if (m<k) cout << "consonant letters the most" << endl;
}
if (n==k){
if (m==n) cout << "other symbols, vowels and consonants alike" << endl;
if (m>n) cout << "other characters the most" << endl;
if (m<n) cout << "vowels and consonants the most" << endl;
}
}
cout << endl;
}
void arr(){
cout << "Array-based stack: " << endl;
const char vowels[] = "AEYUIOaeyiou";
const char constt[] = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz";
int a=0, b=0, c=0;
for (int i=0; i<SIZE; ++i)
addarray(char(rand() % 94+32));
seearray();
for (int i=0; i<SIZE; i++){
if (strchr(vowels, stck[i]) != NULL){
a++;
}
else{
if (strchr(constt, stck[i]) != NULL)
b++;
else c++;
}
}
count(a, b, c);
}
void list(){
cout << "Stack based on a linked list: " << endl;
Node* temp=top;
const char vowels[] = "AEYUIOaeyiou";
const char constt[] = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz";
int a=0, b=0, c=0;
for (int i=0; i<SIZE; ++i)
addlist(char(rand() % 94+32));
seelist(temp);
for (temp=top; temp != NULL; temp=temp->next){
if (strchr(vowels, temp->data) != NULL){
a++;
}
else{
if (strchr(constt, temp->data) != NULL)
b++;
else c++;
}
}
count(a, b, c);
}
int main(){
srand(time(NULL));
arr();
list();
return 0;
}
|