A Practical Example

Until now, simple examples have been provided to demonstrate operator overloading and conversion operators. The following code contains a more practical example. Summation notation iterates and totals an expression in a specified range. Figure 9-1 provides three examples of summation notation.

Examples of summation notation

Figure 9-1. Examples of summation notation

Summation is not a predefined type in C#. The following class defines the Summation type. It contains several methods, and the objective is to make the Summation class as similar to a primitive type as possible:

  • The Summation class has three constructors. The two- and three-argument constructors call the four-argument constructor.

  • The Calculate method calculates the result of the summation, and updates the result whenever any of the parameters change.

  • The class has two operator+ methods, which allow instances of the class to be either the lhs operand or the rhs operand in a binary expression. The operator+ methods of the Summation class extend the iteration by adding to the stop value of the summation. A new instance with an updated number of iterations is returned.

  • The operator++ method extends the number of iterations of the current object by one. The current object is then returned.

  • The class has an operator int conversion constructor. This method converts a Summation object into an int value by returning the result of the summation.

  • There are two string functions: ToString returns the result of the operation; ToNotationString displays in summation notation the object and the parameters of the summation.

Here is the code for the class:

public class Summation {


    public Summation(int _start, int _stop) :
            this(_start, _stop, 1, 1) {
    }

    public Summation(int _start, int _stop,
        int _product) :
            this(_start, _stop, _product, 1) {
    }

    public Summation(int _start, int _stop,
        int _product, int _power) {
        start=_start;
        stop=_stop;
        product=_product;
        power=_power;
        Calculate();
    }

    private void Calculate() {
        propResult=0;
        int temp;
        for (int count = start; count <= stop;
                ++count) {
            temp = (int)Math.Pow(count, power);
            temp = temp * product;
            propResult += temp;
        }
    }
    public static Summation operator+(Summation sum, int val) {
        return new Summation(sum.start, sum.stop + val,
            sum.product, sum.power);
    }

    public static Summation operator+(int val, Summation sum) {
        return new Summation(sum.start, sum.stop + val,
            sum.product, sum.power);
    }

    public static Summation operator++(Summation sum) {
        ++sum.stop;
        sum.Calculate();
        return sum;
    }

    public static explicit operator int(Summation sum) {
        return sum.propResult;
    }

    private int propResult;
    public int Result {
        get {
            return propResult;
        }
    }

    public override string ToString() {
        return propResult.ToString();
    }

    public string ToNotationString() {
        string line1 = "
  " + stop.ToString();
        string line2 = "

eeeee";
        string line4 = "
ee    "+
             (product == 1 ? "" : product.ToString()) + "i";
        string line3 = "
e" + new string(' ', line4.Length - 2);
        line3 += (power == 1 ? "" : power.ToString());
        string line5 = "
e";
        string line6 = "
eeeee";
        string line7 = "

 i=" + start.ToString();
        return line1 + line2 + line3 + line4 + line5 + line6 + line7;
    }

    private int start;
    private int stop;
    private int product;
    private int power;
}

The following code tests the Summation type. In this example, the parameters of the summation are read from the command line in the following order: start, stop, product, and power. The number of iterations is from the start value to the stop value. In Main of the example code, the summation notation is displayed, followed by the result. The number of iterations then is increased using the operator++, and the new results are displayed:

public static void Main(string [] argv) {
    Summation sum =
        new Summation(int.Parse(argv[0]),
                      int.Parse(argv[1]),
                      int.Parse(argv[2]),
                      int.Parse(argv[3]));
    Console.WriteLine();
    Console.WriteLine(sum.ToNotationString());
    Console.WriteLine("
[Result = {0}]",
        sum.Result);

    ++sum;
    int isum = (int)sum;
    Console.WriteLine();
    Console.WriteLine(sum.ToNotationString());
    Console.WriteLine("
[Result = {0}]", isum);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.42.251