diff --git a/qualdev/compte_rendu_debug.txt b/qualdev/compte_rendu_debug.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/qualdev/qualdev/calcule.ts b/qualdev/qualdev/calcule.ts new file mode 100644 index 0000000000000000000000000000000000000000..ecaa376d001d0d6f814907ea8f3a19ce1560b489 --- /dev/null +++ b/qualdev/qualdev/calcule.ts @@ -0,0 +1,59 @@ +class MathError extends Error { + constructor(message: string) { + super(message); + this.name = "MathError"; + } +} + +class OpérateurError extends Error { + constructor(message: string) { + super(message); + this.name = "OpérateurError"; + } +} + +function calcule(tableau: Array<number>,indice: number,operateur: string,operande: number): number { + if (indice < 0 || indice >= tableau.length) { + throw new RangeError("Indice invalide"); + } + + switch (operateur) { + case "+": + return tableau[indice] + operande; + case "-": + return tableau[indice] - operande; + case "*": + return tableau[indice] * operande; + case "/": + if (operande === 0) { + throw new MathError("Division par zéro"); + } + return tableau[indice] / operande; + default: + throw new OpérateurError("Opérateur inconnu"); + } +} + +try { + console.log(calcule([1,2,3,4],1,"/ ",4)); + console.log(calcule([1,2,3,4],2,'+',3)); +} +catch (e) { + console.log(e); +} + +try { + console.log(calcule([1, 2, 3], 8, "-", 5)); +} +catch (e) { + console.log(e); +} + +try { + console.log(calcule([1, 2, 3], 2, "addition", 10)); +} +catch (e) { + console.log(e); +} + +console.log(calcule([1,2,3,4],2,'+',3)) \ No newline at end of file diff --git a/qualdev/qualdev/debug1.ts b/qualdev/qualdev/debug1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a49cd8bef29c70323963d306c2a84a4eb4a41d94 --- /dev/null +++ b/qualdev/qualdev/debug1.ts @@ -0,0 +1,8 @@ +function deb1():number{ + let j = 200; + for (let i = 0; i < 10; i++){ + j -=10; + } + return j; +} +console.log(deb1()); \ No newline at end of file diff --git a/qualdev/qualdev/etudiant.ts b/qualdev/qualdev/etudiant.ts new file mode 100644 index 0000000000000000000000000000000000000000..e089259673eaac8e74ef025832562f9a21452e8e --- /dev/null +++ b/qualdev/qualdev/etudiant.ts @@ -0,0 +1,153 @@ + +class INEError extends Error{ + constructor(message: string) { + super(message); + this.name = "INEError"; + } +} + +function isLetter(c: string): boolean { + return (c >= "a" && c <= "z") || (c >= "A" && c <= "Z"); +} + +function isDigit(c: string): boolean { + return c >= "0" && c <= "9"; +} + +class Etudiant{ + private ine : string; + private nom : string; + private prenom : string; + + constructor(nom : string, prenom : string){ + this.ine = ""; + this.nom = nom; + this.prenom = prenom; + } + + getINE():string{ + return this.ine; + } + + getPrenom():string{ + return this.prenom; + } + + getNom():string{ + return this.nom; + } + + setINE(s : string) : void{ + if (s.length !== 11){ + throw new INEError("Format incorrect."); + } + for (let i = 0;i<9; i++){ + if (isLetter(s[i])){ + throw new INEError("Format incorrect"); + } + } + if (!(isLetter(s[9]))){ + if(!(isLetter(s[10]))){ + throw new INEError("Format incorrect."); + } + this.ine = s; + } + + else if (isLetter(s[9])){ + if(!(isLetter(s[10]))){ + throw new INEError("Format incorrect."); + } + this.ine = s; + } + else{ + throw new INEError("Format incorrect."); + } + } + + +} + +let e2 : Etudiant; +let probleme : boolean; +do{ + probleme = false; + try{ + let ine = String(prompt("Donner l'INE de l'étudiant : ")); + e2 = new Etudiant('Darwin', 'Charles') + e2.setINE(ine) + } + catch (e){ + console.log(e); + probleme = true + } +} +while(probleme); + + + +class DateInvalideError extends Error { + constructor(message = "La date n'est pas au bon format") { + super(message); + this.name = "DateInvalideError"; + } +} + +class DateUSError extends Error { + constructor(message = "La date est au format US") { + super(message); + this.name = "DateUSError"; + } +} + + +function extraitDate(date: string): Array<number> { + let us = false; + let tab = date.split("/"); + + if (tab.length !== 3) { + tab = date.split("-"); + if (tab.length === 3) { + us = true; + } else { + throw new DateInvalideError("La date doit être au format jj/mm/aaaa"); + } + } + + const iTab = new Array<number>(); + for (const s of tab) { + for (const car of s) { + if (!isDigit(car)) { + throw new DateInvalideError("La date doit être au format jj/mm/aaaa"); + } + } + iTab.push(Number.parseInt(s)); + } + + let iJour = 0; + let iMois = 1; + if (us) { + iJour = 1; + iMois = 0; + } + if (iTab[iJour] < 1 || iTab[iJour] > 31) { + throw new DateInvalideError("les jours sont entre 1 et 31"); + } + if (iTab[iMois] < 1 || iTab[iMois] > 12) { + throw new DateInvalideError("les mois sont entre 1 et 12"); + } + + if (us) { + throw new DateUSError(); + } + return iTab; + } + + function testeDate(date: string): void { + console.log("Date testée : " + date); + + try { + console.log(extraitDate(date)); + } catch (e) { + console.log(e); + } + } diff --git a/qualdev/qualdev/exception.ts b/qualdev/qualdev/exception.ts new file mode 100644 index 0000000000000000000000000000000000000000..0164f5ebad90700b175276e033f74d3f6780eaca --- /dev/null +++ b/qualdev/qualdev/exception.ts @@ -0,0 +1,52 @@ +class NomEtPrenomVideError extends Error { + constructor(message: string) { + super(message); + this.name = "NomEtPrenomVideError"; + } +} + +class NomVideError extends Error { + constructor(message: string) { + super(message); + this.name = "NomVideError"; + } +} +class PrenomVideError extends Error { + constructor(message: string) { + super(message); + this.name = "PrenomVideError"; + } +} + +function creeInitiales(nom: string, prenom: string): string { + if ((nom === "") && (prenom === "")){ + throw new NomEtPrenomVideError("Le nom et le prénom ne doivent pas etre vide."); + } + else if (nom === ""){ + throw new NomVideError("Le nom ne doit pas etre vide."); + } + else if (prenom === ""){ + throw new PrenomVideError("Le prenom ne doit pas etre vide."); + } + else{ + return nom[0].toUpperCase() + prenom[0].toUpperCase(); + } +} + + + +try{ + creeInitiales("","clement"); +} +catch (e){ + console.log(e) +} +finally{ + console.log("Fin du programme") +} + +// Question 2 : TypeError : Cannot read properties of undefined (reading 'toUpperCase') + +let nom = 'bailly' +let prenom = 'clément' +console.log(creeInitiales(nom,prenom)) \ No newline at end of file diff --git a/qualdev/qualdev/test.ts b/qualdev/qualdev/test.ts new file mode 100644 index 0000000000000000000000000000000000000000..a65ab5742c62c2782e02118b93b160bf4fdc6b12 --- /dev/null +++ b/qualdev/qualdev/test.ts @@ -0,0 +1,2 @@ +console.log("1"); +console.log("2"); \ No newline at end of file