C语言写的一个任意进制转换的程序,不使用库提供的转换函数. / scale.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <math.h> #define WIDTH 10 #define DATAWIDTH 50 #define MAX 4294967295 #define CONST_2 2 #define CONST_3 3 #define CONST_8 8 #define CONST_9 9 #define CONST_10 10 #define CONST_16 16 #define CHAR_0 '0' #define CHAR_9 '9' #define CHAR_A 'A' #define CHAR_a 'a' #define CHAR_Z 'Z' #define CHAR_SPACE ' ' #define STRING_0 "0" #define STRING_Z "Z" bool Scale(); bool GetSourceScale(int * scale_in); bool GetObjectScale(int * scale_out); double OtherToDeci(int scale_in,char inputdata[]); void DeciToOther(unsigned long deci,int scale_out,char outputdata[]); bool GetData(char data[],int CONCOUNT,int flg); bool GetScale(char temp[]); bool GetSource(int in_scale,char inputdata[]); void ScaleChange(int scale_in,char inputdata[],int scale_out,char outputdata[]); void CleanScreen(); int _tmain(int argc, _TCHAR* argv[]) { int flg = 0; char cs[WIDTH]; memset(cs,0x00,sizeof(cs)); while(1) { printf("-----------------------------------------------------------------\n"); printf(" Data Transform Between Two Scales \n"); printf(" ----------------------------------------\n"); /*进制转换主函数*/ if(!Scale()) { break; } printf("-----------------------------------------------------------------\n"); printf("input any key and press 'Enter' key to continue, 'Z' for exit:"); if(!GetData(cs,WIDTH,flg)) /*判断是否继续执行下一次转换*/ { break; } system("cls"); /*清屏*/ } return 0; } bool Scale() { int scale_in,scale_out; char inputdata[DATAWIDTH]; char outputdata[DATAWIDTH]; /*输入源数据进制*/ if(!GetSourceScale(&scale_in)) { return false; } /*输入源数据*/ if(!GetSource(scale_in,inputdata)) { return false; } /*输入目标数据进制*/ if(!GetObjectScale(&scale_out)) { return false; } /*进行进制转换*/ ScaleChange(scale_in,inputdata,scale_out,outputdata); return true; } bool GetSourceScale(int * scale_in) /*输入源数据进制*/ { char temparray[WIDTH]; memset(temparray,0x00,sizeof(temparray)); printf("Input source data scale and press 'Enter' key(one of 2,8,10,16. 'Z' for exit): \n"); if(!GetScale(temparray)) { return false; } *scale_in = atoi(temparray); return true; } bool GetObjectScale(int * scale_out) /*输入目标数据进制*/ { char temparray[WIDTH]; memset(temparray,0x00,sizeof(temparray)); printf("Input object data scale and press 'Enter' key(one of 2,8,10,16. 'Z' for exit): \n"); if(!GetScale(temparray)) { return false; } *scale_out = atoi(temparray); return true; } bool GetData(char data[],int CONCOUNT,int flg) /*输入数据*/ { int i,icount; char c; char *p; bool spaceFlag; i = 0; icount = 0; spaceFlag = true; while ((c = getchar()) != 0x0a) { if (spaceFlag && c == CHAR_SPACE) /*去掉前空格*/ { continue; } if (!spaceFlag && c == CHAR_SPACE) /*去掉后空格*/ { icount++; continue; } else if (icount > 0) { if (flg) { CleanScreen(); printf("ERROR! Please input again('Z' for exit): \n"); /*输入进制或者源数据时,中间有空格,则重新输入*/ i = 0; icount = 0; spaceFlag = true; fflush(stdin); continue; } else { break; } } if (i == CONCOUNT - 1) /*判断是否输入数据过长*/ { if (flg) { CleanScreen(); printf("ERROR! Too many characters.Please input again('Z' for exit): \n"); memset(data,0x00,sizeof(data)); fflush(stdin); i = 0; continue; } else { break; } } if (c == CHAR_0 && !strcmp(data,STRING_0)) { continue; } data[i] = c; i++; spaceFlag = false; } data[i] = '\0'; p = data; if (*p == CHAR_0 && strlen(p) > 1) { p++; } strcpy(data,p); if (strlen(data) && !strcmp(data,STRING_Z)) { return false; } return true; } /*输入数据进制*/ bool GetScale(char temp[]) { int i,num,kbn_flg; char scalewidth[WIDTH]; char * s; bool flag; kbn_flg = 1; flag = false; while (!flag) { memset(scalewidth,0x00,sizeof(scalewidth)); memset(temp,0x00,sizeof(temp)); if (!GetData(scalewidth,WIDTH,kbn_flg)) /*输入数据*/ { return false; } s = scalewidth; if (!strlen(s)) /*判断是否输入数据*/ { CleanScreen(); printf("ERROR! Please input the scale(one of 2,8,10,16. 'Z' for exit): \n"); continue; } i = 0; while (*s) { if (*s >= CHAR_0 && *s <= CHAR_9) /*判断是否输入整数*/ { temp[i] = *s; i++; s++; flag = true; } else { CleanScreen(); printf("ERROR! Please input the scale(one of 2,8,10,16. 'Z' for exit): \n"); flag = false; break; } } temp[i] = '\0'; num = 0; if (flag) { num = atoi(temp); if (!(num == CONST_2 || num == CONST_8 || num == CONST_10 || num == CONST_16)) /*判断是否输入合法整数*/ { CleanScreen(); printf("ERROR! Please input the scale(one of 2,8,10,16. 'Z' for exit): \n"); flag = false; } } } return true; } /*输入源数据*/ bool GetSource(int in_scale,char inputdata[]) { int i,kbn_flg; char * s; char sourcedata[DATAWIDTH]; bool flag; kbn_flg = 1; flag = false; printf("Please input one %d scale data('Z' for exit): \n",in_scale); while (!flag) { memset(sourcedata,0x00,sizeof(sourcedata)); memset(inputdata,0x00,sizeof(inputdata)); if(!GetData(sourcedata,DATAWIDTH,kbn_flg)) /*输入数据*/ { return false; } fflush(stdin); s = sourcedata; if (!strlen(s)) /*判断是否输入数据*/ { CleanScreen(); printf("ERROR! The input is not available!Please input again('Z' for exit): \n"); continue; } i = 0; while(*s) /*判断是否输入合法数据*/ { if ((in_scale >= CONST_2 && in_scale <= CONST_10) && ( CHAR_0 <= *s && *s < in_scale + CHAR_0)) { inputdata[i] = *s; i++; s++; flag = true; } else if ((in_scale == CONST_16) && ((CHAR_0 <= *s && *s <= CHAR_9) || (CHAR_A <= *s && *s <= in_scale + CHAR_A - CONST_10 - 1) || (CHAR_a <= *s && *s <= in_scale + CHAR_a - CONST_10 - 1))) { inputdata[i] = *s; i++; s++; flag = true; } else { CleanScreen(); printf("ERROR! The input is not available!Please input again('Z' for exit):\n"); flag = false; break; } } inputdata[i] = '\0'; if (OtherToDeci(in_scale,inputdata) > (double)MAX) { CleanScreen(); printf("ERROR! The input is not available!Please input again(less than "); switch (in_scale) { case CONST_2: printf("32 bits 1."); break; case CONST_8: printf("37777777777."); break; case CONST_10:printf("4294967295."); break; case CONST_16:printf("8 bits F or f."); break; } printf(" 'Z' for exit): \n"); flag = false; continue; } } return true; } /*其他进制转换为十进制*/ double OtherToDeci(int scale_in,char inputdata[]) { int i,len,midint; double middbl,temp; len = strlen(inputdata); temp = 0.00; for(i = 0;i < len; i++) { midint = inputdata[i]; if ( CHAR_0 <= midint && midint <= CHAR_9 ) { midint = midint - CHAR_0; /*0-9*/ } else if ( CHAR_A <= midint && midint <= CHAR_Z ) { midint = midint - CHAR_A + CONST_10; /*A-Z*/ } else { midint = midint - CHAR_a + CONST_10; /*a-z*/ } middbl = midint * pow(scale_in,len-i-1); temp = temp + middbl; } return temp; } /*十进制转换为其他进制*/ void DeciToOther(unsigned long deci,int scale_out,char outputdata[]) { int m,n; m = 0; n = 0; while (deci) { m = deci % scale_out; if (m > CONST_9) { outputdata[n] = m + CHAR_A - CONST_10; } else { outputdata[n] = m + CHAR_0; } deci = deci / scale_out; n++; } for (m = 0; m <= n / 2 - 1; m++) /*反序得到目标数据*/ { char t; t = outputdata[m]; outputdata[m] = outputdata[n - 1 - m]; outputdata[n - 1 - m] = t; } outputdata[n] = '\0'; } void PutSource(char inputdata[]) /*输出源数据*/ { int k; if (!strcmp(inputdata,STRING_0)) /*源数据为0*/ { printf("(The source data is: 0)\n"); } else { printf("(The source data is: "); for( k = 0; k < strlen(inputdata); k++ ) { printf("%c",inputdata[k]); } printf(")\n"); } } void ScaleChange(int scale_in,char inputdata[],int scale_out,char outputdata[]) /*进制转换*/ { int k; unsigned long deci; deci = 0; if (scale_in == scale_out) /*原数据进制和目标数据进制相同,不用转换 */ { if (!strcmp(inputdata,STRING_0)) { printf("The object data is: \n0\n"); } else { printf("The object data is: \n"); for( k = 0; k < strlen(inputdata); k++ ) { printf("%c",inputdata[k]); } printf("\n"); } PutSource(inputdata); } else { if (scale_in == CONST_10) { deci = atol(inputdata); /*源数据是十进制,直接转换为十进制数*/ } else { deci = (unsigned long)OtherToDeci(scale_in,inputdata); /*其他进制转换为十进制*/ } if (scale_out == CONST_10) /*如果目标进制是十进制,直接输出十进制数*/ { printf("The object data is: \n%u \n",deci); } else { if (!deci) /*源数据为0*/ { printf("The object data is: \n0\n"); } else { DeciToOther(deci,scale_out,outputdata); /*十进制转换为其他进制*/ printf("The object data is: \n"); /*输出转换后结果*/ for( k = 0; k < strlen(outputdata); k++ ) { printf("%c",outputdata[k]); } printf("\n"); } } PutSource(inputdata); } } void CleanScreen() /*清屏*/ { static int errcount = 0; errcount++; if(!(errcount % CONST_3)) { errcount = 0; system("cls"); } } |
|