I am trying to learn how to get the sum from a list of numbers in a file that represent currency in C# -


i trying sum of list of numbers file represents currency amounts. not work created array inside code , got working assignment way; still want know how program myself future reference. here copy of code did write , values of file sales.txt are:

1245.67 1189.55 1098.72 1456.88 2109.34 1987.55 1872.36 

here copy of code:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.io;  namespace lab7._2 {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }         // readscore method         private void readsales(list<decimal> saleslist)         {             try             {                 // open file sales.txt                 streamreader inputfile = file.opentext("sales.txt");                  // read sales list.                 while (!inputfile.endofstream)                 {                     saleslist.add(decimal.parse(inputfile.readline()));                 }                  // close file                 inputfile.close();             }             catch (exception ex)             {                 // display error message                 messagebox.show(ex.message);             }         }           // displaysales method displaying listbox         private void displaysales(list<decimal> saleslist)         {             foreach (decimal sales in saleslist)             {                 runningtotallistbox.items.add(sales);             }         }         // average method returns average of values         private double average(list<decimal> saleslist)         {             decimal total = 0;  //accumulator             double average; // hold average              // calculate total of sales             foreach (decimal sales in saleslist)             {                 total += sales;             }              // calculate average of scores.             average = (double)total / saleslist.count;               // return average.             return average;         }             private void calbutton_click(object sender, eventargs e)         {             double averagesales; // hold average sales              // create list hold sales             list<decimal> saleslist = new list<decimal>();              // create decimal array             double[] units = { 1245.67, 1189.55, 1098.72, 1456.88, 2109.34, 1987.55,        1872.36 };              // declair , initilize accululator variable.             double totals = 0;              // step through array adding each element             (int index = 0; index < units.length; index++)                 {                 totals += units[index];                 }               // read sales             readsales(saleslist);              // display sales             displaysales(saleslist);              // display total             outputlabel.text = totals.tostring();              // display average sales cost             averagesales = average(saleslist);             averagecostlabel.text = averagesales.tostring("n1");         }                private void button2_click(object sender, eventargs e)         {             // close window             this.close();         }     } } 

simpler way read lines in bulk:

list<decimal> values = file.readalllines("sales.txt")                            .select(s => decimal.parse(s, cultureinfo.invariantculture))                            .tolist(); 

in code:

private void readsales(list<decimal> saleslist) {     saleslist.addrange(file.readalllines("sales.txt")                            .select(s => decimal.parse(s, cultureinfo.invariantculture))); } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -