Free programs‎ > ‎

Basic arithmetic operations

Program "Basic arithmetic operations" ja namenjen za izračunavanje četri proste matematičke operacije :
sabiranje , oduzimanje,množenje i deljenje.
Kao što je navedeno u toku razvoja   ALGORITMA   za potrebe programa koriste se :
Varijable X , Y i Z za smeštanje vrednosti promenljivih i rezultata tipa double ( broj koji pripada skupu realnih brojeva , u pokretnom zarezu s dvostrukom preciznošću pri izračunavanjima ).
Varijable A i B su pomoćne promenljive koje se koriste prilikom unosa vrednosti promenljivih od strane korisnika
tipa string ( textualni tip podataka ).Ovo je zbog toga jer se u okviru programa koriste funkcije
za proveru da li je korisnik ukucao broj iz skupa realnih brojeva, a to se odrađuje na ovom tipu podataka,
Tako da program posmatra unetu vrednost s tastature kao text , a zatim ga prebaci u brojčanu vrednost pod uslovom da je uneti text broj iz skupa realnih brojeva.
Varijabla Answer tipa string se koristi za smeštanje odgovora korisnika na pitanje "da li hoće opet da pokrene program ?".
 
 
Program u odnosu na algoritam sadrži proširenje u smislu dodate tri matematičke operacije ali se u suštini ništa ne menja.
 
/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 27.01.2012
 * Time: 6:41
 * 
 *  A simple console application to calculate four simple math operations
 *  Addition, subtraction, multiplication and division
 */
using System;

namespace Basic_arithmetic_operations
{
    class Program
    {
        public static void Main(string[] args)
        {
            //
            // declaration of variables
            //
            string A = " "
            string B = " ";  
            string Answer = "N";  
            double X = 0;
            double Y = 0;
            double Z = 0;
            bool NumberRelevance = false;
            
            //
            // beginning label , 
            // marks the place of return in program
            // if the user wants to re-start program
            //
beginning :
                
            //
                // Set console dimensions , text color , clear console and write Title
                //
            Console.SetWindowSize(80,40);
            Console.Title = "Basic arithmetic operations";
            Console.ForegroundColor = ConsoleColor.Green;
                Console.Clear();
                Console.WriteLine("***************************************************************");
                Console.WriteLine("              Program basic arithmetic operations");
                Console.WriteLine("***************************************************************");
            

            // 
                // Get X value from Console
                //
                A = " ";
                NumberRelevance = false;
                Console.WriteLine();
                Console.WriteLine("Enter value for X : ");
              A = Console.ReadLine();
              NumberRelevance = double.TryParse(A,out X);
              
              //
              // Check that the number X is from the set of real numbers
            // If not re-request the value of X              
              //
                while (NumberRelevance == false)
                {
                    A = " ";
                    NumberRelevance = false;
                    Console.WriteLine("Error , You must enter a number. Please enter value for X : ");
                    A = Console.ReadLine();
                       NumberRelevance = double.TryParse(A,out X);
                }
                
                // 
                // Get Y value from Console
                //
                B = " ";
                NumberRelevance = false;
                Console.WriteLine();
            Console.WriteLine("Enter value for Y: ");
            B = Console.ReadLine();
            NumberRelevance = double.TryParse(B,out Y);
            
            //
              // Check that the number Y is from the set of real numbers 
              // If not re-request the value of Y          
              //
            while (NumberRelevance == false)
                {
                B = " ";
                       NumberRelevance = false;
                  Console.WriteLine("Error , You must enter a number. Please enter value for Y : ");
                  B = Console.ReadLine();
                  NumberRelevance = double.TryParse(B,out Y);
            }
            
            //
            //  Clear console for result output and write Title
            //
            Console.Clear();
                Console.WriteLine("***************************************************************");
                Console.WriteLine("               Program basic arithmetic operations");
                Console.WriteLine("***************************************************************");
                Console.WriteLine();
                Console.WriteLine("   X = " + X.ToString() + "       Y = " + Y.ToString());
                
            //
            // Basic arithmetic operations + , - , * , /
            // Calculate and write the result
            //
            
            //
            // Z = X + Y
            //
            Z = X + Y;
            Console.WriteLine();
                Console.WriteLine("***************************************************************");
                   Console.WriteLine();
                Console.WriteLine("   Z = X + Y" + "    Z = " + X.ToString() + " + " + Y.ToString() + "     Z = " + Z.ToString());
                Console.WriteLine();

                //
            // Z = X - Y
            //
                Z = X - Y;
            Console.WriteLine();
                Console.WriteLine("***************************************************************");
                Console.WriteLine();
                Console.WriteLine("   Z = X - Y" + "    Z = " + X.ToString() + " - " + Y.ToString() + "     Z = " + Z.ToString());
                Console.WriteLine();
            
            //
            // Z = X * Y
            //
            Z = X * Y;
            Console.WriteLine();
                Console.WriteLine("***************************************************************");
                Console.WriteLine();
                Console.WriteLine("   Z = X * Y" + "    Z = " + X.ToString() + " * " + Y.ToString() + "     Z = " + Z.ToString());
               
                //
                // Z = X / Y
                //
                // If Y equals zero , dividing by zero is not possible.
                // You may try to skip this question , the result is interesting
                // from math aspect , if you have adequate scool you should anderstand.
                if ( Y != 0 )
                {
                    Z = X / Y;
                Console.WriteLine();
                    Console.WriteLine("***************************************************************");
                    Console.WriteLine();
                    Console.WriteLine("   Z = X / Y" + "    Z = " + X.ToString() + " / " + Y.ToString() + "     Z = " + Z.ToString());
                    Console.WriteLine();
                    Console.WriteLine("***************************************************************");
                    Console.WriteLine();
                }
            else
            {
                Console.WriteLine();
                    Console.WriteLine("***************************************************************");
                    Console.WriteLine();
                    Console.WriteLine("   Z = X / Y" + "    Z = ?" + "     Dividing by zero is not possible ");
                    Console.WriteLine();
                    Console.WriteLine("***************************************************************");
                    Console.WriteLine();

            }
            
            //
            // The request for an answer , if user wants to start program again.
            //
                Answer = " ";
                Console.WriteLine("Do you want to start the program again <Y/N>");
                Answer = Console.ReadLine();
                Answer = Answer.ToUpper();
                
                while(Answer != "Y" && Answer != "N")
                {
                    Answer = " ";
                    Console.WriteLine("Error, answer could only be Y for Yes or N for No !");
                    Console.WriteLine("Do you want to start the program again <Y/N>");
                    Answer = Console.ReadLine();
                    Answer = Answer.ToUpper();
                }
                
                if (Answer == "Y")
                {
                    goto beginning;
                }
                
                Console.WriteLine();
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
 
U prilogu se nalazi Basic arithmetic operations.rar arhiva s kompletnim rešenjem pisanim za IDE SharpDevelop C,
koje možete preuzeti i u okviru koga se nalazi i Release folder sa exe fajlom programa.Neophodan netframework 4.0 ili noviji.
 
Sve najbolje,
Autor


 
ċ
Perić Željko,
27.01.2012. 20.09
Comments