Advertisement
valent1n

Homework ClassesAndObjects - Task 06

Jan 9th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4.     class CalculateSum
  5.     {
  6.         static void Main()
  7.         {
  8.             string input = "25 28 1 8 15 11";
  9.             input = input.Trim();
  10.  
  11.             Console.WriteLine("\n{0} -> Sum = {1}", input, GetSum(input));
  12.         }
  13.  
  14.         static int GetSum(string input)
  15.         {
  16.             int sum = 0;
  17.             StringBuilder expression = new StringBuilder(input + ' ');
  18.  
  19.             while (expression[0] !=  ' ')
  20.             {
  21.                 string num = expression.ToString().Substring(0, expression.ToString().IndexOf(' '));
  22.                 sum += int.Parse(num);
  23.                 expression = expression.Remove(0, expression.ToString().IndexOf(' ') + 1);
  24.                 expression = new StringBuilder(expression.ToString().Trim() + ' ');          
  25.             }
  26.  
  27.             return sum;
  28.         }
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement