点击(此处)折叠或打开
-
//3、输入一个表达式,没有括号,数字在0-9之间,输出计算结果,所有的中间结果化为整形。
-
//例如: 输入:3+8×2/9-2
-
//输出:2
-
//函数原型
-
//public int getMyRet(String str)
-
-
#include<stdio.h>
-
#include<stdlib.h>
-
-
//#include <iostream>
-
//#include <string>
-
//#include <cstring>
-
//using namespace std;
-
-
int get_my_ret(char *, int);
-
-
int
-
main( int argc, char *argv[] )
-
{
-
//输入字符串
-
char my_chars[] = "3+8*2/9-2";
-
int my_ret, my_chars_cnt;
-
my_chars_cnt = sizeof(my_chars)/sizeof(char);
-
-
//显示需计算的字符串
-
int i;
-
char *one_char_p = my_chars;
-
printf("Need to calculate :\n");
-
for(i=0; i<my_chars_cnt; one_char_p++, i++){
-
printf("%c", *one_char_p);
-
}
-
printf("\n");
-
-
//调用计算函数
-
my_ret = get_my_ret(my_chars, my_chars_cnt);
-
-
//显示计算结果
-
printf("\n my_ret is %d\n", my_ret);
-
}
-
-
int
-
get_my_ret(char *my_chars_p, int cnt)
-
{
-
int my_ret;
-
//获取操作符号个数
-
int operations_cnt = 0;
-
int i;
-
char *cpy_my_chars_p = my_chars_p;
-
for(i=0; i<cnt; i++){
-
if(*cpy_my_chars_p == '+' || \
-
*cpy_my_chars_p == '-' || \
-
*cpy_my_chars_p == '*' || \
-
*cpy_my_chars_p == '/'){
-
operations_cnt++;
-
}
-
cpy_my_chars_p++;
-
}
-
-
//申请数字个数
-
int *nums_p_first = (int *) calloc(operations_cnt+1, sizeof(int));
-
char *operations_p_first = (char *) calloc(operations_cnt, sizeof(char));
-
-
//将字符串转化为数字存在数组里
-
int *nums_p = nums_p_first;
-
char *operations_p = operations_p_first;
-
cpy_my_chars_p = my_chars_p;
-
-
for(i=0; i<cnt; i++){
-
if(*cpy_my_chars_p >= '0' && *cpy_my_chars_p <= '9'){
-
*nums_p = *cpy_my_chars_p - '0';
-
nums_p++;
-
}else{
-
*operations_p = *cpy_my_chars_p;
-
operations_p++;
-
}
-
cpy_my_chars_p++;
-
}
-
-
//显示获取的操作数
-
nums_p = nums_p_first;
-
printf("the nums are:\n");
-
for(i =0; i<(operations_cnt+1); i++){
-
printf("%d, ", *nums_p);
-
nums_p++;
-
}
-
printf("\n");
-
-
//显示获取的操作符号
-
operations_p = operations_p_first;
-
printf("the oprations are:\n");
-
for(i =0; i<(operations_cnt); i++){
-
printf("%c, ", *operations_p);
-
operations_p++;
-
}
-
printf("\n");
-
-
//根据两个操作符号,而进行计算
-
char *now_opration_p = operations_p_first;
-
char *next_opration_p = ++operations_p_first;
-
for(i=0; i<operations_cnt; i++){
-
//bad beaf
-
//bad beaf
-
//bad beaf
-
//bad beaf
-
//bad beaf
-
//bad beaf
-
//bad beaf
-
//bad beaf
-
}
-
-
free(nums_p_first);
-
free(operations_p_first);
-
return my_ret;
- }
//强大的网友的答案……难以理解的递归!!!
点击(此处)折叠或打开
-
#include <stdio.h>
-
#include <string.h>
-
#include <stdlib.h>
-
-
//
-
// 封装常用的判断是否为数字的判断函数,如果是返回1否则0
-
//
-
int IsNum(char ch)
-
{
-
return (ch >= '0' && ch <= '9');
-
}
-
-
//
-
// 把指向Calc的INDEX放于全局变量的位置,因为在递归的过程中需要递增
-
//
-
int INDEX = 0;
-
-
-
int Calc(char sub_str[],int first)
-
{
-
//
-
// 1.记录下读取到的符号
-
//
-
char item = sub_str[INDEX];
-
-
char temp[10] = {0};
-
char temp_index = 0;
-
-
//
-
// 2.如果为终结符,返回first
-
//
-
if (item == '=' || item == '\0' || item == ')')
-
{
-
printf("2.return %d\n",first);
-
INDEX++;
-
return first;
-
}
-
-
//
-
// 3.1如果为前括号,
-
//
-
int sub_first = 0;
-
if (item == '(')
-
{
-
INDEX++;
-
printf("3.1sub_first = Calc(%s,%d)\n",sub_str,0);
-
sub_first = Calc(sub_str,0);
-
return Calc(sub_str,sub_first);
-
}
-
-
//
-
// 3.2如果为数字,读取数字作为first送入
-
//
-
else if (IsNum(item))
-
{
-
while (IsNum(sub_str[INDEX]))
-
{
-
temp[temp_index++] = sub_str[INDEX++];
-
}
-
sub_first = atoi(temp);
-
-
printf("3.2Calc(%s,%d)\n",sub_str,sub_first);
-
return Calc(sub_str,sub_first);
-
}
-
-
INDEX++;
-
-
//
-
// 4.如果为前括号,把后面的内容作为second
-
//
-
int second = 0;
-
if (sub_str[INDEX] == '(')
-
{
-
INDEX++;
-
printf("4.second = Calc(%s,0);\n",sub_str);
-
second = Calc(sub_str,0);
-
}
-
-
//
-
// 5.如果为数字,把后面的数字作为second
-
//
-
else
-
{
-
while (IsNum(sub_str[INDEX]))
-
{
-
temp[temp_index++] = sub_str[INDEX];
-
INDEX++;
-
}
-
second = atoi(temp);
-
}
-
-
//
-
// 判断加减乘除的函数,是程序的精华所在
-
//
-
printf("5.first = %d\titem = %c\tsecond = %d\n", first,item,second);
-
if (item == '+' )
-
{
-
return first + Calc(sub_str, second);
-
}
-
else if (item == '-')
-
{
-
return first - Calc(sub_str, second);
-
}
-
else if (item == '*')
-
{
-
return Calc(sub_str, first * second);
-
}
-
else if (item == '/')
-
{
-
return Calc(sub_str, first / second);
-
}
-
}
-
-
//
-
// 使用递归的思路解决代括号的四则运算的问题
-
//
-
-
int mainn(int argc, char *argv[])
-
{
-
char str[100] = {"((1+((12+50)/2+10))-20*10)+9*(10*(18-11)-20)"};
-
// char str[100] = {"8+9*5-2/2+6"};
-
while (1)
-
{
-
//
-
// 循环输入函数,请输入规范的四则运算式子
-
//
-
printf("str = %s\n", str);
-
int answer = Calc(str,0);
-
-
if(INDEX = strlen(str))
-
{
-
printf("final_answer = %d\n", answer);
-
}
-
else
-
{
-
printf("算式错误,请检查语法\n");
-
}
-
-
scanf("%s", str);
-
INDEX = 0;
-
}
-
return 0;
- }