用c语言设置计算器,能运行加减乘除。而且运行的时候不能有任何错误
发布网友
发布时间:2022-04-30 19:35
我来回答
共2个回答
热心网友
时间:2023-10-09 19:42
最近自己做的,可以进行乘除优先,括号优先的运算
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void bracket(char *a);/*处理含有括号的式子,将括号部分计算后值放回原数组*/
int digit(double a);/*计算一个浮点型数整数部分的位数*/
int partlength;/*存放括号处字符串的长度*/
void pick(double a,char *ch);/*将浮点数a存入数组ch中(不包括小数点)*/
double part_take1(char a[],int b,int c);/*取出数组a下标b到c的字符所组成的数*/
double multiply(char h[]);/*对数组h中存放的式子进行乘除运算*/
void part_take2(char d[],char *e,int f,int g);/*取出数组d下标f到g的字符并存入数组e中*/
double sum(char express[]);/*对数组express中字符组成的表达式进行运算*/
int main(void)
{
char express[100];/*存放运算式*/
double result;/*存放结果*/
puts("请输入计算式,或输入q退出");
gets(express);/*输入运算式*/
while(express[0]!='q')/*输入q则退出*/
{
bracket(express);/*处理括号*/
result=sum(express);/*计算*/
printf("计算结果为%f\n",result);
gets(express);
}
return 0;
}
double sum(char express[])/*对数组express中字符组成的表达式进行运算*/
{
int i;/*用于控制循环次数*/
int k;/*用于记录+或-的位置*/
int length;/*用于存放express的长度*/
char ch2[100];/*用于存放式子中乘除部分的运算式*/
double part1;/*存放乘除部分的运算式的结果*/
double result=0;/*存放结果*/
k=-1;
length=strlen(express);
for(i=0;i<(int)(strlen(express));i++)
{
if(express[i]=='+'||express[i]=='-')
{
part_take2(express,ch2,k+1,i-1);
part1=multiply(ch2);
if(k==-1)
{
result=result+part1;
}
if(express[k]=='+'&&k>0)
{
result=result+part1;
}
if(express[k]=='-'&&k>0)
{
result=result-part1;
}
ch2[0]='\0';
k=i;
}
}
part_take2(express,ch2,k+1,length-1);
part1=multiply(ch2);
if(k==-1||express[k]=='+')
{
result=result+part1;
}
else
{
result=result-part1;
}
return(result);
}
double part_take1(char a[],int b,int c)
{
char ch1[100];
int j;
int h;
double result;
for(h=b,j=0;h<=c;h++,j++)
{
ch1[j]=a[h];
}
result=atof(ch1);
return(result);
}
double multiply(char h[])
{
int k=-1;
int i;
double part=1;
double num;
int s=strlen(h);
for(i=0;i<s;i++)
{
if(h[i]=='*'||h[i]=='/')
{
num=part_take1(h,k+1,i-1);
if(k<0)
{
part=part*num;
}
if(h[k]=='*'&&k>0)
{
part=part*num;
}
if(h[k]=='/'&&k>0)
{
part=part/num;
}
k=i;
}
}
num=part_take1(h,k+1,s-1);
if(h[k]=='*'||k==-1)
{
part=part*num;
}
else
{
part=part/num;
}
return(part);
}
void part_take2(char d[],char *e,int f,int g)
{
int i;
int j;
for(i=f,j=0;i<=g;i++,j++)
{
e[j]=d[i];
}
e[j]='\0';
}
void bracket(char *a)
{
int length=strlen(a);
int i;
int k;
int j;
int count;
char ch1[100];
double num;
char ch[100];
for(i=0;i<length;i++)
{
if(a[i]=='(')
{
k=i;
}
if(a[i]==')')
{
partlength=i-k+1;
part_take2(a,ch,k+1,i-1);
num=sum(ch);
count=digit(num);
pick(num,ch1);
for(j=1;j<=count+1;j++)
{
a[k]=ch1[j-1];
++k;
}
a[k-1]='.';
while(j<=partlength-count+1)
{
a[k]=ch1[j-1];
j+=1;
k+=1;
}
}
}
}
void pick(double a,char *ch)
{
int i;
int k;
double c;
int d;
for(k=0,i=1-digit(a);i<=partlength;i++)
{
d=i;
c=a*pow(10.0,(double)d);
ch[k]=((int)c)%10+48;
k++;
}
ch[k]='\0';
}
int digit(double a)
{
int num=a;
int i;
for(i=1;num/10!=0;i++)
{
num/=10;
}
return(i);
}追问你确定能运行出来么??这是期末作业,我必须确定它能运行,没有一点错误,还有你能给我截图么.给我看一下运行出来是什么样子的
追答
......你不会自己编译一下吗?
热心网友
时间:2023-10-09 19:42
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
typedef struct
{
int data[MAXSIZE];
int top;
} Stack;
int table[] = {0,0,2,1,0,1,0,2};//查询运算符优先级表
void infix_to_suffix(Stack *sta, char *infix, int *suffix, int *length);
//将后缀表达式转换为结果直接返回
int suffix_to_result(Stack *sta, int *suffix, int length);
void init(Stack *sta);
int main()
{
Stack sta;
int length;
int result;
int sstr[MAXSIZE];
char istr[MAXSIZE];
printf("请输入以 + - * / 组成的四则运算\n(注意负数需要在两旁添加上括号)\n");
scanf("%s", istr);
init(&sta);
infix_to_suffix(&sta, istr, sstr, &length);
init(&sta);
result = suffix_to_result(&sta, sstr, length);
printf("%d\n", result);
return 0;
}
void infix_to_suffix(Stack *sta, char *infix, int *suffix, int *length)
{
int i;
int b = 0;
int j = 0;
int priority = 0;
for (i = 0; i < strlen(infix); )
{
if (infix[i] >= '0' && infix[i] <= '9')
{
b = 0;
while (infix[i] >= '0' && infix[i] <= '9')
{
b = b * 10 + (infix[i] - '0');
i++;
}
suffix[j] = b;
j++;
continue;
}
if (infix[i] == 41)
{
while (sta->data[sta->top] != 40)
{
suffix[j] = sta->data[sta->top];
sta->data[sta->top] = 0;
sta->top--;
j++;
}
sta->data[sta->top] = 0;
sta->top--;
priority = table[sta->data[sta->top] % 10];
i++;
continue;
}
if (infix[i] == 40)
{
sta->top++;
sta->data[sta->top] = infix[i];
priority = table[sta->data[sta->top] % 10];
i++;
continue;
}
if (infix[i] >= 42 && infix[i] <= 47)
{
if (priority >= table[infix[i] % 10])
{
while (priority >= table[infix[i] % 10] && sta->data[sta->top] != 40)
{
suffix[j] = sta->data[sta->top];
sta->data[sta->top] = 0;
sta->top--;
priority = table[sta->data[sta->top] % 10];
j++;
}
sta->top++;
sta->data[sta->top] = infix[i];
priority = table[sta->data[sta->top] % 10];
i++;
}
else
{
if (infix[i] == 45 && sta->data[sta->top] == 40)
{
b = 0;
while (infix[i+1] >= '0' && infix[i+1] <= '9')
{
b = b * 10 + (infix[i+1] - '0');
i++;
}
suffix[j] = b * -1;
sta->data[sta->top] = 0;
sta->top--;
j++;
i += 2;
priority = table[sta->data[sta->top] % 10];
continue;
}
sta->top++;
sta->data[sta->top] = infix[i];
priority = table[sta->data[sta->top] % 10];
i++;
}
}
}
while (sta->top != -1)
{
suffix[j] = sta->data[sta->top];
sta->top--;
j++;
}
*length = j;
}
int suffix_to_result(Stack *sta, int *suffix, int length)
{
int i;
int j;
int result = 0;
for (i = 0; i < length; i++)
{
switch (suffix[i])
{
case 42:
result = sta->data[sta->top - 1] * sta->data[sta->top];
sta->top -= 1;
sta->data[sta->top] = result;
break;
case 43:
result = sta->data[sta->top - 1] + sta->data[sta->top];
sta->top -= 1;
sta->data[sta->top] = result;
break;
case 45:
result = sta->data[sta->top - 1] - sta->data[sta->top];
sta->top -= 1;
sta->data[sta->top] = result;
break;
case 47:
result = sta->data[sta->top - 1] / sta->data[sta->top];
sta->top -= 1;
sta->data[sta->top] = result;
break;
default:
sta->top++;
sta->data[sta->top] = suffix[i];
break;
}
}
return result;
}
//初始化栈空间
void init(Stack *sta)
{
int i;
for (i = 0; i < MAXSIZE; i++)
{
sta->data[i] = 0;
}
sta->top = -1;
}追问亲,你确定能运行出来么??没有一点错误??