C# 如何计算并写入文本文件
如何计算以下粗体项目以创建文本文件中所有项目的总数? 发票。添加(计数器+”、“+FreshGrovery.Name+”、“++FreshGrovery.Price+”、“+FreshGrovery.Weight”); 发票。添加(计数器+”、“+杂货店名称+”、“+价格”、“+杂货店数量) 完整代码如下。不确定如何从不同的字符串添加intC# 如何计算并写入文本文件,c#,C#,如何计算以下粗体项目以创建文本文件中所有项目的总数? 发票。添加(计数器+”、“+FreshGrovery.Name+”、“++FreshGrovery.Price+”、“+FreshGrovery.Weight”); 发票。添加(计数器+”、“+杂货店名称+”、“+价格”、“+杂货店数量) 完整代码如下。不确定如何从不同的字符串添加int using System; using System.Collections.Generic; using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Groceries3
{
class Program
{
static void Main(string[] args)
{
string[] groceries = File.ReadAllLines("Groceries.txt");
List<string> invoices = new List<string>();
int counter = 0;
foreach (var grocery2 in groceries)
{
counter++;
var list = grocery2.Split(',');
if (list[0].Equals("fresh"))
{
FreshGrocery freshGrocery = new FreshGrocery();
freshGrocery.Name = list[1];
freshGrocery.Price = double.Parse(list[2]);
freshGrocery.Weight = double.Parse(list[3].Replace(";", ""));
invoices.Add(counter + "," + freshGrocery.Name + "," + freshGrocery.Price + "," + freshGrocery.Weight);
}
else if (list[0].Equals("regular"))
{
Grocery grocery = new Grocery();
grocery.Name = list[1];
grocery.Price = double.Parse(list[2]);
grocery.Quantity = int.Parse(list[3].Replace(";", ""));
double price = grocery.Calculate();
invoices.Add(counter + "," + grocery.Name + "," + price + "," + grocery.Quantity);
}
}
File.WriteAllLines("Invoice.txt", invoices.ToArray());
{
File.AppendAllText("Invoice.txt", string.Format("{0}{1}", "Groceries for you" + " " + DateTime.Now, Environment.NewLine));
File.AppendAllText("Invoice.txt", string.Format("{0}{1}", "Total of all groceries = ", Environment.NewLine));
}
}
abstract class GroceryItem
{
private string name;
private double price = 0;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public double Price
{
get
{
return price;
}
set
{
price = value;
}
}
public abstract double Calculate();
}
class FreshGrocery : GroceryItem
{
private double weight = 0;
public double Weight
{
get
{
return weight;
}
set
{
weight = value;
}
}
public override double Calculate()
{
return this.Price * this.weight;
}
}
class Grocery : GroceryItem
{
private int quantity = 0;
private double gst = 10;
public int Quantity
{
get
{
return quantity;
}
set
{
quantity = value;
}
}
public override double Calculate()
{
double calculatedPrice = this.Price * this.Quantity;
if (calculatedPrice < 0)
{
calculatedPrice += calculatedPrice * (gst / 100);
}
return calculatedPrice;
}
}
class ShoppingCart
{
private List<GroceryItem> orders;
public List<GroceryItem> Orders
{
get
{
return orders;
}
set
{
orders = value;
}
}
public double Calculate()
{
double price = 0;
if (this.Orders != null)
{
foreach (GroceryItem order in this.Orders)
{
price += order.Calculate();
}
}
return price;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
名称空间杂货店3
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串[]groceries=File.ReadAllLines(“groceries.txt”);
列表发票=新列表();
int计数器=0;
foreach(食品杂货中的var grocery2)
{
计数器++;
var list=grocery2.Split(',');
if(列表[0]。等于(“新”))
{
新鲜食品杂货店=新的新鲜食品杂货店();
freshgrovery.Name=list[1];
freshgrovery.Price=double.Parse(列表[2]);
freshgrovery.Weight=double.Parse(列表[3]。替换(“;”,”);
发票。添加(计数器+”、“+FreshGrovery.Name+”、“+FreshGrovery.Price+”、“+FreshGrovery.Weight”);
}
else if(列表[0]。等于(“常规”))
{
杂货店=新杂货店();
.Name=列表[1];
screery.Price=double.Parse(列表[2]);
.Quantity=int.Parse(列表[3]。替换(“;”,”);
双倍价格=杂货店。计算();
发票。添加(计数器+“,“+杂货店名称+”,“+价格+”,“+杂货店数量);
}
}
File.writeAllines(“Invoice.txt”,invoices.ToArray());
{
AppendAllText(“Invoice.txt”,string.Format(“{0}{1}”,“为您准备的杂货”+“”+DateTime.Now,Environment.NewLine));
AppendAllText(“Invoice.txt”,string.Format(“{0}{1}”,“所有杂货总计=”,Environment.NewLine));
}
}
抽象类GroceryItem
{
私有字符串名称;
私人双价=0;
公共字符串名
{
得到
{
返回名称;
}
设置
{
名称=值;
}
}
公共双价
{
得到
{
退货价格;
}
设置
{
价格=价值;
}
}
公共抽象双重计算();
}
班级:杂货店项目
{
私人双权=0;
公众双倍重量
{
得到
{
返回重量;
}
设置
{
重量=价值;
}
}
公共覆盖双计算()
{
返回此。价格*此。重量;
}
}
类别:杂货店
{
私有整数数量=0;
私人双gst=10;
公共整数
{
得到
{
退货数量;
}
设置
{
数量=价值;
}
}
公共覆盖双计算()
{
双重计算价格=此价格*此数量;
如果(计算价格<0)
{
计算价格+=计算价格*(gst/100);
}
返回计算价格;
}
}
类购物车
{
私人名单订单;
公开名单命令
{
得到
{
退货订单;
}
设置
{
订单=价值;
}
}
公共双计算()
{
双倍价格=0;
if(this.Orders!=null)
{
foreach(本订单中的杂货店订单)
{
价格+=订单。计算();
}
}
退货价格;
}
}
}
}如果您只想转换为字符串,请使用string.Format()。string.Format()接受一个字符串参数,该参数指定在括号为后续参数的占位符时字符串的显示方式。然后对每个参数调用.ToString()
invoices.Add(string.Format("{0},{1},{2},{3}",
counter, freshGrocery.Name, freshGrocery.Price, freshGrocery.Weight));
以上内容与以下内容相同,但更易于阅读:
invoices.Add(counter + "," + freshGrocery.Name.ToString() + "," +
freshGrocery.Price.ToString() + "," + freshGrocery.Weight.ToString());
请同时发布Groceries.txt的内容,这些内容可用于重现您的问题。嗅味作业您必须在编写它们之前计算它们添加值的最佳方法是使用类型为
double
的变量添加值,并且不要尝试从字符串中添加值。但是,如果坚持使用字符串值,则需要提取感兴趣的值的格式化文本(请参见string.Split()
或Regex
类),将它们转换回数值(解析double
的工作原理与解析int
类似)并添加它们。最后一部分的建议见标记的副本。如果这还不够,你需要将你的问题简化为一个好的问题,清楚地显示你已经尝试了什么,描述你已经做了什么研究,并解释你具体遇到了什么问题。