c#销售(编写一个将用于计算和显示的嵌套循环)
部门季度1季度2季度3季度4总和部门1750660910800部门2800700950900部门3700600750600部门48508001000950部门590080...
部门 季度1 季度2 季度3 季度4 总和
部门1 750 660 910 800
部门2 800 700 950 900
部门3 700 600 750 600
部门4 850 800 1000 950
部门5 900 800 960 980
(1)计算每个部门每一季度的销售额占总销售额的百分比和全年销售额占总销售额的百分比
(2)计算每一季度的销售额占总销售额的百分比 展开
部门1 750 660 910 800
部门2 800 700 950 900
部门3 700 600 750 600
部门4 850 800 1000 950
部门5 900 800 960 980
(1)计算每个部门每一季度的销售额占总销售额的百分比和全年销售额占总销售额的百分比
(2)计算每一季度的销售额占总销售额的百分比 展开
1个回答
展开全部
double dTotal = 0;//销售总额
double dDeptTotal = 0;//部门年销售额
Dictionary<string, double> deptDic = new Dictionary<string, double>();//部门年销售额
Dictionary<string, double> seasonDic = new Dictionary<string, double>();//季度销售总额
//循环取值
foreach (DataRow dr in dataTable.Rows)
{
dDeptTotal = 0;
foreach (DataColumn dc in dataTable.Columns)
{
if (dc.ColumnName.StartsWith("季度"))
{
dDeptTotal += (double)dr[dc];
if (!seasonDic.ContainsKey(dc.ColumnName))
seasonDic.Add(dc.ColumnName, (double)dr[dc]);
else
seasonDic[dc.ColumnName] += (double)dr[dc];
}
}
dTotal += dDeptTotal;
deptDic.Add(dr["部门"].ToString(), dDeptTotal);
}
DataRow drDepartment = dataTable.Select(string.Format("部门 = '{0}'", "部门1"), "").Single();
//部门每一季度的销售额占总销售额的百分比
double Percent1 = (double)drDepartment["季度1"] / dTotal * 100;
double Percent2 = (double)drDepartment["季度2"] / dTotal * 100;
double Percent3 = (double)drDepartment["季度3"] / dTotal * 100;
double Percent4 = (double)drDepartment["季度4"] / dTotal * 100;
//部门全年销售额占总销售额的百分比
double Percent5 = deptDic["部门1"] / dTotal * 100;
//每一季度的销售额占总销售额的百分比
double Percent6 = (double)seasonDic["季度1"] / dTotal * 100;
double Percent7 = (double)seasonDic["季度2"] / dTotal * 100;
double Percent8 = (double)seasonDic["季度3"] / dTotal * 100;
double Percent9 = (double)seasonDic["季度4"] / dTotal * 100;
===========================================================
这个代码是以DataTable为例的,希望对你有点帮助。
不过本人不推荐使用嵌套循环,可以参考参考DataTable.Compute()和计算列。
double dDeptTotal = 0;//部门年销售额
Dictionary<string, double> deptDic = new Dictionary<string, double>();//部门年销售额
Dictionary<string, double> seasonDic = new Dictionary<string, double>();//季度销售总额
//循环取值
foreach (DataRow dr in dataTable.Rows)
{
dDeptTotal = 0;
foreach (DataColumn dc in dataTable.Columns)
{
if (dc.ColumnName.StartsWith("季度"))
{
dDeptTotal += (double)dr[dc];
if (!seasonDic.ContainsKey(dc.ColumnName))
seasonDic.Add(dc.ColumnName, (double)dr[dc]);
else
seasonDic[dc.ColumnName] += (double)dr[dc];
}
}
dTotal += dDeptTotal;
deptDic.Add(dr["部门"].ToString(), dDeptTotal);
}
DataRow drDepartment = dataTable.Select(string.Format("部门 = '{0}'", "部门1"), "").Single();
//部门每一季度的销售额占总销售额的百分比
double Percent1 = (double)drDepartment["季度1"] / dTotal * 100;
double Percent2 = (double)drDepartment["季度2"] / dTotal * 100;
double Percent3 = (double)drDepartment["季度3"] / dTotal * 100;
double Percent4 = (double)drDepartment["季度4"] / dTotal * 100;
//部门全年销售额占总销售额的百分比
double Percent5 = deptDic["部门1"] / dTotal * 100;
//每一季度的销售额占总销售额的百分比
double Percent6 = (double)seasonDic["季度1"] / dTotal * 100;
double Percent7 = (double)seasonDic["季度2"] / dTotal * 100;
double Percent8 = (double)seasonDic["季度3"] / dTotal * 100;
double Percent9 = (double)seasonDic["季度4"] / dTotal * 100;
===========================================================
这个代码是以DataTable为例的,希望对你有点帮助。
不过本人不推荐使用嵌套循环,可以参考参考DataTable.Compute()和计算列。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询