08 多文件

多文件

  • 一个源程序可以划分为多个源文件

    • 类声明文件(.h文件)

    • 类实现文件(.cpp文件)

    • 类的使用文件(main()所在的.cpp文件)

  • 利用工程来组合各个文件

ATTENTION

vscode中多文件编译运行

image-20211118215917333 image-20211118220016354

vscode的c++配置及多文件项目编译(主要是后者) - 知乎 (zhihu.com)

(1条消息) vscode 中C++运行编译运行多文件问题总结_audioFrame的博客-CSDN博客

挑把趁手的兵器——VSCode配置C/C++学习环境(小白向) - 知乎 (zhihu.com)

Example 1. 多文件,利用类表示栈

image-20211119183345700

“stack.h”

#include <iostream>
#include <iomanip>
#include <ctype.h>
const int SIZE = 10;
class stack
{
    int stck[SIZE]; //数组,用于存放栈中数据
    int tos;        //栈顶位置(数组下标)

public:
    stack();
    void push(int ch); //将数据ch压入栈
    int pop();         //将栈顶数据弹出栈
    void ShowStack();
};

“stack.cpp”

//#include <iostream>
#include "stack.h"
using namespace std;
stack::stack() { tos = 0; }
void stack::push(int ch)
{
    if (tos == SIZE)
    {
        cout << "Stack is full" << endl;
        return;
    }
    stck[tos] = ch;
    tos++;
    cout << "You have pushed a data into the stack!" << endl;
}
int stack::pop()
{
    if (tos == 0)
    {
        cout << "Stack is empty" << endl;
        return 0;
    }
    tos--;
    return stck[tos];
}
void stack::ShowStack()
{
    cout << "The content of stack: " << endl;
    if (tos == 0)
    {
        cout << "The stack has no data!" << endl;
        return;
    }
    for (int i = tos - 1; i >= 0; i--)
    {
        cout << stck[i] << " ";
    }
    cout << endl
         << endl;
}

“stackmain.cpp”

#include <iostream>
using namespace std;
#include "stack.h"
int main()
{
    cout << endl;
    stack ss;
    int x;
    char ch;
    cout << "<I>------Push data to stack" << endl;
    cout << "<O>------Pop data from stack" << endl;
    cout << "<S>------Show the content of stack" << endl;
    cout << "<Q>------Quit..." << endl;
    while (1)
    {
        cout << "Please select an item: ";
        cin >> ch;
        ch = toupper(ch);
        switch (ch)
        {
        case 'I':
            cout << "Enter the value that you want to push: ";
            cin >> x;
            ss.push(x);
            break;
        case 'O':
            x = ss.pop();
            cout << "Pop " << x << " from stack" << endl;
            break;
        case 'S':
            ss.ShowStack();
            break;
        case 'Q':
            return 0;
        default:
            cout << "You have inputted a wrong item! Please try again!" << endl;
            break;
        }
    }
    return 0;
}

测试结果

<I>------Push data to stack
<O>------Pop data from stack
<S>------Show the content of stack
<Q>------Quit...
Please select an item: I
Enter the value that you want to push: 1
You have pushed a data into the stack!
Please select an item: I
Enter the value that you want to push: 2
You have pushed a data into the stack!
Please select an item: I
Enter the value that you want to push: 6
You have pushed a data into the stack!
Please select an item: S
The content of stack:
6 2 1

Please select an item: O
Pop 6 from stack
Please select an item: S
The content of stack:
2 1

Please select an item: O
Pop 2 from stack
Please select an item: S
The content of stack:
1

Please select an item: Q
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值