用C#编写一个简易计算器,麻烦把程序写出来。

2024-11-30 04:51:27
推荐回答(1个)
回答1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
private static double c = 0;
private static double comp(int a, int b,string sig)
{
if(sig == "+"){ c = a + b;}
if(sig == "-"){ c = a - b;}
if(sig == "*"){ c = a*b;}
if(sig == "/" && b!=0){ c = a/b;}
return c;
}
static void Main(string[] args)
{
double result=0;
Console.WriteLine("请输入两个操作数及运算符:");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
char opr= Convert.ToChar(Console.ReadLine());
result = comp(a, b,opr);
Console.WriteLine(result);
Console.ReadLine();
}
}
}