您的位置:首页 > 理论基础 > 数据结构算法

数据结构 (栈):进制转换

2019-03-24 15:04 78 查看

 数据结构实验之栈一:进制转换

输入一个十进制非负整数,将其转换成对应的 R (2 <= R <= 9) 进制数,并输出。

Input

第一行输入需要转换的十进制非负整数;
第二行输入 R。

Output

输出转换所得的 R 进制数。

Example Input

1279
8

Example Output

2377

c++ 代码如下1:

[code]#include"stdio.h"

#include<stdlib.h>

#include<iostream>

using namespace std;

typedef int elemtype; //声明 elemtpye 代表 int类型 元素类型

typedef int status;	//声明			   状态

#define MAXSIZE 100

#define OVERFLOW -2

#define another 50

#define true 1

#define false 0

typedef struct{		//命名一个新的类型名代表结构体类型

//顺序栈的类型定义

elemtype *base;

elemtype *top;

int stacksize;

}Sqstack;

status isEmpty(Sqstack &S){	//判断是否 空栈

if(S.top == S.base)		//判空操作,top = base;

return true;

else

return false;

}

void initStack(Sqstack &S){	//初始化操作

S.base = new elemtype[MAXSIZE];

S.top = S.base;

S.stacksize = MAXSIZE;

}

elemtype getTop(Sqstack &S){	//取栈顶操作

if(S.base == S.top)

return false;

else

return *(S.top-1);

}

void Push(Sqstack &S, elemtype e){	//入栈操作

if(S.top-S.base >= S.stacksize){

S.base = (elemtype *)realloc(S.base,(another+S.stacksize)*sizeof(elemtype));

S.top = S.base + S.stacksize;

S.stacksize += another;

}

*S.top++ = e;

}

int Pop(Sqstack &S, elemtype &e){	//出栈操作

return e = *--S.top;

}

int main(){

int n, r;

Sqstack S; 		//原型int的类结构体

initStack(S);	//初始化函数

scanf("%d %d", &n, &r);

if(n < 0)		// n < 0

printf("-"),n = -n;	//n = 0

if(n == 0)

printf("0");

while(n){

int cnt = n%r;	//求余数 传过来是一个一个

n = n/r;	//除

Push(S,cnt);

}

while(!isEmpty(S)){	//结合判空操作

int cnt = Pop(S, cnt);

printf("%d",cnt);

}

printf("\n");

return 0;

}
[code]
#include<iostream>

#include<stdlib.h>

using namespace std;

const int maxsize = 100000;

class Stack

{

int data[maxsize];

public:

int top;

Stack();

bool isEmpty();

bool isFull();

void Push(int);

int Pop();

void Print();

void Clear();

};

Stack::Stack()

{

top = -1;

}

bool Stack::isEmpty()

{

if (top == -1)   return true;

else return false;

}

bool Stack::isFull()

{

if (top== maxsize - 1)   return true;

else return false;

}

void Stack::Push(int e)

{

if (isFull())  exit(0);

else data[++top] = e;

}

int Stack::Pop()

{

if (isEmpty())  exit(0);

else return data[top--];

}

void Stack::Print()

{

while (top!=-1)

cout << Pop();

cout << endl;

}

int main()

{

Stack S;

Stack();

int n, m; cin >> n >> m;

if (n == 0)  cout << "0" << endl;    //注意当n==0时的情况

else

{

while (n)

{

S.Push(n%m);

n = n / m;

}

S.Print();

}

return 0;

}

 

c语言代码如下2:

如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。

[code]
#include<stdio.h>

#include<string.h>

int main()

{

int n,r,i;

while(scanf("%d %d",&n,&r)!=EOF)

{

if(n<0)

{

printf("-");n=-n;

}

if(n==0){printf("0\n");continue;}

int c=0,a[100];

while(n)

{

a[c]=(n%r);

c++;

n/=r;

}

for(i=c-1;i>=0;i--)

{

if(a[i]>=10)

{

printf("%c",'A'+a[i]-10);

}

else printf("%d",a[i]);

}

printf("\n");

}

}

 

原文:https://blog.csdn.net/qq_39051129/article/details/76736618 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐