-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
38 lines (34 loc) · 1.11 KB
/
Copy pathProgram.cs
File metadata and controls
38 lines (34 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 사칙연산 계산기 (C#)
// 실행: dotnet run
// 사용: 3 + 4 형태로 입력, q 입력 시 종료
Console.WriteLine("계산기 (예: 3 + 4, 종료: q)");
while (true)
{
Console.Write("> ");
string? line = Console.ReadLine();
if (line is null || line.Trim().ToLower() == "q")
break;
string[] parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 3
|| !double.TryParse(parts[0], out double a)
|| !double.TryParse(parts[2], out double b))
{
Console.WriteLine("입력 형식 오류 (예: 3 + 4)");
continue;
}
switch (parts[1])
{
case "+": Console.WriteLine($"= {a + b}"); break;
case "-": Console.WriteLine($"= {a - b}"); break;
case "*": Console.WriteLine($"= {a * b}"); break;
case "/":
if (b == 0)
Console.WriteLine("0으로 나눌 수 없습니다");
else
Console.WriteLine($"= {a / b}");
break;
default:
Console.WriteLine($"지원하지 않는 연산자: {parts[1]}");
break;
}
}