|
|
|
|
#include <iostream.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <fcntl.h> #include <sys/types.h> #include <stdio.h> int p[19][2]; int Ecom[5]; int Eopt[5]; char motif[5][20]; void start() {
system("clear");
cout<<"********************************************************************************"<<endl;
cout<<"* *"<<endl;
cout<<"* MONTY SHELL (mini quand meme) *"<<endl;
cout<<"* *"<<endl;
cout<<"* Bonjours !-) *"<<endl;
cout<<"* *"<<endl;
cout<<"* pour sortir taper end<Return> *"<<endl;
cout<<"* *"<<endl;
cout<<"* toutes les commandes UNIX peuvent etre utilisees *"<<endl;
cout<<"* plus les caracteres speciaux |, < et > *"<<endl;
cout<<"* avec gestion de l'espace incluse *"<<endl;
cout<<"* *"<<endl;
cout<<"********************************************************************************"<<endl;
}
void end() {
system("clear");
cout<<"********************************************************************************"<<endl;
cout<<"* *"<<endl;
cout<<"* MONTY SHELL *"<<endl;
cout<<"* vous remercie pour votre utilisation *"<<endl;
cout<<"* *"<<endl;
cout<<"* Good bye !-) *"<<endl;
cout<<"* *"<<endl;
cout<<"* pour contact vvesseli@ufr.lirmm.fr *"<<endl;
cout<<"* http://www.MultiMania.com/montcook *"<<endl;
cout<<"* *"<<endl;
cout<<"********************************************************************************"<<endl;
}
void input(int &nb,char *lc) {
do {
cout << "MontyShell> "<< flush;
nb = read(0,lc,256);
lc[nb-1]='\0';
}
while(nb<=1);//Attends que le nombre de caractères de la ligne de commande soit superieur a 1
}
void test(char *com[10][10],char *lc,int i,int &qqch,int &nbCom,int &nbOpt,char* &inFile, char* &outFile) {
if(lc[i-1]=='\0') {
switch(qqch) {
case 1:
inFile=lc+i;
break;
case 2:
nbOpt=0;
com[++nbCom][nbOpt++]=lc+i;
break; //nvlle commande
case 3:
outFile=lc+i;
break;
default:
com[nbCom][nbOpt++]=lc+i;//nvlle option
}
}
qqch=0;
}
void analyse(char *com[10][10],char *lc,int nb,int &pb,int &nbCom,char* &inFile,char* &outFile,int &Ecompt) {
outFile=0;
pb=0;
nbCom=0;
int nbOpt=0;
int qqch=0;
for(int i=0;i<nb;i++) {
switch (lc[i]) {
case '<':
if(nbCom==0 && nbOpt!=0)
qqch=1;
lc[i]='\0';
break;
case ' ':
lc[i]='\0';
break;
case '|':
if(nbOpt!=0) {
com[nbCom][nbOpt]=0;
qqch=2;
}
else {
cout<<"Mauvaise utilisation du pipe"<<endl;
pb=1;
}
lc[i]='\0';
break;
case '>':
qqch=3;
lc[i]='\0';
break;
default :
if(nbCom==0 && nbOpt==0)
com[nbCom][nbOpt++]=lc+i;
else
test(com,lc,i,qqch,nbCom,nbOpt,inFile,outFile);
if(lc[i]=='*' || lc[i]=='?')
if(Ecompt<6 && (Ecom[Ecompt-1]!=nbCom || Eopt[Ecompt-1]!=nbOpt-1)) {
Ecom[Ecompt]=nbCom;
Eopt[Ecompt++]=nbOpt-1;
}
}
}
if(nbCom==0 && nbOpt==0)
pb=1;//si la ligne est composer que d'espaces vides il y a un pb
else
com[nbCom++][nbOpt]=0;
}
void etoile(char *com[10][10],int Ecompt) {
for(int i=0;i<Ecompt;i++) {
int j=0;
int car=1;
motif[i][0]='^';
while(com[Ecom[i]][Eopt[i]][j]!='\0') {
switch (com[Ecom[i]][Eopt[i]][j]) {
case '*':
motif[i][j+car]='.';
car++;
motif[i][j+car]=com[Ecom[i]][Eopt[i]][j];
break;
case '?':
motif[i][j+car]='.';
break;
case '.':
motif[i][j+car]='\\';
car++;
motif[i][j+car]=com[Ecom[i]][Eopt[i]][j];
break;
default:
motif[i][j+car]=com[Ecom[i]][Eopt[i]][j];
}
j++;
}
}
pipe(p[0]);
for(int i=0;i< 2;i++) {
switch (fork()) {
case 0:
if(i==0) {
close(p[0][0]);
dup2(p[0][1],1);
execlp("ls","ls",0);
}
else {
int fdt=creat("temp",0644);
dup2(fdt,1);
close(p[0][1]);
dup2(p[0][0],0);
execlp("grep","grep",motif,0);
}
break;
default :
if(i==1) {
close(p[0][0]);
close(p[0][1]);
}
wait (0);
}
}
cout<<"Fichier temp : "<<endl;
system ("cat temp");
FILE *f;
f=fopen("temp","r");
int l=0;
int k=1;
char chaine[100][256];
while(fgets(chaine[l],256,f)) {
chaine[l][strlen(chaine[l])-1]='\0';
com[0][k++]=chaine[l++];
}
com[0][k]=0;
}
void in(int i) {
dup2(p[i-1][0],0);
close(p[i-1][1]);
}
void out(int i) {
dup2(p[i][1],1);
close(p[i][0]);
}
void outF(char* outFile) {
int fd=creat(outFile,0644);//fd=file descripteur
if(fd!=-1)
dup2(fd,1);//1 ecriture standat
else
cout<<"ATTENTION: Le fichier "<<outFile<<" existe"<<endl;
}
void exec(char *com[10][10],int nbCom,char* inFile,char* outFile) {
for (int i=0;i<nbCom;i++) {
if (i<nbCom-1)
pipe(p[i]);
switch (fork()) {
case 0:
if(i==0) {
if(inFile!=0) {//***************************Fichier d'entree******
int fd=open(inFile,O_RDONLY);//fd=file descripteur
dup2(fd,0);//0 lecture standart
cout<<inFile<<": fichier inexistant"<<endl;
}
if(nbCom>1)
out(i);//Redirection de la première commande******
}
if(i==nbCom-1) {
if(outFile!=0)
outF(outFile);//**********Fichier de sortie******
if(nbCom>1)
in(i);//***Redirection de la derniere commande******
}
//********************Redirection de la commande intermediaire******
if(i>0 && i<nbCom-1) {
in(i);
out(i);
}
if(outFile!=0 && nbCom==1)
outF(outFile);
execvp(com[i][0],com[i]);//**************************Execution******
cout<<com[i][0]<<": commande inconnue ;-)"<<endl;//Message d'erreur
exit(0);
default :
if(i>0) {
close(p[i-1][0]);
close(p[i-1][1]);
}
//IMPORTANT: Fermeture des entrees et sorties du pere avant wait et Femeture des p[i-1] et pas de p[i]
wait(0);
}
}
}
void affichage(char *com[10][10],int nbCom,char* outFile,char* inFile,int Ecompt) {
cout<<"Fichier d'entree : "<<inFile<<endl;
cout<<"Fichier de sortie : "<<outFile<<endl;
for(int i=0;i<nbCom;i++) {
int j=0;
while(com[i][j]!=0) {
cout<<"Commande["<<i<<"]["<<j<<"] = "<<com[i][j]<<endl;
j++;
}
cout<<"Commande["<<i<<"]["<<j<<"] = "<<com[i][j]<<endl;
}
if(Ecompt!=0)
for(int i=0;i<Ecompt;i++) {
cout<<"Etoile ("<<i+1<<") : "<<com[Ecom[i-1]][Eopt[i]]<<" -> "<<motif[i]<<endl;
}
}
void main() {
start();
char lc[256];
do {
int nb;
input(nb,lc);
if(strcmp(lc,"end")!=0) {
int pb;
int nbCom;
char *inFile=0;
char *outFile=0;
char *com[10][10];
int Ecompt=0;
Ecom[0]=-1;
Eopt[0]=-1;
analyse(com,lc,nb,pb,nbCom,inFile,outFile,Ecompt);
if(Ecompt!=0)
etoile(com,Ecompt);
affichage(com,nbCom,outFile,inFile,Ecompt);
if(pb==0)
exec(com,nbCom,inFile,outFile);
}
}
while(strcmp(lc,"end")!=0);
end();
}
pour télécharger download/MONTYSHE.CPP
#include <iostream.h> #include <string> #include <string.h> #include <unistd.h> class tableau
{
private:
int nbLine;
char ** table;
public:
tableau()
{
cout<<"debut tableau"<<endl;
string lineBrut;
cout<<"Saisissez votre ligne (le contenu de chaque ligne doit se termine par le caractere '*') :"<<endl;
cin>>lineBrut;
nbLine=0;
for(int i=0;i<lineBrut.length();i++)
{ if(lineBrut[i]=='*') nbLine++; }
table=new (char*)[nbLine];
int nbEtoile=0;
int startLine=0;
int longLine=lineBrut.length();
for(int i=0;i<lineBrut.length();i++)
{
if(lineBrut[i]=='*')
{
longLine=i+1-startLine-longLine;
startLine=i+1-longLine;
table[nbEtoile]=new char[longLine];
for(int j=0;j<longLine;j++)
table[nbEtoile][j]=lineBrut[startLine+j];
nbEtoile++;
}
}
cout<<"Nombre de lignes : "<<nbLine<<endl;
for(int i=0;i<nbLine;i++) {
int j=0;
while(table[i][j]!='*')
cout<<table[i][j++]<<" ";
cout<<endl;
}
}
~tableau ()
{ }
void affiche()
{
cout<<"Nombre de lignes : "<<nbLine<<endl;
for(int i=0;i<nbLine;i++) {
int j=0;
while(table[i][j]!='*')
cout<<table[i][j++]<<" ";
cout<<endl;
}
}
};
void main()
{
system("clear");
cout<<"debut main"<<endl;
tableau a();
// a.affiche();
cout<<"fin main"<<endl;
}
pour télécharger download/LIGNESEL.CC
Monome.h #ifndef Monome_h #define Monome_h class Monome {
int coef,deg;
Monome* suiv;
public:
Monome(int=0,int=0);
~Monome();
Monome* retSuiv() const;
void affiche() const;
void saisie();
int retCoef() const;
int retDeg() const;
void affSuiv(Monome*);
Monome somme(Monome);
};
typedef Monome* PtMon;
#endif
Monome.cc #include <iostream.h> #include "Monome.h" Monome::Monome(int c=0,int d=0) {
coef=c;
deg=d;
suiv=NULL;
}
Monome::~Monome() {
}
Monome* Monome::retSuiv() const {
return suiv;
}
void Monome::affiche() const {
if(deg==0)
cout<<coef;
else
if(deg==1)
cout<<coef<<"*x";
else
cout<<coef<<"*x^"<<deg;
}
void Monome::saisie() {
int c,d;
int ok=0;
while(ok==0) {
cout<<"Coefficient : ";
cin>>c;
cout<<"Degre : ";
cin>>d;
if(c==0)
cout<<"ERREUR: "<<c<<"*x^"<<d<<" n'est un monome\n";
else
ok=1;
}
coef=c;
deg=d;
}
int Monome::retCoef() const {
return coef;
}
int Monome::retDeg() const {
return deg;
}
void Monome::affSuiv(Monome* p) {
suiv=p;
}
Monome Monome::somme(Monome m2) {
Monome aux;
if(retDeg()==m2.retDeg())
Monome aux(retCoef()+m2.retCoef(),retDeg());
else
Monome aux;
return aux;
}
Polynome.h #include <string> #include "Monome.h" class Polynome {
string nom;
PtMon tete;
void affPremier(PtMon);
void affSuivant(PtMon j,PtMon nvsuiv);
public:
Polynome(string);
~Polynome();
PtMon premier() const;
PtMon suivant(PtMon) const;
int degr(PtMon) const;
int coef(PtMon) const;
void affiche() const;
void saisie();
void inserav(int,PtMon);
void degre() const;
void val(int) const;
void ajout();
void somme(const Polynome &,const Polynome &);
void multiplication(const Polynome &,const Polynome &);
};
Polynome.cc #include <iostream.h> #include <math.h> #include "Polynome.h" #include "Monome.h" Polynome::Polynome(string n) {
nom=n;
tete=new Monome; }
Polynome::~Polynome() {
//cout<<"warning in destructeur\t: "<<nom<<endl;
PtMon p=premier(), q=premier();
//int c=0; cout<<c<<" ";
while(p!=NULL) {
//cout<<++c<<" ";
q=p;
p=suivant(p);
delete q;
}
//cout<<"\nok out destructeur\t: "<<nom<<endl;
}
PtMon Polynome::premier() const {
return tete;
}
PtMon Polynome::suivant(PtMon j) const {
return j->retSuiv();
}
int Polynome::degr(PtMon j) const {
return j->retDeg();
}
int Polynome::coef(PtMon j) const {
return j->retCoef();
}
void Polynome::affiche() const {
cout<<nom<<"(x)=";
PtMon p=premier();
while(p!=NULL) {
p->affiche();
if(suivant(p)!=NULL)
cout<<"+";
p=suivant(p);
}
cout<<endl;
}
void Polynome::saisie() {
cout<<"*************** "<<nom<<" ****************\n";
int nbmon;
cout<<"entrez le nombre de polynomes : ";
cin>>nbmon;
Monome mon;
int p=0;
while(p!=nbmon) {
mon.saisie();
PtMon j=new Monome(mon.retCoef(),mon.retDeg());
inserav(mon.retDeg(),j);
p++;
}
cout<<"***********************************\n";
}
void Polynome::inserav(int d, PtMon j) {
if(degr(premier())==0 || degr(premier())==d)
if(degr(premier())==0)
affPremier(j);
else {
PtMon p=premier();
PtMon aux=new Monome(coef(j)+coef(premier()),degr(premier()));
affPremier(aux);
affSuivant(aux,suivant(p));
}
else {
if(degr(premier())<d) {
//cout<<"a1\n";
affSuivant(j,premier());
affPremier(j);
}
else {
//cout<<"a2\n";
PtMon p=premier();
while(suivant(p)!=NULL && degr(suivant(p))>d)
p=suivant(p);
if(suivant(p)==NULL) {
//cout<<"a3\n";
affSuivant(p,j);
}
else {
if(degr(suivant(p))<d) {
affSuivant(j,suivant(p));
affSuivant(p,j);
}
else {
PtMon aux=new Monome(coef(j)+coef(suivant(p)),degr(suivant(p)));
affSuivant(aux,suivant(suivant(p)));
affSuivant(p,aux);
}
}
}
}
//cout<<"+++++++++++++++++++++++++++++++++\n";
//affiche();
//cout<<"+++++++++++++++++++++++++++++++++\n";
}
void Polynome::affPremier(PtMon j) {
tete=j;
}
void Polynome::affSuivant(PtMon j, PtMon nvsuiv) {
j->affSuiv(nvsuiv);
}
void Polynome::degre() const {
cout<<"max degre("<<nom<<")="<<degr(tete)<<endl;
}
void Polynome::val(int v) const {
double valeur=0;
PtMon p=premier();
while(p!=NULL) {
valeur+=coef(p)*exp(log(v)*degr(p));
//cout<<valeur<<endl;
p=suivant(p);
}
cout<<nom<<"("<<v<<")="<<valeur<<endl;
}
void Polynome::ajout() {
cout<<"*************** "<<nom<<" ****************\n";
Monome mon;
mon.saisie();
PtMon j=new Monome(mon.retCoef(),mon.retDeg());
inserav(mon.retDeg(),j);
}
void Polynome::somme(const Polynome & p1,const Polynome & p2) {
PtMon p=premier();
PtMon pu=p1.premier();
PtMon pd=p2.premier();
//int c=0;
while(p!=NULL) {
if(pu==NULL || pd==NULL) {
if(pd!=NULL) {
//cout<<++c<<". p1==NULL\n";
PtMon aux=new Monome(p2.coef(pd),p2.degr(pd));
inserav(p2.degr(pd),aux);
p=aux;
pd=p2.suivant(pd);
}
else {
if(pu!=NULL) {
//cout<<++c<<". p2==NULL\n";
PtMon aux=new Monome(p1.coef(pu),p1.degr(pu));
inserav(p1.degr(pu),aux);
p=aux;
pu=p1.suivant(pu);
}
else
p=NULL;
}
}
else {
if(p2.degr(pd)>p1.degr(pu)) {
//cout<<++c<<". p2>p1\n";
PtMon aux=new Monome(p2.coef(pd),p2.degr(pd));
inserav(p2.degr(pd),aux);
p=aux;
pd=p2.suivant(pd);
}
else {
if(pd==NULL || p2.degr(pd)<p1.degr(pu)) {
//cout<<++c<<". p2<p1\n";
PtMon aux=new Monome(p1.coef(pu),p1.degr(pu));
inserav(p1.degr(pu),aux);
p=aux;
pu=p1.suivant(pu);
}
else {
//cout<<++c<<". p2==p1\n";
PtMon aux=new Monome(p2.coef(pd)+p1.coef(pu),p2.degr(pd));
inserav(p2.degr(pd),aux);
p=aux;
pd=p2.suivant(pd);
pu=p1.suivant(pu);
}
}
}
//cout<<"+++++++++++++++++++++++++++++++++++\n";
//affiche();
//cout<<"+++++++++++++++++++++++++++++++++++\n";
}
}
void Polynome::multiplication(const Polynome & p1,const Polynome & p2) {
PtMon p=premier();
PtMon pu=p1.premier();
//int c=0;
// cout<<++c<<" init\n";
while(pu!=NULL) {
//cout<<"in 1\n";
PtMon pd=p2.premier();
while(pd!=NULL) {
//cout<<"in 2\n";
if(degr(premier())==0 && coef(premier())==0) {
//cout<<++c<<". debut\n";
PtMon j=new Monome(p1.coef(pu)*p2.coef(pd),p1.degr(pu)+p2.degr(pd));
affPremier(j);
pd=p2.suivant(pd);
}
else {
//cout<<++c<<". millieu\n";
PtMon j=new Monome(p1.coef(pu)*p2.coef(pd),p1.degr(pu)+p2.degr(pd));
inserav(p1.degr(pu)+p2.degr(pd),j);
pd=p2.suivant(pd);
}
}
pu=p1.suivant(pu);
}
}
Main.cc #include "Polynome.h" #include "Monome.h" void main() {
system("clear");
Polynome p1("p1"),p2("p2");
p1.saisie();
p1.affiche();
p2.saisie();
p2.affiche();
cout<<"***********************************\n";
p1.degre();
p2.degre();
cout<<"***********************************\n";
p1.val(3);
p2.val(3);
p1.ajout();
p1.affiche();
cout<<"***********************************\n";
Polynome p3("p3");
p3.somme(p1,p2);
p3.affiche();
p1.affiche();
p2.affiche();
cout<<"***********************************\n";
Polynome p4("p4");
p4.multiplication(p1,p2);
p4.affiche();
p3.affiche();
p2.affiche();
p1.affiche();
cout<<"***************end*****************\n";
}
pour télécharger download/MONOME.H, download/MONOME.CC, download/POLYNOME.H, download/POLYNOME.CC, download/MAIN.CC |
|