重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
//c语言任意进制转换
创新互联-专业网站定制、快速模板网站建设、高性价比平湖网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式平湖网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖平湖地区。费用合理售后完善,十年实体公司更值得信赖。
// 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");
}
}
一次性帮你彻底解决问题! 2-16任意进制转换通用程序。输入一个10进制数,输出它的2-16进制数。 #include #include #define M sizeof(unsigned int)*8 int trans(unsigned n, int d, char s[])/* 函数trans将无符号整数n翻译成d(2=d=16)进制表示的字符串s */ { static char digits[] ="0123456789ABCDEF"; /* 十六进制数字的字符 */ char buf[M+1]; int j, i = M; if(d16) { s[0]='\0'; /* 不合理的进制,置s为空字符串 */ return 0; /* 不合理的进制,函数返回0 */ } buf[i]='\0';/* 这个是关键,它不但是字符串的结束标志,也是(s[j]=buf[i])!='\0'结束的条件 */ do { buf[--i]=digits[n%d]; /*译出最低位,对应字符存入对应工作数组中*/ n/=d; }while(n); /* 将译出在工作数组中的字符串复制到s */ for(j=0;(s[j]=buf[i])!='\0';j++,i++); /* 其中控制条件可简写成s[j]=buf[i] */ return j; } /* 主函数用于测试函数 trans() */ int main() { unsigned int num; int i,scale[]={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; /* 如果只需要2,4,8,16进制转换,去掉其它数字 */ char str[33]; system("cls"); printf("Please input an unsigned digit:\n"); scanf("%u",num); for(i=0;isizeof(scale[0]);i++) { if(trans(num,scale[i],str)) printf("%5d = %s(%d)\n",num,str,scale[i]); else printf("%5d = (%d) Error! \n",num,scale[i]); } system("pause"); return 0;
你先看看吧,思路大概就是这样,不懂的问我。
#includestdio.h
#includemath.h
#includeiostream
using namespace std;
struct point
{
float x;
float y;
};
void translation(point*pt, float xp,float yp,int num)//num代表点的个数
{
for(int i=0;inum;i++)
{
(pt+i)-x+=xp;
(pt+i)-y+=yp;
}
}
void scale(point *pt,float xs,float ys,int num)
{
for(int i=0;inum;i++)
{
(pt+i)-x*=xs;
(pt+i)-y*=ys;
}
}
void rotation(point *pt,float angle,int num)
{
int a[2][2];
angle=angle/180*3.141592657;
a[0][0]=cos(angle);
a[0][1]=-sin(angle);
a[1][0]=sin(angle);
a[1][1]=cos(angle);
point* temp;
for(int i=0;inum;i++)
{
temp-x=(pt+i)-x;
temp-y=(pt+i)-y;
(pt+i)-x=temp-x*a[0][0]+a[0][1]*temp-y;
(pt+i)-y*=temp-x*a[1][0]+a[1][1]*temp-y;
}
}
int main()
{
int i=0,N,mode,angle,xp,yp,xk,yk,num;
cout"please input the number of point "endl;
scanf("%d",N);
num=N;
point pt[10];
while(N--)
{
printf("please input points(x,y):\n");
scanf("%f%f",pt[i].x,pt[i].y);
i++;
}
printf("please input motions\n");
printf("0 stand for translation:\n");
printf("1 stand for scale:\n");
printf("2 stand for rotation:\n");
scanf("%d",mode);
switch(mode)
{
case 0:
printf("please input the translation in x and y direction respectivly:\n");
cinxpyp;
translation(pt, xp,yp,num);
break;
case 1:
printf("please input the scale in x and y direction respectivly:\n");
scanf("%f%f",xk,yk);
scale(pt, xk,yk,num);
break;
case 2:
printf("please input the angle:\n");
scanf("%f",angle);
rotation(pt, angle,num);
break;
}
printf("after translatiton or scale or rotation:\n");
for(int i=0;inum;i++)
printf("%f %f\n",pt[i].x,pt[i].y);
}
以上DSP的函数虽然是汇编的,但在库函数的目录中有相关文档,其中详细解释了C语言的调用规则,还有算法原理,大部分会有相应C代码。你可以去目录中查一下。
汇编是针对DSP进行过优化的,目的是为了实现效率最大化。