C菜练手,有诸多不足,望高手指点
功能介绍,能处理256字符内含括号表达式,带优先级^幂为最高级,*/%为次高级,+-最低,%为计算余数,如果是小数则转换成整数再取余数
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#define INPUT_LEN 256//定义输入长度
//变量
char input[INPUT_LEN]={"1+((1+2)+1)"};//定义输入数组
char *pinput=input; //数组指针(用于格式化函数类)
//以下是函数
void finput(char *input);//格式化输入
int checkchar(char cchar); //判断有效字符
double calculate(char input[INPUT_LEN]); //计算无括号有优先级
double singlecalculate(double num1,char sign,double num2);//简单计算
double kuohao(char *input);
void main()
{
//输入循环:
printf("本计算器能计算加+、减-、乘*、除/、取模%\n"
"只要你输入一条表达式就能立即得到结果\n"
"清屏输入clear,退出输入quit\n"
"现在请输入一串不超过255个字符的表达式:\n");
while(fgets(input,INPUT_LEN,stdin)!="quit\n")
{
if(strcmp(input,"clear\n")==0)
{
system("cls");
}
else
{
printf("=%lf\n",kuohao(input));
}
}
}
//括号区:
double kuohao(char *input)
{
char copy[INPUT_LEN]={'\0'},kuostr[INPUT_LEN]={'\0'}; //备用字符
int index=0,dexto=-1,to=0;
strcpy(copy,input);
while(true) //大循环
{
index=0;dexto=-1;
while(copy[index]) //检查内括号
{
if(copy[index]=='(')
{
dexto=++index;
}
else if(copy[index]==')'&&dexto!=-1)
{
copy[dexto-1]='\0';
to=0; //缓存数据清零
while(dexto<index) //复制最内括号
{
// printf("aa%c\n",copy[dexto]);
//将最内括号前括号换成结束
kuostr[to++]=copy[dexto++];
}
dexto++;
kuostr[to]='\0';
//printf("括号内数据:%s\n",kuostr);
sprintf(kuostr,"%lf",calculate(kuostr));
for(to=0;kuostr[to];to++);
while(copy[dexto])//将括号后的复制到括号结果后
{
kuostr[to++]=copy[dexto++];
}
kuostr[to]='\0';
strcat(copy,kuostr); //将括号结果连到copy上
//printf("第一括号提取计算结果%s\n",copy);
break;
}else index++;
}
if(dexto==-1)//如果没有全的括号
{
index=0;
dexto=0;
while(copy[index])//去掉残留括号
{
if(copy[index]!='('&©[index]!=')')
{
copy[dexto++]=copy[index++];
}else
{
index++;
}
}
copy[dexto]='\0';
return calculate(copy);//最后计算
}
}
}
//以下为计算区
//格式化输入ok 经过代码优化,已经不用:
/*void finput(char *input)
{
int index=0; //源偏移量
int dexto=0; //格式后偏移量
while(*(input+index)) //格式化循环
{
if(checkchar(*(input+index))) //判断格式
*(input+dexto++)=*(input+index++);
else index++;
}
*(input+dexto)='\0';
}*/
//判断有效字符ok
int checkchar(char cchar)
{
char che[]={'0','1','2','3','4','5','6','7','8','9','.','+','-','*','/','^','%'};
for(int i=0;i<(int)strlen(che);i++)
if(cchar==che[i])return i+1;
return 0;
}
//计算函数
double calculate(char input[INPUT_LEN])
{
int index=0,dexto=0;
bool firsttime =0;
double num[4]={0};
char sign[4]={'s'};
功能介绍,能处理256字符内含括号表达式,带优先级^幂为最高级,*/%为次高级,+-最低,%为计算余数,如果是小数则转换成整数再取余数
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#define INPUT_LEN 256//定义输入长度
//变量
char input[INPUT_LEN]={"1+((1+2)+1)"};//定义输入数组
char *pinput=input; //数组指针(用于格式化函数类)
//以下是函数
void finput(char *input);//格式化输入
int checkchar(char cchar); //判断有效字符
double calculate(char input[INPUT_LEN]); //计算无括号有优先级
double singlecalculate(double num1,char sign,double num2);//简单计算
double kuohao(char *input);
void main()
{
//输入循环:
printf("本计算器能计算加+、减-、乘*、除/、取模%\n"
"只要你输入一条表达式就能立即得到结果\n"
"清屏输入clear,退出输入quit\n"
"现在请输入一串不超过255个字符的表达式:\n");
while(fgets(input,INPUT_LEN,stdin)!="quit\n")
{
if(strcmp(input,"clear\n")==0)
{
system("cls");
}
else
{
printf("=%lf\n",kuohao(input));
}
}
}
//括号区:
double kuohao(char *input)
{
char copy[INPUT_LEN]={'\0'},kuostr[INPUT_LEN]={'\0'}; //备用字符
int index=0,dexto=-1,to=0;
strcpy(copy,input);
while(true) //大循环
{
index=0;dexto=-1;
while(copy[index]) //检查内括号
{
if(copy[index]=='(')
{
dexto=++index;
}
else if(copy[index]==')'&&dexto!=-1)
{
copy[dexto-1]='\0';
to=0; //缓存数据清零
while(dexto<index) //复制最内括号
{
// printf("aa%c\n",copy[dexto]);
//将最内括号前括号换成结束
kuostr[to++]=copy[dexto++];
}
dexto++;
kuostr[to]='\0';
//printf("括号内数据:%s\n",kuostr);
sprintf(kuostr,"%lf",calculate(kuostr));
for(to=0;kuostr[to];to++);
while(copy[dexto])//将括号后的复制到括号结果后
{
kuostr[to++]=copy[dexto++];
}
kuostr[to]='\0';
strcat(copy,kuostr); //将括号结果连到copy上
//printf("第一括号提取计算结果%s\n",copy);
break;
}else index++;
}
if(dexto==-1)//如果没有全的括号
{
index=0;
dexto=0;
while(copy[index])//去掉残留括号
{
if(copy[index]!='('&©[index]!=')')
{
copy[dexto++]=copy[index++];
}else
{
index++;
}
}
copy[dexto]='\0';
return calculate(copy);//最后计算
}
}
}
//以下为计算区
//格式化输入ok 经过代码优化,已经不用:
/*void finput(char *input)
{
int index=0; //源偏移量
int dexto=0; //格式后偏移量
while(*(input+index)) //格式化循环
{
if(checkchar(*(input+index))) //判断格式
*(input+dexto++)=*(input+index++);
else index++;
}
*(input+dexto)='\0';
}*/
//判断有效字符ok
int checkchar(char cchar)
{
char che[]={'0','1','2','3','4','5','6','7','8','9','.','+','-','*','/','^','%'};
for(int i=0;i<(int)strlen(che);i++)
if(cchar==che[i])return i+1;
return 0;
}
//计算函数
double calculate(char input[INPUT_LEN])
{
int index=0,dexto=0;
bool firsttime =0;
double num[4]={0};
char sign[4]={'s'};