게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[C#] 질문...글입니다.
게시물ID : programmer_12085짧은주소 복사하기
작성자 : 와사비덮밥
추천 : 0
조회수 : 574회
댓글수 : 10개
등록시간 : 2015/07/14 11:06:27
옵션
  • 본인삭제금지
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Text.RegularExpressions;
 
 
namespace WindowsApplication4
{
 
    public partial class Form1 : Form
    {
        string[] add = null;
        string[] min = null;
        string[] mul = null;
        string[] div = null;
        int num1, num2, num3, num4;
        int sum = 0, sum2 = 0;
        double dsum;
        char calc1, calc2, calc3, calc4;
        string n;
        int count = 0;
        public Form1()
        {
            InitializeComponent();
            Create_UserEvent();
        }
        #region "USER_EVNET"
        private void Create_UserEvent()
        {
            //"[Event: UserClick/Select]==============================================================================="
            this.button1.Click += new System.EventHandler(this.button_Click);
            this.button2.Click += new System.EventHandler(this.button_Click);
            this.button3.Click += new System.EventHandler(this.button_Click);
            this.button4.Click += new System.EventHandler(this.button_Click);
            this.button5.Click += new System.EventHandler(this.button_Click);
            this.button6.Click += new System.EventHandler(this.button_Click);
            this.button7.Click += new System.EventHandler(this.button_Click);
            this.button8.Click += new System.EventHandler(this.button_Click);
            this.button9.Click += new System.EventHandler(this.button_Click);
            this.button10.Click += new System.EventHandler(this.button_Click);
            this.button11.Click += new System.EventHandler(this.button_Click);
            this.button12.Click += new System.EventHandler(this.button_Click);
            this.button13.Click += new System.EventHandler(this.button_Click);
            this.button14.Click += new System.EventHandler(this.button_Click);
            this.button15.Click += new System.EventHandler(this.button_Click);
            this.button16.Click += new System.EventHandler(this.button_Click);
            //"[Event: KeyDown/KeyPress]==============================================================================="
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_KeyDown);
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form_KeyPress);
            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Fields_KeyPress);
        }
        #endregion
        #region "KEYDOWN/KEYPRESS"
        private void Form_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.C) { Clipboard.Clear(); return; }
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    break;
            }
        }
        private void Form_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Strings.Asc(e.KeyChar) >= 97 && Strings.Asc(e.KeyChar) <= 122) e.KeyChar = Strings.Chr(Strings.Asc(e.KeyChar) - 32);
            if (Strings.Asc(e.KeyChar) == 34 || Strings.Asc(e.KeyChar) == 39) e.KeyChar = Strings.Chr(0);
        }
        private void Fields_KeyPress(object sender, KeyPressEventArgs e)
        {
            Control sender1 = (Control)sender;
            switch (sender1.Name)
            {
                case "textBox1":  //숫자,대문자,지정문자만 허용
                    if (Strings.Asc(e.KeyChar) >= 97 && Strings.Asc(e.KeyChar) <= 122) e.KeyChar = Strings.Chr(Strings.Asc(e.KeyChar) - 32);
                    if ((Strings.Asc(e.KeyChar) < 48 || Strings.Asc(e.KeyChar) > 57) && Strings.Asc(e.KeyChar) != 61 && Strings.Asc(e.KeyChar) != 47
                        && Strings.Asc(e.KeyChar) != 45 && Strings.Asc(e.KeyChar) != 42 && Strings.Asc(e.KeyChar) != 43)
                    {
                        e.KeyChar = (char)0;
                    }
                    break;
            }
        }
        #endregion
        private void button_Click(object sender, EventArgs e)
        {
            Control sender1 = (Control)sender;
            switch (sender1.Name)
            {
                case "button1":
                case "button2":
                case "button3":
                case "button4":
                case "button5":
                case "button6":
                case "button7":
                case "button8":
                case "button9":
                case "button10":
                    textBox1.Text += sender1.Text;
                    break;
                case "button11":
                    n = textBox1.Text;
                    textBox1.Text += sender1.Text;
                    calc1 = '+';
                    break;
                case "button12":
                    n = textBox1.Text;
                    textBox1.Text += sender1.Text;
                    calc2 = '-';
                    break;
                case "button13":
                    n = textBox1.Text;
                    textBox1.Text += sender1.Text;
                    calc3 = '*';
                    break;
                case "button14":
                    n = textBox1.Text;
                    textBox1.Text += sender1.Text;
                    calc4 = '/';
                    break;
                case "button16":
                    n = textBox1.Text;
                    textBox1.Text += sender1.Text;
                    if (calc1 == '+')
                    {
                        for (int j = 0; j < textBox1.Text.Split('+').Length; j++)
                        {
                            add = n.Split('+', '-');
                            sum += int.Parse(add[j]);
                        }
                        num1 = sum;
                    }
                    if (calc2 == '-')
                    {
                        min = n.Split('+', '-');
                        sum = int.Parse(min[0]);
                        for (int j = 1; j < textBox1.Text.Split('-').Length; j++)
                        {
                            min = n.Split('-', '+');
                            sum -= int.Parse(min[j]);
                        }
                        num2 = sum;
                       
                    }
                    if (calc3 == '*')
                    {
                        sum = 1;
                        for (int j = 0; j < textBox1.Text.Split('*').Length; j++)
                        {
                            mul = n.Split('*');
                            sum *= int.Parse(mul[j]);
                           
                        }
                        num3 = sum;
                    }
                    if (calc4 == '/')
                    {
                        div = n.Split('/');
                        sum = int.Parse(div[0]);
                        for (int j = 1; j < textBox1.Text.Split('/').Length; j++)
                        {
                            div = n.Split('/');
                            sum /= int.Parse(div[j]);
                         
                        }
                        num4 = sum;
                    }
                    textBox1.Text += sum.ToString();
                    break;
                case "button15":
                    textBox1.Clear();
                    break;
            }

        }
    }
}
 
이게 전체적인 코드입니다.
기본적인 설계로는 텍스트박스에 수식을 입력하면 텍스트박스의 내용을 읽어와서 연산을 하려고 하고 있습니다.
문제는
텍스트박스에 입력을 2+2+2 혹은 3-1-2, 3*1*2, 6/2/1 이런식으로 한가지의 연산만 쓸때는 정상적으로 연산이 되지만,
3+2-1/2 같은 연산자를 여러개 쓸경우 값이 이상하게 나옵니다...
아예 처음부터 컨셉을 잘못잡았는지.. 지금 이상태에서 해결하려 해도 잘 되지가 않네요..
해결하는 방법이 무엇이 있을까요?
 
출처 제 머릿속...
결코 학교 과제물이 아니며...
개인적으로 씨샵 공부하던중 도저히 해결법이 보이지 않아 이렇게 질문드립니다.
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호