본문 바로가기
코스웨어/16년 스마트컨트롤러

2016-05-18_조재찬_업무일지_ C#-OOP와 클래스

by 알 수 없는 사용자 2016. 5. 18.
728x90
반응형

c# - plus,minus 


OP 프로젝트-OP_CLS.cs

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

namespace OP
{
    public class OP_CLS
    {
        public string PLUS(string a, string b)
        {
            string temp = (int.Parse(a) + int.Parse(b)).ToString();
            return temp;
        }
        public string MINUS(string a, string b)
        {
            string temp = (int.Parse(a) - int.Parse(b)).ToString();
            return temp;
        }
    }
}




Con_Cal 프로젝트-Program.cs


/*project의 참조에서 OP를 추가

6번째 줄 using OP; 추가*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OP;

namespace Con_Cal
{
    class Program
    {
        static void Main(string[] args)
        {
            string n1 = Console.ReadLine();
            string n2= Console.ReadLine();
            OP_CLS o = new OP_CLS();
            string sum = o.PLUS(n1, n2);
            string result = o.MINUS(n1, n2);
            Console.WriteLine(sum + "\t" + result);
        }
    }
}


WinCal 프로젝트 추가(window form)

// WinCal 프로젝트를 시작 프로젝트로 설정


windows form



두개의 정수의 입력할 text box 1과 2(txt1, txt2)

+, - 연산을 위한 버튼

그리고 결과값을 표시할 text box 3(txt3)



WinCal프로젝트-Form1.cs   

/*textbox의 값을 읽어들이고 +,-연산을 할 두개의 버튼에 코드 추가

마찬가지로 OP를 참조로 한다*/

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;
using OP;

namespace WinCal
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OP_CLS o = new OP_CLS();
            txt3.Text = (o.PLUS(txt1.Text, txt2.Text)).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OP_CLS o = new OP_CLS();
            txt3.Text = (o.MINUS(txt1.Text, txt2.Text)).ToString();
        }

        private void txt1_TextChanged(object sender, EventArgs e)
        {            
        }
    }
}


Output : 





728x90