Chuyen Doi Co So Dung Stack
Written By 1 on Chủ Nhật, 20 tháng 5, 2012 | 03:35
// DoiCoSoDungStack.cpp : Defines the entry point for the console application.
// Nhap vao la so nguyen duong; co so la nhi phan (2), bat phan (8), thap luc (16)
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
typedef long ele;
struct Node
{
ele data;
Node*next;
};
typedef Node *Stack;
//khoi tao ngan xep
void CreateStack(Stack &S)
{
S = NULL;
}
//kiem tra rong
bool EmptyStack(Stack S)
{
return (S==NULL);
}
//khoi tao node
Stack CreateNode(ele x)
{
Stack p =new(Node);
if(!p)
{
printf("Loi cap phat.."); return NULL;
}
p->data = x;
p->next = NULL;
}
//chen push
void Push(Stack &S,ele x)
{
Node*p = CreateNode(x);
if(! EmptyStack(S))
p->next = S;
S=p;
}
//lay doi tuong dau va xoa
void Pop(Stack &S,ele &x)
{
if(EmptyStack(S))
{
printf("Rong "); return;
}
Node*p=S;
x=p->data;
S=S->next;
delete (p);
}
//lay ma ko xoa
void Top(Stack S,ele &x)
{
if(EmptyStack(S))
{
printf("rong ");
return;
}
x=S->data;
}
void Doicoso(Stack &S,ele so,ele coso)
{
CreateStack(S);
while(so>0)
{
int du =so%coso;
Push(S,du);
so=so/coso;
}
}
void OutPut(Stack S)
{
if(S==NULL)
{
printf("DS rong ");
return;
}
Stack p = S;
while(p!= NULL)
{
switch (p->data)
{
case 0:printf(" --> %d",p->data ); break;
case 1 :printf(" --> %d",p->data ); break;
case 2:printf(" --> %d",p->data ); break;
case 3: printf(" --> %d",p->data ); break;
case 4 : printf(" --> %d",p->data ); break;
case 5: printf(" --> %d",p->data ); break;
case 6:printf(" --> %d",p->data ); break;
case 7 :printf(" -- >%d",p->data ); break;
case 8:printf(" --> %d",p->data ); break;
case 9 :printf(" --> %d",p->data ); break;
default: // neu so du la 10 thi printf la A, 11 la B ...
printf(" --> %c",'A' + p->data - 10);
}
p=p->next;
}
}
int _tmain(int argc, _TCHAR* argv[]) // khi viet code o C++ tu phien ban 2010 khong dung void main()
{
Stack S;
ele so,coso;
printf("\n Nhap so nguyen duong can doi: ");
scanf("%d",&so);
printf("\n Nhap co so doi: ");
scanf("%d",&coso);
Doicoso(S,so,coso);
printf("so %ld doi tu he 10 sang he %d : " ,so,coso);
OutPut(S);
printf("\n \n");
getch();
return 0;
}
0 nhận xét:
Đăng nhận xét