i dont know why no one want to answer this question right for me?

+1 Mahmoud Abdel Ghani · February 3, 2015
actually i made a calculator using class and functions,i created class and inside it functions...function for get numbers and function for summation..etc..okay...now the user made some operation and he want to make another operation with last result..like 1+1=2..and i want to make another operation by using the last result like...2+3=5....so how to return the answer to continue??

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Mahmoud Abdel Ghani · February 7, 2015
@sohrab zia

//can you explain ur answer in coding please 
+1 c student · February 7, 2015
you can put it in a loop and use a function to calculate it if that's what you mean
int sum = 0;

for loop condition
sum = addFunction (num1, num2); // sum is your current result
+1 Mahmoud Abdel Ghani · February 7, 2015
//this is the class and his functions

 class Calculator
    {
        float numb1;
        float numb2;
        float answer;
        bool test1 = true;
        bool test2 = true;
        string first_input;
        string second_input;
        public void initalization(float x, float y, ref float ans)
        {
            numb1 = x;
            numb2 = y;
            answer = ans;
        }
        public void getnumbers()
        {
            while (test1)
            {
                Console.Write("Please Enter first Number: ");
                first_input = Console.ReadLine();
                if (true == float.TryParse(first_input, out numb1))
                {
                    test1 = false;
                }
                else
                {
                    test1 = true;
                    Console.WriteLine("Error!-->please enter something numeric");

                }
            }
            while (test2)
            {
                Console.Write("please Enter Second number: ");
                second_input = Console.ReadLine();
                if (true == float.TryParse(second_input, out numb2))
                {
                    test2 = false;
                }
                else
                {
                    test2 = true;
                    Console.WriteLine("Error!-->please enter something numeric");

                }
            }
        }
        public float sum()
        {

            answer = numb1 + numb2;
            Console.WriteLine("Answer = " + answer);
            return answer;
        }
        public float subtract()
        {

            answer = numb1 - numb2;
            Console.WriteLine("Answer = " + answer);
            return answer;


        }
        public float divide()
        {

            if (numb2 == 0)
            {
                Console.WriteLine("You cannot divide by 0 answer is infinity");
            }
            else
            {
                answer = numb1 / numb2;
                Console.WriteLine("Answer = " + answer);
            }


            return answer;



        }
        public float multiply()
        {

            answer = numb1 * numb2;
            Console.WriteLine("Answer = " + answer);
            return answer;
        }


    }

//this is the main................................................

  static void Main(string[] args)
        {
            string choice;
            char con = 'y';
            
            string continueornot;
            bool testcontinue = true;
            while (con == 'y')
            {             
                Console.Write("Select the operation that you to do(+,-,/,*):");
                choice = Console.ReadLine();
                
                switch (choice)
                {
                    case "+":

                        Calculator sum = new Calculator();
                        sum.getnumbers();
                        sum.sum();
                        break;
                    case "-":

                        Calculator Sub = new Calculator();
                        Sub.getnumbers();
                        Sub.subtract();
                        break;

                    case "/":
                        Calculator divide = new Calculator();
                        divide.getnumbers();
                        divide.divide();
                        break;
                    case "*":
                        Calculator multiply = new Calculator();
                        multiply.getnumbers();
                        multiply.multiply();
                        break;
                    default:
                        Console.WriteLine("Error!-->please select one of shown operation!");
                        break;
                }
                while (testcontinue)
                {
                    Console.WriteLine("Press y to continue or anykey to exit");
                    continueornot = Console.ReadLine();
                    if (char.TryParse(continueornot, out con))
                    {
                        testcontinue = false;
                    }
                    else
                    {
                        testcontinue = true;
                        Console.WriteLine("Not Allowed!!! ");
                    }
                }
            }
        }
+1 Mahmoud Abdel Ghani · February 7, 2015
so where i can use what u said
+1 Abdullah Nauman · February 8, 2015

class Calculator
    {
        float numb1;
        float numb2;
        float answer;
        bool test1 = true;
        bool test2 = true;
        string first_input;
        string second_input;
        public void initalization(float x, float y, ref float ans)
        {
            numb1 = x;
            numb2 = y;
            answer = ans;
        }
        public void getnumbers()
        {
            while (test1)
            {
                Console.Write("Please Enter first Number: ");
                first_input = Console.ReadLine();
                if (true == float.TryParse(first_input, out numb1))
                {
                    test1 = false;
                }
                else
                {
                    test1 = true;
                    Console.WriteLine("Error!-->please enter something numeric");

                }
            }
            while (test2)
            {
                Console.Write("please Enter Second number: ");
second_input = Console.ReadLine();
                if (true == float.TryParse(second_input, out numb2))
                {
                    test2 = false;
                }
                else
                {
                    test2 = true;
                    Console.WriteLine("Error!-->please enter something numeric");

                }
            }
        }
        public float sum()
        {

            answer = numb1 + numb2;
            Console.WriteLine("Answer = " + answer);
            return answer;
        }
        public float subtract()
        {

            answer = numb1 - numb2;
            Console.WriteLine("Answer = " + answer);
            return answer;


        }
        public float divide()
        {

            if (numb2 == 0)
            {
                Console.WriteLine("You cannot divide by 0 answer is infinity");
            }
            else
            {
                answer = numb1 / numb2;
                Console.WriteLine("Answer = " + answer);
            }


            return answer;



        }
        public float multiply()
        {

            answer = numb1 * numb2;
            Console.WriteLine("Answer = " + answer);
            return answer;
        }


    }

//this is the main................................................

  static void Main(string[] args)
        {
            string choice;
            char con = 'y';
           
            string continueornot;
            bool testcontinue = true;
            while (con == 'y')
            {            
                Console.Write("Select the operation that you to do(+,-,/,*):");
                choice = Console.ReadLine();
               
                switch (choice)
                {
                    case "+":

                        Calculator sum = new Calculator();
                        sum.getnumbers();
                        sum.sum();
                        break;
                    case "-":

                        Calculator Sub = new Calculator();
                        Sub.getnumbers();
                        Sub.subtract();
                        break;

                    case "/":
                        Calculator divide = new Calculator();
                        divide.getnumbers();
                        divide.divide();
                        break;
                    case "*":
                        Calculator multiply = new Calculator();
                        multiply.getnumbers();
                        multiply.multiply();
                        break;
                    default:
                        Console.WriteLine("Error!-->please select one of shown operation!");
                        break;
                }
                while (testcontinue)
                {
                    Console.WriteLine("Press y to continue or anykey to exit");
                    y = Console.Read();
                    if(y == "y") {

                      getnumbers();

            }
            }
        }

Here, I added that feature for you. It should work now.
+1 Mahmoud Abdel Ghani · February 8, 2015
its wrong bro
0 Abdullah Nauman · February 8, 2015
I have not done C# in 1 year, I might have done something strong. Could I please have the error message, and location. 
0 Branislav Lazic · February 8, 2015
I played a bit with your code, here is improved version:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSPlayground
{
    class Calculator
    {
        // Declare a delegate that takes two values, does something with them
        // and returns "int" result
        private delegate int Operation(int startValue, int arg);

        /**Calculate method takes array of int's and applies Operation delegate.
         * As you can see, we take argument at "0" position in "args" array and declare
         * it as a start value. Thus "for" loop will start from "1" and not from "0". 
         * For each operation we want to conduct, we will use "Calculate" method
         * with different expression. Perfect example of how to reuse your method.**/
        private int Calculate(int[] args, Operation o)
        {
            int startValue = args[0];

            for (int i = 1; i < args.Length; i++)
            {
                startValue = o(startValue, args[i]);
            }
            return startValue;

        }

        public int Add(int[] args)
        {
            /**Example of use:
             * We pass "int" array and lambda expression. "init" value will be value at
             * "0" position of "args" int array. Then we cycle through array and add
             * next value from "args" array. Our "init" value mutates. In the end, it will
             * produce final result**/
            return Calculate(args, (init, next) => init += next);
        }

        public int Substract(int[] args)
        {
            return Calculate(args, (init, next) => init -= next);
        }

        public int Multiply(int[] args)
        {
            return Calculate(args, (init, next) => init *= next);
        }

        /**This is a bit specific case. We will have to check whether our "next" value
         * will be "0".**/
        public int Divide(int[] args)
        {
            return Calculate(args, (init, next) =>
            {
                if (next != 0)
                {
                    return init /= next;
                }
                else
                {
                    Console.WriteLine("Infinite!");
                    return 0;
                }
            });
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Calculator calc = new Calculator();
            
            Console.Write("Select the operation that you to do(+,-,/,*):");
            string choice = Console.ReadLine();
            // I don't care how you are going to get this numbers
            int[] nums = new int[] { 10, 2, 3 };

            switch (choice)
            {
                case "+":
                    int resOfAddition = calc.Add(nums);
                    Console.WriteLine("Answer = " + resOfAddition);
                    break;

                case "-":
                    int resultOfSubstraction = calc.Substract(nums);
                    Console.WriteLine("Answer = " + resultOfSubstraction);
                    break;

                case "*":
                    int resOfMultiplication = calc.Multiply(nums);
                    Console.WriteLine("Answer = " + resOfMultiplication);
                    break;
                case "/":
                    int resOfDivision = calc.Divide(nums);
                    Console.WriteLine("Answer = " + resOfDivision);
                    break;
            }

            Console.ReadLine();
        }
    }
}
0 Mahmoud Abdel Ghani · February 8, 2015
Branislav Lazic

i think you didnt solve the problem and made everything static


/images/forum/upload/2015-02-08/50322982056d9a13f96ac5795d6111a1.PNG
0 Branislav Lazic · February 8, 2015

i think you didnt solve the problem and made everything static


I come with most perfect solution and you expect what? To solve everything?

Find way how to populate array with user input values;
Store result of some operation to some int variable and reuse it in some other operation;
  • 1
  • 2

C#

106,983 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators