fl2010
Messages postés24Date d'inscriptionvendredi 10 décembre 2010StatutMembreDernière intervention19 juin 2011
-
15 janv. 2011 à 17:09
cs_rt15
Messages postés3874Date d'inscriptionmardi 8 mars 2005StatutModérateurDerniè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);
// 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();
}