در این برنامه می توانید عدد هم بدهید
در این برنامه می توانید چند حرف هم بدهید
حرف ها باید با حروف بزرگ باشند مثلا AB و FG
برای مثال عبارت AB*V-A/(A^32-48+(8*CD%3)-A/5)+805/GFH+Y را وارد کنید
include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
#include <string.h>
//********************************* Mid *********************************//
int mid ( char source[] , int start , int count , char *dest )
{
//int c = 0;
for ( int i = start ; i < start + count ; i++)
*dest = source[i];
return 0;
}
//********************************* String Stack *********************************//
struct strstack
{
char info;
strstack *link;
}*strs;
int strpush ( char item )
{
strstack *ptr;
ptr = (strstack*) malloc ( sizeof ( strstack ));
ptr->link = strs;
ptr->info = item;
strs = ptr;
return 0;
}
char strpop()
{
strstack *ptr;
char temp;
ptr = strs;
strs = strs->link;
temp = ptr->info;
free( ptr );
return temp;
}
//********************************* Linked List *********************************//
struct pulishstack
{
int id;
char info[10];
pulishstack *link;
}*pulishs;
int pulishpush( char item[] , int id)
{
pulishstack *ptr ;
ptr = ( pulishstack*) malloc (sizeof (pulishstack));
strcpy ( ptr->info , item );
ptr->id = id;
ptr->link = pulishs;
pulishs = ptr;
return 0;
}
int pulishpop(int id , char item[] )
{
pulishstack *ptr;
ptr = pulishs;
while ( ptr != NULL )
{
if ( ptr->id == id )
{
strcpy (item , ptr->info );
return 0;
}
ptr = ptr->link;
}
return 0;
}
//********************************* Priority *********************************//
int priority ( char str1 ,char str2 )
{
if ( str1 == '^' ) return 1;
if ( str2 == '^' ) return 0;
if ( str1 == '*' || str1 == '/' || str1 == '%' ) return 1;
if ( str2 == '*' || str2 == '/' || str2 == '%' ) return 0;
return 1;
}
//********************************* Pulish *********************************//
int pulish( char p[] , int len )
{
char temp[10] , ctemp , ctemp2[2];
int count = 0 , c = 0;
for ( int i = 0 ; i < len ; i++ )
{
mid ( p , i , 1 , &ctemp );
switch (ctemp)
{
case '(' :
strpush( '(' );
break;
case ')' :
if ( count != 0)
pulishpush (temp , C++);
count = 0;
while (strs->info != '(')
{
ctemp2[0] = strpop();
ctemp2[1] = 0x0;
pulishpush ( ctemp2 , C++);
}
strpop();
break;
case '^' :
case '*' :
case '%' :
case '/' :
case '+' :
case '-' :
if ( count != 0)
{
pulishpush (temp, C++);
count = 0;
}
if ( priority ( strs->info , ctemp ))
{
while ( strs->info != '(' && priority ( strs->info , ctemp ))
{
ctemp2[0] = strpop();
ctemp2[1] = 0x0;
pulishpush ( ctemp2 ,C++ );
}
}
strpush(ctemp);
break;
default :
if ( ctemp >= 48 && ctemp <= 122 )
{
temp[count++] = ctemp;
temp[count] = 0x0;
}
}
}
return c-1;
}
//********************************* Long Stack *********************************//
struct lngstack
{
long info;
lngstack *link;
}*lngs;
int lngpush ( long item )
{
lngstack *ptr;
ptr = (lngstack*) malloc ( sizeof ( lngstack));
ptr->info = item;
ptr->link = lngs;
lngs = ptr;
return 0;
}
long lngpop()
{
lngstack *ptr;
ptr = lngs;
lngs = lngs->link;
long temp;
temp = ptr->info;
free (ptr);
return temp;
}
//********************************* Power *********************************//
long power ( long x , long y)
{
long temp = 1;
for ( long i = 0 ; i < y ; i++)
temp *= x;
return temp;
}
//********************************* Char *********************************//
struct mychar
{
char chr[10];
long value;
mychar *link;
}*mychars;
long charexist(char item[])
{
mychar *ptr;
ptr = mychars;
while ( ptr != NULL )
{
if ( strcmp ( item , ptr->chr ) == 0 )
return ptr->value;
ptr = ptr->link;
}
return 0;
}
int charinsert(char item[] , long value)
{
mychar *ptr;
ptr = (mychar*) malloc(sizeof(mychar));
strcpy( ptr->chr , item );
ptr->value = value;
ptr->link = mychars;
mychars = ptr;
return 0;
}
//********************************* Value *********************************//
long value (int count)
{
char temp[10];
long tmp;
long x , y , result;
for ( int i = 0 ; i <= count ; i++)
{
pulishpop ( i , temp );
if ( (temp [0] >= 98 && temp[0] <= 122) || (temp [0] >= 65 && temp[0] <= 90))
{
tmp = charexist ( temp );
if ( tmp == 0 )
{
printf ( "Please Enter The Number For %s " , temp );
cin >> tmp;
lngpush ( tmp);
charinsert ( temp , tmp );
}
else
{
lngpush ( tmp);
}
continue;
}
if ( temp[0] > 47 && temp[0] < 58 )
{
tmp = atol(temp);
lngpush ( tmp);
continue;
}
y = lngpop();
x = lngpop();
if ( temp[0] == '+' )
{
result = x + y;
lngpush(result);
continue;
}
if ( temp[0] == '*' )
{
result = x * y;
lngpush(result);
continue;
}
if ( temp[0] == '/' )
{
result = x / y;
lngpush(result);
continue;
}
if ( temp[0] == '%' )
{
result = x % y;
lngpush(result);
continue;
}
if ( temp[0] == '-' )
{
result = x - y;
lngpush(result);
continue;
}
if ( temp[0] == '^' )
{
result = power(x , y);
lngpush(result);
continue;
}
}
return lngpop();
}
//********************************* Main *********************************//
int main()
{
clrscr();
char p[102];
int len , plen = 0;
printf ( "Algebra Phrases\nPlease Enter The Phrase\n For Example : \nAB*V-A/(A^32-48+(8*CD%3)-A/5)+805/GFH+Y \n" );
scanf ( "%s" , p );
len = strlen( p );
for ( int i = len ; i > 0 ; i--)
{
p [i] = p [i - 1];
if ( p[i] == ')' ) plen++;
if ( p[i] == '(' ) plen--;
if ( plen < 0 )
{
printf ("Error In Phrase\nUnexpected ')' or '(' ");
getch();
return 0;
}
}
if ( plen != 0 )
{
printf ("Error In Phrase\nUnexpected ')' or '(' ");
getch();
return 0;
}
p[ 0 ] = '(' ;
p[ ++len ] = ')' ;
p[ ++len ] = 0x0;
int c;
c = pulish ( p , len );
long result;
result = value(c);
cout << result;
getch();
return 0;
}