我怎么能阻止我的整数显示为十六进制

How can I stop my integers from displaying as HEX?

本文关键字:显示 十六进制 整数 我的 怎么能      更新时间:2023-10-16

我正在练习一些实现不同数据结构的代码。对于这个例子,我试图实现一个简单的堆栈数据结构。到目前为止,它按预期工作,但我不断得到十六进制字符时,试图显示我的堆栈。有人能帮我弄清楚为什么会这样吗?

我也在努力更好地组织我的代码,任何已经参与这个行业的人都可以给我一些建设性的批评,关于我到目前为止所写的代码。谢谢。

#include <iostream>
using namespace std;
// stack_MAX == maximum height of stack
const int stack_MAX = 10;
class stack{
    public:
        stack(){
        //Constructor initializes top of stack
            top = -1;
        }
        bool isFull(int top){
        //isFull() will check to make sure stack is not full
        //Will return TRUE if stack is FULL, and FALSE if 
        //stack is NOT FULL
            if(top == stack_MAX - 1)
                return true;
            else
                return false;
        }
        bool isEmpty(int top){
        //isEmpty() will check to make sure stack is not empty
        //Will return TRUE if stack is EMPTY, and FALSE if
        //stack is NOT EMPTY
            if(top == -1)
                return true;
            else
                return false;
        }
        void push(int x){
        //push() will push new element on top of stack
            if(isFull(top)){
                cout << "Sorry, but the stack is full!" << endl;
                exit(1);
            }
            else{
                top++;
                x = stk[top];
            }
        }
        void pop(){
        //pop() will pop the top element from the stack
            if(isEmpty(top)){
                cout << "Sorry, but the stack is empty!" << endl;
                exit(1);
            }
            else{
                cout << stk[top] << " is being popped from stack!" << endl;
                top--;
            }
        }
        void display_stack(){
        //diplay_stack() will show all elements currently in the stack
            int temp;   //will temporarily hold position of stack
            temp = top;
            while(!isEmpty(temp)){
                cout << stk[temp] << endl;
                temp--;
            }
        }
    private:
        int top;
        int stk[stack_MAX];
};
int menu(){
    int choice;
    cout << "Welcome to my stack!" << endl;
    cout << "What would you like to do? (select corresponding #)" << endl << endl;
    cout << "1. Push" << endl;
    cout << "2. Pop" << endl;
    cout << "3. Display" << endl;
    cout << "4. Quit" << endl;
    cin >> choice;
    return choice;
}
int main()
{
    int selection, x;
    stack myStack;
    selection = menu();
    while(selection != 4)
    {
        switch(selection){
            case 1:
                cout << "please enter number to be pushed: ";
                cin >> x;
                myStack.push(x);
                selection = menu();
                break;
            case 2:
                myStack.pop();
                selection = menu();
                break;
            case 3:
                myStack.display_stack();
                selection = menu();
                break;
            default:
                cout << "Oops that's not a selection, try again" << endl;
                selection = menu();
                break;
        }
    }
    cout << "Thank you for stopping by and using my stack!" << endl;
    system("pause");
    return 0;
}

您的push函数中的语句是错误的,修改如下:

void push(int x)
{
    //push() will push new element on top of stack
    if(isFull(top))
    {
        cout << "Sorry, but the stack is full!" << endl;
        exit(1);
    }
    else
    {
        top++;
        /***************************
        x = stk[top];
        ****************************/
        stk[top] = x;
    }
}

建议:

  • 学习调试,这里是教程
  • 包含头文件cstdlib时,你想使用exit在您的代码
  • 不要用与STL中任何类相同的名字命名你的类

正如史前企鹅指出的,你的push()函数是不正确的:

x = stk[top];

应该改成:

stk[top] = x;

无论如何我都想评论一下,按你的要求提供一些一般性的评论:

  • 这样的If语句可以用一行代码替换:

        if(top == stack_MAX - 1)
            return true;
        else
            return false;
    

就变成:

return (stack_MAX - 1 == top);

  • 将常量表达式放在比较表达式的左侧。例如:

    (top == stack_MAX - 1)

就变成:

(stack_MAX - 1 == top)

原因是有一天你会不小心输入这样的东西:

(top = stack_MAX - 1)

和你或其他人会浪费很多时间调试它:)

  • 你的isFull()和isEmpty()函数不应该带参数。它们应该只使用私有成员变量top。如果没有对top的访问,怎么能调用这些函数呢?

  • 一般避免using。在我看来,它违背了名称空间的全部目的。使用名称空间STD是一种常用的异常,但即便如此,键入STD::cout有那么难吗?

  • 总是在if语句的子句周围加上花括号,即使它们只有一行。如果稍后需要在子句中添加更多语句,很容易忘记添加大括号,这可能会非常令人困惑。

  • 你的代码格式很好,但是选择一个括号样式并保持一致。要么总是把左花括号放在函数头/控制语句等的同一行,要么总是把它放在后面的行。

希望对你有帮助。