Senin, 17 Oktober 2011

Simple Calculator Using c#

Baru-baru belajar FCBD, ngebuat simple Calculator String dengan c#
sebenarnya operator nya masih + - / * plus priority "(" dan ")".
jadi masi simple

contoh : 8*2 + (5*2) = 26
(7+2)*8 = 72

cekidot...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;

namespace Collection
{
    class ArithmeticDemo
    {
        public static void Main() {
            ArrayList operation = new ArrayList();
            Console.WriteLine(" Contoh inputan : 8*(2-1+(7-3+(4-(4+7)-(1+1))))");
            string str = Console.ReadLine();
            ArrayList oper = new ArrayList();
            ArrayList all = new ArrayList();
            addOperator(oper);
            divideChar(all, oper, str);
            for (int i = 0; i < all.Count; i++){
                Console.Write(all[i] + " ");
            }
            Console.WriteLine(" = " + hitung(all, oper, str));
            Console.ReadKey();
        }

        static void addOperator(ArrayList al) {
            al.Add('*');
            al.Add('+');
            al.Add('-');
            al.Add('/');
        }

        static double hitung(ArrayList al, ArrayList al2, string str) {

            for (int i = 0; i < al.Count; i++) {
                if (Regex.IsMatch(Convert.ToString(al[i]), @"\(*\)")) {
                    ArrayList alTemp = new ArrayList();
                    string s = Convert.ToString(al[i]);
                    s = s.Substring(1, s.Length - 2);
                    divideChar(alTemp, al2, s);
                    al[i] = hitung(alTemp, al2, str);
                }
            }

            while (al.IndexOf('*') != -1){
                int n = al.IndexOf('*');
                al[n] = Convert.ToDouble(al[n - 1]) * Convert.ToDouble(al[n + 1]);
                al[n + 1] = null;
                al.Remove(al[n + 1]);
                al[n - 1] = null;
                al.Remove(al[n - 1]);
            }

            while (al.IndexOf('/') != -1){
                int n = al.IndexOf('/');
                al[n] = Convert.ToDouble(al[n - 1]) / Convert.ToDouble(al[n + 1]);
                al[n + 1] = null;
                al.Remove(al[n + 1]);
                al[n - 1] = null;
                al.Remove(al[n - 1]);
            }


            while(al.IndexOf('-') != -1) {
                int n = al.IndexOf('-');
                al[n] = Convert.ToDouble(al[n - 1]) - Convert.ToDouble(al[n + 1]);
                al[n + 1] = null;
                al.Remove(al[n + 1]);
                al[n - 1] = null;
                al.Remove(al[n - 1]);
            }

            while (al.IndexOf('+') != -1){
                int n = al.IndexOf('+');
                al[n] = Convert.ToDouble(al[n - 1]) + Convert.ToDouble(al[n + 1]);
                al[n + 1] = null;
                al.Remove(al[n + 1]);
                al[n - 1] = null;
                al.Remove(al[n - 1]);
            }

            return Convert.ToDouble(al[0]);
        }

        static void divideChar(ArrayList al, ArrayList al2,string s) {
            char[] str2 = s.ToArray();
            int stat = 0;
            string temp = "";
            bool inbracket = false;
            int countBracket = 0;
            foreach (char c in str2){
                foreach (char cc in al2){
                    if (c == cc && inbracket == false){
                        stat = 1;
                    }
                }

                if (stat == 1 && inbracket == false){
                    if (temp == ""){
                        al.Add("0");
                        al.Add(c);
                    }
                    else{
                        al.Add(temp);
                        al.Add(c);
                    }
                    temp = "";
                    stat = 0;
                }
                else if (stat == 0 && inbracket == false){
                    temp = temp + "" + c;
                }
                else {
                    temp = temp + "" + c;
                }

                if (c == '(' && inbracket == false){
                    inbracket = true;
                    countBracket++;
                }
                else if(c == '(' && inbracket == true){
                    countBracket++;
                }

                if (c == ')' && countBracket > 1){
                    countBracket--;
                }
                else if (c == ')' && countBracket > 0){
                    countBracket--;
                    inbracket = false;
                }

            }
            if (stat == 0) {
                al.Add(temp);
            }
        }
    }
}

Tidak ada komentar:

Posting Komentar

Best Buy Coupons