原创|其它|编辑:郝浩|2008-09-02 11:16:58.000|阅读 2019 次
概述:C语言-进制转换
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
这个程序比较小,在TC2下运行,实现的功能是多种进制间转换。
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
#include<string.h>
#include<conio.h>
#include<graphics.h>
#define STACK_INIT_SIZE 10
#define SIZE_INCREMENT 5
#define ARRAY_SIZE 33
#define NULL 0
#define OK 1
#define ERROR 0
#define OVERFLOW 0
/*#define LEFTUP 201
#define LEFTDOWN 200
#define LINE 205
#define RIGHTUP 187
#define RIGHTDOWN 188 */
/*******************************TYPEDEF******************************/
typedef int SELEMTYPE,Status;
typedef struct{
SELEMTYPE *base;
SELEMTYPE *top;
int stacksize;
}SqStack,*SQSTACK;
/*****************************DATA_STRUCTURE******************************/
Status Init_stack(SQSTACK s){
(*s).base=(SELEMTYPE *)malloc(STACK_INIT_SIZE*sizeof(SELEMTYPE));
if(!(*s).base) exit(OVERFLOW);
(*s).top=(*s).base;
(*s).stacksize=STACK_INIT_SIZE;
return OK;
}/*INIT*/
Status Push_stack(SQSTACK s,SELEMTYPE e){
if((*s).base+(*s).stacksize==(*s).top){
(*s).base=(SELEMTYPE *)realloc((*s).base,(SIZE_INCREMENT+(*s).stacksize)*sizeof(SELEMTYPE));
(*s).top=(*s).base+(*s).stacksize;
(*s).stacksize+=SIZE_INCREMENT;
}
*(*s).top=e;
(*s).top+=1;
return OK;
}/*PUSH*/
Status Pop_stack(SQSTACK s,SELEMTYPE *p){
if((*s).base==(*s).top) return ERROR;
*p=*((*s).top-1);
(*s).top-=1;
return OK;
}/*POP*/
Status Gettop_stack(SqStack s,SELEMTYPE *e){
if(s.base==s.top) return ERROR;
*e=*(s.top-1);
return OK;
}/*GETTOP*/
Status Length_stack(SqStack s){
int y;
if(s.base==s.top) return 0;
/*size=sizeof(SELEMTYPE); */
y=s.top-s.base;
return y;
}/*LENGTH*/
Status Free_stack(SQSTACK s){
free((*s).base);
(*s).top=(*s).base=NULL;
return OK;
}/*FREE*/[SPAN]
/******************************FUNCTIONS************************************/
Status Transform(num,sys,s)
unsigned long num;
int sys;
SQSTACK s;
{
unsigned long dividend=num;
int divisor=sys,rem;
/* int i,*p=NULL,e,length;*/
if(!Init_stack(s)) exit(0);
/***********-ALGORITHM-***********/
do{
if(dividend<sys) { rem=dividend;
Push_stack(s,rem);
break;}
else{
rem=dividend%divisor;
Push_stack(s,rem);
dividend=(dividend-rem)/divisor; /* dividend=dividend/divisor; */
}/*ELSE*/
}while(dividend);
}/*//TRANSFORM//*/
/******************************/
Status Output(SQSTACK s){
int i,length,*p=NULL;
length=Length_stack(*s); /* ERROR: */
for(i=1;i<=length;i++){ /* for(i=1;i<=Length_stack(*s);i++){....} */
Pop_stack(s,p); /* */
if(*p<10) printf("%d",*p);
else printf("%c",*p+55);
}
Free_stack(s);
return OK;
} /*//OUTPUT//*/
/***********************************************/
Status Output_BM(char *mark,SQSTACK s){
int i,j,length,*p=NULL,zerro=0;
int array[32];
length=Length_stack(*s); /*printf("%d",length);*/
if(*mark==45){
for(i=0;i<length;i++){
Pop_stack(s,p);
if(*p) {array[i]=0;zerro=1;}
else array[i]=1;
/* printf("%d",array[i]); */
}
/*length=Length_array(array); printf("%d",length);*/
if(array[length-1]==0) array[length-1]=1;
else { array[length-1]=0;
for(i=1;i<length;i++){
array[length-1-i]+=1;
if(array[length-1-i]==2)
array[length-1-i]=0;
else break;
}/*for*/
}/*else*/
if(length==32) array[0]=1;
/* length=Length_array(array); printf("%d",length);*/
for(i=1,j=1;i<=32-length;i++,j++){
if(!zerro) putchar('0'); /********ATTENTION *p ********/
else putchar('1');
if(!(j%8)) putchar(' ');
/*putchar('1');*/
}
for(i=0;i<length;i++,j++){
printf("%d",array[i]);
if(!((j)%8)) putchar(' ');
/* printf("%d",array[i]); */
}
}/*if*/
else{
for(i=1,j=1;i<=32-length;i++,j++){
putchar('0');
if(j%8==0) putchar(' ');
}
for(i=1;i<=length;i++,j++){
Pop_stack(s,p);
printf("%d",*p);
if(j%8==0) putchar(' ');
}
}/*else*/
Free_stack(s);
}/*//OUTPUT//*/[SPAN]
/*******************************************************/
Status Length_array(int *p){ /*when array[i]!=0,the function is ok;*/
int i;
if(p==NULL) exit(0);
for(i=0;*p++!=NULL;i++);
return i;
}/*LENGTHARRAY*/
Status Input_transD(int sys,unsigned long *p,char *MARK){
int i,j;
unsigned int length;
unsigned long sum=0,pow;
char array_char[ARRAY_SIZE];
int array_int[ARRAY_SIZE];
scanf("%s",array_char);
/* for(i=0;i<ARRAY_SIZE;i++){
array_char[i]=getchar();
if(array_char[i]=='\n') break;
} */
length=strlen(array_char);
if(array_char[0]==45){
*MARK=array_char[0];
for(i=0;i<length-1;i++)
array_char[i]=array_char[i+1];
array_char[length-1]='\0'; /*the statement missing would attract ERROR!*/
}
length=strlen(array_char);
for(i=0;i<length;i++) {
if(array_char[length-1-i]>=48 && array_char[length-1-i]<=57)
array_int[i]=array_char[length-1-i]-48;
else if(array_char[length-1-i]>=65 && array_char[length-1-i]<=90)
array_int[i]=array_char[length-1-i]-55;
else if(array_char[length-1-i]>=97 && array_char[length-1-i]<=122)
array_int[i]=array_char[length-1-i]-87;
else exit(0);
}
for(i=0;i<length;i++){
if(array_int[i]>=sys) {
gotoxy(20,13); printf("Inupt Error!");
gotoxy(20,15); printf("Inupt Error!");
gotoxy(20,17); printf("Inupt Error!");
gotoxy(20,19); printf("Inupt Error!");
gotoxy(20,22);
exit(0);
}
/* if() */
for(j=1,pow=1;j<=i;j++) /**error: for(j=1;j<=i;j++){...} */
pow*=sys;
sum+=array_int[i]*pow;
}
*p=sum;
return OK;
}/*INPUT*/
/*******************************MAIN********************************/
main()
{ SqStack s;
int sys;
int i,j,length;
char MARK=0;
unsigned long P_num,*p=NULL;
char array[32];
do{
clrscr();
textmode(C80);
textbackground(2);
textcolor(14);
for(i=1;i<=24;i++) {
/* gotoxy(1,i);
printf("%d",i); */
for(j=3;j<80;j++){
/* gotoxy(j,1);
printf("%d",j); */
gotoxy(j,i);
putch(' ');
}
}
gotoxy(30,4);
printf("NUMBER TRANSFORM");
textattr(RED|BLINK|BLUE*16);
for(i=8;i<=20;i++)
for(j=6;j<=52;j++){
gotoxy(7,i);
putch(186);
gotoxy(53,i);
putch(186);
if(i%2==0){
gotoxy(j,i);
putch(205);
}
}[SPAN]
gotoxy(9,9);printf("select number system:");
gotoxy(9,11);printf("inport:");
gotoxy(9,13);printf("binary--2:");
gotoxy(9,15);printf("octal---8:");
gotoxy(9,17);printf("dec----10:");
gotoxy(9,19);printf("hex----16:");
gotoxy(56,9);printf("Select from:2,8,10,16");
gotoxy(56,10);printf(" input confine: ");
gotoxy(56,11);printf("binary 0 to 111..1 (32)");
gotoxy(56,12);printf("octal 0 to 37777777777");
gotoxy(56,13);printf("dec 0 to 4294967295");
gotoxy(56,14);printf("hex 0 to FFFFFFFF");
gotoxy(30,9);
scanf("%d",&sys);
switch(sys){
case 2:/* gotoxy(17,11);
Input_transD(sys,p);
break; */
case 8:/* gotoxy(17,11);
Input_transD(sys,p);
break; */
case 10:
case 16:gotoxy(17,11);
Input_transD(sys,p,&MARK);
P_num=*p;
break;
default: exit(0);
}
gotoxy(20,13); /* printf("%lu",*p); */
Transform(*p,2,&s);
/* length=Length_stack(s); */
if(MARK==45) putchar(MARK);
/* { putchar('1');
for(i=1;i<32-length;i++)
putchar('0');
}
else for(i=1;i<=32-length;i++)
putchar('0'); */
Output(&s);
gotoxy(20,15); /* printf("%lu,%lu",*p,P_num); */
Transform(P_num,8,&s);
if(MARK==45) putchar(MARK);
Output(&s);
gotoxy(20,17);
Transform(P_num,10,&s);
if(MARK==45) putchar(MARK);
Output(&s);
gotoxy(20,19);
Transform(P_num,16,&s);
if(MARK==45) putchar(MARK);
Output(&s);
gotoxy(16,21);
printf("BM:");Transform(P_num,2,&s);
/* if(MARK==45) putchar(MARK);*/
Output_BM(&MARK,&s);
gotoxy(20,23);
printf("CONTINIUE? yes--->1,no--->0: ");
scanf("%d",&MARK);
}while(MARK!=0);
restorecrtmode();
}/*main*/
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自:个人博客