當前位置:編程學習大全網 - 編程語言 - 課程設計:編程實現自然數的十進制、二進制、八進制、十六進制轉換

課程設計:編程實現自然數的十進制、二進制、八進制、十六進制轉換

我給妳解決,呵呵,見下(已經調試成功):

#include<iostream.h>

#include<stdlib.h>

#define STACK_INIT_SIZE 100;

#define STACKINCREMENT 10;

typedef struct

{

int *base;

int *top;

int stacksize;

}SqStack;

SqStack S;

void InitStack(SqStack &S1)

{

S1.base=(int *)malloc(100*sizeof(int));

S1.top=S1.base;

S1.stacksize=STACK_INIT_SIZE;

}

void Push(SqStack &S2,int e)

{

*S2.top++=e;

}

void Pop(SqStack &S3,int &e)

{

e=* --S3.top;

}

void tentotwo(int n)

{

while(n)

{

Push(S,n%2);

n=n/2;

}

cout<<"轉成的二進制數是:"<<endl;

while(S.top!=S.base)

{

int e;

Pop(S,e);

cout<<e;

}

cout<<endl;

}

void tentoeight(int n)

{

while(n)

{

Push(S,n%8);

n=n/8;

}

cout<<"轉成的八進制數是:"<<endl;

while(S.top!=S.base)

{

int e;

Pop(S,e);

cout<<e;

}

cout<<endl;

}

void tentosixteen(int n)

{

while(n)

{

Push(S,n%16);

n=n/16;

}

cout<<"轉成的十六進制數是:"<<endl;

while(S.top!=S.base)

{

int e;

Pop(S,e);

switch(e)

{

case 10: cout<<'A';

break;

case 11: cout<<'B';

break;

case 12: cout<<'C';

break;

case 13: cout<<'D';

break;

case 14: cout<<'E';

break;

case 15: cout<<'F';

break;

}

cout<<e;

}

cout<<endl;

}

void main()

{

InitStack(S);

f:

cout<<"請選擇您想進行的操作:1.十進制轉換成二進制 2.十進制轉換成八進制 3.十進制轉換成十六進制"<<endl;

int a,m;

cin>>a;

switch (a)

{

case 1:

{

cout<<"請輸入壹個十進制數"<<endl;

cin>>m;

tentotwo(m);

break;

}

case 2:

{

cout<<"請輸入壹個十進制數"<<endl;

cin>>m;

tentoeight(m);

break;

}

case 3:

{

cout<<"請輸入壹個十進制數"<<endl;

cin>>m;

tentosixteen(m);

break;

}

}

goto f;

}

  • 上一篇:城市建設綜合信息系統的設計與實現
  • 下一篇:DBGrid相同數據合並成壹個格[最好用Delphi回答]
  • copyright 2024編程學習大全網