1.字符串去空格处理
实现一:
void spacess(char *str)//删除空格
{
int i = 0;
int j = 0;
while (str[i]!='\0')
{
str[i] = str[j];
if (str[i]!=' ')
{
i++;
}
j++;
}
}
实现二:
void spacess(char *str)//删除空格
{
int i = 0;
int j = 0;
while ((str[i]=str[j++])!='\0')
{
if (str[i]!=' ')
{
i++;
}
}
}
2.提取数值包含小数
int isnum(char ch)//判断是否为数字
{
int is = 0;//默认不是
if (ch >= '0'&&ch <= '9')
{
is = 1;
}
return is;
}
double getnum(char *str,int *pin)//提取数字
{
double value = 0.0;
int index = *pin;
while (isnum(*(str+index)))//处理整数
{
value = value * 10 + (str[index] - '0');
index++;
}
if (*(str+index)=='.')//处理小数
{
double xiaoshu = 1.0;
while (isnum(*(str + ++index)))
{
xiaoshu /= 10;
value += xiaoshu*(*(str + index) -'0');
}
}
*pin = index;//改变数字的位置
return value;
}
3.实现简单的加减法
double fenxi(char *str)//加减法实现
{
double value = 0.0;
int index = 0;//下标地址
value = getnum(str, &index);//获取第一个数据
while (1)
{
char ch = *(str + index);//取出字符
index++;//循环遍历
switch (ch)
{
case '+':
value += getnum(str,&index);
break;
case '-':
value -= getnum(str, &index);
break;
case '\0':
return value;
break;
default:
break;
}
}
}
4.实现简单的加减乘除法
double fenxi(char *str)//乘除加减法实现
{
double value = 0.0;
int index = 0;//下标地址
value = comfenxi(str, &index);//通过乘除获取第一个数据,乘除法优先级高
while (1)
{
char ch = *(str + index);//取出字符
index++;//循环遍历
switch (ch)
{
case '+':
value += comfenxi(str,&index);//判断以后的数据有没有乘除,有的话先计算
break;
case '-':
value -= comfenxi(str, &index);//判断以后的数据有没有乘除,有的话先计算
break;
case '\0':
return value;
break;
default:
break;
}
}
}
double comfenxi(char *str,int *pindex)//乘除法计算
{
double value = 0.0;//保存数据
value = getnum(str, pindex);//获取第一个数据
while (1)
{
if (*(str+(*pindex))=='*')
{
(*pindex)++;//下标移动
value *= getnum(str, pindex);
}
else if (*(str + (*pindex)) == '/')
{
(*pindex)++;//下标移动
value /= getnum(str, pindex);
}
else
{
break;
}
}
return value;
}
5.带括号的加减乘除计算
全都代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <string.h>
double comfenxi(char *str, int *pindex);
double fenxi(char *str);
int isnum(char ch)//判断是否为数字
{
int is = 0;//默认不是
if (ch >= '0'&&ch <= '9')
{
is = 1;
}
return is;
}
char *kuohao(char *str,int *pindex)//处理括号
{
char *pstr = NULL;
int num = 0;//用以计算有多少括号
int leftindex = *pindex;//记录左括号位置
do
{
switch (*(str+(*pindex)))//处理字符
{
case '(':
num++;
break;
case ')':
if (num==0)
{
(*pindex)++;//移动到括号的后面
pstr = malloc(sizeof(char)*(*pindex-leftindex));//分配内存大小
strncpy_s(pstr,*pindex-leftindex,str+leftindex, *pindex - leftindex-1);
// printf("%s\n",pstr);
return pstr;
}
else
{
num--;//处理右括号
}
break;
}
} while (*(str+(*pindex)++)!='\0');//判断字符结尾
}
double getnum(char *str,int *pin)//提取数字
{
double value = 0.0;
int index = *pin;
while (*(str+index)=='(')//检测左括号
{
char *psubstr = NULL;//取出字符串
*pin = ++index;//跳到括号后面
psubstr = kuohao(str, pin);//获取字符串
value = fenxi(psubstr);
// printf("value=%f\n", value);
// free(psubstr);//释放内存
psubstr = NULL;//指针置空,防止野指针
return value;
}
while (isnum(*(str+index)))//处理整数
{
value = value * 10 + (str[index] - '0');
index++;
}
if (*(str+index)=='.')//处理小数
{
double xiaoshu = 1.0;
while (isnum(*(str + ++index)))
{
xiaoshu /= 10;
value += xiaoshu*(*(str + index) -'0');
}
}
*pin = index;//改变数字的位置
//printf("value=%f\n", value);
return value;
}
void spacess(char *str)//删除空格
{
int i = 0;
int j = 0;
while ((str[i]=str[j++])!='\0')
{
if (str[i]!=' ')
{
i++;
}
}
}
double fenxi(char *str)//乘除加减法实现
{
double value = 0.0;
int index = 0;//下标地址
value = comfenxi(str, &index);//通过乘除获取第一个数据,乘除法优先级高
while (1)
{
// printf("ind=%d\n", index);
char ch = *(str + index);//取出字符
index++;//循环遍历
switch (ch)
{
case '+':
value += comfenxi(str,&index);//判断以后的数据有没有乘除,有的话先计算
break;
case '-':
value -= comfenxi(str, &index);//判断以后的数据有没有乘除,有的话先计算
break;
case '\0':
return value;
break;
}
}
}
double comfenxi(char *str,int *pindex)//乘除法计算
{
double value = 0.0;//保存数据
//printf("pindex=%d\n",*pindex);
value = getnum(str, pindex);//获取第一个数据
while (1)
{
if (*(str+(*pindex))=='*')
{
(*pindex)++;//下标移动
value *= getnum(str, pindex);
}
else if (*(str + (*pindex)) == '/')
{
(*pindex)++;//下标移动
value /= getnum(str, pindex);
}
else
{
break;
}
}
return value;
}
void main()
{
char str[1024] = {0};
scanf("%[^\n]s", str);
spacess(str);
int in = 0;
// double a=getnum(str, &in);
// printf("str=%s\n", str);
printf("in=%f\n", fenxi(str));//打印计算结果
system("pause");
}