Bonjour,
Je suis débutante en c# et je suis entrain de réaliser une calculatrice avec interface graphique et prend en considération la priorité des opérations .
J'ai déjà fait un code mais lorsque j'ajoute la parti de la priorité le programme ça marche pas.
Est ce q'il y a quelqu'un qui peut m'aider ?
Merci

Voici le code du programme.
Le 1er programme c'est pour la priorité:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
namespace calcul
{
    public class operation : object {
		public string phrase;
		public string operateur;
		public operation droite;
		public operation gauche;
 
 
		public operation(string phrase) {
			this.phrase=phrase;
 
 
		}
 
		public void decouper(){
			//on recherche un + (le plus a doite possible) qui n'est pas dans une parenthese
			int position_plus=this.phrase.LastIndexOf("+");
			bool trouver_bon_plus = false;
			while (position_plus != -1 && trouver_bon_plus==false ){
				if (this.compte_jusqua("[", position_plus) != this.compte_jusqua("]", position_plus) ){
					position_plus = this.phrase.LastIndexOf("+", position_plus-1, position_plus);
				}
				else{
					trouver_bon_plus = true;
				}
			}
			if (trouver_bon_plus == false){
				position_plus = -1;
			}
 
			//on recherche un - (le plus a doite possible) qui n'est pas dans une parenthese
			int position_moin=this.phrase.LastIndexOf("-");
			bool trouver_bon_moin = false;
			while (position_moin != -1 && trouver_bon_moin==false ){
				if (this.compte_jusqua("[", position_moin) != this.compte_jusqua("]", position_moin) ){
					position_moin = this.phrase.LastIndexOf("-", position_moin-1, position_moin);
				}
				else{
					trouver_bon_moin = true;
				}
			}
			if (trouver_bon_moin == false){
				position_moin = -1;
			}
 
			// priorité au + et -
 
			// il ya a d'abord un +
			if ( (position_plus!=-1 && position_moin==-1) || (position_plus!=-1 && position_moin!=-1 && position_plus>position_moin) ){
				this.operateur = "+";
 
				string phrase_gauche=this.phrase;
				phrase_gauche = phrase_gauche.Remove(position_plus,phrase_gauche.Length-position_plus);	
				this.gauche = new operation(phrase_gauche);
 
				string phrase_droite=this.phrase;
				phrase_droite = phrase_droite.Remove(0,position_plus+1);
				this.droite = new operation(phrase_droite);				
			}
			// il ya a d'abord un -
			else if ( (position_plus==-1 && position_moin!=-1) || (position_plus!=-1 && position_moin!=-1 && position_moin>position_plus) ){
				this.operateur = "-";
 
				string phrase_gauche=this.phrase;
				phrase_gauche = phrase_gauche.Remove(position_moin,phrase_gauche.Length-position_moin);	
				this.gauche = new operation(phrase_gauche);
 
				string phrase_droite=this.phrase;
				phrase_droite = phrase_droite.Remove(0,position_moin+1);
				this.droite = new operation(phrase_droite);	
 
			}
 
 
 
 
			// maintenant * et /
			else {
				//on recherche un * (le plus a doite possible) qui n'est pas dans une parenthese
				int position_multiplier=this.phrase.LastIndexOf("*");
				bool trouver_bon_multiplier = false;
				while (position_multiplier != -1 && trouver_bon_multiplier==false ){
					if (this.compte_jusqua("[", position_multiplier) != this.compte_jusqua("]", position_multiplier) ){
						position_multiplier = this.phrase.LastIndexOf("*", position_multiplier-1, position_multiplier);
					}
					else{
						trouver_bon_multiplier = true;
					}
				}
				if (trouver_bon_multiplier == false){
					position_multiplier= -1;
				}
 
				//on recherche un / (le plus a doite possible) qui n'est pas dans une parenthese
				int position_diviser=this.phrase.LastIndexOf("/");
				bool trouver_bon_diviser = false;
				while (position_diviser != -1 && trouver_bon_diviser==false ){
					if (this.compte_jusqua("[", position_diviser) != this.compte_jusqua("]", position_diviser) ){
						position_diviser = this.phrase.LastIndexOf("/", position_diviser-1, position_diviser);
					}
					else{
						trouver_bon_diviser = true;
					}
				}				
				if (trouver_bon_diviser == false){
					position_diviser= -1;
				}				
 
 
 
 
				// il ya a d'abord un *
				if ( (position_multiplier!=-1 && position_diviser==-1) || (position_multiplier!=-1 && position_diviser!=-1 && position_multiplier>position_diviser) ){	// il ya a d'abord un *
					this.operateur = "*";
 
					string phrase_gauche=this.phrase;
					phrase_gauche = phrase_gauche.Remove(position_multiplier,phrase_gauche.Length-position_multiplier);	
					this.gauche = new operation(phrase_gauche);
 
					string phrase_droite=this.phrase;
					phrase_droite = phrase_droite.Remove(0,position_multiplier+1);
					this.droite = new operation(phrase_droite);				
				}
				// il y a d'abord un /
				else if ( (position_multiplier==-1 && position_diviser!=-1) || (position_multiplier!=-1 && position_diviser!=-1 && position_diviser>position_multiplier) ){	// il ya a d'abord un /
					this.operateur = "/";
 
					string phrase_gauche=this.phrase;
					phrase_gauche = phrase_gauche.Remove(position_diviser,phrase_gauche.Length-position_diviser);	
					this.gauche = new operation(phrase_gauche);
 
					string phrase_droite=this.phrase;
					phrase_droite = phrase_droite.Remove(0,position_diviser+1);
					this.droite = new operation(phrase_droite);	
 
				}
            }
 
		}
 
		public float calculer(float x){
			float resultat=0;
			this.decouper();
			// cas: + - * / ^
			if(this.gauche!=null && this.droite!=null){
				if(this.operateur=="+"){
					resultat=this.gauche.calculer(x)+this.droite.calculer(x);
					return resultat;
				}
				else if(this.operateur=="*"){
					resultat=this.gauche.calculer(x)*this.droite.calculer(x);
					return resultat;
				}
				else if(this.operateur=="-"){
					resultat=this.gauche.calculer(x)-this.droite.calculer(x);
					return resultat;					
				}
				else if(this.operateur=="/"){
					resultat=this.gauche.calculer(x)/this.droite.calculer(x);
					return resultat;
				}	
 
			}
 
			return resultat;
 
		}
 
 
 
 
 
 
 
 
 
		// exemple: compte_depuis("a", 3) de "aaaaaa" =>3
		public int compte_depuis(string ch, int debut){
			int pos;
			int nb=0;
			bool arreter=false;
			while( (debut <= this.phrase.Length) && (arreter == false) ){
				pos=phrase.IndexOf(ch,debut);
				if(pos != -1){
					nb++;
					debut=pos+1;
				}
				else{
					arreter=true;
				}
 
 
			}
			return nb;
		}
 
 
 
		// exemple: compte_jusqua("a", 3) de "aaaaaa" =>3
		public int compte_jusqua(string ch, int fin){
			int pos;
			int nb=0;
			bool arreter=false;
			int i=0;
			while( (i <= fin) && (arreter == false) && (i <= this.phrase.Length) ){
				pos=phrase.IndexOf(ch, i, fin-i);
				if(pos != -1){
					nb++;
					i=pos+1;
				}
				else{
					arreter=true;
				}
 
 
			}
			return nb;
		}
 
 
 
	}
}
Le 2eme est ce lui le principale:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using calcul;
namespace tetcalc
{
    public partial class Form1 : Form
    {
        string op = "";
        double op1 = 0;
        double op2 = 0;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void buttonegal_Click(object sender, System.EventArgs e)
        {
            string equation = this.txtinsert.Text;
            string string_x = this.txtresults.Text;
            float x;
 
            if (equation != "")
            {
                if (string_x != "")
                {
                    decimal decimal_x = Convert.ToDecimal(string_x);
                    x = (float)decimal_x;
                }
                else
                {
                    x = 0f;
                }
 
                operation lounis = new operation(equation);
                float res = lounis.calculer(x);
                //this.textBox_resultat.Text=Convert.ToString(res);
                this.txtresults.Text = Convert.ToString(res);
            }
        }
 
        private void txtinsert_TextChanged(object sender, EventArgs e)
        {
 
        }
 
        private void button08_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "8";
        }
 
        private void button01_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "1";
        }
 
        private void button02_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "2";
        }
 
        private void button03_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "3";
        }
 
        private void button04_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "4";
        }
 
        private void button05_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "5";
        }
 
        private void button06_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "6";
        }
 
        private void button09_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "9";
        }
 
        private void buttondivis_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "/";
        }
 
        private void buttonmultipl_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "*";
 
        }
 
        private void buttonmoins_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "-";
        }
 
        private void buttonplus_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "+";
 
        }
 
        private void buttonvergul_Click(object sender, EventArgs e)
        {
            txtinsert.Text += ",";
        }
 
        private void buttonplusmoins_Click(object sender, EventArgs e)
        {
            double op = -Convert.ToDouble(txtinsert.Text);
            txtinsert.Text = op.ToString();
        }
 
        private void button00_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "0";
        }
 
        private void buttonparent2_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "]";
        }
 
        private void buttonparent1_Click(object sender, EventArgs e)
        {
            txtinsert.Text += "[";
        }
 
        private void buttonAC_Click(object sender, EventArgs e)
        {
            txtinsert.Text = "";
            txtresults.Text = "";
            op1 = op2 = 0;
        }
 
        private void txtresults_TextChanged(object sender, EventArgs e)
        {
            this.txtresults.SelectionStart = txtresults.Text.Length;
            this.txtresults.ScrollToCaret();
 
        }
 
        private void txtinsert_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                  && !char.IsDigit(e.KeyChar)
                       && e.KeyChar != ',')
            {
                e.Handled = true;
            }
 
            // only allow one decimal point
            if (e.KeyChar == ','
                && (sender as TextBox).Text.IndexOf(',') > -1)
            {
                e.Handled = true;
            }
        }
    }
}