Code C đổi số hệ cơ số 10 sang các hệ cơ số khác

/*----------------------------------------------
Chuong trinh doi co so he 10 ra co so n bat ky
-----------------------------------------------*/
#include <conio.h>
#include <stdio.h>
typedef int ElementType;
struct Node
{
 ElementType Element;
 Node *Next;
};
typedef Node *Stack;
typedef Node *Position;
void MakeNullStack(Stack S)
{
 S->Next=NULL;
}
Position Pop(Stack S)
{
 return S->Next;
}
void Push(Stack S,ElementType value)
{
 Node *newnode=new Node;
 newnode->Element=value;
 newnode->Next=Pop(S);
 S->Next=newnode;
}
void InsertStack(Stack S)
{
 int sophantu=1;
 while(1)
 {
  ElementType value;
  printf("Nhap phan tu thu %d: ",sophantu);
  scanf("%d",&value);
  if(value==0)
   break;
  Push(S,value);
  sophantu+=1;
 }
}
void DoiCoSo(int X10,int coso_n)
{
 if(coso_n!=1)
 {
  int xtam=X10;
  Stack S;
  MakeNullStack(S);
  while(xtam>=1)
  {
   Push(S,xtam%coso_n);
   xtam=(int)xtam/coso_n;
  }
  Position P=Pop(S);
  while(P!=NULL)
  {
   if(coso_n!=16)
    printf("%d",P->Element);
   else
   {
    if(P->Element<=9)
     printf("%d",P->Element);
    else
     {
      switch(P->Element)
      {
       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;
       }
      }
     }
   }
   P=P->Next;
  }
 }
 else printf("Co so phai khac 1");
}
int main()
{
 //clrscr();
 printf("Chuong trinh doi co so\n");
 int sohe10,heso;
 printf("Nhap so can doi (he 10): ");
 scanf("%d",&sohe10);
 printf("Doi ra he so: ");
 scanf("%d",&heso);
 DoiCoSo(sohe10,heso);
 getch();
}

 

nosomovo

Chia sẻ lên