您的位置:首页 > 理论基础 > 数据结构算法

数据结构——c语言描述 第三章 (2)栈的练习(四则运算的实现)

2016-06-26 13:15 711 查看
栈的基本概念和实现我在上一篇文章中实现了,现在做一下练习,一个简单的四则运算的实现,还是比较简单的,我并没有再往下实现括号的四则运算,这个都是次要的,主要是掌握栈的操作方法,和一些基本的注意事项,其实这个代码我之前删除了又重写了一边,第一遍在实现的过程中对自己的代码并没有感到满意。在这个练习的过程中自己还是学到了很多的细节上的问题,有些东西只有自己动手去做才会明白其中的重要的部分。多多实践才是提高的方法,博客就是监督自己的手段之一。

下面上代码:

#
4000
include <stdio.h>
#include "seqstack.h"
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 1024

SeqStack stack_number;
SeqStack stack_char;

long char_to_long (char *c , int count) ;
int is_vaild (char *buf , int count) ;
long search_buf (char * buf , int count) ;
long count_res (long m , long n , char op) ;

int main () {
int count = 0;
char c;
char buf[BUF_SIZE];
long res;

init_stack (&stack_char);
init_stack (&stack_number);

while ((c = getchar()) != '#') {
if (count == BUF_SIZE - 1) {
printf ("the buf is full\n");
break;
}
else {
buf[count] = c;
count++;
}
}
if (!is_vaild (buf , count)) {
printf ("the input string error\n");
return 1;
}
res = search_buf(buf , count);
printf ("the res is %ld\n" , res);
}

long search_buf (char *buf , int count) {
char *point_arr[BUF_SIZE];
int i , point_count;
char temp;
long op;
long s , t ;
char op_arr[BUF_SIZE];
int op_count = 0;
long data_arr[BUF_SIZE];
int data_count = 0;
int length;
long res;

point_arr[0] = buf-1;
point_count = 1;

for (i = 0 ; i < count ; i++) {
temp = *(buf+i);
if (temp == '*' || temp == '\\' || temp == '+' || temp == '-') {
point_arr[point_count] = buf+i;
point_count++;
}
}
point_arr[point_count] = buf+count;
point_count++;

for (i = 0 ; i < point_count - 2 ; i++) {
op_arr[op_count] = *point_arr[i+1];
op_count++;
}
for (i = 0 ; i < point_count - 1 ; i++) {
length = point_arr[i+1] - (point_arr[i]+1);
data_arr[data_count] = char_to_long(point_arr[i]+1 , length);
data_count++;
}
//above all the search is ok
push (&stack_number , data_arr[0]);

for (i = 0 ; i < op_count ; i++) {
if (op_arr[i] == '+' || op_arr[i] == '-') {
push (&stack_char , (long)op_arr[i]);
push (&stack_number , data_arr[i+1]);
}else if (op_arr[i] == '*' || op_arr[i] == '\\') {
pop (&stack_number , &s);
t = count_res (s , data_arr[i+1] , op_arr[i]);
push (&stack_number , t);
}
}
pop (&stack_number , &res);
while (!is_empty (&stack_char)) {
pop (&stack_char , &op);
pop (&stack_number , &s);
res = count_res (s , res , (char)op);
}
return res;
}

int is_vaild (char *buf , int count) {
int i;
char temp;
int flag = 1;

for (i = 0 ; i < count ; i++) {
temp = *(buf+i);

if ((temp >='0' && temp <='9') || temp == '+' || temp == '-' || temp == '*' || temp == '\\')
continue;
else {
flag = 0;
break;
}
}
return flag;
}

long char_to_long (char *c , int count) {
int i;
long res = 0;
int temp;

for (i = 0 ; i < count ; i++) {
if (*(c+i) > '9' || *(c+i) < '0') {
printf ("the input seq error\n");
return -1;
}
temp = *(c+i) - '0';
res *= 10;
res += temp;
}
return res;
}

long count_res (long m , long n , char op) {
long temp;
switch (op) {
case '+':
return m+n;
case '-':
return m-n;
case '*':
return m*n;
case '\\':
return (m/n);
default:
return -1;
}
}
主要思路上,先对输入的字符串(以字符'#'结尾)进行扫描,将扫描到的符号和数字都分别放到数组当中,再进行进栈,出栈操作,判断优先级,将乘号和除号先进行运算,运算完的值再进行进栈操作就可以了。这个就不进行很详细的注释了。哈哈我真懒XD!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息