dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to to calculate Splitting the Bill in C#

  1. #1
    Join Date
    Feb 2009
    Posts
    117

    How to to calculate Splitting the Bill in C#

    How to calculate Splitting the Bill in C# from a text file (expenses.txt) and write output to a new text file (expenses.txt.out)?
    A number of friends go camping every year at provincial parks.

    The group agrees in advance to share expenses equally, but it is not practical to have them share every expense as it occurs. So individuals in the group pay for particular things, like food, drinks, supplies, the camp site, parking, etc. After the camping trip, each personís expenses are tallied and money is exchanged so that the net cost to each is the same. Read from a list of expenses, the amount of money that each person must pay or be paid.

    Sample Input:
    3 (# of people)
    2 (# of bills for the 1st person)
    10.00 (Amount paid by 1st person)
    20.00 (Amount paid by 1st person)
    4 (# of bills for the 2nd person)
    15.00 (Amount paid by 2nd person)
    15.01 (Amount paid by 2nd person)
    3.00 (Amount paid by 2nd person)
    3.01(Amount for the 2nd person)
    3 (# of bills for the 3rd person)
    5.00 (Amount paid by 3rd person)
    9.00 (Amount paid by 3rd person)
    4.00 (Amount paid by 3rd person)

    2 (# of people)
    2 (# of bills for the 1st person)
    8.00 (Amount paid by 1st person)
    6.00 (Amount paid by 1st person)
    2 (# of bills for the 2nd person)
    9.20 (Amount paid by 2nd person)
    6.75 (Amount paid by 2nd person)
    0 End of program

    Output for Sample Input
    ($1.99)
    ($8.01)
    $10.01

    $0.98
    ($0.98)

    This is what I tried So far-. But how would I distinguish people, bills and amount in this case?
    Code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.IO;
    
    namespace SplittingTheBill
    {
        class Program
        {
            static void Main(string[] args)
            {           
                try
                {
                    //Pass the file path and file name to the StreamReader constructor
                    StreamReader sr = new StreamReader("C:\\Users\\makhtar\\Desktop\\P S\\SplittingTheBill\\SplittingTheBill\\expenses.txt");
    
                String line;
                ArrayList BillArray = new ArrayList();
    
                //Read the first line of text
                line = sr.ReadLine(); 
    
                //Continue to read until you reach end of file
                while (line != null)
                {
                    BillArray.Add(line);
                    //write the lie to console window
                    //Console.WriteLine(line);
                    //Read the next line
                    line = sr.ReadLine();
                }
    
                //close the file
                sr.Close();
    
                foreach (var item in BillArray)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }
    }

  2. #2
    Join Date
    Apr 2017
    Posts
    1
    Interesting.. have you already find the solution?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center