- 左迁
-
既然大家都没发程序,I am coming.
说明:由于幂的运算应该比括号低比乘除要高,所以为避免混淆用幂运算时用括号括起来,比如2+9/3^2-5表示2+(9/3)^2-5,要是想要9除以3^2,这样输入:2+9/(3^2)-5,以免引起歧义。
程序功能强大代码简练。可以对任意数值类型计算,这里全部按double型处理,程序还可以使用大括号{}中括号[]小括号()三种括号,当然优先级是小括号>中括号>大括号>运算符。
要是只想结果取整,将所有double类型改为int即可,不过不主张这样做,既然是计算器,当然要精确计算。
一个完整的c程序如下,程序在win-tc和Dev-c++下都调试通过。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
char s[201];/*全局变量s[]与i分别表示算术表达式字符串以及其当前位置指针变量*/
int i=0;
double proc1(char ch);
void proc2(double *a,char *c);
void str_read(double *a,char *c) /*读取下一个操作数*/
{ int j=0;
char s1[30];
while ((s[i]!=" ")&&(s[i]!="+")&&(s[i]!="-")&&(s[i]!="(")&&(s[i]!=")")
&&(s[i]!="=")&&(s[i]!="^")&&(s[i]!="*")&&(s[i]!="/")&&(s[i]!="[")&&(s[i]!="]")
&&(s[i]!="{")&&(s[i]!="}")) /*操作数结束标志*/
s1[j++]=s[i++];
s1[j]=" ";
*a=atof(s1);
*c=s[i++];
}
void input_str() /*表达式接收模块*/
{ int j,k,t=0;
char c,s1[80];
printf(" 请输入任一算术表达式: ");
while (!t)
{ gets(s);
j=k=0; t=1;
while ((s[j]!="=")&&(s[j]!=" ")&&(t)) /*判断括号配对*/
{if ((s[j]=="(")||(s[j]=="[")||(s[j]=="{"))
switch(s[j])
{ case "(":s1[k++]=")";break;
case "[":s1[k++]="]";break;
case "{":s1[k++]="}";
}
else if ((s[j]==")")||(s[j]=="]")||(s[j]=="}"))
if (s1[--k]!=s[j])
{ printf("输入出错!请重新输入(直接回车退出): ");
t=0;
}
j++;
}
if ((k!=0)&&(t))
{printf("输入出错!请重新输入(直接回车退出): ");
t=0;
}
}
}
void proc2(double *a,char *c)
/*算术运算符之后只有两种情况:要么是各种左括号,要么是操作数*/
{ char ch;
if ((s[i]=="(")||(s[i]=="[")||(s[i]=="{"))
{ switch(s[i])
{ case "(":ch=")";break;
case "[":ch="]";break;
case "{":ch="}";
}
++i;
*a=proc1(ch);
/*计算从当前左括号开始到与其相对应的右括号结束这段表达式的值*/
*c=s[i++]; /*跳过右括号*/
}
else str_read(a,c); /*读取一个操作数*/
}
double proc1(char ch)
/*该过程对算术表达式s从当前位置s[i]开始到表达式结束或s[i]==ch这部分进行求值*/
{ double a1=0,a2,a=0;
char c1="+",c="+",c2="+";
/*给表达式前面加上"0+"(第一操作数与第一操作符)*/
while((c!="=")&&(c!=" ")&&(c!=ch))
{ proc2(&a,&c); /*读取第二操作数与第二操作符.*/
while (c=="^" || c=="*" || c=="/")
{ proc2(&a2,&c2); /*读取第三操作数与第三操作符.*/
switch (c) /*对第二操作符进行运算*/
{ case "*":a=a*a2; break;
case "/":a=a/a2; break;
case "^":a=pow(a,a2);
}
c=c2; /*把第三操作符赋给第二操作符变量*/
}
switch (c1) /*对第一操作符进行运算*/
{ case "+":a1=a1+a;break;
case "-":a1=a1-a;
}
c1=c; /*把第二操作符赋给第一操作符变量*/
}
return(a1); /*返回这部分表达式的值*/
}
int main(void)
{ system("cls");
input_str();
if (s[0]==" ") return 0;
printf("该表达式的值是: ");
printf("%-8f ",proc1(" "));
system("pause");
return 0;
}
- LuckySXyd
-
写好了
不能算小数,根据你的题意来的,如果输入小数,则只取整数部分运算,结果不能为负数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
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] = " ";
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] = " ";
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] = " ";
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 ",replace(temp,rep,final));
}
else
{
temp = back;
p = temp;
tmp = pos;
while(*p != " ")
{
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] = " ";
op = temp[pos];
oper1 = atoi(op1);
pos++;
k = 0;
while( temp[pos] >= "0" && temp[pos] <= "9")
{
op2[k] = temp[pos];
pos++;
k++;
}
op2[k] = " ";
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] = {" "};
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 ", temp);
}
}
printf("%d ",fin);
return 0;
}
- cloud123
-
这个实在有点麻烦,但是不难!!!
曾经做过一个,是用C写的图形界面计算器.....
可运算 符点型数据,带括号,乘方............
另外,还带有 排错功能,错误的式子能辨别出..
这是我学C后的课程设计.......可以去看看,有源码,如果不要图形,就把图形那部分删掉,大概有500多行....
在 QQ787052538 空间里...
- nicehost
-
计算器和括号有什么关系??如果你是想仿一个windows的计算器,那就不是一个“简单”的计算器。考虑括号太烦了。。。
- FinCloud
-
篮灵儿
题意不清
“可以运行括号和幂函数”是什么意思
- 阿啵呲嘚
-
mark