我是初学者,对重载很不了解,但是作业要求又要在函数中实现重载,所以特来请教!!!
希望大家帮帮我!!!谢谢!!!!
这是我写的一个类,里面有函数,算法。(算法是经济预测里面的一次指数平滑和二次指数平滑)
intArrayNumber:数据的组数。
decWeight:加权系数。
strTime:时间。
decQuantity:实际需求量。
decInitial1:一次平滑初始值。
decInitial2:二次平滑初始值。
这个 method_once(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1)
算是重载么????
我希望能够写出下面有重载要求的函数。
method(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1)
时函数计算一次指数平滑。
method(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1,decimal decInitial2)
时函数计算二次指数平滑。
我先写了类,然后在Button事件中调用类中的函数。
下面是一次指数平滑的代码:
using System;
namespace ForecastSystem
{
/// <summary>
/// method_weight 的摘要说明。
/// </summary>
public class method_once
{
//private variables to hold class properties
private string strTime;
private int intArrayNumber;
private decimal decQuantity,decWeight,decInitial1,decSingleTotal,decResult,decMSE;
private static int intCount;
private static decimal []decSingle=new decimal[100];
private static decimal []decTotal_number=new decimal1[100];
public method_once(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1)
{
//
this.Time=strTime;
this.Quantity=decQuantity;
this.Weight=decWeight;
this.Initial=decInitial1;
this.ArrayNumber=intArrayNumber;
AddTotals();
Add_number();
Forecast();
CalculateMSE();
//
}
public string Time
{
get
{
return strTime;
}
set
{
strTime=value;
}
}
public int ArrayNumber
{
get
{
return intArrayNumber;
}
set
{
intArrayNumber=value;
}
}
public decimal Initial
{
get
{
return decInitial;
}
set
{
decInitial=value;
}
}
public decimal Quantity
{
get
{
return decQuantity;
}
set
{
decQuantity=value;
}
}
public decimal Weight
{
get
{
return decWeight;
}
set
{
decWeight=value;
}
}
public decimal SingleTotal
{
get
{
return decSingleTotal;
}
set
{
decSingleTotal=value;
}
}
public decimal Result
{
get
{
return decResult;
}
set
{
decResult=value;
}
}
public decimal MSE
{
get
{
return decMSE;
}
set
{
decMSE=value;
}
}
public static int Count
{
get
{
return intCount;
}
}
protected void AddTotals()
{
decTotal_number1[0]=decInitial;
decSingle[intCount]=decQuantity;
intCount++;
}
protected void Add_number()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decTotal_number1[a+1]=decWeight*decSingle[a]+(1-decWeight)*decTotal_number1[a];
}
}
}
protected void Forecast()
{
decResult=decTotal_number1[intArrayNumber];
}
protected void CalculateMSE()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decSingleTotal+=(decSingle[a]-decTotal_number1[a])*(decSingle[a]-decTotal_number1[a]);
}
decMSE=decSingleTotal/intArrayNumber;
decSingleTotal=0;
}
}
}
}
下面是二次指数平滑的代码
using System;
namespace ForecastSystem
{
/// <summary>
/// method_weight 的摘要说明。
/// </summary>
public class method_twice
{
//private variables to hold class properties
private string strTime;
private int intArrayNumber;
private decimal decQuantity,decWeight,decInitial1,decInitial2,decSingleTotal,decResult,decMSE;
private static int intCount;
private static decimal []decSingle=new decimal[100];
private static decimal []decTotal_number1=new decimal[100];
private static decimal []decTotal_number2=new decimal[100];
public method_twice(int intArrayNumber,decimal decWeight,string strTime,decimal decQuantity,decimal decInitial1,decimal decInitial2)
{
//
this.Time=strTime;
this.Quantity=decQuantity;
this.Weight=decWeight;
this.Initial1=decInitial1;
this.Initial2=decInitial2;
this.ArrayNumber=intArrayNumber;
AddTotals();
Add_number();
Forecast();
CalculateMSE();
//
}
public string Time
{
get
{
return strTime;
}
set
{
strTime=value;
}
}
public int ArrayNumber
{
get
{
return intArrayNumber;
}
set
{
intArrayNumber=value;
}
}
public decimal Initial1
{
get
{
return decInitial1;
}
set
{
decInitial1=value;
}
}
public decimal Initial2
{
get
{
return decInitial2;
}
set
{
decInitial2=value;
}
}
public decimal Quantity
{
get
{
return decQuantity;
}
set
{
decQuantity=value;
}
}
public decimal Weight
{
get
{
return decWeight;
}
set
{
decWeight=value;
}
}
public decimal SingleTotal
{
get
{
return decSingleTotal;
}
set
{
decSingleTotal=value;
}
}
public decimal Result
{
get
{
return decResult;
}
set
{
decResult=value;
}
}
public decimal MSE
{
get
{
return decMSE;
}
set
{
decMSE=value;
}
}
public static int Count
{
get
{
return intCount;
}
}
protected void AddTotals()
{
decTotal_number1[0]=decInitial1;
decTotal_number2[0]=decInitial2;
decSingle[intCount]=decQuantity;
intCount++;
}
protected void Add_number()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decTotal_number1[a+1]=decWeight*decSingle[a]+(1-decWeight)*decTotal_number1[a];
decTotal_number2[a+1]=decWeight*decTotal_number1[a]+(1-decWeight)*decTotal_number2[a];
}
}
}
protected void Forecast()
{
decResult=decTotal_number2[intArrayNumber];
}
protected void CalculateMSE()
{
if(intArrayNumber<=intCount)
{
for(int a=0;a<intArrayNumber;a++)
{
decSingleTotal+=(decSingle[a]-decTotal_number2[a])*(decSingle[a]-decTotal_number2[a]);
}
decMSE=decSingleTotal/intArrayNumber;
decSingleTotal=0;
}
}
}
}