首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

用栈实现二进制转十进制时遇到异常

2012-02-27 
用栈实现二进制转十进制时遇到错误C/C++ code#ifndef __STACK_H__#define __STACK_H__#define MAXSIZE 100

用栈实现二进制转十进制时遇到错误

C/C++ code
#ifndef __STACK_H__#define __STACK_H__#define MAXSIZE 100typedef char ElemType;typedef struct{    ElemType *top;    ElemType *base;    int stackSize;} sqStack;class Stack{public:    Stack();    ~Stack();    void initStack();    void push(ElemType e);    ElemType pop();    int stackLongth();    void clearStack();    void destoryStack();    void print();private:    sqStack *items;};#endif // __STACK_H__#include "Stack.h"#include <iostream>#include <cmath>using namespace std;Stack::Stack(){    this->initStack();}Stack::~Stack(){    this->destoryStack();}void Stack::initStack(){    this->items = new sqStack();    this->items->base = (ElemType *) new ElemType(MAXSIZE);    if (!this->items->base)    {        exit(0);    }    this->items->top = this->items->base;    this->items->stackSize = MAXSIZE;}void Stack::push(ElemType e){    *(this->items->top) = e;    ++(this->items->top);}ElemType Stack::pop(){    //if (this->items->base == this->items->top)    //{    //    cout<<"the stack is NULL"<<endl;    //    exit(0);    //}    --(this->items->top);    return *(this->items->top);}int Stack::stackLongth(){    return (this->items->top - this->items->base);}void Stack::clearStack(){    this->items->top = this->items->base;}void Stack::destoryStack(){    for (int i=0;i!=this->stackLongth();++i)    {        delete this->items->base;        ++(this->items->base);    }    this->items->base = NULL;    this->items->top = NULL;    this->items->stackSize = 0;}void Stack::print(){    cout<<"show items:"<<endl;    for (ElemType *e=this->items->base;e!=this->items->top;++e)    {        cout<<*e<<endl;    }}int main(){    Stack *stack = new Stack();    char ch[11] = "1011011001";    for (int i=0;i!=10;++i)    {        stack->push(ch[i]);    }    stack->print();    //stack->push('1');    //stack->print();    //cout<<"temp char is:"<<stack->pop()<<endl;    //stack->print();    //cout<<"stack longth:"<<stack->stackLongth()<<endl;    char c;    int sum = 0;    for (int i=0;i<=stack->stackLongth();++i)    {        c = stack->pop();        cout<<"item:"<<c<<endl;        sum += (c - 48)*pow(2.0,i);    }    cout<<"decimail :"<<sum<<endl;    getchar();    return 0;}


[解决办法]
Java code
/** * @(#)CsdnDemoTwo.java * * * @author longtengbing * @version 1.00 2011/12/13 */import java.util.*;public class CsdnDemoTwo {            public static void main(String[] args) throws Exception  {        // TODO code application logic here        Stack<Integer> stack = new Stack<Integer>();        Scanner cin = new Scanner(System.in);        System.out.println("Please input the binary :");        String str = cin.next();        for(int i = 0;i<str.length();i++){            int temp = str.charAt(i)-'0';            stack.push(temp);        }        int k = 0;        int sum = 0;        while(!stack.empty()){            int temp = stack.pop();            sum += (int)Math.pow(2.0,k++)*temp;        }        System.out.println("The decimal is :\r\n"+sum);    }}
[解决办法]
C/C++ code
char c;int sum = 0;for (int i=0;i<=stack->stackLongth();++i){   c = stack->pop();   cout<<"item:"<<c<<endl;   sum += (c - 48)*pow(2.0,i);} 

热点排行