程序仅适用于调试模式,但不适用于普通的exe(使用c lion)?

问题描述 投票:0回答:1

所以我创建了一个程序,从中缀转换为post fix和前缀,工作得很好。问题是我正在使用C-LION,它有自己的调试器,允许我一步一步,我这样做程序工作正常并输出预期的结果然后当我正常运行它不工作并给我“main.c not working”错误。

这个菜单的主要功能是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <tgmath.h>

char*infixToPostfix(char *infinx);
char* postixToinfix(char *infinx);
char* postixToprefix(char *infinx);
char* prefixToinfix(char *infinx);
char* prefixTopostfix(char *infinx);
char* infixToPrefix(char *infinx);
char*evaluate(char *infinx );
int exp_det(char*exp);
typedef struct node
{
    char *op;
    int  p;
    struct node *next; /* Pointer to the next node. Notice that the
    existence of the structure tag enables us to declare its type. */
} node;
node *head=NULL; /* Global pointer that always points to the head of the
    stack. */
int precedence(char symbol);
void add_stack(const node *p);
void pop(void);
int main(void)
{
    char postfix[100];
    int choice;
    //converting from ininfinx to postfix
    printf("\t\t***** Conversion Calculator 1.0 ******\t\t\n");
    printf("\t\t1.Convert\n\t\t2.Evaluate\n\t\t3.Exit\nEnter Choice : ");
    scanf("%d",&choice);
    //switch (choice){
        if (choice==1) {
            printf("\n\t\t1.Input from File\n\t\t2.standered  input\nEnter Choice :");
            int ch2;
            scanf("%d", &ch2);
            switch (ch2) {
                case 1:
                    printf("FILE MANGAMENT STILL NOT DONE !!!");
                    break;
                case 2:
                    printf("Enter Expression : ");
                    char line[256];


                    scanf(" %[^\n]s", postfix);
                    char in2[100] = {'\0'};
                    char in3[100] = {'\0'};
                    char *conv;
                    char *conv2;

                    strcpy(in2, postfix);
                    strcpy(in3, postfix);
                    int exp = exp_det(in2);

                    if (exp == 1) {
                        printf("\nThis is a Prefix expression do you want to\n\t\t1.Infix\n\t\t2.Postfix\n\t\t3.Both\nEnter Choice :");
                        int ch3;
                        scanf("%d", &ch3);
                        switch (ch3) {
                            case 1:
                                conv = prefixToinfix(in3);
                                printf("Expression in Infix form: %s \n", in3);
                                break;
                            case 2:
                                conv = prefixTopostfix(in3);
                                printf("Expression in Postfix form: %s \n", in3);
                                break;
                            case 3:
                                conv = prefixToinfix(in3);
                                conv2 = prefixTopostfix(postfix);
                                printf("Expression in Infix form: %s \n", conv);
                                printf("Expression in Postfix form: %s \n", conv2);
                                break;
                            default:
                                printf("ERROROR WHEN EXPRESSION IN PREFIX ");

                                break;
                        }
                    } else if (exp == 2) {
                        printf("\nThis is a Infix expression do you want to\n\t\t1.Prefix\n\t\t2.Postfix\n\t\t3.Both\nEnter Choice :");
                        int ch3;
                        scanf("%d", &ch3);
                        switch (ch3) {
                            case 1:
                                printf("Expression in prefix form: %s \n", infixToPrefix(postfix));
                                break;
                            case 2:
                                printf("Expression in Postfix form: %s \n", infixToPostfix(postfix));
                                break;
                            case 3:
                                printf("Expression in prefix form: %s \n", infixToPrefix(postfix));
                                printf("Expression in Postfix form: %s \n", infixToPostfix(postfix));
                                break;
                            default:
                                printf("ERROROR R");

                                break;
                        }
                    } else if (exp == 3) {
                        printf("This is a Postfix expression do you want to\n\t\t1.Infix\n\t\t2.Prefix\n\t\t3.Both\nEnter Choice :");
                        int ch3;
                        scanf("%d", &ch3);
                        switch (ch3) {
                            case 1:
                                printf("Expression in Infix form: %s \n", postixToinfix(postfix));
                                break;
                            case 2:
                                printf("Expression in prefix form: %s \n", postixToprefix(postfix));
                                break;
                            case 3:
                                printf("Expression in Infix form: %s \n", postixToinfix(postfix));
                                printf("Expression in Prefix form: %s \n", postixToprefix(postfix));
                                break;
                            default:
                                printf("ERROR... 3:(\n");
                                break;
                        }
                    }
                    break;//for the switch with ch2 case 1
                default:
                    printf("ERROR... 2:(\n");
                    break;
            }
            //break;
        }if(choice==2) {
        printf("Enter Expression : ");

        scanf(" %[^\n]s", postfix);
        char in2[100] = {'\0'};
        char in3[100] = {'\0'};
        char *conv;
        char *conv2;

        strcpy(in2, postfix);
        conv = evaluate(in2);
        printf("\nExpression evaluated = %s \n", conv);
        //break;
    }if(choice==3) {
        printf("BYE...... :D\n");


    }

    system("PAUSE");
}

好了经过多次试验,我开始认为问题出在转换本身。这是我使用的功能之一,它看起来很好。如果有人有另一种意见帮助被大大挪用。

char* infixToPostfix(char *infinx){
char* token;

char * infinx1=malloc(sizeof(infinx)+1);

infinx1=strcpy(infinx1,infinx);

token = strtok(infinx1," ");

char* res;
res=malloc(sizeof(infinx)+sizeof(head->op)*strlen(infinx));

strcpy(res," ");

if(*token=='\n' ){token=strtok(NULL," ");}

while( token != NULL ) {

    node n;
    n.op=token;
    n.p=precedence(*token);
    if(isalpha(*token) || isdigit(*token)){
        // strcat(result,infinx[i]);
        //printf("%c",infinx[i]);

        res=strcat(res,token);
        res=strcat(res," ");
    }
        //case when encounter a left paranthessisis
    else if(*token=='(' || *token==')'){
        if (*token=='('){
            add_stack(&n);
        }else if(*token==')') {

            while (*head->op != '(') {
                // strcat(result, n.op);
                //printf("%c",(char)head->op);
                res=strcat(res,head->op);
                res=strcat(res," ");
                pop();
            }
            pop();
        }
    }
        //if head if null meaning the stack is empty or if the presendance of the head is less thatn or equal to new character
    else if(head==NULL || head->p < n.p ){
        if (head->p == n.p){}
        add_stack(&n);
    }
        //in case the head has higher presendance he we pop and print untill we reach the same presedance
    else  {
        while( head!=NULL && head->p >= n.p){
            //strcat(result,n.op);
            //printf("%c",(char)head->op);
            res=strcat(res,head->op);
            res=strcat(res," ");
            pop();
        }
        add_stack(&n);
    }
    token=strtok(NULL," ");
}
while(head!=NULL){
    //strcat(result,head->op);
    //printf("%c",(char)head->op);
    res=strcat(res,head->op);
    res=strcat(res," ");
    pop();
}
return res;

}

c debugging data-structures clion infix-operator
1个回答
1
投票

这就是你的问题的答案“所以你说我应该在switch语句之外定义它们?”这正确反映了代码中的一个问题。

要么:您可以在外面定义它们来解决问题。

或者:您可以引入适当的块范围来解决问题。

由于前者是微不足道的,我将详细说明后者:

1.范围和变量

局部变量的生命周期从其声明开始,以周围的块范围结束。

例:

int main()
{
  int a = 0; /* a starts to live. */
  { /* new scope */
    int b = 1; /* b starts to live */
    int a = 2; /* a new a starts to live. (The one of out scope is eclipsed.) */
  } /* Life of b and the inner a ends. The eclipsed outer a becomes visible again. */
  return 0;
}

2. switchcase

与其他语言(例如Pascal)相反,C switch语句相当于“goto取决于表达式”而不是具有多个替代方案的多路分支。 (这并不意味着switch不能用于后者,但它也可以使用不同。)(请参阅Wikipedia: Control Flow: 5.2 Case and switch statements以了解我的意思。)

想象一下以下(错误的)代码:

#include <stdio.h>

int main()
{
  goto L1;
  int i = 1;
L1:
  printf("%d\n", i);
  return 0;
}

goto L1;跳过int i = 1;的声明,但在L1:之后它被用于printf() - 哎哟!

出于好奇,我在ideone.com尝试了它 - 它编译并运行没有抱怨。输出是0虽然它可能也有12,或任何其他数字可以存储为int

以下(错误)样本中的情况相同:

#include <stdio.h>

int main()
{
  int cond = 2;
  switch (cond) {
    case 1:
      printf("case 1\n");
      int i = 1;
    case 2:
      printf("case 2: %d\n", i);
  } /* Here ends the life-time of i */
  return 0;
}

再次,我在ideone.com编译和测试。输出是case 2: 0。哎呀!

要正确模拟多分支,需要执行以下操作:

  1. case结束每个break
  2. case的每个冒号后开始一个范围。
  3. 在相应的break之前结束此范围。

示例:

#include <stdio.h>

int main()
{
  int cond = 2;
  switch (cond) {
    case 0: case 1: { /* <- start scope */
      int i = 1;
      printf("case 1: %d\n", i);
    } break; /* <- end scope and jump to end of switch */
    case 2:
      printf("case 2: %d\n", i); /* i is recognized as unknown identifier */
  }
  return 0;
}

编译在ideone

prog.c: In function ‘main’:
prog.c:12:34: error: ‘i’ undeclared (first use in this function)
           printf("case 2: %d\n", i); /* i is recognized as unknown identifier */
                                  ^

由于变量i的范围被限制在从可能的入口(case 0: case 1:)到可能的出口(break)的范围内 - 没有其他可能的代码路径可以访问它。

© www.soinside.com 2019 - 2024. All rights reserved.