我正在尝试在C中实现一个分流码算法然后使用RPN计算器,但是我遇到了间歇性故障,当我逐行逐行扫描时,这种情况不会发生!我的智慧结束了!
我将每个项目存储在一个struct中,当遇到的字符是* / - +符号时,is_operator为true。
struct operator{
char symbol;
associativity assoc;
uint8_t prec;
float value;
bool is_operator, is_number;
};
这只是在我的代码的第53行 - 在" assign_operator'中分配的。功能
operator* assign_operator(char symbol, associativity ass, int prec){
//check the precedence is in limits
assert(prec <= 6);
//get memory for the struct
operator *op= calloc(1, sizeof(op));
mem_check(op);
//assign the struct
op->symbol = symbol;
op->assoc = ass;
op->prec = prec;
op->is_operator = true;
op->is_number = false;
return op;
}
显示它如何最终变为假,我怀疑在print_array函数中,但我不知道如何!
void print_array(operator ** array, int size){
fflush(stdout);
for (int i = 0; array[i]; i++) {
if (array[i]->is_number) {
printf("%f ", array[i]->value);
}
else{
printf("%c ", array[i]->symbol);
}
}
printf("\n");
}
任何最终我的调车场功能都可以完成&#39;肉&#39;:
void infix_rpn(operator** output, const char* equation){
const char symbols[] = { '/', '*', '+', '-' };
unsigned int number_of_operators = num_elems(symbols), q_len = (int)strlen(equation)-1, out_pos = 0;
char input_symbol;
int in_int, i, z = 0;
//get memory for array of operators and set them up
operator **opers = setup_operators(symbols, number_of_operators);
/*
the FILO 'stack' area, where 0 is the bottom, so the thing to pop is the highest index occupied
*/
operator **stack = calloc(q_len, sizeof(operator));
mem_check(stack);
//iterate through the letter in the equation string
while(equation[z]){
//test for blank and skip over until it's not blank
while(isblank(equation[z])){
z++;
}
//is it's a character then set in_int to 0 and put the character in 'input'
if(!isdigit(equation[z])){
in_int = 0;
input_symbol = equation[z++];
}
//must be a number entered
else{
in_int = atoi(&equation[z++]);
}
//if the number is a 0 it must have been a character
if (!in_int) {
//loop though the symbols to find a match
for (i = 0; i < number_of_operators; ++i) {
if (opers[i]->symbol == input_symbol) {
int j = 0;
//find next empy element in queue
while(stack[j] != NULL && j < q_len){
j++;
}
/*
cant have a + on top of a / so check the precedence
a + has a higher precedence than a / therefore
if the number is higher, the precedence is less!
inverse precedence
*/
if (j == 0) {
stack[j] = assign_operator(symbols[i], left, i + 3);
}
//if the top thing in the queue has a lower precedence than the new operator's
else {
//get the precedence numbers
int queue_prec = stack[j - 1]->prec;
int new_prec = opers[i]->prec;
//if the + is coming down on top of the * they need switching
if (queue_prec < new_prec){
//pop to output and point to next available element
output[out_pos] = stack[j - 1];
out_pos += 1;
//put new one in the queue in it's place
stack[j - 1] = assign_operator(symbols[i], left, new_prec);
}
//if the things are the same or it's a * on toof a + can just be put on the queue
else{
//put it on the queue
stack[j] = assign_operator(symbols[i], left, new_prec);
}
}
}
}
}
//add number to the output queue, then increment the index of the queue
else{
output[out_pos] = assign_value(in_int);
out_pos += 1;
}
}
//find last thing in the queue
i = 0;
while(stack[i+1]){
i++;
}
//add the queue to te output stack
while(i >= 0){
output[out_pos++] = stack[i];
//needs to be 'popd' here so that the queue no long has any contents
stack[i--] = NULL;
}
//free_ops(queue, q_len);
free(stack);
free_ops(opers, number_of_operators);
free(opers);
}
有关为什么会发生这种情况的任何提示将不胜感激!
谢谢,