C任意進制轉(zhuǎn)換程序_第1頁
C任意進制轉(zhuǎn)換程序_第2頁
C任意進制轉(zhuǎn)換程序_第3頁
免費預(yù)覽已結(jié)束,剩余17頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、C 任意進制轉(zhuǎn)換程序C 語言寫的一個任意進制轉(zhuǎn)換的程序,不使用庫提供的轉(zhuǎn)換函數(shù) . / scale.cpp : 定義控制臺應(yīng)用程序的入口點。/ #include "stdafx.h"#include#include#include#include#include#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_

2、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 *

3、 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_ou

4、t,char outputdata);void CleanScreen(); int _tmain(int argc, _TCHAR* argv) int flg = 0;char csWIDTH; memset(cs,0x00,sizeof(cs); while(1)printf("-n");printf(" Data Transform Between Two Scales n");printf(" -n"); /*進制轉(zhuǎn)換主函數(shù)*/if(!Scale()break;printf("-n");printf(&q

5、uot;input any key and press 'Enter' key to continue, 'Z' for exit:");if(!GetData(cs,WIDTH,flg) /*判斷是否繼續(xù)執(zhí)行下一次轉(zhuǎn)換*/break;system("cls"); /* 清屏 */ return 0; bool Scale()int scale_in,scale_out;char inputdataDATAWIDTH;char outputdataDATAWIDTH; /* 輸入源數(shù)據(jù)進制 */ if(!GetSourceScale

6、(&scale_in)return false; /* 輸入源數(shù)據(jù) */ if(!GetSource(scale_in,inputdata)return false; /* 輸入目標(biāo)數(shù)據(jù)進制 */ if(!GetObjectScale(&scale_out)return false; /* 進行進制轉(zhuǎn)換 */ScaleChange(scale_in,inputdata,scale_out,outputdata); return true; bool GetSourceScale(int * scale_in) /*輸入源數(shù)據(jù)進制*/char temparrayWIDTH;mem

7、set(temparray,0x00,sizeof(temparray); printf("Input sourcedata 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) /*輸入目標(biāo)數(shù)據(jù)進制*/char temparrayWIDTH;

8、memset(temparray,0x00,sizeof(temparray); printf("Input objectdata 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) /*輸入數(shù)據(jù)*/int i,icount

9、;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 inp

10、ut again('Z' for exit): n"); /*輸入進制或者源數(shù)據(jù)時,中間有空格,則重新輸入*/i = 0;icount = 0;spaceFlag = true;fflush(stdin);continue;elsebreak; if (i = CONCOUNT - 1) /*判斷是否輸入數(shù)據(jù)過長*/if (flg)CleanScreen();printf("ERROR! Too many characters.Please input again('Z' forexit): n");memset(data,0x00

11、,sizeof(data);fflush(stdin);i = 0;continue;elsebreak; if (c = CHAR_0 && !strcmp(data,STRING_0)continue; datai = c; i+;spaceFlag = false; datai = '0' p = data;if (*p = CHAR_0 && strlen(p) > 1)p+;strcpy(data,p); if (strlen(data) && !strcmp(data,STRING_Z)return false;

12、return true; /* 輸入數(shù)據(jù)進制 */bool GetScale(char temp)int i,num,kbn_flg;char scalewidthWIDTH;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) /*輸入數(shù)據(jù) */return false;s = scalewidth; if (

13、!strlen(s) /*判斷是否輸入數(shù)據(jù)*/CleanScreen();printf("ERROR! Please input the scale(one of 2,8,10,16. 'Z' forexit): n");continue; i = 0; while (*s)if (*s >= CHAR_0 && *s <= CHAR_9) /*判斷是否輸入整數(shù)*/tempi = *s;i+;s+;flag = true;elseCleanScreen();printf("ERROR! Please input the

14、scale(one of 2,8,10,16. 'Z' forexit): n");flag = false;break;tempi = '0' num = 0;if (flag)num = atoi(temp);if (!(num = CONST_2 | num = CONST_8 | num = CONST_10 | num = CONST_16) /* 判斷是否輸入合法整數(shù) */CleanScreen();printf("ERROR! Please input the scale(one of 2,8,10,16. 'Z'

15、; forexit): n");flag = false;return true; /* 輸入源數(shù)據(jù) */bool GetSource(int in_scale,char inputdata)int i,kbn_flg;char * s;char sourcedataDATAWIDTH;bool flag; kbn_flg = 1;flag = false; printf("Please input one %d scale data('Z' for exit):n",in_scale);while (!flag)memset(sourcedata

16、,0x00,sizeof(sourcedata); memset(inputdata,0x00,sizeof(inputdata); if(!GetData(sourcedata,DATAWIDTH,kbn_flg) /* 輸入數(shù)據(jù) */ return false;fflush(stdin); s = sourcedata; if (!strlen(s) /*判斷是否輸入數(shù)據(jù)*/CleanScreen();printf("ERROR! The input is not available!Please input again('Z'for exit): n"

17、);continue; i = 0;while(*s) /* 判斷是否輸入合法數(shù)據(jù)*/if (in_scale >= CONST_2 && in_scale <= CONST_10) && ( CHAR_0 <= *s && *s < in_scale + CHAR_0) inputdatai = *s;i+;s+;flag = true;else if (in_scale = CONST_16)&& (CHAR_0 <= *s && *s <= CHAR_9)| (CHAR_A

18、 <= *s && *s <= in_scale + CHAR_A - CONST_10- 1)| (CHAR_a <= *s && *s <= in_scale + CHAR_a - CONST_10 - 1)inputdatai = *s; i+;s+;flag = true;elseCleanScreen();printf("ERROR! The input is not available!Please input again('Z' for exit):n");flag = false; br

19、eak;inputdatai = '0' if (OtherToDeci(in_scale,inputdata) >(double)MAX)CleanScreen();printf("ERROR! The input is not available!Please inputagain(less than ");switch (in_scale)case CONST_2: printf("32 bits 1.");break;case CONST_8: printf("37777777777."); break;

20、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; /* 其他進制轉(zhuǎn)換為十進制 */double OtherToDeci(int scale_in,char inputdata)int i,len,midint;double middbl,temp; len = strlen

21、(inputdata); temp = 0.00; for(i = 0;i < len; i+) midint = inputdatai; 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*/elsemidint = midint - CHAR_a + C

22、ONST_10; /*a-z*/ middbl = midint * pow(scale_in,len-i-1); temp = temp + middbl;return temp; /* 十進制轉(zhuǎn)換為其他進制 */void DeciToOther(unsigned long deci,int scale_out,charoutputdata)int m,n; m = 0;n = 0; while (deci)m = deci % scale_out;if (m > CONST_9)outputdatan = m + CHAR_A - CONST_10;elseoutputdatan =

23、 m + CHAR_0;deci = deci / scale_out;n+; for (m = 0; m <= n / 2 - 1; m+) /*反序得到目標(biāo)數(shù)據(jù)*/char t;t = outputdatam;outputdatam = outputdatan - 1 - m;outputdatan - 1 - m = t;outputdatan = '0' void PutSource(char inputdata) /*輸出源數(shù)據(jù) */int k;if (!strcmp(inputdata,STRING_0) /*源數(shù)據(jù)為0*/printf("(The

24、source data is: 0)n");elseprintf("(The source data is: ");for( k = 0; k < strlen(inputdata); k+ )printf("%c",inputdatak);printf(")n"); void ScaleChange(int scale_in,char inputdata,int scale_out,char outputdata) /* 進制轉(zhuǎn)換 */int k;據(jù)進制和目標(biāo)數(shù)據(jù)進制相同unsigned long deci; deci = 0; if (scale_in = scale_out) /*原數(shù),不用轉(zhuǎn)換*/if (!strcmp(inputdata,STRING_0)printf("The object data is: n0n");elseprintf("The object d

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論