malek2212
Messages postés18Date d'inscriptionsamedi 19 juillet 2014StatutMembreDernière intervention22 janvier 2018
-
Modifié par KX le 5/12/2015 à 14:22
Bonjour,
mon problem aujourd'hui c'est que je veux verifier un fichier qui contient des expression arithmÉétique apres je veut les compter
le problem c'est que quand je
mais par exemple dans mon fichier
5+7
8+9
il compte la somme de la 2eme expression ????
et voila c'est mon code soucre
public static void main(String[] args) throws IOException {
int s=0,m=0;
float k=1;
//declaration
Path p=Paths.get("C:\\Users\\Toshiba\\Desktop\\p.txt");
Path h=Paths.get("C:\\Users\\Toshiba\\Desktop\\exp.txt");
String Str=new String(Files.readAllBytes(p));
List<String> lines=Arrays.asList(Str.split("\n"));
for(int i=0;i<lines.size();i++)
{ System.out.print(lines.get(i));
if(lines.get(i).matches("([0-9]*[+-/*][0-9]+)"))
{
System.out.println("la lign num "+i+" est bien ecrit");
}
else{
System.out.println("la lign num "+i+" est mal ecrit");
System.out.println("votre fichier est mal ecrit ");
return;
}
}/// traitement de chaque lign
for(int i=0;i<lines.size();i++)
{
if(lines.get(i).matches("([0-9]+[+][0-9]*)*"))
{
String st[]= lines.get(i).split("[+]");
for(int j=0;j<st.length;j++)
{
s=s+Integer.parseInt(st[j]);
}
}
else if(lines.get(i).matches("([0-9]+[-][0-9]*)*"))
{
String st[]= lines.get(i).split("[-]");
for(int j=1;j<st.length;j++)
{
m=Integer.parseInt(st[0])+((-1)*(Integer.parseInt(st[i])));
}
System.out.println("la somme de soustarction est"+m);
}
else if(lines.get(i).matches("([0-9]+[*][0-9]*)*"))
{
String st[]= lines.get(i).split("[*]");
for(int j=0;j<st.length;j++)
{
k=k*Integer.parseInt(st[j]);
}
System.out.println(k);
}
else if (lines.get(i).matches("([0-9]+[/][0-9]*)*"))
{
String st[]= lines.get(i).split("[/]");
for(int j=1;j<st.length;j++)
{
k=Integer.parseInt(st[0])/Integer.parseInt(st[j]);
}
System.out.println(" "+k);
}
}
}
}