当前位置:文档之家› (C语言)10进制转换2,8,16进制

(C语言)10进制转换2,8,16进制

(C语言)10进制转换2,8,16进制作者:vinseven#include"stdio.h"#include"conio.h"#include"malloc.h"#include"windows.h"#define ElemType intvoid menu();void TenToTwo();void TenToEight();void TenToSixteen();void InitStack(struct sNode **HS);void Push(struct sNode **HS,ElemType x);ElemType Pop(struct sNode **HS);ElemType Peek(struct sNode **HS);int EmptyStack(struct sNode **HS);void ClearStack(struct sNode **HS);int ten;/*要输入的10进制数*/int x;/*把将要插入到栈中的元素暂时存进 x 中*/struct sNode hs;struct sNode{ElemType data;struct sNode *next;};void main(){menu();getch();}void menu(){char choice;int flag=1;while(1){printf("\n\t\t\t________________________\n\n\n");printf("\t\t\t 1,10进制转换2进制\n\n\n");printf("\t\t\t 2,10进制转换8进制\n\n\n");printf("\t\t\t 3,10进制转换16进制\n\n\n");printf("\t\t\t 0,退出\n\n\n");printf("\t\t\t________________________\n\n");printf("\t\t\t请输入编号:");while(flag){fflush(stdin);choice=getch();switch(choice){case '1':{system("cls");TenToTwo();flag=0;break;}case '2':{system("cls");TenToEight();flag=0;break;}case '3':{system("cls");TenToSixteen();flag=0;break;}case '0':{printf("\n\t\t\t按任意键退出");getch();exit(0);}default:{printf("\n\t\t\t无此选项可选!请重新输入!\n");flag=1;break;}}}system("cls");flag=1;}}/*10进制转换2进制界面*/void TenToTwo(){printf("\n\t\t\t________________________\n\n\n");printf("\t\t\t 10进制转换2进制界面\n\n\n");printf("\t\t\t________________________\n\n");printf("\t\t\t请输入10进制数:");scanf("%d",&ten);while(ten!=0){x=ten%2;Push(&hs,x);ten=ten/2;}printf("\t\t\t二进制数为:");while(!EmptyStack(&hs))printf("%d",Pop(&hs));getch();}/*10进制转换8进制界面*/void TenToEight(){printf("\n\t\t\t________________________\n\n\n");printf("\t\t\t 10进制转换8进制界面\n\n\n");printf("\t\t\t________________________\n\n");printf("\t\t\t请输入10进制数:");scanf("%d",&ten);while(ten!=0){x=ten%8;Push(&hs,x);ten=ten/8;}printf("\t\t\t八进制数为:");while(!EmptyStack(&hs))printf("%d",Pop(&hs));getch();}/*10进制转换16进制界面*/void TenToSixteen(){int temp;printf("\n\t\t\t________________________\n\n\n");printf("\t\t\t 10进制转换16进制界面\n\n\n");printf("\t\t\t________________________\n\n");printf("\t\t\t请输入10进制数:");scanf("%d",&ten);while(ten!=0){x=ten%16;Push(&hs,x);ten=ten/16;}printf("\t\t\t十六进制数为:");while(!EmptyStack(&hs)){temp=Pop(&hs);switch(temp){case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:printf("%d",temp);break;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;}}getch();}/*关于栈的函数*//*1,初始化链栈为空*/void InitStack(struct sNode **HS) {*HS=NULL;}/*2,向栈中插入一个元素*/void Push(struct sNode **HS,ElemType x){struct sNode *newp;newp=malloc(sizeof(struct sNode));if(newp==NULL){printf("内存动态空间用完,退出运行!");system("pause");exit(0);}newp->data=x;newp->next=*HS;*HS=newp;}/*3,从栈中删除一个函元素并返回它*/ ElemType Pop(struct sNode **HS){struct sNode *p;ElemType temp;if(*HS==NULL){printf("栈空!无法删除!");system("pause");exit(0);}p=*HS;*HS=p->next;temp=p->data;free(p);return temp;}/*4,读取栈顶元素*/ElemType Peek(struct sNode **HS){if(*HS==NULL){printf("栈空!无法读取栈顶结点!");system("pause");exit(1);}return (*HS)->data;}/*5,检查链栈是否为空,空返回1,否则返回0*/ int EmptyStack(struct sNode **HS){if(*HS==NULL)return 1;elsereturn 0;}/*6,清除链栈为空*/void ClearStack(struct sNode **HS){struct sNode *cp,*np;cp=*HS;while(cp!=NULL){np=cp->next;free(cp);cp=np;}*HS=NULL; }。

相关主题