本文主要是介绍数制转换·数据结构,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
头文件:
#pragma once
#include<iostream>
#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<iostream>
#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);
}
这篇关于数制转换·数据结构的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!