Calculatrice avec priorite

fl2010 Messages postés 24 Date d'inscription vendredi 10 décembre 2010 Statut Membre Dernière intervention 19 juin 2011 - 15 janv. 2011 à 17:09
cs_rt15 Messages postés 3874 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 7 novembre 2014 - 17 janv. 2011 à 10:38
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'aidez c'est urgent.
merci
voici le code du programme.
le 1er programme c'est pour la priorité:
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:

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;
}
}
}
}

1 réponse

cs_rt15 Messages postés 3874 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 7 novembre 2014 13
17 janv. 2011 à 10:38
Bonjour,

Pour une question C#, merci de poster sur csharpfr.com dans un thème C#.

[ Déplacé sur csharpfr.com ]
0
Rejoignez-nous