Anonymní profil Stamp – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil Stamp – Programujte.comAnonymní profil Stamp – Programujte.com

 

Příspěvky odeslané z IP adresy 78.98.24.–

Stamp
C / C++ › C++ Stack implementation
19. 6. 2018   #221408

No trochu som to Rule of three postudoval, tak dufam ze to je aspon o nieco lepsie :-) 

// Stack.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Stack.h"
#include <assert.h>
#include <iostream>
#include <stdlib.h>
#include <vector>

Stack::Stack()
{
	Stack::sizeOfStack = 5;
	Stack::numbersInStack = 0;
	Stack::st = new int[Stack::sizeOfStack];
}

Stack::Stack(const Stack &n) : // copy constructor
	st(new int[n.numbersInStack]), 
	numbersInStack(n.numbersInStack), 
	sizeOfStack(n.sizeOfStack)
{
	std::copy(n.st,n.st+n.numbersInStack,st);
}

Stack& Stack::operator=(const Stack &source) { // copy assignment constructor
	int *newData = new int[source.numbersInStack];
	numbersInStack = source.numbersInStack;
	sizeOfStack = source.sizeOfStack;
	std::copy(source.st, source.st + source.numbersInStack, newData);
	delete[] st;
	st = newData;
	return *this;
}

Stack::~Stack() { // destructor
	delete[] Stack::st;
}

void Stack::push(int num) {
	Stack::st[numbersInStack++] = num;
	if (numbersInStack == sizeOfStack) {
		sizeOfStack *= 2;
		int *newSt = new int[sizeOfStack];
		std::copy(st, st + numbersInStack, newSt);
		delete[] st;
		st = newSt;
	}
}

int Stack::size() {
	return Stack::numbersInStack;
}

bool Stack::isEmpty() {
	return Stack::numbersInStack == 0;
}

int Stack::pop() {
	assert(!(Stack::numbersInStack == 0));
	return Stack::st[--numbersInStack];
}

void Stack::print() {
	for (int i = 0; i < Stack::numbersInStack; ++i)
		std::cout << Stack::st[i] << " ";
	std::cout << std::endl;
}
#ifndef STACK
#define STACK
#include <algorithm>
class Stack {
	private:
		unsigned short sizeOfStack;
		unsigned short numbersInStack;
		int *st = NULL;

	public:
		bool isEmpty();
		int pop();
		void print();
		void push(int num);
		int size();
		Stack();
		Stack(const Stack &n);
		~Stack();
		Stack &operator=(const Stack &source);
		
};
#endif 
Stamp
C / C++ › C++ Stack implementation
19. 6. 2018   #221397

Dík za odpoveď, potom mi to aj nejako došlo že nie je inicializovaná takže v nej bude asi nejaká veľká hodnota.
Trochu som to opravil, prechádzam z C a jediný spôsob čo mi napadol bol cez realloc čo mi nepríde moc C++ :-)
Za hocijaké postrehy budem rád 
 

// Stack.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Stack.h"
#include <assert.h>
#include <iostream>
#include <stdlib.h>
#include <vector>

Stack::Stack()
{
	Stack::sizeOfStack = 5;
	Stack::numbersInStack = 0;
	Stack::st = new int[Stack::sizeOfStack];
}

Stack::~Stack() {
	delete[] Stack::st;
}

void Stack::push(int num) {
	Stack::st[numbersInStack++] = num;
	if (numbersInStack == sizeOfStack) {
		sizeOfStack *= 2;
		st = static_cast<int *>(std::realloc(st,sizeof(int) * sizeOfStack));
	}
}

int Stack::size() {
	return Stack::numbersInStack;
}

bool Stack::isEmpty() {
	return Stack::numbersInStack == 0;
}

int Stack::pop() {
	assert(!(Stack::numbersInStack == 0));
	return Stack::st[--numbersInStack];
}

void Stack::print() {
	for (int i = 0; i < Stack::numbersInStack; ++i)
		std::cout << Stack::st[i] << " ";
	std::cout << std::endl;
}
// Stack.h
#ifndef STACK
#define STACK

class Stack {
	private:
		unsigned short sizeOfStack;
		unsigned short numbersInStack;
		int *st = NULL;

	public:
		bool isEmpty();
		int pop();
		void print();
		void push(int num);
		int size();
		Stack();
		~Stack();
		
};
#endif 

 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý