c语言程序设计计算器答案,题目18:用C语言设计一个简单的计算器,要求能够对输入的数 1.进行+,-,*,/,运算; 2.可以带括号( );...

满意答案

00e27ab806e4881f8254fe7ae8741834.png

s402465903

2013.09.06

00e27ab806e4881f8254fe7ae8741834.png

采纳率:42%    等级:12

已帮助:6714人

#include

#include

#include

int deal(int op1,int op2,char op) //运算

{

switch(op)

{

case '+': op1 += op2; break;

case '-': op1 -= op2; break;

case '*': op1 *= op2; break;

case '/': op1 /= op2; break;

case '^':

{

int temp = op2;

for(int i = 1; i < op2 - 1; i++)

op1 *= temp;

break;

}

}

return op1;

}

char *replace(char *source, char *sub, char *rep) //字符串替换

{

char *result;

char *pc1, *pc2, *pc3;

int isource, isub, irep;

isub = strlen(sub);

irep = strlen(rep);

isource = strlen(source);

if(NULL == *sub)

return strdup(source);

result = (char *)malloc(( (irep > isub) ? (float)strlen(source) / isub* irep+ 1:isource ) * sizeof(char));

pc1 = result;

while(*source != NULL)

{

pc2 = source;

pc3 = sub;

while(*pc2 == *pc3 && *pc3 != NULL && *pc2 != NULL)

pc2++, pc3++;

if(NULL == *pc3)

{

pc3 = rep;

while(*pc3 != NULL)

*pc1++ = *pc3++;

pc2--;

source = pc2;

}

else

*pc1++ = *source;

source++;

}

*pc1 = NULL;

return result;

}

int main()

{

char s[1000],op1[100],op2[100],*p,*temp,*back;

scanf("%s",&s);

temp = s;

back = s;

int pos = -1;

int fin,oper1,oper2,tmp, size;

char op;

while(1)

{

size = (int)strlen(temp);

for(int i = 0; i < size; i++,temp++)

{

if( *temp == '(')

{

p = (temp + 1);

pos =(i + 1); //找出最后一个(的位置

}

}

int k = 0;

if(pos != -1)

{

temp = back;

tmp = pos;

while(*p != ')')

{

p++;

tmp++;

if( *p == '*' || *p == '/')

{

tmp--;

for(;(temp[tmp] >= '0' && temp[tmp] <= '9') || temp[tmp] == '.';)

{

tmp--;

break;

}

}

else

continue;

}

if(tmp != pos && *p != ')')

{

pos = ++tmp;

}

int start = pos;

while( temp[pos] >= '0' && temp[pos] <= '9')

{

op1[k] = temp[pos];

pos++;

k++;

}

op1[k] = '\0';

while((temp[pos] >= '0' && temp[pos] <= '9') || temp[pos] == '.')

pos++;

op = temp[pos];

oper1 = atoi(op1);

pos++;

k = 0;

while( temp[pos] >= '0' && temp[pos] <= '9')

{

op2[k] = temp[pos];

pos++;

k++;

}

op2[k] = '\0';

while((temp[pos] >= '0' && temp[pos] <= '9') || temp[pos] == '.')

pos++;

oper2 = atoi(op2);

int end;

if( temp[pos] != ')')

end = --pos;

else

{

if(temp[start-1] == '(')

{

end = pos;

start--;

}

}

char rep[100];

k = 0;

for(; start <= end; start++,k++)

{

rep[k] = temp[start];

}

rep[k] = '\0';

fin = deal(oper1,oper2,op);

char final[100];

itoa(fin,final,10);

temp = replace(temp,rep,final);

back = temp;

pos = -1;

p = temp;

k = 0;

*rep = NULL;

*final = NULL;

printf("%s\n",replace(temp,rep,final));

}

else

{

temp = back;

p = temp;

tmp = pos;

while(*p != '\0')

{

p++;

tmp++;

if( *p == '*' || *p == '/')

{

tmp--;

for(;(temp[tmp] >= '0' && temp[tmp] <= '9') || temp[tmp] == '.';)

{

tmp--;

break;

}

}

else

continue;

break;

}

if(tmp != pos && tmp != (int)strlen(temp)-1)

{

pos = ++tmp;

}

else

pos = 0;

int start = pos;

while( temp[pos] >= '0' && temp[pos] <= '9')

{

op1[k] = temp[pos];

pos++;

k++;

}

while((temp[pos] >= '0' && temp[pos] <= '9') || temp[pos] == '.')

pos++;

op1[k] = '\0';

op = temp[pos];

oper1 = atoi(op1);

pos++;

k = 0;

while( temp[pos] >= '0' && temp[pos] <= '9')

{

op2[k] = temp[pos];

pos++;

k++;

}

op2[k] = '\0';

while((temp[pos] >= '0' && temp[pos] <= '9') || temp[pos] == '.')

{

pos++;

if(pos >= (int)strlen(temp))

break;

}

oper2 = atoi(op2);

int end = --pos;

char rep[100] = {'\0'};

k = 0;

for(; start <= end; start++)

{

rep[k] = temp[start];

k++;

}

fin = deal(oper1,oper2,op);

char final[100];

itoa(fin,final,10);

temp = replace(temp,rep,final);

back = temp;

for(k = 0; k < (int)strlen(temp); k++)

{

if(temp[k] < '0' || temp[k] > '9' && temp[k] != '.')

break;

}

if(k == (int)strlen(temp))

break;

pos = -1;

p = temp;

k = 0;

printf("%s\n", temp);

}

}

printf("%d\n",fin);

return 0;

}

10分享举报

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值