2~5. C# 윈도우 프로그래밍: 사칙연산을 하는 계산기를 만들자.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Clear();
}
private void Clear()
{
textNum1.Clear();
textNum2.Clear();
textResult.Clear();
}
private void Calc(String op)
{
try
{
if(string.IsNullOrEmpty(textNum1.Text) || string.IsNullOrEmpty(textNum2.Text)){
MessageBox.Show("공백이 입력되었습니다.");
Clear();
}
else{
switch(op){
case "+": textResult.Text = (double.Parse(textNum1.Text)+double.Parse(textNum2.Text)).ToString(); break;
case "-": textResult.Text = (double.Parse(textNum1.Text) - double.Parse(textNum2.Text)).ToString(); break;
case "*": textResult.Text = (double.Parse(textNum1.Text) * double.Parse(textNum2.Text)).ToString(); break;
case "/": textResult.Text = (double.Parse(textNum1.Text) / double.Parse(textNum2.Text)).ToString(); break;
}
lbResult.Text = op;
}
}
catch { MessageBox.Show("숫자를 입력하세요."); Clear(); };
}
private void btnClear_Click(object sender, EventArgs e)
{
Clear();
}
private void btnPlus_Click(object sender, EventArgs e)
{
Calc("+");
}
private void btnMinus_Click(object sender, EventArgs e)
{
Calc("-");
}
private void btnMulti_Click(object sender, EventArgs e)
{
Calc("*");
}
private void btnDivide_Click(object sender, EventArgs e)
{
Calc("/");
}
}
}
15. 등차수열: 'X'와 공백(' ')을 이용하여 피라미드 계단을 만들자.
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(String[] args)
{
Console.WriteLine("삼각형을 출력합니다.");
Console.Write("정수 한 개를 입력하세요: ");
int num = int.Parse(Console.ReadLine());
int i, j;
for (i = 0; i < num; i++)
{
for (j = 0; j < num - i; j++) { Console.Write(" "); }
for (j = 0; j < 2*i + 1; j++) { Console.Write("X"); }
Console.WriteLine();
}
Console.WriteLine();
for (i = 0; i < num; i++)
{
for (j = 0; j < num - i; j++) { Console.Write("X"); }
for (j = 0; j < i * 2 + 1; j++) { Console.Write(" "); }
for (j = 0; j < num - i; j++) { Console.Write("X"); }
Console.WriteLine();
}
}
}
}
16. Math 클래스: 수학 메서드를 이용하여 다이아몬드를 만들자.
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(String[] args)
{
Console.WriteLine("다이아몬드를 출력합니다.");
Console.Write("홀수 정수 한 개를 입력하세요: ");
int num = int.Parse(Console.ReadLine());
int i, j;
for (i = 0; i < num; i++)
{
for (j = 0; j < Math.Abs(i - num / 2); j++) { Console.Write(" "); }
for (j = 0; j < num - 2 *Math.Abs(i - num /2 ); j++) { Console.Write("X"); }
Console.WriteLine();
}
Console.WriteLine();
for (i = 0; i < num; i++)
{
for (j = 0; j < Math.Abs(i - num / 2); j++) { Console.Write("X"); }
for (j = 0; j < num - 2 * Math.Abs(i - num / 2); j++) { Console.Write(" "); }
for (j = 0; j < Math.Abs(i - num / 2); j++) { Console.Write("X"); }
Console.WriteLine();
}
}
}
}
'C# > 공부' 카테고리의 다른 글
C# 실력 늘리기 3일차 (0) | 2022.07.21 |
---|---|
C# 실력 늘리기 2일차 (0) | 2022.07.20 |
시리얼 통신 (0) | 2022.07.11 |
4. 클래스와 메소드 (0) | 2022.07.06 |
3. 조건문 / 반복문 / 제어문 (0) | 2022.07.06 |