classes
Can someone remake this code to make program work not with char datatype,but int?
CharStack.h
typedef char elem[5];
class CharStack {
private:
elem *arr;
int length;
int topElem;
public:
CharStack(int);
~CharStack();
char *pop();
char *top();
void push(elem);
int count();
bool isEmpty();
bool isFull();
};
CharStack.cpp
#include <iostream>
#include <cstdlib>
#include "CharStack.h"
using namespace std;
CharStack::CharStack(int len) {
arr = new elem[len];
length = len;
topElem = 0;
}
CharStack::~CharStack() {
delete[] arr;
cout << "stack deleted. " << count() << " elements in stack upon deletion."
<< endl;
}
void CharStack::push(elem pushableElem) {
if (isFull()) {
cout << "\npush() failed; stack is full!\n" << endl;
} else {
cout << "\nPushing " << endl;
for (int i = 0; i < 5; i++) {
arr[topElem][i] = pushableElem[i];
cout << pushableElem[i];
}
cout << " into stack.\n" << endl;
arr[topElem][5] = 0;
topElem++;
}
}
char *CharStack::pop() {
if (isEmpty()) {
return "\npop() failed; stack is empty!\n";
} else {
topElem--;
char *tmpElem = new char[5];
for (int i = 0; i < 5; i++)
tmpElem[i] = arr[topElem][i];
for (int i = 0; i < 5; i++)
arr[topElem][i] = 0;
return tmpElem;
delete[] tmpElem;
}
}
int CharStack::count() {
return topElem;
}
char *CharStack::top() {
if (isEmpty()) {
return "\ntop() failed; stack is empty!\n";
} else {
char *tmpElem = new char[5];
for (int i = 0; i < 5; i++)
tmpElem[i] = arr[topElem-1][i];
return tmpElem;
delete[] tmpElem;
}
}
bool CharStack::isEmpty() {
return (topElem==0);
}
bool CharStack::isFull() {
return (topElem==length);
}
main.cpp
/*
Emils Solmanis, es09260
D15:
Izveidot klasi "Simbolu steks", kura glabajas masivs ar simboliskam vertibam
(char) garuma 5 un steka elementu skaits. Klasei izveidot sadas metodes:
(1) konstruktors;
(2) destruktors, kurs pazino par objekta likvidesanu un likvideto elementu
skaitu (ja likvidesanas bridi steks nav tukss);
(3) metode "Push", kas pievieno stekam elementu, ja tas nav pilns;
(4) metode "Pop", kas iznem no steka augsejo elementu un atgriez ta vertibu;
(5) metode "Count", kas atgriez elementu skaitu steka;
(6) metode "Top", kas atgriez augseja elementa vertibu;
(7) metode "IsEmpty", kas noskaidro, vai steks ir tukss;
(8) metode "IsFull", kas noskaidro, vai steks ir pilns.
Pirms uzdevuma veiksanas precizi noskaidrot, ko nozime jedziens steks (stack).
Programma izveidota: 16 - 17.09.2009.
*/
#include <cstdlib>
#include <iostream>
#include "CharStack.h"
using namespace std;
int main(){
bool running = true;
string actionstring;
elem pushableElem;
int stackLen;
cin >> stackLen;
CharStack stack(stackLen);
while (running) {
cin >> actionstring;
if (actionstring=="push") {
cin >> pushableElem;
stack.push(pushableElem);
}
if (actionstring=="pop")
cout << stack.pop() << endl;
if (actionstring=="top")
cout << stack.top() << endl;
if (actionstring=="count")
cout << stack.count() << endl;
if (actionstring=="isEmpty")
cout << stack.isEmpty() << endl;
if (actionstring=="isFull")
cout << stack.isFull() << endl;
if (actionstring=="quit")
running = false;
}
return 0;
}[/quote][output] |
[/output]
Are you asking us to rewrite the code for you, or are you asking how you would go about rewriting this code?
I just want to know which plces have to be changed to make it work with int
Find the spots that say "Char" or "char" and replace them with "Int" or "int". Your files should also be renamed "IntStack.h" and "IntStack.cpp".
It is worth your time to do this. Your professor is about to teach you something important.
Good luck!
Ooo now i'm interested, Duoas explain :)
You don't want Duoas to steal the teacher's thunder, now do you?
I'm more than happy to receive a PM lol.
Done.
I could very well be wrong.
:-)
Topic archived. No new replies allowed.