luv
发表于 2013-7-27 20:03:59
:lol:lol:lol:lol支持甲鱼
revenking
发表于 2013-7-28 11:11:07
真是难得给力的帖子啊。
hydkmy
发表于 2013-9-29 21:47:45
必须顶上这种高端霸气上档次的题目
Victor1991
发表于 2013-10-4 18:45:59
强烈支持楼主ing……
旭日东升
发表于 2013-10-4 19:39:53
楼主加油,鱼C加油!我们都看好你哦!
小睿webster
发表于 2013-10-22 10:07:47
强烈支持楼主ing……
a5218164
发表于 2013-10-25 00:45:38
强烈支持楼主ing……
岁月如歌
发表于 2013-11-20 15:16:05
太生气了,无法HOLD啦 >_<......
岁月如歌
发表于 2013-11-20 16:17:19
激动人心,无法言表!
815116820
发表于 2013-12-5 21:16:55
我只是路过打酱油的。
ananxiamu
发表于 2014-1-5 00:08:54
我只是路过打酱油的。
柠“萌”圆
发表于 2014-2-6 21:15:14
应该是模拟笔算得出结果
柠“萌”圆
发表于 2014-2-6 21:19:31
其实不用以#为结束标志,以\n为结束标志不就行了吗
柠“萌”圆
发表于 2014-2-6 21:22:30
我用C++实现行么?我都自己写了一个Stack类了
柠“萌”圆
发表于 2014-2-7 12:55:32
#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;
}
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;
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (int i = 0; i < n; i++)
Push(arr);
}
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;
}
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;
}
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;
}
catch (std::bad_alloc & ba) {
StackSize = 0;
base = top = nullptr;
delete [] temp;
return false;
}
for (int i = 0; i < size; i++)
Push(temp);
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;
}
#endifStack.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);
for (int i = 0; i < len2; i++)
num2.Push(buffer2);
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;
}
じO-联合
发表于 2014-2-16 22:30:01
淡定,淡定,淡定……
じO-联合
发表于 2014-2-16 22:30:48
微博评论 发表于 2013-1-5 04:44 static/image/common/back.gif
我准备讲一套objective-c的视频,不知道有没有鼓励啊
来自 -追梦的小猪- 的新浪微博
怒求-------------
tsp150600
发表于 2014-2-21 19:01:13
强烈支持楼主ing……
LC2790
发表于 2014-2-22 09:30:03
强烈支持楼主ing……
魂归一隅
发表于 2014-3-25 22:59:02
看看什么样