c#制作计算器实现加、减、乘、除、指数、对数、平方、开根号、正弦、余弦、绝对值代码
时间: 2023-11-05 08:06:04 浏览: 122
用c#编写的计算器可以实现加减乘除等功能
5星 · 资源好评率100%
以下是C#制作计算器实现加、减、乘、除、指数、对数、平方、开根号、正弦、余弦、绝对值的代码:
```
using System;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数字:");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入运算符:");
string op = Console.ReadLine();
Console.WriteLine("请输入第二个数字:");
double num2 = Convert.ToDouble(Console.ReadLine());
switch (op)
{
case "+":
Console.WriteLine("结果为:" + (num1 + num2));
break;
case "-":
Console.WriteLine("结果为:" + (num1 - num2));
break;
case "*":
Console.WriteLine("结果为:" + (num1 * num2));
break;
case "/":
Console.WriteLine("结果为:" + (num1 / num2));
break;
case "^":
Console.WriteLine("结果为:" + Math.Pow(num1, num2));
break;
case "log":
Console.WriteLine("结果为:" + Math.Log(num1, num2));
break;
case "sqrt":
Console.WriteLine("结果为:" + Math.Sqrt(num1));
break;
case "sin":
Console.WriteLine("结果为:" + Math.Sin(num1));
break;
case "cos":
Console.WriteLine("结果为:" + Math.Cos(num1));
break;
case "abs":
Console.WriteLine("结果为:" + Math.Abs(num1));
break;
default:
Console.WriteLine("无效的运算符!");
break;
}
Console.ReadLine();
}
}
}
```
阅读全文