茅塞顿开的C#代码——通用型科学计算器
发布人:shili8
发布时间:2023-12-06 05:23
阅读次数:75
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. Please try again.); break; } Console.WriteLine(The result is: + result); } } } }
以上是一个简单的科学计算器的C#代码示例。用户可以选择不同的操作,然后输入相应的数字进行计算。这个示例中只包含了加法的代码,其他操作的代码需要根据具体的算法进行编写。这个示例可以作为一个基础框架,根据需要进行扩展和完善。