- #include <iostream>
- #include <cstdlib>
- #include <cctype>
- using namespace std;
-
- void espace(char *str);
- double expr (char *str);
- double term(char *str,int& index);
- double nombre (char *str,int& index);
-
- const int max = 80;
-
-
-
-
- int main()
- {
- char buffer[max]={0};
-
- cout<<endl
- <<"Salut!Saisir"
- <<endl;
-
- for(;;)
- {
- cin.getline(buffer,sizeof buffer);
- espace(buffer);
-
- if (!buffer[0])
- return 0;
-
- cout << "\t="<<expr(buffer)
- <<endl<<endl;
- }
- }
-
- // efface les espace
- void espace (char *str)
- {
- int i=0;
- int j=0;
-
- while ((*(str + i)=*(str + j++))!='\0')
-
- if (*(str + i)!=' ')
- i++;
-
- return;
- }
-
- //extrait le premier term et fait l'operation
- double expr (char *str)
- {
- double valeur=0.0;
- int index=0;
- valeur = term(str,index);
-
- for(;;)
- {
- switch(*(str + index++))
- {
- case '\0':
- return valeur;
- break;
-
- case '+':
- valeur+=term(str,index);
- break;
-
- case '-':
- valeur-=term(str,index);
- break;
-
- default:
- cout<<endl
- <<"Erreur mec #?~§!!!!"
- <<endl;
-
- exit(1);
- }
- }
- }
-
- //operation mult et div
- double term(char *str,int& index)
- {
- double valeur =0.0;
- valeur = nombre(str,index);
-
- while ((*(str + index) =='*') || (*(str + index) == '/'))
- {
- if (*(str + index) =='*')
- valeur *=nombre(str, ++index);
-
- if (*(str + index) =='/')
- valeur /=nombre(str, ++index);
- }
- return valeur;
- }
-
- //du char en nombre puis la virgule
- double nombre (char *str,int &index)
- {
- double valeur =0.0;
-
- /*if(*(str+index) == '(')
- {
- char* psubstr =0;
- psubstr = extract(str,++index);
- valeur=expr(psubstr)
- delete[]psubstr;
- return valeur;
- }*/
-
- while (isdigit(*(str + index)))
- valeur = 10*valeur + (*(str + index++) - 48);
-
- if (*(str + index )!='.')
- return valeur ;
-
- double factor = 1.0;
- while (isdigit(*(str + (++index))))
- {
- factor *=0.1;
- valeur = valeur + (*(str + index)-48)*factor;
- }
-
- return valeur;
- }
-
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
void espace(char *str);
double expr (char *str);
double term(char *str,int& index);
double nombre (char *str,int& index);
const int max = 80;
int main()
{
char buffer[max]={0};
cout<<endl
<<"Salut!Saisir"
<<endl;
for(;;)
{
cin.getline(buffer,sizeof buffer);
espace(buffer);
if (!buffer[0])
return 0;
cout << "\t="<<expr(buffer)
<<endl<<endl;
}
}
// efface les espace
void espace (char *str)
{
int i=0;
int j=0;
while ((*(str + i)=*(str + j++))!='\0')
if (*(str + i)!=' ')
i++;
return;
}
//extrait le premier term et fait l'operation
double expr (char *str)
{
double valeur=0.0;
int index=0;
valeur = term(str,index);
for(;;)
{
switch(*(str + index++))
{
case '\0':
return valeur;
break;
case '+':
valeur+=term(str,index);
break;
case '-':
valeur-=term(str,index);
break;
default:
cout<<endl
<<"Erreur mec #?~§!!!!"
<<endl;
exit(1);
}
}
}
//operation mult et div
double term(char *str,int& index)
{
double valeur =0.0;
valeur = nombre(str,index);
while ((*(str + index) =='*') || (*(str + index) == '/'))
{
if (*(str + index) =='*')
valeur *=nombre(str, ++index);
if (*(str + index) =='/')
valeur /=nombre(str, ++index);
}
return valeur;
}
//du char en nombre puis la virgule
double nombre (char *str,int &index)
{
double valeur =0.0;
/*if(*(str+index) == '(')
{
char* psubstr =0;
psubstr = extract(str,++index);
valeur=expr(psubstr)
delete[]psubstr;
return valeur;
}*/
while (isdigit(*(str + index)))
valeur = 10*valeur + (*(str + index++) - 48);
if (*(str + index )!='.')
return valeur ;
double factor = 1.0;
while (isdigit(*(str + (++index))))
{
factor *=0.1;
valeur = valeur + (*(str + index)-48)*factor;
}
return valeur;
}