weixin_43844798
weixin_43844798
采纳率0%
2018-11-29 02:07

16进制计算器代码输入的16进制不能转换为十进制

#include
#include
#include
#include
#define STACK_INIT_SIZE 100
#define SIZE_INCREMENT 5
using namespace std;
typedef struct
{
int base;
int *top;
int stacksize;
}SqStack,*SQSTACK;
int StackInit(SQSTACK s)
{
s->base=(int
)malloc(STACK_INIT_SIZE * sizeof(int));
if(!(s->base))
exit(0);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return 1;
}
int PUSH(SQSTACK s, int e)
{
if((s->base+s->stacksize)==s->top)
{
s->base=(int *)realloc(s->base,(SIZE_INCREMENT+s->stacksize)*sizeof(int));
s->top=s->base+s->stacksize;
s->stacksize+=SIZE_INCREMENT;
}
*(s->top)=e;
s->top+=1;
return 1;
}

int POP(SQSTACK s, int p)
{
if(s->base==s->top)
return 0;
*p=
(s->top-1);
s->top--;
return 1;
}
int StackLength(SQSTACK s)
{
return (s->top-s->base);
}
int StackFree(SQSTACK s)
{
free(s->base);
s->top=s->base=NULL;
return 1;
}

class Hexadecimal
{
public:
Hexadecimal()
{
numstr='\0';
}
int otherToDec(int sys, char* in_str);
char* decToOther(unsigned int num,int sys);
void parenthesisMatch(char expr);
int pri(char op);
char * toPostfix(char * infix);
int calc(int d1,char op,int d2);
int eval(char
postfix);
char* getnumstr(void);
void setnumstr(char str);
/*bool ishex(char op);
/
//char* toString();
private:
char * numstr;
};

int Hexadecimal::otherToDec(int sys, char* in_str)
{
int i,j,length,start=0;
unsigned long sum=0,pow;
int *in_bit;

length=strlen(in_str);
if(!(in_bit=(int*)malloc(sizeof(int)*length))){
cout<<"Allocation error in otherToDec()!";
exit(0);
}
if(in_str[0]=='-')
start++;
j=0;
for(i=length-1;i>=start;i--)
{
if(in_str[i]>='0' && in_str[i]<='9')
in_bit[j]=in_str[i]-'0';
else if(in_str[i]>='A' && in_str[i]<='F')
in_bit[j]=in_str[i]-'A'+10;
else if(in_str[i]>='a' && in_str[i]<='f')
in_bit[j]=in_str[i]-'a'+10;
else
exit(0);
j++;
}
length-=start;
for(i=0;i {
if(in_bit[i]>=sys)
{
cout<<"Invalid number!";
exit(0);
}
for(j=1,pow=1;j<=i;j++)
pow*=sys;
sum+=in_bit[i]*pow;
}
return sum;
}

char* Hexadecimal::decToOther(unsigned int num,int sys)
{
SqStack s;
int rem,i,length,num1,inc=1;
char out,*p;
if(!StackInit(&s))
exit(0);
do{
if((int)num<(int)sys)
{
rem=num;
PUSH(&s,rem);
break;
}
else
{
rem=num%sys;
PUSH(&s,rem);
num=num/sys;
}
}while(num);
if(sys==16)
inc++;
length=StackLength(&s);
if(!(out=(char
)malloc(sizeof(char)*length+inc)))
{
cout<<"Allocation error in decToOther()!";
exit(0);
}
p=out;
if(sys==16)
for(i=1;i<=length;i++)
{
POP(&s,&num1);
if(num1<10)
*p++=num1+'0';
else
*p++=num1+'A'-10;
}
StackFree(&s);
*p='\0';
return (out);
}

void Hexadecimal::parenthesisMatch(char *expr)
{
SqStack s;
int i,len,no;
if(!StackInit(&s))
exit(0);
len=strlen(expr);
for(i=1;i<=len;i++)
{
if(expr[i-1]=='(')
PUSH(&s,i);
else if(expr[i-1]==')')
{
if(POP(&s,&no))
cout<<"parathesis matching:"<<no
<<"--"<<i<<endl;
else
{
cout<<"The No."<<no<<" left parathes is not match with right parathesis!"<<endl;
continue;
}

}
}
while(StackLength(&s)>0)
{
POP(&s,&no);
cout<<"The No."<<no<<" left parathes is not matching with a right one!"<<endl;
}
StackFree(&s);
}

int Hexadecimal::pri(char op)
{
switch(op)
{
case '+':
case'-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}

char * Hexadecimal::toPostfix(char * infix)
{
int length=strlen(infix);
char * stack,*buf,*p;
char flag=0;
char op;
int i,top=0;
if(!(stack=(char*)malloc(sizeof(char)*length)))
{
cout<<"Allocation error in toPostfix()--stack!"< exit(0);
}
if(!(buf=(char*)malloc(sizeof(char)*(length*2))))
{
cout exit(0);
}
p=buf;
for(i=0;i {
op=infix[i];
switch(op)
{
case '(':
if(top {
top++;
stack[top]=op;
}
flag=0;
break;
//case '-':
case '+':
//
case '*':
case '/':
while(pri(stack[top])>=pri(op))
{
p++=stack[top];//save the operator in string
top--;
flag=0;//flag=1 mean postfix can output number
}
if(top<length)
{
top++;
stack[top]=op;
if(flag==1)
{
*p++=',';
/*while(*p-- != '\0' || *p-- != ',')//store a operand
{
char * temp='\0';
*temp++=*p;
}
/
flag=0;
}
}
break;
case '-':
if(i==0)
{
i++;
break;
}
else
{
while(pri(stack[top])>=pri(op))
{
p++=stack[top];//save the operator in string
top--;
flag=0;//flag=1 mean postfix can output number
}
if(top<length)
{
top++;
stack[top]=op;
if(flag==1)
{
*p++=',';
/*while(*p-- != '\0' || *p-- != ',')//store a operand
{
char * temp='\0';
*temp++=*p;
}
/
flag=0;
}
}
}
break;
case ')':
while(stack[top]!='(')
{
p++=stack[top];
top--;
}
flag=0;
top--;
break;
default:
/
if(isalpha(op))
{
if(op>='0' && op<='9')
op=op-'0';
else if(op>='A' && op<='F')
op=op-'A'+10;
else if(op>='a' && op<='f')
op=op-'a'+10;
}*/
if(isdigit(op) || (isalpha(op)&&(tolower(op)<='f'||tolower(op)>='a')))//prevent the invalid hexadeciaml number inputting
{
*p++=op;
flag=1;
}
else
{
cout<<"Invalid expression!\n";
exit(0);
}
break;
}
}
while(top>0)
{
*p++=stack[top];//pop out the operator that in stack
top--;//modifify the stack pointer
}
delete stack;
stack=NULL;
*p='\0';
return (buf);
}

int Hexadecimal::calc(int d1,char op,int d2)
{
switch(op)
{
case '+':
return d1+d2;
case '-':
return d1-d2;
case '*':
return d1*d2;
case '/':
if(d2==0)
{
cout<<"NaN!(Denominator is zero.)";
exit(0);
}
else
return d1/d2;
}

return 0;
}
int Hexadecimal::eval(char* postfix)
{
int stack,num,k=1;
int i,length,top=0,dec=0,flag;
char token;
length=strlen(postfix);
if(!(stack=(int
)malloc(sizeof(int)*length)))
{
cout<<"Allocation error in eval--stack"<<endl;
exit(0);
}
num=0;
for(i=0;i<length;i++)
{
token=postfix[i];

switch(token)
{
case '+':

case '*':
case '/':
if(top <length && flag==1)
{
top++;
stack[top]=(int)num;
num=0;
}//overload a function to implement convert
stack[top-1]=calc(/*otherToDec(10,*/stack[top-1],token,/*otherToDec(10,*/stack[top]);

top--;
dec=0;
flag=0;
break;
case '-':
if(i==0)
{
i++;
break;
}
else
{
top++;
stack[top]=(int)num;
num=0;
}
stack[top-1]=calc(/*otherToDec(10,*/stack[top-1],token,/*otherToDec(10,*/stack[top]);

top--;
dec=0;
flag=0;
break;
default:
if(token == ',')
{
if(top<length)
{
top++;
stack[top]=(int)num;
num=0;
dec=0;
break;
}
}
else if(token =='.')
{
k=1;
dec=1;
break;
}
if(dec==1)
{
k=k*1;
num=num+(token-'0')*k;
}
else
{
num=num*10+token-'0';
}
flag=1;
break;
}
}
return stack[top];
}
char* Hexadecimal::getnumstr(void)
{
return numstr;
}
void Hexadecimal::setnumstr(char *str)
{
numstr=str;
}

char* toLower(char * str);
/* 1.thre problem in dealing with the first '-'.
2.if enter a char(a-f), output will become negative number.*/

int main()
{
char str[100]={'c'};
Hexadecimal hex;
cout<<"Enter a arithematic: \n";
cin>>str;

hex.parenthesisMatch(str);//parathesis matching
char *temp='\0';
while(str !=0 && strcmp(toLower(str),"quit"))
{

hex.setnumstr(str);
hex.parenthesisMatch(str);
int sum=hex.eval(hex.toPostfix(hex.getnumstr()));//maybe this exist a problem,because the sum is a valid number---very big!
int i=0;
temp=str;
while(temp!=0 && i<(int)strlen(temp) && strcmp(toLower(temp),"quit"))
{
cout<<temp[i]<<endl;
i++;
}

cout<<"="<<hex.decToOther(sum,16);

if(!strcmp(toLower(str),"quit"))
{
exit(1);
}
if(!*str)
{
cout<<"Space arithematic!"< continue;
}
cout cin>>str;

cout<<endl;
}

//char * temp=;

//delete temp;
//temp=NULL;
return 0;
}

char* toLower(char * str)
{
int i=0;
while(i <(int)(strlen(str)))
{
str[i]=tolower(str[i]);
i++;
}
return str;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

相关推荐