本文共 2092 字,大约阅读时间需要 6 分钟。
注:本程序由Visual Studio 2015编写,与VC++6.0稍有区别,复制到VC++6.0注释掉“#include “stdafx.h””即可运行,复制到VS可直接运行。
#include “stdafx.h”#include <stdio.h>
#include
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define UNDERFLOW -2
#define STACK_INIT_SIZE 80
#define STACKINCREMENT 10
typedef int status;
#define ElemType char
typedef struct {
ElemType *base;ElemType *top;int stacksize;
}SqStack;
SqStack S;
status InitStack(SqStack &S) //初始化栈
{
S.base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));if (!S.base)exit(OVERFLOW);S.top = S.base;S.stacksize = STACK_INIT_SIZE;return OK;
}
status Push(SqStack &S, ElemType e) {//入栈
if (S.top-S.base == S.stacksize) { S.base = (ElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(ElemType)); if (!S.base)exit(OVERFLOW); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT;}*S.top++ = e;return OK;
}
status Pop(SqStack &S, ElemType &e) {//出栈
if (S.top == S.base)exit(UNDERFLOW);e = *(S.top=S.top-1);return OK;
}
status StackEmpty(SqStack S) {//是否为空栈
return S.base == S.top;
}
char inttochar(int n) {//整型转化为字符型
switch (n) {case 0: return '0';case 1: return '1';case 2: return '2';case 3: return '3';case 4: return '4';case 5: return '5';case 6: return '6';case 7: return '7';case 8: return '8';case 9: return '9';case 10: return 'A';case 11: return 'B';case 12: return 'C';case 13: return 'D';case 14: return 'E';case 15: return 'F';}
}
void conversion(int data, int r) {//进制转换
ElemType e;InitStack(S);while (data) { Push(S, inttochar(data%r)); data = data / r;}while (!StackEmpty(S)) { Pop(S, e); cout << e;}cout << endl << endl;
}
int main() {
ElemType e;int data, r;cout << "\t\t\t\t*\t\t\t\t\t*";cout << endl << "\t\t\t\t*\t计科1512-02210151232-杨少通\t*" << endl;cout << "\t\t\t\t*****************************************" << endl << endl;cout << "**************十进制转换为R进制***************" << endl << endl;cout << " 请输入十进制数:";cin >> data;cout << " 请输入要转换的进制数(如:2、8、16):";cin >> r;cout << " " << data << "转换为" << r << "进制数为:";conversion(data, r);return 0;
}
如有转载请注明来源: www.dreamload.cn/blog/?p=243&preview=true (洋葱先生)
转载地址:https://blog.csdn.net/Kindergarten_Sir/article/details/108587446 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!