here i added comments to it now
Code:
//Alexander Constantin Bledea - Beginner Programming Challenge #11
#include <stdio.h> //standard for i/o
#include <stdlib.h> // standard again
#include <string.h> // string library
#include <math.h> // for the power function
int i,true=2,k[99],is,is2,w,iv,si,dubios,nc,number,zero,neo,num[99];
/*
i is standard
true is something that is always true since i don't know the c version of "while true"
is is the empty spaces in the string counter
is2 is the 'how much till the end' counter
w is like a secondary i
si counts how many words are between two spaces since operators can only use one space
dubios seeks dubious behaviour
nc is the number counter
number is the temporary number
zero alerts if someone wants to divide by 0
neo is the not enough operators counter
num is the number array
*/
char x[100];
//the string
float n_to_f;
// for the dubious output
int main(){
while (true){
for (i=0;i<99;++i) num[i]=k[i]=0; // reset the arrays
i=is=is2=w=iv=si=dubios=nc=zero=neo=number=0; // reset the variables
printf("> ");
gets(x);
if (strcmp(x,"exit")== 0) break;
for (i=0;i<strlen(x);++i){ //find empty spaces and keep track of them in k
if (x[i]==' ') {
k[is] = i;
++is;
}
}
if (k[is] != strlen(x)) {k[is]=strlen(x);++is;} // add to the counter the last element
iv=0;// needed later on
for (w=0;w<is;++w){ // for all the empty spaces
is2=k[w]-iv; //how many characters does the string fragment have
number=0; //reset the number
for (i=iv;i<k[w];++i){ //figure out what the number is
si=is2;
switch(x[i]){
case '0'...'9': number += ((x[i] - '0') * pow( 10.0 , is2-1));
--is2 ;
if (is2==0) { //when is reaches 0 save the number
num[nc]=number;
++nc;
}
break;
case '+': if (si==1){ if(nc>1){ num[nc-2]=num[nc-2]+num[nc-1]; num[nc-1]=num[nc];--nc;} else ++neo;} else ++dubios;break;
case '-': if (si==1){ if(nc>1){ num[nc-2]=num[nc-2]-num[nc-1]; num[nc-1]=num[nc];--nc;} else ++neo;} else ++dubios;break;
case '/': if (si==1){ if(nc>1){ if (num[nc-1]!=0){ num[nc-2]=num[nc-2]/num[nc-1]; num[nc-1]=num[nc]; --nc;} else ++zero;} else ++neo;} else ++dubios;break;
case '*': if (si==1){ if(nc>1){ num[nc-2]=num[nc-2]*num[nc-1]; num[nc-1]=num[nc]; --nc;} else ++neo;} else ++dubios;break;
case '^': if (si==1){ if(nc>1){ num[nc-2]=pow(num[nc-2],num[nc-1]); num[nc-1]=num[nc]; --nc;} else ++neo;} else ++dubios;break;
case '%': if (si==1){ if(nc>1){ num[nc-2]=num[nc-2]%num[nc-1]; num[nc-1]=num[nc]; --nc;} else ++neo;} else ++dubios;break;
//for all cases of operands if the string fragment is wider than 1 caracter mark it as dubious behavior and if you dont have at least 2 operands mark it down
default: ++dubios;break;
//anything else goes to dubious behaviour
}
}
iv=k[w];//we need this to find out the word size
++iv;
}
n_to_f=num[nc-1];//transform it to float because thats what the post asks for
if (dubios==0){//if nothing is dubious ... this should be obvious
if (zero!=0) printf("Divided by Zero\n");
else if (neo>0) printf("Not enough operands\n");
else if (neo==0 &&nc>1) printf("Too many operands\n");
else if (nc==0); //if empty string
else printf("%.1f\n",n_to_f); }// if everything goes to plan print the number
else printf("Unrecognized token\n");
}
return 0;
}
Bookmarks