程序只能在调试模式下工作,但不能在正常的exe模式下工作(使用c语言)?
所以我创建了一个程序,可以将中缀转换为后缀和前缀,效果很好。问题是,我使用的是C-LION,它有自己的调试器,允许我一步一步地运行,我这样做,程序运行良好并输出预期结果,但当我正常运行时,它不工作,并给我一个main.C NOT working错误 这是具有菜单的主要功能:程序只能在调试模式下工作,但不能在正常的exe模式下工作(使用c语言)?,c,debugging,data-structures,clion,infix-operator,C,Debugging,Data Structures,Clion,Infix Operator,所以我创建了一个程序,可以将中缀转换为后缀和前缀,效果很好。问题是,我使用的是C-LION,它有自己的调试器,允许我一步一步地运行,我这样做,程序运行良好并输出预期结果,但当我正常运行时,它不工作,并给我一个main.C NOT working错误 这是具有菜单的主要功能: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include &
#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;
}这是对您问题的回答,您是说我应该在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.开关和外壳
与其他语言(如Pascal)相反,C switch语句是一个依赖于表达式的goto语句,而不是一个具有多个可选项的多路分支。这并不意味着开关不能用于后者,但也可以用于不同的用途。请你明白我的意思
想象一下以下错误代码:
#include <stdio.h>
int main()
{
goto L1;
int i = 1;
L1:
printf("%d\n", i);
return 0;
}
我再次在中编译和测试。
输出为案例2:0。哎哟
解决方案
要正确模拟多分支,必须执行以下操作:
每一个案例都以休息结束
在案例的每个冒号后启动作用域
在相应的中断之前结束此范围
再举一个例子:
#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;
}
由于变量i的范围被限制在从可能的入口情况0:情况1:到可能的出口中断的范围内,因此没有其他可能的代码路径可以访问它。您如何正常运行它?听起来你是想运行main.c而不是executablenormally你是指在IDE中按run图标,或者通过目录或终端启动生成的输出文件?是的,我是指在IDE中使用run图标运行它你在嵌套的case语句中声明变量的方式很奇怪。我的编译器无法确定它们是否被使用。或者在使用时声明它们。比如,如果ch2==2,一些变量被声明,如果exp==1和ch3==3,它们可能会被使用?你有几个ch3变量让我们完全困惑。如果你的编译器也被搞糊涂了,我一点也不奇怪。在调试中工作,但不是没有调试,是UB的典型症状。未初始化的变量、越界的数组访问、缺少NUL终止符等等。
#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;
}