📄 编译 刘汉雄 19.cpp
字号:
/*
<实数>->+<无符号实数>|-<无符号实数>|<无符号实数>
<无符号实数>-><无符号整数>.<无符号整数>
<无符号整数>-><数字>{<数字>}
<数字>->0|1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
char sym;
char buffer[100];
int flag=0; //是否为有符号数
int sign=0; //符号标志
int overpoint=0;
char* pbuf=buffer;
static int k=0; //生成中间变量的下标
char* point=NULL; //记录.的位置
double result=0.0;
struct{
char op;
char ag1[8];
char ag2[8];
char sum[8];
}quad[100]; //存贮中间结果
void RealNumber(void);
void USRealNumber(void);
void USInteger(void);
void Number(void);
void read(void);
void error(void);
char* newtemp(void);
int main()
{
printf("Please input the bin code:");
gets(buffer);
puts(buffer);
point=strchr(buffer,'.');
read();
RealNumber();
if (sym=='#')
{
printf("Success\n");
if(sign==0)
printf("The Dec code is :%f",result);
else
printf("The Dec code is :%f",-result);
printf("\n ");
}
else
printf("Failure\n");
return 0;
}
//<实数>->+<无符号实数>|-<无符号实数>|<无符号实数>
void RealNumber(void)
{
if(sym=='+')
{
flag=1;
read();
USRealNumber();
}
else if(sym=='-')
{
flag=1;
sign=1;
read();
USRealNumber();
}
else
USRealNumber();
}
//<无符号实数>-><无符号整数>.<无符号整数>
void USRealNumber(void)
{
USInteger();
if(sym=='.')
{
overpoint=1;
read();
}
else
error();
USInteger();
}
//<无符号整数>-><数字>{<数字>}
void USInteger(void)
{
Number();
while(sym=='0'||sym=='1'){Number();}
}
//<数字>->0|1
void Number(void)
{
double temppow;
if(sym=='0'||sym=='1')
{
char* tp=(char*)malloc(3);
tp=newtemp();
if(!overpoint)
{
int s;
s=(point-pbuf);
temppow=pow(2,s);
printf("(");
printf("*");
printf(",");
printf("%c",sym);
printf(",");
printf("2^%d",s);
printf(",");
printf("%s",tp);
printf(")");
printf(" ");
printf("%s=%c*%f\n",tp,sym,temppow);
printf("(");
printf("+");
printf(",");
printf("%s",tp);
printf(",");
printf("result");
printf(",");
printf("result");
printf(")");
printf(" ");
printf("result=result+%s=%f+%s\n",tp,result,tp);
result=result+(sym-48)*temppow;
}
else
{
int t;
t=point-pbuf+1;
temppow=pow(2,t);
printf("(");
printf("*");
printf(",");
printf("%c",sym);
printf(",");
printf("2^(%d)",t);
printf(",");
printf("%s",tp);
printf(")");
printf(" ");
printf("%s=%c*%f\n",tp,sym,temppow);
printf("(");
printf("+");
printf(",");
printf("%s",tp);
printf(",");
printf("result");
printf(",");
printf("result");
printf(")");
printf(" ");
printf("result=result+%s=%f+%s\n",tp,result,tp);
result=result+(sym-48)*temppow;
}
read();
}
else
error();
}
void read(void)
{
//printf("pbuf=%d,",pbuf-buffer);
sym=*pbuf++;
//printf("sym=%c\n",sym);
}
void error(void)
{
printf("The error:\n");
printf("Location:%d,word:%c\n",pbuf-buffer,sym);
exit(1);
}
char *newtemp(void)
{
char *p;
char m[8];
p=(char *)malloc(8);
k++;
itoa(k,m,10);
strcpy(p+1,m);
p[0]='t';
return(p);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -