locked
C# or LINQ - How to Find Exact Index and caluculate values. RRS feed

  • Question

  • User-1024101449 posted

    How to Find Exact Index and caluculate values.

    My requirement is below code and logic :


    int AddlMark = 150;

    string InputStr = "Mark1 + Mark2 - AddlMark + Mark3" - Mark4"; (Formula)

    string String1 = "Mark1-Mark2-Mark3-Mark4-Mark5-Mark6"; // and its values is below

    string String2 = "86-95-96-98-99-100"; (Mark1-Mark2-Mark3-Mark4-Mark5-Mark6)

    Using InputStr (Formula) I want to find the string first match the exact string from string1 and then followed by get values in string2 as order using Inputstr Formula.

    i want to use the above calculation using C# with LINQ.

    So the Final Output would be as below.

    double Result = 0;

    Result = 86 + 95 - 150 + 96 - 98; (As per Formula, we need to find and calculate the results)

    Result = 525 (Final)..

    How to write C# code using LINQ.?

    I am geting confused whether we need to go for LINQ or C#.

    Can anybody give sample idea to start the above logic using code..?

    Thursday, September 7, 2017 9:54 AM

Answers

  • User-707554951 posted

    Hi gani7787,

    Using the following code:

        static string result = "";
           static int s = 0;

    protected void Button1_Click(object sender, EventArgs e) { string InputStr = "DIM H + QUT OF PIP DIM"; var total = Caculate(InputStr); Response.Write(total); } protected int Caculate(string InputStr) { double AddlMark = 381; string String1 = "SPT TGA-DIM H-DIM L-PIP DIM-SPT PIP CLS"; // and its values is below string String2 = "S6-100-450-30-CS"; // Dictionary<string, string> direct = new Dictionary<string, string>(); string[] string1 = String1.Split('-'); string[] string2 = String2.Split('-'); string[] string3 = InputStr.Replace(" ",String.Empty).Replace("-", "+-").Split('+'); for (int i = 0; i < string1.Length; i++) { direct.Add(string1[i], string2[i]); } direct.Add("QUT OF PIP DIM", Convert.ToString(AddlMark)); foreach (string s1 in string3) { foreach (var d in direct) { string orinal = InputStr.Replace(" ", String.Empty).ToUpper(); string match = d.Key.Replace(" ", String.Empty).ToUpper(); //var flag = ; if (s == 1) { if (s1.TrimStart('-')== match) result = result.Replace(match, d.Value.ToString()); } if (s == 0) { if (s1.TrimStart('-') == match) { result = orinal.Replace(match, d.Value.ToString()); s = 1; } } } } int total = 0; var re = result.Replace("-", "+-"); string[] res = re.Split('+'); foreach (string st in res) { total += Convert.ToInt16(st); } return total; }

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 13, 2017 2:11 AM

All replies

  • User303363814 posted

    Why does it have to be linq?  Is that a part of the assignment?

    Thursday, September 7, 2017 11:54 PM
  • User-707554951 posted

    Hi 

    Friday, September 8, 2017 7:49 AM
  • User-707554951 posted

    Hi gani7787,

    it seems the final result you calculate is wrong. it should be 29.

    Please check the following code:

      static string result = "";
            static int i = 0;
            protected void Page_Load(object sender, EventArgs e)
            {         
                string InputStr = "Mark1+Mark2-AddlMark+Mark3-Mark4";
                var total = Caculate(InputStr);
                Response.Write(total);
    
    
            }
            protected int Caculate( string InputStr) {
                int AddlMark = 150;
                string String1 = "Mark1-Mark2-Mark3-Mark4-Mark5-Mark6"; // and its values is below
                string String2 = "86-95-96-98-99-100"; //(Mark1 - Mark2 - Mark3 - Mark4 - Mark5 - Mark6)
                Dictionary<string, int> direct = new Dictionary<string, int>();
                string[] string1 = String1.Split('-');
                string[] string2 = String2.Split('-');
                for (int i = 0; i < string1.Length; i++)
                {
                    direct.Add(string1[i], Convert.ToInt16(string2[i]));
                }
                direct.Add("AddlMark", AddlMark);
                foreach (var d in direct)
                {
    
                    string orinal = InputStr.ToLower();
                    string match = d.Key.ToLower();
                    var flag = orinal.IndexOf(match);
    
                    if (i == 1)
                    {
                        if (flag > 0)
                            result = result.Replace(match, d.Value.ToString());
                    }
                    if (i == 0)
                    {
                        if (flag >= 0)
                        {
                            result = orinal.Replace(match, d.Value.ToString());
                        }
                        i = 1;
                    }
                }
                int total = 0;
                var re = result.Replace("-", "+-");
                string[] res = re.Split('+');
                foreach (string st in res)
                {
                    total += Convert.ToInt16(st);
                }
                return total;
            }

    Output:

    Best regards

    Cathy

    Friday, September 8, 2017 9:45 AM
  • User-1024101449 posted

    Thanks for your excellent coding.

    Getting Error and result not coming of my final modified code

    Error line : 

    total += Convert.ToInt64(st);

    Final Code

     protected void Button8_Click(object sender, EventArgs e)
        {
    
            string InputStr = "DIM H + QUT OF PIP DIM";
            var total = Caculate(InputStr);
            Response.Write(total);
        }
    
        protected int Caculate(string InputStr)
        {
            double AddlMark = 381;
            string String1 = "SPT TGA-DIM H-DIM L-PIP DIM-SPT PIP CLS"; // and its values is below
            string String2 = "S6-100-450-30-CS"; //
            Dictionary<string, string> direct = new Dictionary<string, string>();
            string[] string1 = String1.Split('-');
            string[] string2 = String2.Split('-');
            for (int i = 0; i < string1.Length; i++)
            {
                direct.Add(string1[i], string2[i]);
            }
            direct.Add("QUT OF PIP DIM", Convert.ToString(AddlMark));
            foreach (var d in direct)
            {
    
                string orinal = InputStr.ToUpper();
                string match = d.Key.ToUpper();
                var flag = orinal.IndexOf(match);
    
                if (i == 1)
                {
                    if (flag > 0)
                        result = result.Replace(match, d.Value.ToString());
                }
                if (i == 0)
                {
                    if (flag >= 0)
                    {
                        result = orinal.Replace(match, d.Value.ToString());
                    }
                    i = 1;
                }
            }
            int total = 0;
            var re = result.Replace("-", "+-");
            string[] res = re.Split('+');
            foreach (string st in res)
            {
                total += Convert.ToInt64(st);
            }
            return total;
        }
    
        }

    What is the problem over here and where it is...????

    Monday, September 11, 2017 12:28 PM
  • User-707554951 posted

    Hi gani7787,

    After debug your code step by step. I found that you need to change the name in your ‘InputStr’ Formula string.

    PIP DIM——>PIP DIM Total

    Code as below:

     protected void Button1_Click(object sender, EventArgs e)
            {
                string InputStr = "DIM H + QUT OF PIP DIM";
                var total = Caculate(InputStr);
                Response.Write(total);
            }      
    
            protected int Caculate(string InputStr)
            {
                double AddlMark = 381;
             // string String1 = "SPT TGA-DIM H-DIM L-PIP DIM-SPT PIP CLS"; // and its values is below
           1.   string String1 = "SPT TGA-DIM H-DIM L-PIP DIM Total-SPT PIP CLS"; // and its values is below
                string String2 = "S6-100-450-30-CS"; //
                Dictionary<string, string> direct = new Dictionary<string, string>();
                string[] string1 = String1.Split('-');
                string[] string2 = String2.Split('-');
                for (int i = 0; i < string1.Length; i++)
                {
                    direct.Add(string1[i], string2[i]);
                }
                direct.Add("QUT OF PIP DIM", Convert.ToString(AddlMark));
               
                foreach (var d in direct)
                {
    
                    string orinal = InputStr.ToUpper();
                    string match = d.Key.ToUpper();
                    var flag = orinal.IndexOf(match);
    
                    if (s == 1)
                    {
                        if (flag > 0)
                            result = result.Replace(match, d.Value.ToString());
                    }
                    if (s == 0)
                    {
                        if (flag >= 0)
                        {
                            result = orinal.Replace(match, d.Value.ToString());
                            s = 1;
                        }
                       
                    }
                }
                int total = 0;
                var re = result.Replace("-", "+-");
                string[] res = re.Split('+');
                foreach (string st in res)
                {
              2.    total += Convert.ToInt16(st);
                }
                return total;
            }

    Output:

    Best regards

    Cathy

    Tuesday, September 12, 2017 2:35 AM
  • User-1024101449 posted

    Thanks for your reply...

     1.   string String1 = "SPT TGA-DIM H-DIM L-PIP DIM Total-SPT PIP CLS"; // and its values is below

    I understood your solution.

    But, This value coming from table. so we cannot change.

    instead of changing we need to find some other logic...?????

    i'm struggling...????

    also i tried your code. but other error is coming.

    Error : Input string is not in a correct format

     protected void Button8_Click(object sender, EventArgs e)
        {
    
            string InputStr = "DIM H + QUT OF PIP DIM";
            var total = Caculate(InputStr);
            Response.Write(total);
        }
    
        protected int Caculate(string InputStr)
        {
            double AddlMark = 381;
            string String1 = "SPT TGA-DIM H-DIM L-PIP DIM Total-SPT PIP CLS"; // and its values is below
            string String2 = "S6-100-450-30-CS"; //
            Dictionary<string, string> direct = new Dictionary<string, string>();
            string[] string1 = String1.Split('-');
            string[] string2 = String2.Split('-');
            for (int i = 0; i < string1.Length; i++)
            {
                direct.Add(string1[i], string2[i]);
            }
            direct.Add("QUT OF PIP DIM", Convert.ToString(AddlMark));
            foreach (var d in direct)
            {
    
                string orinal = InputStr.ToUpper();
                string match = d.Key.ToUpper();
                var flag = orinal.IndexOf(match);
    
                if (s == 1)
                {
                    if (flag > 0)
                        result = result.Replace(match, d.Value.ToString());
                }
                if (s == 0)
                {
                    if (flag >= 0)
                    {
                        result = orinal.Replace(match, d.Value.ToString());
                    }
                    s = 1;
                }
            }
            int total = 0;
            var re = result.Replace("-", "+-");
            string[] res = re.Split('+');
            foreach (string st in res)
            {
                total += Convert.ToInt16(st); // Error : Input string is not a corect format.
            }
            return total;
        }

    Also, i want to use PIP DIM only. not required PIP DIM Total.

       string String1 = "SPT TGA-DIM H-DIM L-PIP DIM Total-SPT PIP CLS"; // and its values is below

    pls. give suggestion...

    Tuesday, September 12, 2017 4:32 AM
  • User-707554951 posted

    Hi gani7787,

    Using the following code:

        static string result = "";
           static int s = 0;

    protected void Button1_Click(object sender, EventArgs e) { string InputStr = "DIM H + QUT OF PIP DIM"; var total = Caculate(InputStr); Response.Write(total); } protected int Caculate(string InputStr) { double AddlMark = 381; string String1 = "SPT TGA-DIM H-DIM L-PIP DIM-SPT PIP CLS"; // and its values is below string String2 = "S6-100-450-30-CS"; // Dictionary<string, string> direct = new Dictionary<string, string>(); string[] string1 = String1.Split('-'); string[] string2 = String2.Split('-'); string[] string3 = InputStr.Replace(" ",String.Empty).Replace("-", "+-").Split('+'); for (int i = 0; i < string1.Length; i++) { direct.Add(string1[i], string2[i]); } direct.Add("QUT OF PIP DIM", Convert.ToString(AddlMark)); foreach (string s1 in string3) { foreach (var d in direct) { string orinal = InputStr.Replace(" ", String.Empty).ToUpper(); string match = d.Key.Replace(" ", String.Empty).ToUpper(); //var flag = ; if (s == 1) { if (s1.TrimStart('-')== match) result = result.Replace(match, d.Value.ToString()); } if (s == 0) { if (s1.TrimStart('-') == match) { result = orinal.Replace(match, d.Value.ToString()); s = 1; } } } } int total = 0; var re = result.Replace("-", "+-"); string[] res = re.Split('+'); foreach (string st in res) { total += Convert.ToInt16(st); } return total; }

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 13, 2017 2:11 AM
  • User-1024101449 posted

    Thanks....Excellent...

    Thursday, September 14, 2017 4:18 AM