Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "nekiheader.h"
- int main(){
- char ulaz[MAXLINE+1];
- char i,j,k,c;
- char operatori[MAXLINE / 2];
- float operandi[MAXLINE / 2];
- int brChar, brOperatora;
- int operandPos, operatorPos, rb;
- char errorF;
- while(1){
- operandPos=0;
- operatorPos=0;
- errorF=0;
- brChar = newreadline(ulaz,MAXLINE);
- printf("%s",ulaz);
- if(brChar){ // broj karaktera mora biti > 0
- if(ulaz[0] <'0' || ulaz[0] > '9'){
- continue;
- }
- for(i=0; i<brChar; i++){
- if((ulaz[i] < '0' || ulaz[i] > '9')
- && ulaz[i] != '+' && ulaz[i] != '-' && ulaz[i] != '*' && ulaz[i] != '/'){
- continue;
- }
- }
- brOperatora=0;
- for(i=0; i<brChar; i++){
- if(ulaz[i] == '+' || ulaz[i] == '-' || ulaz[i] == '*' || ulaz[i] == '/') brOperatora++;
- }
- if(brOperatora==0){
- continue;
- }
- if(ulaz[brChar-1] =='+' || ulaz[brChar-1] == '-' || ulaz[brChar-1] == '*' || ulaz[brChar-1] == '/'){
- continue;
- }
- for(i=0; i<brChar-1; i++){
- if((ulaz[i] == '+' || ulaz[i] == '-' || ulaz[i] == '*' || ulaz[i] == '/') &&
- (ulaz[i+1] == '+' || ulaz[i+1] == '-' || ulaz[i+1] == '*' || ulaz[i+1] == '/')){
- break;
- }
- }
- if(i<brChar-1) continue;
- }
- else continue;
- i=0;
- rb=brOperatora;
- while(rb){
- sscanf(ulaz, "%f", &operandi[operandPos]);
- operandPos++;
- while(i<brChar){
- if(ulaz[i] == '+' || ulaz[i] == '-' || ulaz[i] == '*' || ulaz[i] == '/' ){
- operatori[operatorPos] = ulaz[i];
- operatorPos++;
- k=0;
- for(j=i+1; j<brChar; j++,k++){
- ulaz[k] = ulaz[j];
- }
- ulaz[k] = 0;
- brChar -= i+1;
- i=0;
- break;
- }
- i++;
- }
- rb--;
- }
- sscanf(ulaz, "%f", &operandi[operandPos]);
- while(brOperatora){
- for(i=0; i< brOperatora; i++){
- if(operatori[i] == '*' || operatori[i] == '/') break;
- }
- if(i<brOperatora){
- if(operatori[i] == '*')
- operandi[i] *= operandi[i+1];
- else
- if(operandi[i+1]==0){
- printf("Error - dijeljenje s 0!\n");
- errorF = 1;
- break;
- }
- else operandi[i] /= operandi[i+1];
- for(j=i+1; j<brOperatora+1; j++){
- operandi[j] = operandi[j+1];
- }
- for(j=i; j<brOperatora-1; j++){
- operatori[j] = operatori[j+1];
- }
- brOperatora--;
- }
- else{
- for(i=0; i< brOperatora; i++){
- if(operatori[i] == '+' || operatori[i] == '-') break;
- }
- if(i<brOperatora){
- if(operatori[i] == '+')
- operandi[i] += operandi[i+1];
- else
- operandi[i] -= operandi[i+1];
- for(j=i+1; j<brOperatora+1; j++){
- operandi[j] = operandi[j+1];
- }
- for(j=i; j<brOperatora-1; j++){
- operatori[j] = operatori[j+1];
- }
- brOperatora--;
- }
- }
- }
- if(!errorF)
- printf("=%f\n",operandi[0]);
- }
- return 0;
- }
- char newreadline(char *buf, int max){
- int c;
- char *start, *end;
- start = buf;
- end = buf + max;
- while((c = getchar()) != '\n')
- if(buf < end) *buf++ = c;
- *buf = 0;
- return buf - start;
- }
- nekiheder.h
- #ifndef KALK_H
- #define KALK_H
- #define MAXLINE 200
- char newreadline(char *buf, int max);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement