用堆栈实现由十进制数向其他进制的转换

来源:互联网 发布:我要找网络推广客户 编辑:程序博客网 时间:2024/05/16 06:13
#include"Stack.h"#include<stdio.h>#include"TryStack.h"#include<stdlib.h>#define _CRT_SECURE_NO_DEPRECATE#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1void convert(Stack &s, int N,int n){InitStack(&s);do{Push(&s, N%n);N /= n;} while (N!=0);int i, e;while (!IsEmpty(&s)){e = Pop(&s);if (e > 9)//十六进制时输出字母{e = e + 55;printf("%c", e);}else{printf("%d", e);}}printf("\n");}int main(){Stack s;InitStack(&s);/*for (int i = 1; i < 20; i += 2)Push(&s, i);Print(&s);printf("栈顶的元素为:\n");int k = Top(&s);printf("%d\n", k);Clear(&s);if (IsEmpty(&s))printf("栈为空\n");elseprintf("栈不为空\n");*/    unsigned n, N;//要转换的进制数和要转换的数printf("输入要转换的十进制数和要转换为的进制数:\n");scanf("%d,%d", &N, &n);printf("%d转换为%d进制后为:\n", N, n);convert(s, N, n);system("pause");return 0;}

0 0
原创粉丝点击