The BODMAS Class - Extracting numbers from string and performing mathematical calculations
Author: Ravichandran J.V.
Rating:
Rate this Resource
Visits: 699
Discuss in Newsgroups
using System;
class Bodmas
{
public static string s;
public Bodmas()
{
s=null;
}
public static void Test(string str)
{
char ch=' ';
int ctr=1,plusctr=0,slashctr=0,result=0;
string s1=null,s2=null,s3=null,s4=null;
bool plusSign=false,divSign=false,flag=false;
while (ch!='\n')
{
ch=str[ctr];
if (ch!='('){
if (ch!=')'){
s1+=ch;
}
}
ctr++;
}
s1+='\n';
ctr=0;
while (flag==false){
if (s1[ctr]=='+'){
int plus=s2.ToInt32();
plusSign=true;
plusctr=ctr;
flag=true;
if (s1[ctr]=='-'){
if ( s1[ctr]=='*'){
if (s1[ctr]=='/'){
}
}
}
}
else{
s2+=s1[ctr];
}
ctr++;
}
ctr=plusctr+1;
while (flag==true)
{
if (s1[ctr]=='/'){
slashctr=ctr;
divSign=true;
flag=false;
}
else{
s3+=s1[ctr];
}
ctr++;
}
ctr=slashctr+1;
while (flag==false)
{
if (s1[ctr]=='\n')
{
flag=true;
}
else{
s4+=s1[ctr];
}
ctr++;
}
if (plusSign==true)
{
result=s2.ToInt32()+s3.ToInt32();
}
if (divSign==true)
{
result=result/s4.ToInt32();
}
Console.WriteLine( result);
}
public static void Main()
{
Test("(10+6)/8\n");
}
}
Visit my guru profile
Visitor Comments
Be the first to rate this code sample!