Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- typedef struct intStack {
- int * stackArr;
- int count;
- int top;
- int stackMax;
- } intStack;
- intStack* creatStack(int max){
- intStack* stack;
- stack = new intStack;
- if (stack == NULL)
- return 0;
- stack->stackArr = new int[max];
- stack->count = 0;
- stack->top = -1;
- stack->stackMax = max;
- return stack;
- }
- void destroyStack(intStack* stack){
- delete [] stack->stackArr;
- delete stack;
- }
- int pushStack(intStack* stack, int val){
- if (stack->count == stack->stackMax)
- return 0;
- (stack->count)++;
- (stack->top)++;
- stack->stackArr[stack->top] = val;
- return 1;
- }
- int popStack(intStack* stack, int* val){
- if (stack->count == 0)
- return 0;
- *val = stack->stackArr[stack->top];
- (stack->count)--;
- (stack->top)--;
- return 1;
- }
- int topStack(intStack* stack, int* val){
- if (stack->count == 0)
- return 0;
- *val = stack->stackArr[stack->top];
- return 1;
- }
- int isEmpty(intStack* stack){
- return !(stack->count);
- }
- int isFull(intStack* stack){
- return (stack->count == stack->stackMax);
- }
- void printStack(intStack* stack){
- for(int i = 0; i < stack->count; i++)
- cout << stack->stackArr[i] << " ";
- cout << endl;
- }
- void baseChange(int n, int b){
- char* digits = "0123456789ABCDEF";
- intStack* stack = creatStack(100);
- while (n) {
- pushStack(stack, n % b);
- n /= b;
- }
- while(!isEmpty(stack)){
- int temp;
- popStack(stack, & temp);
- cout << digits[temp];
- }
- cout << endl;
- destroyStack(stack);
- }
- int main(){
- int n, b;
- cin >> n >> b;
- baseChange(n , b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment