主题:递归和非递归算法
VC菜鸟
[专家分:0] 发布于 2006-04-23 12:22:00
编写将十进制正整数n转换为k(2=<k=<9)进制数的递归和非递归算法!(C++语言描述)
小弟想请教各位老师这个问题。请给出算法,谢谢各位了![em2]
回复列表 (共14个回复)
11 楼
findlyhl [专家分:280] 发布于 2006-04-28 12:40:00
#include "stdio.h"
#include "malloc.h"
typedef struct node
{
int hl;
struct node *link;
}num,*stlink;
//进制转换
void conversion(int number,int base)
{
stlink p,top=NULL;
do
{
p=(stlink)malloc(sizeof(num)); //进栈
p->hl=number%base;
p->link=top;
top=p;
number=number/base;
}while(number!=0);
while(top!=NULL) //依次退栈
{
p=top;
if(p->hl>=10)
switch(p->hl)
{
case 10: printf("A");break;
case 11: printf("B");break;
case 12: printf("C");break;
case 13: printf("D");break;
case 14: printf("E");break;
case 15: printf("F");break;
}
else
printf("%d",p->hl);
top=top->link;
free (p);
}
printf("\n");
}
void main()
{
int number,base;
printf("input number:\n");
scanf("%d",&number);
printf("input base:\n");
scanf("%d",&base);
printf("%d转换成%d进制为:\n",number,base);
conversion(number,base);
}
12 楼
findlyhl [专家分:280] 发布于 2006-04-28 12:41:00
#include "stdio.h"
#include "malloc.h"
typedef struct node
{
int hl;
struct node *link;
}num,*stlink;
//进制转换
void conversion(int number,int base)
{
stlink p,top=NULL;
do
{
p=(stlink)malloc(sizeof(num)); //进栈
p->hl=number%base;
p->link=top;
top=p;
number=number/base;
}while(number!=0);
while(top!=NULL) //依次退栈
{
p=top;
if(p->hl>=10)
switch(p->hl)
{
case 10: printf("A");break;
case 11: printf("B");break;
case 12: printf("C");break;
case 13: printf("D");break;
case 14: printf("E");break;
case 15: printf("F");break;
}
else
printf("%d",p->hl);
top=top->link;
free (p);
}
printf("\n");
}
void main()
{
int number,base;
printf("input number:\n");
scanf("%d",&number);
printf("input base:\n");
scanf("%d",&base);
printf("%d转换成%d进制为:\n",number,base);
conversion(number,base);
}
13 楼
jay0518 [专家分:3150] 发布于 2006-05-03 12:24:00
//Header1.h
#ifndef HEADER_Header1
#define HEADER_Header1
const int Max=30;
const int DefaultInt=25;
class Stack{
private:
int* data;
int top;
int maxSize;
public:
Stack();
~Stack();
bool FullStack();
bool EmptyStack();
bool PushTop(int element);
int PopTop();
int GetTop();
};
class Conver_NumForm:public Stack{
private:
int num,Base;
public:
Conver_NumForm(int numValue,int baseValue=2);
void non_RecursionConverse();
void RecursionConverse(int Divisor_tempt);
void ShowRecursionResult();
void ShowNonRecResult();
};
#endif
//Source1.cpp
#include <iostream>
#include "Header1.h"
using namespace std;
extern const int Max;
int a[Max],i;
Stack::Stack(){
maxSize=Max;
data=new int[maxSize];
if(data==0){
cout<<endl<<"Allocation failed!";
exit(1);
}
top=0;
}
Stack::~Stack(){
delete[] data;
}
bool Stack::FullStack(){
return top!=maxSize?false:true;
}
bool Stack::EmptyStack(){
return top!=0?false:true;
}
bool Stack::PushTop(int element){
if(FullStack()){
cout<<endl<<"Error happened!";
return false;
}
data[top]=element;
top++;
return true;
}
int Stack::GetTop(){
if(EmptyStack()){
cout<<endl<<"No elements left in Stack!";
return -1;
}
return data[top-1];
}
int Stack::PopTop(){
if(EmptyStack()){
cout<<endl<<"The stack is already empty!Cannot delete the top element.";
return -1;
}
top--;
return data[top];
}
Conver_NumForm::Conver_NumForm(int numValue,int baseValue){
if(numValue<=0){
num=DefaultInt;
}
else
num=numValue;
Base=baseValue;
}
void Conver_NumForm::non_RecursionConverse(){
int temptNum=num,Residue_tempt=temptNum%Base,Divisor_tempt=temptNum/Base;
while(Divisor_tempt!=0){
PushTop(Residue_tempt);
temptNum=Divisor_tempt;
Residue_tempt=temptNum%Base;
Divisor_tempt=temptNum/Base;
}
PushTop(Residue_tempt);
}
void Conver_NumForm::RecursionConverse(int Divisor_tempt){
if(Divisor_tempt!=0){
a[i]=Divisor_tempt%Base;
i++;
RecursionConverse(Divisor_tempt/Base);
}
}
void Conver_NumForm::ShowRecursionResult(){
cout<<endl<<"运用非递归(栈)实现数制转换:\n十进制数"<<num<<" 被转换成"<<Base<<"进制数是:";
while(!EmptyStack()){
cout<<PopTop();
}
}
void Conver_NumForm::ShowNonRecResult(){
cout<<endl<<"运用递归实现数制转换:\n十进制数"<<num<<" 被转换成"<<Base<<"进制数是:";
RecursionConverse(num);
i--;
while(i>=0){
cout<<a[i];
i--;
}
cout<<endl;
}
//main.cpp
#include "stdafx.h"
#include <iostream>
#include "Source1.cpp"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Conver_NumForm myStack(25,2);
myStack.non_RecursionConverse();
myStack.ShowRecursionResult();
myStack.ShowNonRecResult();
return 0;
}
14 楼
zhangsan5421 [专家分:140] 发布于 2006-12-29 11:00:00
[quote]不要把栈说得那么玄乎……
不就是FILO LIFO原则嘛,
数据结构可以用数组来实现:
void print2(int n,int k){
int arr[100];
int i=0;
while(n>k&&i<100){
arr[++i]=n%k;
n/=k;
}
arr[++i]=n;
for(;i>0;printf("%d",arr[i]),i--);
}[/quote]
不错!!!!!
我来回复