#ifndef STACK_H_
#define STACK_H_
#include <new>
#include <initializer_list> //initializer_list是C++11新增的模板类
template <typename T>
class Stack
{
private:
T * base;
T * top;
unsigned int StackSize; //表示栈内剩余容量
public:
Stack(const unsigned int size, const T * begin, const T * end);
Stack(const unsigned int size, const T arr[], const int n);
Stack(const unsigned int size, const std::initializer_list<T> & l);
Stack(const unsigned int size = 100);
~Stack() {delete [] base;}
bool Push(const T & item);
bool Pop(T & e);
bool Clear();
bool Is_Empty()const {return base == top;}
unsigned int Remaining()const {return StackSize;}
unsigned int StackLen()const {return top - base;}
unsigned int Size()const {return StackSize + (top - base);}
};
template <typename T>
Stack<T>::Stack(const unsigned int size, const T * begin, const T * end) : StackSize(size)
{
if (StackSize < end - begin)
StackSize = end - begin;
try {
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (T * p = begin; p != end; p++)
Push(*p);
}
template <typename T>
Stack<T>::Stack(const unsigned int size, const T arr[], const int n) : StackSize(size)
{
if (StackSize < n)
StackSize = n + 1;
try {
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (int i = 0; i < n; i++)
Push(arr[i]);
}
template <typename T>
Stack<T>::Stack(const unsigned int size, const std::initializer_list<T> & l) : StackSize(size)
{ //initializer_list是C++11新增的模板类
int i = 0;
try{
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (auto p = l.begin(); p != l.end(); p++, i++)
Push(*p);
}
template <typename T>
Stack<T>::Stack(const unsigned int size) : StackSize(size)
{
try {
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
}
template <typename T>
bool Stack<T>::Push(const T & item)
{
if (StackSize == 0)
{
T * temp = base;
unsigned int size = top - base;
StackSize = size + 10;
try {
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
StackSize = 0;
base = top = nullptr;
delete [] temp;
return false;
}
for (int i = 0; i < size; i++)
Push(temp[i]);
Push(item);
delete [] temp;
return true;
}
*(top++) = item;
StackSize--;
return true;
}
template <typename T>
bool Stack<T>::Pop(T & e)
{
if (top && !Is_Empty())
{
e = *(--top);
StackSize++;
return true;
}
return false;
}
template <typename T>
bool Stack<T>::Clear()
{
if(top)
{
StackSize += top - base;
top = base;
return true;
}
return false;
}
#endif
Stack.h头文件,需要编译器支持C++11
下面是实现代码#include <iostream>
#include <string>
#include <cctype>
#include "Stack.h"
int main()
{
using std::cout;
using std::endl;
using std::cin;
cout << "请输入第一个数: ";
char ch;
Stack<char> num1(10);
Stack<char> num2(10);
Stack<int> sum(10);
std::string buffer;
std::string buffer2;
cin.get(ch);
while (ch != '\n')
{
if (std::isdigit(ch))
buffer += ch;
cin.get(ch);
}
int len = buffer.size();
cout << "请输入第二个数: ";
cin.get(ch);
while (ch != '\n')
{
if (std::isdigit(ch))
buffer2 += ch;
cin.get(ch);
}
int len2 = buffer2.size();
int max = len > len2 ? len : len2;
int min = len < len2 ? len : len2;
int difference = max - min;
if (max == len)
for (int i= 0; i < difference; i++)
num2.Push('0');
else
for (int i= 0; i < difference; i++)
num1.Push('0');
for (int i = 0; i < len; i++)
num1.Push(buffer[i]);
for (int i = 0; i < len2; i++)
num2.Push(buffer2[i]);
int carry = 0;
while (!num1.Is_Empty())
{
char c1;
char c2;
int n1;
int n2;
int rem;
num1.Pop(c1);
num2.Pop(c2);
n1 = c1 - 48;
n2 = c2 - 48;
n1 += n2 + carry;
carry = n1 / 10;
rem = n1 % 10;
sum.Push(rem);
if (num1.Is_Empty() && carry)
sum.Push(carry);
}
cout << "两数相加的结果是: ";
while (!sum.Is_Empty())
{
int temp;
sum.Pop(temp);
cout << temp;
}
cout << endl;
return 0;
}
|