用顺序栈做大整数加法遇到的有关问题
用顺序栈做大整数加法遇到的问题
在调用Add()和Sub()的时候老是得不到结果,要么是0要么什么都没有,问题出在哪?
------解决思路----------------------
问题出在Pop函数:
if (S.top = S.base) return ERROR;
应改为:
if (S.top == S.base) return ERROR;
再有,此函数既返回类型为Status的ERROR,又返回类型为SElemType的e,这是不严格的。
建议改为:
Status Pop(SqStack &S, SElemType *e) {
//若栈不空,则删除S栈顶元素,用e返回其值,并返回OK;否则返回ERROR
*e = 0;
if (S.top == S.base) return ERROR; //栈空
*e = *--S.top; //栈顶指针减一,将栈顶元素赋给e
return OK;
}
并修改所有Pop的调用,例如:
Pop(s, &d);//d = Pop(s);
在调用Add()和Sub()的时候老是得不到结果,要么是0要么什么都没有,问题出在哪?
#include "stdafx.h"
#include <iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef int SElemType;
typedef int Status;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
SqStack InitStack(){
//构造一个空栈 S
SqStack S;
S.base = new SElemType[MAXSIZE];
if (!S.base) exit(OVERFLOW); //为顺序栈分配一个最大容量为MAXSIZE的数组空间
S.top = S.base;
S.stacksize = MAXSIZE;
return S;
}
Status Push(SqStack &S, SElemType e) {
//插入元素e为新的栈顶元素
if (S.top - S.base == S.stacksize) return ERROR; //栈满
*S.top++ = e; //元素e压入栈顶,栈顶指针加1
return OK;
}
Status Pop(SqStack &S) {
//若栈不空,则删除S栈顶元素,用e返回其值,并返回OK;否则返回ERROR
int e = 0;
if (S.top = S.base) return ERROR; //栈空
e = *--S.top; //栈顶指针减一,将栈顶元素赋给e
return e;
}
Status DisplayStack(SqStack &S) //从栈底到栈顶依次对栈的元素进行访问
{
SElemType * p;
p = S.base;
while (p != S.top)
{
cout << *p << " " ;
p++;
}
cout << endl;
return OK;
}
void Output(SqStack &s)
{
int d = 0;
while (s.top != s.base){
d = Pop(s);
cout << d;
}
cout << endl;
}
SqStack Input(SqStack s)
{
char c;
c = getchar();
c = getchar();
while (c != '#'){
Push(s, atoi(&c));
c = getchar();
}
return s;
}
void Add(SqStack &s1, SqStack &s2){
int a, b, c, d;
SqStack addtion;
addtion = InitStack();
c = 0; // 进位初值为0
while (s1.top != s1.base && s2.top != s2.base){
a = Pop(s1);
b = Pop(s2);
d = a + b + c;
Push(addtion, (d + 10) % 10);
c = d / 10;
}
if (s1.top != s1.base){
while (s1.top != s1.base){
d = Pop(s1) + c;
Push(addtion, (d + 10) % 10);
c = d / 10;
}
if (c) Push(addtion, c);
}
if (s2.top != s2.base){
while (s2.top != s2.base){
d = Pop(s2) + c;
Push(addtion, (d + 10) % 10);
c = d / 10;
}
if (c) Push(addtion, c);
}
if (c) Push(addtion, c);
Output(addtion);
}
void Sub(SqStack &S1, SqStack &S2)
{
int a, b, c, d, *p;
SqStack S;
S = InitStack();
c = 0;
while (S1.top != S1.base && S2.top != S2.base)
{
a = Pop(S1);
b = Pop(S2);
d = a - b - c;
Push(S, (d + 10) % 10);
d >= 0 ? (c = 0) : (c = 1);
}
if (!c)
{
while (S1.top != S1.base){
d = Pop(S1) - c;
Push(S, (d + 10) % 10);
d > 0 ? (c = 0) : (c = 1);
}
Output(S);
}
else {
while (S2.top != S2.base) {
d = 9 - Pop(S2);
Push(S, d);
}
p = S.base;
c = 0;
while (p != S.top) {
*p = 10 - *p - c;
c = 1;
p++;
}
cout << "-";
Output(S);
}
}
int main()
{
SqStack S1,S2;
int i; int z = 1;
S1 = InitStack();
S2 = InitStack();
cout << " 大整数加法运算器 " << endl;
cout << "--------------------------------------------" << endl;
cout << "| 1------输入第一个数 |" << endl;
cout << "| 2------输入第二个数 |" << endl;
cout << "| 3------输出和 |" << endl;
cout << "| 4------输出差 |" << endl;
cout << "| 0------退出 |" << endl;
cout << "--------------------------------------------" << endl;
cout << " 请选择菜单号(0--4):" << endl;
while (z)
{
cin >> i;
switch (i)
{
case 1:
cout << "请输入第一个数:";
S1 = Input(S1);
DisplayStack(S1);
break;
case 2:
cout << "请输入第二个数:";
S2 = Input(S2);
DisplayStack(S2);
break;
case 3:
Add(S1, S2);
break;
case 4:
Sub(S1, S2);
break;
default:
exit(1);
}
}
system("pause");
return 0;
}
------解决思路----------------------
问题出在Pop函数:
if (S.top = S.base) return ERROR;
应改为:
if (S.top == S.base) return ERROR;
再有,此函数既返回类型为Status的ERROR,又返回类型为SElemType的e,这是不严格的。
建议改为:
Status Pop(SqStack &S, SElemType *e) {
//若栈不空,则删除S栈顶元素,用e返回其值,并返回OK;否则返回ERROR
*e = 0;
if (S.top == S.base) return ERROR; //栈空
*e = *--S.top; //栈顶指针减一,将栈顶元素赋给e
return OK;
}
并修改所有Pop的调用,例如:
Pop(s, &d);//d = Pop(s);