All source code in .Net Ask a .Net Pro Discussion Forum Categories All jobs in .Net
class,solve,calculationsif,will,return
   Code/Articles � |  Newest/Best � |  Community � |  Jobs � |  Other � |  Goto � | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
.Net Stats

 Code: 671,337. lines
 Jobs: 822. postings

 How to support the site

 
Sponsored by:
Quick Search for:  in language:    
You are in:
 
Login





Latest postings for .Net.
Click here to see a screenshot of this code!Blaster Master
By T.Jackson on 3/12

(Screen Shot)

Click here to see a screenshot of this code!WPF Clock - with stopwatch
By Timo Boehme on 3/11

(Screen Shot)

web hosting by .net
By p v sreeraj nambiar on 3/10


Click here to see a screenshot of this code!web hosting with well screenplay
By p v sreeraj nambiar on 3/9

(Screen Shot)

Click here to see a screenshot of this code!Media Player
By Maheshs Shitole on 3/9

(Screen Shot)

Click here to see a screenshot of this code!flexi grid
By maytel.mynt on 1/12

(Screen Shot)

CONNECTION!
By p v sreeraj nambiar on 3/8


working employeee details
By p v sreeraj nambiar on 3/8


MySQL Front
By C. David Hamilton on 12/6


Click here to see a screenshot of this code!Serial CommPort simple demo
By Timo Boehme on 2/26

(Screen Shot)

PPM and PGM Images Read Write
By BuzzMiester on 3/6


Click here to see a screenshot of this code!Picture Editor
By arifliminto86 on 3/5

(Screen Shot)

Click here to see a screenshot of this code!Accounting System for Loans and Collectibles
By Jonathan Bantang on 3/3

(Screen Shot)

Click here to see a screenshot of this code!TicTacToe
By Maheshs Shitole on 3/3

(Screen Shot)

Click here to see a screenshot of this code!Testimage gerator / Screen saver
By Timo Boehme on 3/2

(Screen Shot)

Composition
By Mark Griffeth on 2/28


Click here to see a screenshot of this code!iconWorks - Updated...
By Todd S. Spangler on 2/26

(Screen Shot)

CIDR Calc (Classless Inter-Domain Routing)
By luce2047 on 2/26


NFSU II SaveGame Money Editor
By silviu petre on 2/26


Extension Method in ASP.NET 3.5
By weblabs on 2/25


Click here to see a screenshot of this code!Dock To external program
By Kevin S. Gallagher on 2/25

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!



 
 
   

calculations class

Print
Email
 
VB icon
Submitted on: 1/28/2010 8:41:03 AM
By: silviu petre 
Level: Intermediate
User Rating: Unrated
Compatibility:C#

Users have accessed this code 1101 times.
 
 
     class that can solve calculations. if we have (2+3)/(25*65+4)+4*4-2^3+4^-2+2^2^0.5 it will return 10.0655693677103
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    //**************************************
    //     
    // Name: calculations class
    // Description:class that can solve calc
    //     ulations.
    if we have (2+3)/(25*65+4)+4*4-2^3+4^-2+2^2^0.5
    it will return 10.0655693677103
    // By: silviu petre
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/vb/scripts/Sh
    //     owCode.asp?txtCodeId=7650&lngWId=10    //for details.    //**************************************
    //     
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Globalization;
    namespace SimpleCalc
    {
    public class EvaluateClass
    {
    string DecimalSeparator = ".";
    public string Calculate(string ToCalculate)
    {
    DecimalSeparator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.ToString();
    string r = ToCalculate;
    try
    {
    if (Verify(ToCalculate))
    {
    ErrorPower = false;
    ToCalculate = Correct(ToCalculate);
    ToCalculate = SolveAllPar(ToCalculate);
    r = Evaluate(ToCalculate);
    if (ErrorPower)
    {
    r = ToCalculate;
    ErrorPower = false;
    }
    }
    }
    catch
    { }
    return r;
    }
    bool ErrorPower = false;
    bool Verify(string x)
    {
    bool v = false;
    bool v1 = false;
    if (ParCheck(x))
    v1 = true;
    bool v2 = false;
    if (!StopEval(x))
    v2 = true;
    if (v1 & v2)
    v = true;
    return v;
    }
    bool ParCheck(string x)
    {
    bool b = false;
    int m = 0, n = 0;
    for (int i = 0; i < x.Length; i++)
    if (x.Substring(i, 1) == "(")
    m++;
    else if (x.Substring(i, 1) == ")")
    n++;
    if (m == n)
    b = true;
    return b;
    }
    bool StopEval(string x)
    {
    bool b = false;
    for (int i = 0; i < x.Length - 1; i++)
    switch (x.Substring(i, 2))
    {
    case "**":
    case "//":
    case "*/":
    case "/*":
    case "+*":
    case "-*":
    case "+/":
    case "-/":
    case "^*":
    case "^/":
    case "*^":
    case "/^":
    b = true; break;
    }
    return b;
    }
    
    string Sum(double[] x, string[] y)
    {
    double r = 0;
    for (int i = 0; i < x.Length; i++)
    {
    if (y[i] == "+")
    r += x[i];
    if (y[i] == "-")
    r -= x[i];
    }
    return Convert.ToString(r);
    }
    bool PartOfNumber(string x)
    {
    string NumberElements = DecimalSeparator + "0123456789";
    bool r = false;
    for (int i = 0; i < NumberElements.Length; i++)
    if (x == NumberElements.Substring(i, 1))
    {
    r = true;
    break;
    }
    return r;
    }
    string Evaluate(string x)
    {
    x = Correct(x);
    if (x.Length > 0)
    if (PartOfNumber(x.Substring(0, 1)))
    x = "+" + x;
    int NrT = 0;
    int NrF = 0;
    bool PON = false;
    bool Last = false;
    for (int i = 0; i < x.Length; i++)
    {
    PON = PartOfNumber(x.Substring(i, 1));
    if (!PON)
    {
    if (x.Length - i > 2)
    if (x.Substring(i, 2) == "E-" | x.Substring(i, 2) == "E+")
    PON = true;
    if (x.Length - i > 1 & i > 0)
    if (x.Substring(i - 1, 2) == "E-" | x.Substring(i - 1, 2) == "E+")
    PON = true;
    if (x.Length - i > 0 & i > 1)
    if (x.Substring(i - 2, 2) == "E-" | x.Substring(i - 2, 2) == "E+")
    PON = true;
    }
    if (i == 0)
    Last = !PON;
    if (Last != PON)
    {
    if (Last)
    NrF++;
    else
    NrT++;
    }
    Last = PON;
    }
    string[] T = new string[NrT];
    string[] F = new string[NrF];
    NrF = -1;
    NrT = -1;
    for (int i = 0; i < x.Length; i++)
    {
    PON = PartOfNumber(x.Substring(i, 1));
    if (!PON)
    {
    if (x.Length - i > 2)
    if (x.Substring(i, 2) == "E-" | x.Substring(i, 2) == "E+")
    PON = true;
    if (x.Length - i > 1 & i > 0)
    if (x.Substring(i - 1, 2) == "E-" | x.Substring(i - 1, 2) == "E+")
    PON = true;
    if (x.Length - i > 0 & i > 1)
    if (x.Substring(i - 2, 2) == "E-" | x.Substring(i - 2, 2) == "E+")
    PON = true;
    }
    if (i == 0)
    Last = !PON;
    if (Last != PON)
    {
    if (Last)
    {
    NrF++;
    F[NrF] += x.Substring(i, 1);
    }
    else
    {
    NrT++;
    T[NrT] += x.Substring(i, 1);
    }
    }
    else
    {
    if (!Last)
    F[NrF] += x.Substring(i, 1);
    else
    T[NrT] += x.Substring(i, 1);
    }
    Last = PON;
    }
    double[] D = new double[T.Length];
    for (int i = 0; i < D.Length; i++)
    D[i] = Convert.ToDouble(T[i]);
    int k = 1;
    for (int i = 0; i < F.Length; i++)
    {
    F[i] = F[i].Replace("*+", "*");
    F[i] = F[i].Replace("/+", "/");
    if (F[i] == "*-" | F[i] == "/-")
    {
    F[i] = F[i].Replace("*-", "*");
    F[i] = F[i].Replace("/-", "/");
    up:
    if (i - k + 1 > 0)
    switch (F[i - k])
    {
    case "+": F[i - k] = "-"; break;
    case "-": F[i - k] = "+"; break;
    case "*":
    case "/": k++; goto up;
    }
    }
    }
    return Power(D, F);
    }
    string Product(double[] x, string[] y)
    {
    int j = 0;
    for (int i = 0; i < y.Length; i++)
    if (y[i] == "+" | y[i] == "-")
    j++;
    double[] D = new double[j];
    string[] F = new string[j];
    j = 0;
    for (int i = 0; i < y.Length; i++)
    {
    if (y[i] == "*")
    D[j - 1] *= x[i];
    if (y[i] == "/")
    D[j - 1] /= x[i];
    if (y[i] == "+" | y[i] == "-")
    {
    D[j] = x[i];
    F[j] = y[i];
    j++;
    }
    }
    return Sum(D, F);
    }
    string Power(double[] x, string[] y)
    {
    int j = 0;
    for (int i = 0; i < y.Length; i++)
    if (y[i] == "+" | y[i] == "-" | y[i] == "*" | y[i] == "/")
    j++;
    double[] D = new double[j];
    string[] F = new string[j];
    j = 0;
    for (int i = 0; i < y.Length; i++)
    {
    if (y[i] == "^")
    {
    if (i > 0)
    {
    if (y[i - 1] == "-" & x[i] % 2 == 0 & x[i] == Math.Truncate(x[i]))
    F[j - 1] = "+";
    else if (y[i - 1] == "-" & x[i] != Math.Truncate(x[i]))
    ErrorPower = true;
    }
    D[j - 1] = Math.Pow(D[j - 1], x[i]);
    }
    else if (y[i] == "^-")
    {
    if (i > 0)
    {
    if (y[i - 1] == "-" & x[i] % 2 == 0 & x[i] == Math.Truncate(x[i]))
    F[j - 1] = "+";
    else if (y[i - 1] == "-" & x[i] != Math.Truncate(x[i]))
    ErrorPower = true;
    }
    D[j - 1] = Math.Pow(D[j - 1], -x[i]);
    }
    else if (y[i] == "+" | y[i] == "-" | y[i] == "*" | y[i] == "/")
    {
    D[j] = x[i];
    F[j] = y[i];
    j++;
    }
    }
    return Product(D, F);
    }
    string Correct(string x)
    {
    for (int i = 0; i < x.Length - 1; i++)
    {
    if (x.Substring(i, 2) == "--" | x.Substring(i, 2) == "++")
    {
    if (i > 0)
    x = x.Substring(0, i) + "+" + x.Substring(i + 2);
    else
    x = "+" + x.Substring(i + 2);
    i--;
    }
    else if (x.Substring(i, 2) == "+-" | x.Substring(i, 2) == "-+")
    {
    if (i > 0)
    x = x.Substring(0, i) + "-" + x.Substring(i + 2);
    else
    x = "-" + x.Substring(i + 2);
    i--;
    }
    }
    return x;
    }
    string SolveAllPar(string x)
    {
    int j = 0;
    for (int i = 0; i < x.Length; i++)
    if (x.Substring(i, 1) == "(")
    j++;
    for (int i = 0; i < j; i++)
    x = SolvePar(x);
    return x;
    }
    string SolvePar(string x)
    {
    int i, j;
    for (i = x.Length - 1; i >= 0; i--)
    if (x.Substring(i, 1) == "(")
    break;
    for (j = i + 1; j < x.Length; j++)
    if (x.Substring(j, 1) == ")")
    break;
    string r = "";
    try
    {
    r = x.Substring(0, i) + Evaluate(x.Substring(i + 1, j - i - 1)) + x.Substring(j + 1);
    }
    catch
    {
    r = x.Substring(0, i) + Evaluate(x.Substring(i + 1, j - i - 1));
    }
    return r;
    }
    }
    }


Other 6 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.


 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Games | Feedback | Customize | .Net Home | Site Home | Other Sites | Open Letter from Moderators | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright� 1997-2010 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.