Per ora ha le 4 operazioni base e l'elevamento a potenza , appena ne ho voglia potrei provare ad aggiungere qaulche funzione tipo coseno , seno ecc...
- Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct stackNode {
char data ;
struct stackNode * next ;
};
struct nodoint {
int data ;
struct nodoint * next ;
};
typedef struct stackNode StackNode ;
typedef StackNode * StackNodePtr ;
typedef struct nodoint NodoInt ;
typedef NodoInt * NodoIntPtr ;
int Valute ( char * ) ;
char * convertToPostfix ( char * infix ) ;
int isOperator ( char ) ;
int precedence ( char , char ) ;
void push ( StackNodePtr * , char ) ;
int pushInt ( NodoIntPtr * , int ) ;
int popInt ( NodoIntPtr * ) ;
char pop ( StackNodePtr * ) ;
char stackTop ( StackNodePtr ) ;
int isEmpty ( StackNodePtr ) ;
int calculate ( int , int , char ) ;
int main ( void )
{
printf ( "Calcolatore C \n\n"
"<expression> <-p>\n\n"
"<expression> = Espressione da risolvere \n"
"<-p> = Opzionale , visualizza l'espressione in notazione polacca\n\n");
char infix [ 512 ] = "" ;
int debug = 0 , polacca = 0 ;
char * pos ;
while ( 1 ) {
sprintf ( infix , "" );
printf ( "\nEspressione -> " ) ;
fflush ( stdin ) ; gets ( infix ) ;
if ( (pos=strstr ( infix , "-p" )) != NULL ){
strcpy ( pos , "\0" ) ;
printf ( "\nNotazione polacca : %s\n" , convertToPostfix ( infix ) );
}
printf ( "\n==>%d\n", Valute ( convertToPostfix ( infix ) ) ) ;
}
fflush(stdin); getchar();
return 0;
}
void push ( StackNodePtr * s , char c )
{
StackNodePtr nuovo = ( StackNodePtr ) malloc ( sizeof ( StackNode ) ) ;
nuovo->data = c ;
nuovo->next = *s ;
*s = nuovo ;
}
int pushInt ( NodoIntPtr * n , int i )
{
if ( i == EOF ) return EOF ;
NodoIntPtr nuovo = ( NodoIntPtr ) malloc ( sizeof ( NodoIntPtr ) ) ;
nuovo->data = i ;
nuovo->next = *n ;
*n = nuovo;
}
int isEmpty ( StackNodePtr s )
{
return ( s == NULL ) ? 1 : 0 ;
}
int isOperator ( char c )
{
switch ( c ) {
case '+' :
case '-' :
case '*' :
case '/' :
case '^' :
return 1 ;
break;
}
return 0;
}
int precedence ( char a , char b )
{
if ( a == '+' || a == '-' ) return 1 ;
if ( a == '*' || a == '/' )
if ( b == '/' || b == '*' || b == '^' )
return 1 ;
if ( a == '^' ) return -1 ;
return -1;
}
char stackTop ( StackNodePtr s )
{
return s -> data ;
}
char pop ( StackNodePtr * s )
{
StackNodePtr tmp ;
char pop ;
tmp = *s ;
pop = (*s)->data;
*s = (*s)->next ;
free ( tmp ) ;
return pop ;
}
int popInt ( NodoIntPtr * s )
{
NodoIntPtr tmp ;
int pop ;
tmp = *s ;
pop = (*s)->data;
*s = (*s)->next ;
free ( tmp ) ;
return pop ;
}
int calculate ( int a , int b , char c )
{
int j , res = 1 ;
if ( c == '+' ) return a + b ;
else if ( c == '*' ) return a * b ;
else if ( c == '-' ) return b - a ;
else if ( c == '/' ){
if ( a == 0 )
return EOF ;
else
return b / a ;
}
else if ( c == '^' ) {
for ( j = 0 ; j < a ; j++ )
res *= b ;
return res ;
}
}
char * convertToPostfix ( char * infix )
{
int tmp , j , k ;
StackNodePtr stackPtr = NULL ;
char * postfix = ( char * ) malloc ( strlen ( infix ) ) ;
sprintf ( postfix , "" ) ;
strcat ( infix , ")" ) ;
push ( &stackPtr , '(' ) ;
for ( j = 0 , k = 0 ; !isEmpty ( stackPtr ) ; j++ ){
if ( isdigit ( infix [ j ] ) ){
for ( ; isdigit ( infix [ j ] ) ; k++ , j++ )
postfix [ k ] = infix [ j ] ;
postfix [ k++ ] = ' ' ;
}
if ( infix [ j ] == '(' )
push ( &stackPtr , '(' ) ;
if ( isOperator ( infix [ j ] ) ) {
for ( ; precedence ( infix [ j ] , stackTop ( stackPtr ) ) != -1 && isOperator ( stackTop ( stackPtr ) ) ; k++ )
postfix [ k ] = pop ( &stackPtr ) ;
push ( &stackPtr , infix [ j ] ) ;
}
if ( infix [ j ] == ')' ) {
for ( ; stackTop ( stackPtr ) != '(' ; k++ )
postfix [ k ] = pop ( &stackPtr ) ;
pop ( &stackPtr ) ;
}
}
return postfix ;
}
int Valute ( char * postfix )
{
NodoIntPtr risolvi = NULL ;
int j , a , tmp ;
char temporany [ 10 ] = "" ;
for ( j = 0 ; postfix [ j ] != '\0' ; j++ ) {
if ( isdigit ( postfix [ j ] ) ){
for ( sprintf ( temporany , "" ) , a = 0 ; isdigit ( postfix [ j ] ) ; a++ , j++ )
temporany [ a ] = postfix [ j ] ;
temporany [ a++ ] = '\0' ;
pushInt ( &risolvi , atoi ( temporany ) ) ;
}
else if ( isOperator ( postfix [ j ] ) ){
if ( pushInt ( &risolvi , calculate ( popInt ( &risolvi ) , popInt ( &risolvi ) , postfix [ j ] ) ) == EOF ){
printf ( "\nErrore : divisione per zero -> Failed\n" ) ;
return -1;
}
}
}
return popInt ( &risolvi ) ;
}



