weixin_45524887 2026-01-28 20:20 采纳率: 80%
浏览 3
已结题

这个c++类程序哪里了啊出错

#ifndef _MAIN_H_
#define _MAIN_H_
#include <string>
#include <iostream>
#include <cstring>
using namespace std;


struct customer
{
    char fullname[50];
    double payment;
};

typedef customer Item;

class Stack
{
private:
    static const int MAX = 10;
    double total;
    Item line[MAX];
    int num;
public:
    Stack() { num = 0; total = 0.0; }
    void pull(const Item& it);
    const Item pop();
    bool is_full();
    bool is_void();
    int show_num() { return num; }


};

void showte(const customer& cus);

#endif

#include "main.h"

void Stack::pull(const Item& it)
{
    if (is_full())
        cout << "Full!" << endl;
    else
        line[num++] = it;
}

const Item Stack::pop()
{
    if (is_void())
        cout << "Empty!!" << endl;
    else
    {
        total += line[num].payment;
        cout << "Now, total payment is: " << total << endl;
        return line[num--];
    }
}

bool Stack::is_full()
{
    return num == MAX;
}

bool Stack::is_void()
{
    return num == 0;
}

void showte(const customer& cus)
{
    cout << "Name: " << cus.fullname << endl;
    cout << "Payment: " << cus.payment << endl;
}

#include "main.h"


int main()
{
    customer one = { "diyige", 10.2 };
    customer two = { "dierge", 52.6 };
    customer three = { "disange", 98.5 };
    customer test;

    Stack fline;
    fline.pop();
    fline.pull(one);
    fline.pull(two);
    fline.pull(three);
    test = fline.pop();
    showte(test);
    test = fline.pop();
    showte(test);
    test = fline.pop();
    showte(test);
    return 0;
}

  • 写回答

2条回答 默认 最新

  • 檀越@新空间 2026-01-28 20:21
    关注

    晚上好🌙🌙🌙
    本答案参考通义千问

    你提供的 C++ 代码存在一些错误,下面我将详细分析问题,并提供解决方案。


    一、主要问题分析

    1. pop() 方法中访问了越界索引
      Stack::pop() 方法中,你尝试访问 line[num],但 num 是当前栈的元素数量。当调用 pop() 时,num 的值是当前栈中元素的数量,因此在 line[num] 时会访问到数组的非法位置(因为数组下标从 0 到 MAX-1)。

    2. pop() 方法没有返回值的处理逻辑
      is_void() 为真时,pop() 函数没有返回任何值,这会导致未定义行为。

    3. total 变量未初始化
      total 是一个 double 类型的变量,在构造函数中没有被显式初始化,虽然默认是 0,但在某些编译器中可能不保证。

    4. showte() 函数未被调用
      你定义了 showte() 函数,但在 main() 中没有调用它。

    5. customer 结构体中的 fullname 使用了 char[50],而 cin 或其他输入操作可能导致字符串溢出
      如果用户输入的字符串长度超过 50,可能会导致缓冲区溢出。

    6. main()fline.pop(); 调用时栈为空,导致错误输出
      main() 中,你首先调用了 fline.pop();,此时栈是空的,会触发 Empty!! 的提示,这是预期的行为,但需要确保逻辑正确。


    二、修改后的完整代码

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    #ifndef _MAIN_H_
    #define _MAIN_H_
    
    struct customer
    {
        char fullname[50];
        double payment;
    };
    
    typedef customer Item;
    
    class Stack
    {
    private:
        static const int MAX = 10;
        double total;
        Item line[MAX];
        int num;
    public:
        Stack() : num(0), total(0.0) {}
        void pull(const Item& it);
        const Item pop();
        bool is_full();
        bool is_void();
        int show_num() { return num; }
    };
    
    void showte(const customer& cus);
    
    #endif
    
    #include "main.h"
    
    void Stack::pull(const Item& it)
    {
        if (is_full())
            cout << "Full!" << endl;
        else
            line[num++] = it;
    }
    
    const Item Stack::pop()
    {
        if (is_void())
        {
            cout << "Empty!!" << endl;
            // 返回一个默认的 Item,避免未定义行为
            return {"", 0.0};
        }
        else
        {
            total += line[num - 1].payment;
            cout << "Now, total payment is: " << total << endl;
            return line[--num];
        }
    }
    
    bool Stack::is_full()
    {
        return num == MAX;
    }
    
    bool Stack::is_void()
    {
        return num == 0;
    }
    
    void showte(const customer& cus)
    {
        cout << "Name: " << cus.fullname << endl;
        cout << "Payment: " << cus.payment << endl;
    }
    
    #include "main.h"
    
    int main()
    {
        customer one = { "diyige", 10.2 };
        customer two = { "dierge", 52.6 };
        customer three = { "disange", 98.5 };
        customer test;
    
        Stack fline;
    
        // 第一次 pop():栈为空,会输出 "Empty!!"
        test = fline.pop();
        showte(test);
    
        fline.pull(one);
        fline.pull(two);
        fline.pull(three);
    
        test = fline.pop();
        showte(test);
        test = fline.pop();
        showte(test);
        test = fline.pop();
        showte(test);
    
        return 0;
    }
    

    三、关键修改说明

    1. 修复 pop() 方法中的数组越界问题

      • line[num] 改为 line[num - 1],因为 num 表示当前栈中元素的数量,而索引应为 num - 1
      • pop() 中,使用 --num 来减少计数器。
    2. 添加了 pop() 方法的默认返回值

      • 当栈为空时,返回一个默认的 Item,避免未定义行为。
    3. 初始化 total

      • 在构造函数中显式初始化 total0.0
    4. 修复 main() 中的逻辑顺序

      • 在调用 pop() 之前先进行 pull() 操作,确保栈中有数据。

    四、建议优化点

    • 使用 std::string 替代 char[50]
      如果你需要更安全和灵活的字符串处理,可以将 fullname 改为 std::string

    • 增加异常处理或错误检查
      对于生产环境的代码,建议增加更多错误处理机制。


    五、总结

    主要错误在于 pop() 方法中对数组索引的错误访问,以及 pop() 方法缺少默认返回值。

    以下是修改后的关键部分:

    const Item Stack::pop()
    {
        if (is_void())
        {
            cout << "Empty!!" << endl;
            return {"", 0.0}; // 默认返回
        }
        else
        {
            total += line[num - 1].payment;
            cout << "Now, total payment is: " << total << endl;
            return line[--num];
        }
    }
    

    如果你还有其他疑问,欢迎继续提问!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月5日
  • 已采纳回答 1月28日
  • 创建了问题 1月28日