所以我创建了一个程序,从中缀转换为修复后和前缀,工作得很好。问题是我正在使用C-LION,它有自己的调试器,允许我一步一步,我这样做程序工作正常并输出预期的结果然后当我正常运行它不工作并给我“main.c not working”ERROR。
这是具有菜单的主要功能:
#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");
}
OK现在经过多次试验后,我开始认为问题出在转换本身。这是我使用的功能之一,它看起来很好。如果有人有其他意见帮助被大大挪用。
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;
}
答案 0 :(得分:1)
这是你的问题的答案“所以你说我应该在switch语句之外定义它们?”这正确反映了代码中的一个问题。
要么:你可以在外面定义它们来解决问题。
或者:您可以引入适当的块范围来解决问题。
由于前者是微不足道的,我将详细说明后者:
局部变量的生命周期从其声明开始,以周围的块范围结束。
示例:
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;
}
switch
和case
与其他语言(例如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
,但也可以1
,2
或任何其他可以存储为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
。哎呀!
要正确模仿多分支,需要做以下事情:
使用case
结束每个break
。
在case
的每个冒号后启动一个范围。
在相应的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
)的范围 - 没有其他可能的代码路径可以访问它。