/* מחלקה ראשית המשתמשת במחלקת המחשבון */ using System; public class CalculatorTest { public static void Main() { int operation; int num; int num1; Console.WriteLine("Please enter the number of the operation you would like to perform:"); Console.WriteLine("1: power"); Console.WriteLine("2: is power"); Console.WriteLine("3: digit sum"); operation = int.Parse(Console.ReadLine()); switch (operation) { case 1: Console.Write("Please enter the base number: "); num = int.Parse(Console.ReadLine()); Console.Write("Please enter the exponent: "); num1 = int.Parse(Console.ReadLine()); Console.WriteLine("{0}^{1}={2}", num, num1, Calculator.Power(num, num1)); break; case 2: Console.Write("Please enter the result: "); num = int.Parse(Console.ReadLine()); Console.Write("Please enter the base: "); num1 = int.Parse(Console.ReadLine()); if (Calculator.IsPower(num, num1)) Console.WriteLine("{0} is a power of {1}",num ,num1); else Console.WriteLine("{0} is not a power of {1}", num, num1); break; case 3: Console.Write("Please enter a number: "); num = int.Parse(Console.ReadLine()); Console.WriteLine("The digit sum of {0} is {1}", num, Calculator.DigitSum(num)); break; default: Console.WriteLine("Wrong option"); break; }// Switch }// Main }// ClaculatorTest /* מחלקת שירות: מחשבון */ public class Calculator { //פעולה המקבלת בסיס ומעריך ומחזירה את הבסיס בחזקת המעריך public static int Power(int b, int exponent) { if (exponent == 0) return 1; int result = b; for (int i = 1; i < exponent; i++) result = result * b; return result; } //פעולה המקבלת שני מספרים שלמים ומחזירה: // האם הראשון הוא חזקה של השני public static bool IsPower(int result, int b) { double div = result; if (result == 1) return true; if (result % b != 0) return false; while (result > 1) { div = div / b; result = result / b; } return (div == 1); } //פעולה המקבלת מספר שלם ומחזירה את סכום ספרותיו public static int DigitSum(int num) { int sum = 0; while (num != 0) { sum = sum + num % 10; num = num / 10; } return sum; } }// Calculator