为什么这个使用数组堆栈的 c 中的后缀输入计算器不起作用?
Why this postfix input calculator in c using stack of array doesn;t work?
我正在学习数据结构,在学习过程中,我需要在 c.I 中制作一个后缀输入计算器,想使用堆栈和 array.I 的堆栈编写一些代码,但事实并非如此给出输出而不是它给出我在 it.If 中输入的第一个单词scanf 即使我的输入是 17 长度它也只打印 1.scan f is not working correctly or strlen is not working correctly.My code is :-
#include <stdio.h>
#include <string.h>
#include "stack_forpostfix.h"
int postfix(char *exp);
int isoperator1(char b);
int isnumericdigit1(char c);
int doevaluation(int oper1,char optr,int oper2);
int main(){
char *exp1;
int a;
printf("Enter the postfix expresssion\n");/*if we dont ask and put input in declaration the code works perfect,because if we ask the strlen is not working*/
scanf("%s",exp1); //2 3 * 5 4 * + 9 -
printf(" , %d , ",strlen(exp1));
a=postfix(exp1);
printf("%d\n",a);
}
int postfix(char *exp){
for(int i=0;i<strlen(exp);i++){
if(exp[i]==' ' || exp[i]==','){
continue;
}
else if(isoperator1(exp[i])){
int op2=gettop();
pop();
int op1=gettop();
pop();
int result=doevaluation(op1,exp[i],op2);
push(result);
}
else if(isnumericdigit1(exp[i])){
int oper=0;
while(i<strlen(exp) && isnumericdigit1(exp[i])){
oper=(oper*10)+(exp[i]-'0');
i++;
} //since i++ is there if no i-- exp[i] will escape one further
i--;
push(oper);
}
}
return gettop();
}
int isnumericdigit1(char c){
if (c>='0' && c<='9'){
return 1;
}
else return 0;
}
int isoperator1(char b){
if(b=='+'||b=='-'||b=='*'||b=='/'){
return 1;
}
else {
return 0;
}
}
int doevaluation(int oper1,char optr,int oper2){
if(optr=='+'){
return oper1+oper2;
}else if (optr=='-'){
return oper1-oper2;
}else if (optr=='*'){
return oper1*oper2;
}else if (optr=='/'){
return oper1/oper2;
}else {
printf("not valid");
return -1;
}
}
我的头文件(stack_forpostfix.h)代码是:-
#ifndef stackyfix
#define stackyfix
#define maxsize 111
int a[maxsize];
int top=-1;
void push(int r){
top++;
a[top]=r;
}
void pop(){
top--;
}
int gettop(){
return a[top];
}
#endif
scanf("%s",exp1);
遇到空白字符会停止读取。
您应该改用 fgets()
。
此外,不要忘记为读取表达式分配缓冲区。
char exp1[102400]; // believing that huge expression won't come
fgets(exp1, sizeof(exp1), stdin);
另请注意
printf(" , %d , ",strlen(exp1));
将调用 未定义的行为 类型不匹配:%d
期望 int
而 strlen()
returns size_t
.
打印 size_t
的格式类型说明符是 %zu
。
如果您的环境不支持 %zu
,您应该先将 return 值转换为 int
,然后再将其传递给 %d
。
在后缀函数的中缀中,代替 str[i]=gettop();应该有 str[j]=gettop() 以便可以对括号内输入的表达式进行处理。还要在 fgets 之后添加这段代码,以删除 fgets 可能附加的 '\n'。
fgets(b,sizeof(b),stdin);
for(int i=0;b[i]!='[=10=]';i++){ // removes \n added by fgets
if(b[i]=='[=10=]'){
if(b[i-1]=='\n'){
b[i-1]='[=10=]';
}
}
}
我正在学习数据结构,在学习过程中,我需要在 c.I 中制作一个后缀输入计算器,想使用堆栈和 array.I 的堆栈编写一些代码,但事实并非如此给出输出而不是它给出我在 it.If 中输入的第一个单词scanf 即使我的输入是 17 长度它也只打印 1.scan f is not working correctly or strlen is not working correctly.My code is :-
#include <stdio.h>
#include <string.h>
#include "stack_forpostfix.h"
int postfix(char *exp);
int isoperator1(char b);
int isnumericdigit1(char c);
int doevaluation(int oper1,char optr,int oper2);
int main(){
char *exp1;
int a;
printf("Enter the postfix expresssion\n");/*if we dont ask and put input in declaration the code works perfect,because if we ask the strlen is not working*/
scanf("%s",exp1); //2 3 * 5 4 * + 9 -
printf(" , %d , ",strlen(exp1));
a=postfix(exp1);
printf("%d\n",a);
}
int postfix(char *exp){
for(int i=0;i<strlen(exp);i++){
if(exp[i]==' ' || exp[i]==','){
continue;
}
else if(isoperator1(exp[i])){
int op2=gettop();
pop();
int op1=gettop();
pop();
int result=doevaluation(op1,exp[i],op2);
push(result);
}
else if(isnumericdigit1(exp[i])){
int oper=0;
while(i<strlen(exp) && isnumericdigit1(exp[i])){
oper=(oper*10)+(exp[i]-'0');
i++;
} //since i++ is there if no i-- exp[i] will escape one further
i--;
push(oper);
}
}
return gettop();
}
int isnumericdigit1(char c){
if (c>='0' && c<='9'){
return 1;
}
else return 0;
}
int isoperator1(char b){
if(b=='+'||b=='-'||b=='*'||b=='/'){
return 1;
}
else {
return 0;
}
}
int doevaluation(int oper1,char optr,int oper2){
if(optr=='+'){
return oper1+oper2;
}else if (optr=='-'){
return oper1-oper2;
}else if (optr=='*'){
return oper1*oper2;
}else if (optr=='/'){
return oper1/oper2;
}else {
printf("not valid");
return -1;
}
}
我的头文件(stack_forpostfix.h)代码是:-
#ifndef stackyfix
#define stackyfix
#define maxsize 111
int a[maxsize];
int top=-1;
void push(int r){
top++;
a[top]=r;
}
void pop(){
top--;
}
int gettop(){
return a[top];
}
#endif
scanf("%s",exp1);
遇到空白字符会停止读取。
您应该改用 fgets()
。
此外,不要忘记为读取表达式分配缓冲区。
char exp1[102400]; // believing that huge expression won't come
fgets(exp1, sizeof(exp1), stdin);
另请注意
printf(" , %d , ",strlen(exp1));
将调用 未定义的行为 类型不匹配:%d
期望 int
而 strlen()
returns size_t
.
打印 size_t
的格式类型说明符是 %zu
。
如果您的环境不支持 %zu
,您应该先将 return 值转换为 int
,然后再将其传递给 %d
。
在后缀函数的中缀中,代替 str[i]=gettop();应该有 str[j]=gettop() 以便可以对括号内输入的表达式进行处理。还要在 fgets 之后添加这段代码,以删除 fgets 可能附加的 '\n'。
fgets(b,sizeof(b),stdin);
for(int i=0;b[i]!='[=10=]';i++){ // removes \n added by fgets
if(b[i]=='[=10=]'){
if(b[i-1]=='\n'){
b[i-1]='[=10=]';
}
}
}