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);
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);
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;
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)
{
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;
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;
printf("in=%f\n", fenxi(str));
system("pause");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)