字符串四则运算的实现
题目大意:有字符串表示的一个四则运算表达式,要求计算出该表达式的正确数值。四则运算即:加减乘除"+-*/",另外该表达式中的数字只能是1位(数值范围0~9)。另若有不能整除的情况,按向下取整处理,eg: 8/3得出值为2。
若有字符串"8+7*2-9/3",计算出其值为19。
尝试写应用程序,花了1个多小时,汗。
运行了一下,结果还行。
##################################################
#define MAX_LEN 256
int find_next_run_chara(int * chara, int * chara_len)
{
int i = 0;
for (i = 0;i<*chara_len; i++)
{
if ((*(chara+i) == 3) || (*(chara+i) == 4))
return i;
}
for (i = 0;i<*chara_len; i++)
{
if ((*(chara+i) == 1) || (*(chara+i) == 2))
return i;
}
}
int run_func(int * num, int * num_len, int * chara, int * chara_len)
{
int i;
int next_run_chara = 0;
for (i=0;i<*num_len;i++)
printf("%d ", *(num+i));
printf("\n");
for (i=0;i<*chara_len;i++)
printf("%d ", *(chara+i));
printf("\n");
if ((*num_len == 1) || (*chara_len == 0))
return *num;
next_run_chara = find_next_run_chara(chara, chara_len);
printf("next_run_chara = %d\n", next_run_chara);
switch(*(chara + next_run_chara))
{
case 1:
*(num + next_run_chara) = *(num + next_run_chara) + *(num + next_run_chara + 1);
break;
case 2:
*(num + next_run_chara) = *(num + next_run_chara) - *(num + next_run_chara + 1);
break;
case 3:
*(num + next_run_chara) = (*(num + next_run_chara)) * (*(num + next_run_chara + 1));
break;
case 4:
*(num + next_run_chara) = *(num + next_run_chara) / *(num + next_run_chara + 1);
break;
default:
printf("run over %d\n", *(chara + next_run_chara));
}
for (i = next_run_chara;i<*chara_len - 1;i++)
{
*(num + i + 1) = * (num + i + 2);
*(chara + i) = * (chara + i + 1);
}
*num_len = *num_len - 1;
*chara_len = *chara_len - 1;
return run_func(num, num_len, chara, chara_len);
}
int main(void)
{
char src_str[MAX_LEN];
int * num = NULL;
int * chara = NULL;
int i;
int j = 0;
int k = 0;
int val = 0;
scanf("%s", src_str);
num = malloc(MAX_LEN);
chara = malloc(MAX_LEN);
memset(num, 0, MAX_LEN);
memset(chara, 0, MAX_LEN);
for (i = 0;i < strlen(src_str);i++)
{
switch (src_str[i])
{
case '0':
*(num + j++) = 0;
break;
case '1':
*(num + j++) = 1;
break;
case '2':
*(num + j++) = 2;
break;
case '3':
*(num + j++) = 3;
break;
case '4':
*(num + j++) = 4;
break;
case '5':
*(num + j++) = 5;
break;
case '6':
*(num + j++) = 6;
break;
case '7':
*(num + j++) = 7;
break;
case '8':
*(num + j++) = 8;
break;
case '9':
*(num + j++) = 9;
break;
case '+':
*(chara + k++) = 1;
break;
case '-':
*(chara + k++) = 2;
break;
case '*':
*(chara + k++) = 3;
break;
case '/':
*(chara + k++) = 4;
break;
default:
printf("input string error\n");
return -1;
}
}
val = run_func(num, &j, chara, &k);
free(num);
free(chara);
printf("result = %d\n", val);
return 0;
}
##################################################
结果如下:
[ping@localhost test]$ ./10
1+1+1+9*8*1/2/2+4-9-9+1+1+9-8
1 1 1 9 8 1 2 2 4 9 9 1 1 9 8
1 1 1 3 3 4 4 1 2 2 1 1 1 2
next_run_chara = 3
1 1 1 72 1 2 2 4 9 9 1 1 9 8
1 1 1 3 4 4 1 2 2 1 1 1 2
next_run_chara = 3
1 1 1 72 2 2 4 9 9 1 1 9 8
1 1 1 4 4 1 2 2 1 1 1 2
next_run_chara = 3
1 1 1 36 2 4 9 9 1 1 9 8
1 1 1 4 1 2 2 1 1 1 2
next_run_chara = 3
1 1 1 18 4 9 9 1 1 9 8
1 1 1 1 2 2 1 1 1 2
next_run_chara = 0
2 1 18 4 9 9 1 1 9 8
1 1 1 2 2 1 1 1 2
next_run_chara = 0
3 18 4 9 9 1 1 9 8
1 1 2 2 1 1 1 2
next_run_chara = 0
21 4 9 9 1 1 9 8
1 2 2 1 1 1 2
next_run_chara = 0
25 9 9 1 1 9 8
2 2 1 1 1 2
next_run_chara = 0
16 9 1 1 9 8
2 1 1 1 2
next_run_chara = 0
7 1 1 9 8
1 1 1 2
next_run_chara = 0
8 1 9 8
1 1 2
next_run_chara = 0
9 9 8
1 2
next_run_chara = 0
18 8
2
next_run_chara = 0
10
result = 10
题目大意:有字符串表示的一个四则运算表达式,要求计算出该表达式的正确数值。四则运算即:加减乘除"+-*/",另外该表达式中的数字只能是1位(数值范围0~9)。另若有不能整除的情况,按向下取整处理,eg: 8/3得出值为2。
若有字符串"8+7*2-9/3",计算出其值为19。
尝试写应用程序,花了1个多小时,汗。
运行了一下,结果还行。
##################################################
#define MAX_LEN 256
int find_next_run_chara(int * chara, int * chara_len)
{
int i = 0;
for (i = 0;i<*chara_len; i++)
{
if ((*(chara+i) == 3) || (*(chara+i) == 4))
return i;
}
for (i = 0;i<*chara_len; i++)
{
if ((*(chara+i) == 1) || (*(chara+i) == 2))
return i;
}
}
int run_func(int * num, int * num_len, int * chara, int * chara_len)
{
int i;
int next_run_chara = 0;
for (i=0;i<*num_len;i++)
printf("%d ", *(num+i));
printf("\n");
for (i=0;i<*chara_len;i++)
printf("%d ", *(chara+i));
printf("\n");
if ((*num_len == 1) || (*chara_len == 0))
return *num;
next_run_chara = find_next_run_chara(chara, chara_len);
printf("next_run_chara = %d\n", next_run_chara);
switch(*(chara + next_run_chara))
{
case 1:
*(num + next_run_chara) = *(num + next_run_chara) + *(num + next_run_chara + 1);
break;
case 2:
*(num + next_run_chara) = *(num + next_run_chara) - *(num + next_run_chara + 1);
break;
case 3:
*(num + next_run_chara) = (*(num + next_run_chara)) * (*(num + next_run_chara + 1));
break;
case 4:
*(num + next_run_chara) = *(num + next_run_chara) / *(num + next_run_chara + 1);
break;
default:
printf("run over %d\n", *(chara + next_run_chara));
}
for (i = next_run_chara;i<*chara_len - 1;i++)
{
*(num + i + 1) = * (num + i + 2);
*(chara + i) = * (chara + i + 1);
}
*num_len = *num_len - 1;
*chara_len = *chara_len - 1;
return run_func(num, num_len, chara, chara_len);
}
int main(void)
{
char src_str[MAX_LEN];
int * num = NULL;
int * chara = NULL;
int i;
int j = 0;
int k = 0;
int val = 0;
scanf("%s", src_str);
num = malloc(MAX_LEN);
chara = malloc(MAX_LEN);
memset(num, 0, MAX_LEN);
memset(chara, 0, MAX_LEN);
for (i = 0;i < strlen(src_str);i++)
{
switch (src_str[i])
{
case '0':
*(num + j++) = 0;
break;
case '1':
*(num + j++) = 1;
break;
case '2':
*(num + j++) = 2;
break;
case '3':
*(num + j++) = 3;
break;
case '4':
*(num + j++) = 4;
break;
case '5':
*(num + j++) = 5;
break;
case '6':
*(num + j++) = 6;
break;
case '7':
*(num + j++) = 7;
break;
case '8':
*(num + j++) = 8;
break;
case '9':
*(num + j++) = 9;
break;
case '+':
*(chara + k++) = 1;
break;
case '-':
*(chara + k++) = 2;
break;
case '*':
*(chara + k++) = 3;
break;
case '/':
*(chara + k++) = 4;
break;
default:
printf("input string error\n");
return -1;
}
}
val = run_func(num, &j, chara, &k);
free(num);
free(chara);
printf("result = %d\n", val);
return 0;
}
##################################################
结果如下:
[ping@localhost test]$ ./10
1+1+1+9*8*1/2/2+4-9-9+1+1+9-8
1 1 1 9 8 1 2 2 4 9 9 1 1 9 8
1 1 1 3 3 4 4 1 2 2 1 1 1 2
next_run_chara = 3
1 1 1 72 1 2 2 4 9 9 1 1 9 8
1 1 1 3 4 4 1 2 2 1 1 1 2
next_run_chara = 3
1 1 1 72 2 2 4 9 9 1 1 9 8
1 1 1 4 4 1 2 2 1 1 1 2
next_run_chara = 3
1 1 1 36 2 4 9 9 1 1 9 8
1 1 1 4 1 2 2 1 1 1 2
next_run_chara = 3
1 1 1 18 4 9 9 1 1 9 8
1 1 1 1 2 2 1 1 1 2
next_run_chara = 0
2 1 18 4 9 9 1 1 9 8
1 1 1 2 2 1 1 1 2
next_run_chara = 0
3 18 4 9 9 1 1 9 8
1 1 2 2 1 1 1 2
next_run_chara = 0
21 4 9 9 1 1 9 8
1 2 2 1 1 1 2
next_run_chara = 0
25 9 9 1 1 9 8
2 2 1 1 1 2
next_run_chara = 0
16 9 1 1 9 8
2 1 1 1 2
next_run_chara = 0
7 1 1 9 8
1 1 1 2
next_run_chara = 0
8 1 9 8
1 1 2
next_run_chara = 0
9 9 8
1 2
next_run_chara = 0
18 8
2
next_run_chara = 0
10
result = 10