Printing Calculation result

printing calculator
printing calculator target
best printing calculator
printing calculator online
printing calculator app
casio printing calculator
printing calculator office depot
sharp printing calculator

I'm trying to write a print function that would help me with printing the results of the addition, subtraction, etc. I've tried to do this in a switch - case statement, but it doesn't seems to be working. Is there any easier ways to implement this print() function? Secondly, in case of exception, how can I manage exceptions if for instance one of the operations throw an exception. such as 3/0!

for this program I have a Calculator class and a Program, where all this will be tested.

public class Calculator
{
    public double Add(double x, double y)
    {
        return x + y;
    }

    public double Subtract(double x, double y)
    {
        return x - y;
    }

    public double Multiply(double x, double y)
    {
        return x * y;
    }

    public double Power(double x, double y)
    {
        return Math.Pow(x, y);
    }

    public void PrintCalculation(string action)
    {
        switch (action)
        {
             case "Add":
            Console.WriteLine("Sum is: {0}", Add(x, y));
            break;
        case "Subtract":
            Console.WriteLine("Division is: {0}", Subtract(x, y));
            break;
        case "Multiply":
            Console.WriteLine("Multiply is: {0}", Multiply(x, y);
            break;
        case "Power":
            Console.WriteLine("Power is: {0}", Power(x, y);
            break;
        }
    }
}

This is the Main function

namespace MyCalculator
{
    public class Program : Calculator
    {
        static void Main()
        {
            Calculator myCal = new Calculator();

            myCal.Add(2, 3);
            myCal.Subtract(3, 3);
            myCal.Multiply(5, 5);
            myCal.Power(2, 3);

             myCal.PrintCalculation("Add");
        myCal.PrintCalculation("Subtract");
        myCal.PrintCalculation("Multiply");
        }
    }
}

There are a few problems with the code that are causing compiler errors, such as missing parenthesis, quotes, and break statements. But I think the main issue is that you're not taking in the values of x and y to your PrintCalculation method, so they are undefined in the body of that method.

To fix this, you can just add them to the argument list as you have with the other methods:

public static void PrintCalculation(string action, double x , double y)

But notice that you're doing a string comparison here, which can be prone to errors due to case sensitivity (what happens if they write "add" instead of "Add"?). A different option might be to create an enum to represent valid actions. This also serves to restrict the input allowed to only values we can do something with (previously the user may have tried to enter "Divide", and been confused when nothing happened):

public enum Action { Add, Subtract, Multiply, Power }

Then your method could be changed to accommodate the enum instead of a string. We can also make the class and methods static, since there is no state required:

public static class Calculator
{
    public enum Action { Add, Subtract, Multiply, Power }

    public static double Add(double x, double y)
    {
        return x + y;
    }

    public static double Subtract(double x, double y)
    {
        return x - y;
    }

    public static double Multiply(double x, double y)
    {
        return x * y;
    }

    public static double Power(double x, double y)
    {
        return Math.Pow(x, y);
    }

    public static void PrintCalculation(Action action, double x , double y)
    {
        switch (action)
        {
            case Action.Add:
                Console.WriteLine("The sum of {0} and {1} is: {2}", 
                    x, y, Add(x, y));
                break;

            case Action.Subtract:
                Console.WriteLine("The difference between {0} and {1} is: {2}", 
                    x, y, Subtract(x, y));
                break;

            case Action.Multiply:
                Console.WriteLine("The product of {0} and {1} is: {2}", 
                    x, y, Multiply(x, y));
                break;

            case Action.Power:
                Console.WriteLine("{0} raised to the power of {1} is: {2}", 
                    x, y, Power(x, y));
                break;
        }
    }
}

With these changes, we can now use the class like:

private static void Main(string[] cmdArgs)
{
    var first = 7;
    var second = 3;

    Calculator.PrintCalculation(Calculator.Action.Add, first, second);
    Calculator.PrintCalculation(Calculator.Action.Subtract, first, second);
    Calculator.PrintCalculation(Calculator.Action.Multiply, first, second);
    Calculator.PrintCalculation(Calculator.Action.Power, first, second);

    GetKeyFromUser("\nDone! Press any key to exit...");
}   

Output

in Python how do I print the result of a calculation and then save the , you could do all your calculations up front. i recommend giving the variables (a, b​, c, etc) smarter names than I did here, but this is illustrative  Printing Calculation result. Ask Question Asked 1 year, I'm trying to write a print function that would help me with printing the results of the addition

A few issues, first of all you are not passing strings into PrintCalculation(string s). The format should be something like:

PrintCalculation("Add");

As the function is currently defined. Also in PrintCalculation() you call your calculation functions which are then passed undefined values of x and y. They are not members of the class or the class method itself.

For example you are trying to call Add(x,y) inside of PrintCalculation("Add"), but you never give it an x or y in that function. Instead the function should be defined

public void PrintCalculation(string action, double x, double y){
// code here
}

And it should be called with

PrintCalculation("Add", 2, 3);

Printing Calculators | Amazon.com, Printing Calculators. 1-24 of 190 results for Office Products : Office Electronics : Calculators : Printing  If you right-click on the results overview to open the print view, you will not be able to select or remove the desired print pages from your document because you are only in the view mode. To customize the print output, select the "Print Manager" icon next to the "Save" icon or go back to the start page and open "Print".

I think you can take advantage of logging the actions without requiring a switch case at all. The example below allows you to set an Action<string> for logging the results dynamically.

public class Calculator
{
    public Action<string> LogAction { get; set;}

    private double PerformCalculation(string calculationName, string format, double x, double y, Func<double, double, double> calcFunction)
    {
        double value = calcFunction(x, y);
        LogAction?.Invoke(string.Format(format, x, y, value));

        return calcFunction(x, y);
    }

    public double Add(double x, double y)
    {
        string format = "The sum of {0} and {1} is: {2}";
        return PerformCalculation("Add", format, x, y, (a, b) => a + b);
    } 

    public double Subtract(double x, double y)
    {
        string format = "The difference between {0} and {1} is: {2}";
        return PerformCalculation("Subtract", format, x, y, (a, b) => a - b);
    }

    public double Multiply(double x, double y)
    {
        string format = "The product of {0} and {1} is: {2}";
        return PerformCalculation("Multiply", format, x, y, (a, b) => a * b);
    }

    public double Power(double x, double y)
    {
        string format = "{0} raised to the power of {1} is: {2}";
        return PerformCalculation("Power", format, x, y, (a, b) => Math.Pow(a, b));
    }
}

static void Main(string[] args)
{
    Calculator myCal = new Calculator();
    myCal.LogAction = Console.WriteLine; // Sets all calculation output to the console.

    myCal.Add(2, 3);
    myCal.Subtract(3, 3);
    myCal.Multiply(5, 5);
    myCal.Power(2, 3);

    Console.ReadLine();
}

[PDF] electronic printing calculator operation manual, Power Switch/ Print/ Item Count Mode Selector . from the count. • The count is printed when the calculated result is obtained. • Pressing of the. I want to save the result of the first print line to a variable called after_tax so that then I could write: print " After taxes and license fee the price is: ", after_tax + (base*license /100) (I want the first print command to also save the result of the math calculation to a variable called after_tax so that I can reuse the result without

Your problemn is: you start the method to do something Main:

myCal.Add(2, 3);

Class:

public double Add(double x, double y)
{
    return x + y;
}

but you don't store the outcome anywhere. You see, the method knows to return something, but on the other end is nothing to receive it like

double i = myCal.Add(2, 3);

or, as an alternative you can store your numbers in the class

public double _outcome;

public void Add(double x, double y)
{
    _outcome = x + y;
}

and in main

myCal.PrintCalculation(myCal._outcome.ToString());

As an alternative (to do it the way you actually want it to)

Main:

myCal.TmpNumberOne = 1;
myCal.TmpNumberTwo = 2;

myCal.PrintCalculation("Add");

Class:

public double TmpNumberOne;
public double TmpNumberTwo;

public double Add(double x, double y)
{
    return x + y;
}     

public void PrintCalculation(string action)
{
    switch (action)
    {
        case "Add":
            Console.WriteLine("Sum is: {0}", Add(TmpNumberOne, TmpNumberTwo).ToString());
            break;
    }
}

To your final question: you want exceptionhandling, first you coudl achieve this by adding an if-statement to your method to check for invalid inputs, or you take the holy

try
{
}

catch(Exception ex)
{
}

you can paste your code inside try, when an error would occur that stops the program entirely, the code in catch code is executed instead. If you are fancy you can print out the exception with

ex.ToString();

don't forget to convert your double values to string, otherwise this will throw an error

Keep Track with a Printing Calculator, Keep accurate financial records with these printing calculators that print your calculations as you go. Whether you're adding up your company's total transactions  Many 1-hour photo lab digital machines are usually set to print at up to 250 pixels per inch, but it won't hurt to always provide pixels to print 300 dpi. 250 to 300 dpi is a reasonable and optimum printing resolution for photos.

How to Calculate Cost-Per-Print and Reduce Printing Costs for Your , Here's a step-by-step guide to calculating cost-per-print and a few strategies for reducing This method will produce the most accurate results. Printing calculators release a steady stream of tape as you enter calculations, so you can spot errors before they become a part of your records. When you're finished with a specific set, the tape tears off easily for convenient storage in a file. Most models use standard POS paper, so you can replace the rolls quickly and get back to work.

Browse Printing Calculators, Search for your new printing calculator and accessories today. Find great deals now on top shelf calculators today! The majority of printing calculators do not accept operators and operands the same way as a conventional calculator.  The calculator uses the operators after the numbers you want to manipulate.  As

The Origins of Digital Computers: Selected Papers, One mode of rendering permanent the results of any calculations made by the Printing. Apparatus. It is desireable even when many copies of a calculation  Unable to copy results from Calculator Although, the new calculator is easy to use, I prefer the one in previous versions of Windows. My problem with the new Calculator is that you can't copy to results to another App.

Comments
  • I have fixed a typo case "Subtract -> case "Subtract". I guess that was not relevant for the question. was it?
  • myCal.Add(2, 3); What happens to the value returned by this function?
  • You need to add break statements in each case: case "Add": Console.WriteLine("Sum is: {0}", Add(x, y)); break;
  • Instead of myCal.PrintCalculation(Add) you have to pass strings: myCal.PrintCalculation("Add")
  • You also have to add x and y to the method arguments; currently they're undedfined in that method: public void PrintCalculation(string action, double x, double y)
  • Ahh I like this, it's logical to have the output text vary depending on the calculation action.
  • This is a very nice and easily understandable implementation. I like the idea of using the enumerations.
  • Thank you all very much for all the great tips. It really helps me as a new comer to c#.
  • This is very clever!
  • I have applied the changes that everyone has suggest above. Trying these different methods of implementations gave me a deeper understanding knowledge about my errors. :)
  • @RufusL thanks! After seeing yours I'd want to revise this to where PerformCalculation can accept a string to use when outputting.