茅塞顿开的C#代码——通用型科学计算器
发布人:shili8
发布时间:2023-12-06 13:44
阅读次数:89
csharp using System; namespace ScientificCalculator { class Program { static void Main(string[] args) { Console.WriteLine(Welcome to the Scientific Calculator!); while (true) { Console.WriteLine(Please enter the operation you want to perform:); Console.WriteLine(1. Addition); Console.WriteLine(2. Subtraction); Console.WriteLine(3. Multiplication); Console.WriteLine(4. Division); Console.WriteLine(5. Exponentiation); Console.WriteLine(6. Square root); Console.WriteLine(7. Logarithm); Console.WriteLine(8. Exit); int choice = Convert.ToInt32(Console.ReadLine()); if (choice == 8) { break; } double result = 0; switch (choice) { case 1: Console.WriteLine(Enter the first number:); double num1 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(Enter the second number:); double num2 = Convert.ToDouble(Console.ReadLine()); result = num1 + num2; break; case 2: // Subtraction code break; case 3: // Multiplication code break; case 4: // Division code break; case 5: // Exponentiation code break; case 6: // Square root code break; case 7: // Logarithm code break; default: Console.WriteLine(Invalid choice); break; } Console.WriteLine(The result is: + result); } } } }
以上是一个简单的科学计算器的C#代码示例。用户可以选择进行加法、减法、乘法、除法、指数运算、平方根和对数运算。用户输入选择后,程序会要求用户输入相应的数字,然后进行计算并输出结果。这个程序可以作为一个基础的科学计算器,可以根据需求进行扩展和优化。