zoukankan      html  css  js  c++  java
  • c#读取文本文档实践2-计算商品价格

    商品 数量 单价
    英语 66 100
    语文 66 80
    数学 66 100
    化学 66 40
    物理 66 60

    上面是文本文档中读入的数据。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.IO;
     6 using System.Diagnostics;//Stopwatch所在命名空间
     7 
     8 namespace 书名总价格计算
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             string path = @"C:UsersAdministratorDesktop书名总价格计算.txt";
    15             string[] contents = File.ReadAllLines(path, Encoding.Default);//将文档所有内容放入字符串数组中
    16             Stopwatch sw = new Stopwatch();//创建一个计时器方法
    17             sw.Start();//开始计时
    18 
    19             for (int i = 0; i < contents.Length; i++)//从第二行开始
    20             {
    21                 if (i != 0)
    22                 {
    23                     string[] strNew = contents[i].Split(new char[] { ' ', '	' }, StringSplitOptions.RemoveEmptyEntries);
    24                     Console.WriteLine("{0} {1} {2} {3}", strNew[0], strNew[1], strNew[2], Convert.ToDouble(strNew[1]) * Convert.ToDouble(strNew[2]));
    25                 }
    26                 else//第一行题头不参与计算总价格
    27                 {
    28                     string[] strNew = contents[i].Split(new char[] { ' ', '	' }, StringSplitOptions.RemoveEmptyEntries);
    29                     Console.WriteLine("{0} {1} {2} 总价格", strNew[0], strNew[1], strNew[2]);
    30                 }
    31             }
    32             sw.Stop();//结束计时,以毫秒输出
    33             Console.WriteLine(sw.ElapsedMilliseconds);//以毫秒形式输出结果
    34         }
    35     }
    36 }

    通过上述代码计算总价格输出到控制台上:

  • 相关阅读:
    A New Approach to Line Simplification Based on Image Processing: A Case Study of Water Area Boundaries
    3D模型
    数码相机控制点的自动定位检校
    道路网匹配
    多线程操作数据拷贝要加线程锁
    编程琐事
    C++ 指定路径文件夹存在与否查询及文件夹创建
    C++ 网络编程之正确使用UDP广播及多播
    C++ 获得系统时间
    C++ 数据写入文件与读回
  • 原文地址:https://www.cnblogs.com/zhubinglong/p/5817212.html
Copyright © 2011-2022 走看看