头文件:
#pragma once
#include
#define MAXSIZE 100
using namespace std;
class Stack {
private:
int* data;//数组的起始地址
int top;//top指针
public:
//初始化函数
void Init() {
data = new int[MAXSIZE];
top = -1;
}
//输出
void Output() {
cout << "栈长:" << top + 1 << endl;
if (top > -1) {
cout << "栈的内容:" << endl;
for (int i = 0; i <= top; i++) {
cout << data[i] << ",";
}
}
cout << endl;
}
//压栈
void Push(int value) {
top++;
data[top] = value;
//等于data[top++]=value;
}
//出栈
void Pop(int& value) {
value = data[top--];
//等于value=data[top];top--;
}
//销毁
void Destory() {
delete[]data;
}
//返回栈长
int Getlen() {
return top + 1;
}
//判空
bool isEmpty() {
return top == -1 ? true : false;
}
//取栈顶元素
int Gettop() {
return data[top];
}
};
main函数:
#include
#include"Stack.h"
using namespace std;
void fun(int n, int k) {
Stack s;
s.Init();
while (n) {
int t = n % k;
s.Push(t);
n /= k;
}
int temp = s.Getlen();
while (temp--) {
int t;
s.Pop(t);
switch (t)
{
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
default:
cout << t;
break;
}
}
s.Destory();
}
int main()
{
int n, k;
cout << "请输入一个正整数n:";
cin >> n;
cout << "请输入一个大于1小于等于16的正整数k:";
cin >> k;
fun(n, k);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)