Calculating Postfix in c

I have been asked to calculate a postfix expression , which means that for every consequtive 2 numbers there is an arithmetical operation in between , for example fot the given string “123*+” the result will be 7 –> 1+(2*3)
I tried to write the code in C but for some reason i get that the result is 0
can someone tell me why? or if my direction is wrong?
And how can i return to the user that there was an error with the given string such as uknown char or empty string if the function needs to return an int. I can’t return NULL right.?
thank you

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <stdio.h>
#define ERROR -2147483648
int Postfix(char *str) {
if(!str)
return ERROR;
int str_length;
for ( str_length = 0;str[str_length]!=''; str_length++); // calculating the exact number of chars in the string
int mid=str_length/2;// the length of the string will always be odd because its not possible that the number of digits equals to the number of arithmetics puncuation sice for every artihmetic
//puncutation there are 2 digits.
int *current, *prev, result=0;
char *arithmetic;
arithmetic=str+mid+2;// address of the first arithemetic sign in the array
prev=str+mid;// address of the second last digit in the array
current=str+mid+1; // address of the last digit in the array
for (int i = 0; i < mid; i++) { // the loop will run the exact number of arithmetic operations
prev=str+mid-i;
arithmetic=str+mid+i;
switch(*arithmetic) {
case '+': {
result+=(*current)+(*prev);
break;
}
case '-': {
result+=(*current)-(*prev);
break;
}
case '*': {
result+=(*current)*(*prev);
break;
}
case '/': { // need to handle the case with division by 0?
result+=(*current)/(*prev);
break;
}
default: { // need to handle the case where an arithmetic which is not (+,-,*,/) was inserted?
result+=0;
break;
}
}
*current=result;
}
return result;
}
int main() {
char c[]="231*+";
int result=Postfix(&c);
printf("%d",result);
if(result!=ERROR)
printf("The result is: %d",result);
else
printf("An error occured.");
return 0;
}
</code>
<code>#include <stdio.h> #define ERROR -2147483648 int Postfix(char *str) { if(!str) return ERROR; int str_length; for ( str_length = 0;str[str_length]!=''; str_length++); // calculating the exact number of chars in the string int mid=str_length/2;// the length of the string will always be odd because its not possible that the number of digits equals to the number of arithmetics puncuation sice for every artihmetic //puncutation there are 2 digits. int *current, *prev, result=0; char *arithmetic; arithmetic=str+mid+2;// address of the first arithemetic sign in the array prev=str+mid;// address of the second last digit in the array current=str+mid+1; // address of the last digit in the array for (int i = 0; i < mid; i++) { // the loop will run the exact number of arithmetic operations prev=str+mid-i; arithmetic=str+mid+i; switch(*arithmetic) { case '+': { result+=(*current)+(*prev); break; } case '-': { result+=(*current)-(*prev); break; } case '*': { result+=(*current)*(*prev); break; } case '/': { // need to handle the case with division by 0? result+=(*current)/(*prev); break; } default: { // need to handle the case where an arithmetic which is not (+,-,*,/) was inserted? result+=0; break; } } *current=result; } return result; } int main() { char c[]="231*+"; int result=Postfix(&c); printf("%d",result); if(result!=ERROR) printf("The result is: %d",result); else printf("An error occured."); return 0; } </code>
#include <stdio.h>
#define ERROR -2147483648
int Postfix(char *str) {
    if(!str)
        return ERROR;
    int str_length;
    for ( str_length = 0;str[str_length]!=''; str_length++); // calculating the exact number of chars in the string
    int mid=str_length/2;// the length of the string will always be odd because its not possible that the number of digits equals to the number of arithmetics puncuation sice for every artihmetic
    //puncutation there are 2 digits.
    int *current, *prev, result=0;

    char *arithmetic;
    arithmetic=str+mid+2;// address of the first arithemetic sign in the array

    prev=str+mid;// address of the  second last digit in the array

    current=str+mid+1; // address of the last digit in the array

    for (int i = 0; i < mid; i++) { // the loop will run the exact number of arithmetic operations
        prev=str+mid-i;
        arithmetic=str+mid+i;
        switch(*arithmetic) {
            case '+': {
                result+=(*current)+(*prev);
                break;
            }
            case '-': {
                result+=(*current)-(*prev);
                break;
            }
            case '*': {
                result+=(*current)*(*prev);
                break;
            }
            case '/': { // need to handle the case with division by 0?
                result+=(*current)/(*prev);
                break;
            }
            default: { // need to handle the case where an arithmetic which is not (+,-,*,/) was inserted?
                result+=0;
                break;
            }

        }
        *current=result;
    }
    return result;
}
int main() {
char c[]="231*+";
int result=Postfix(&c);
    printf("%d",result);
    if(result!=ERROR)
        printf("The result is: %d",result);
    else
        printf("An error occured.");
return 0;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật