/^-?[0-9]*(\.)?[0-9]*$/ { stack_in($0); next; } /help/ { display_help(); next; } /quit/ { exit; } /print/ { stack_print(); next; } /list/ { stack_list(); next; } /dup/ { stack_dup(); next; } /change/ { stack_swap(); next; } /pop/ { stack_pop(); next; } /clear/ { stack_clear(); next; } /int/ { stack_int(); next; } /+/ { stack_add(); next; } /\-/ { stack_sub(); next; } /*/ { stack_mul(); next; } /\// { stack_div(); next; } /%/ { stack_mod(); next; } { printf "commande <%s> inconnue\n", $0;} BEGIN \ { \ printf "Calculatrice a pile en awk\n"; \ printf "Christophe Boyanique\n"; \ printf "Emmanuel Pinard\n"; \ printf "\n"; \ stack_clear(); \ } END \ { \ printf "au revoir...\n"; \ } function display_help() { printf "Nombre entiers ou reels\n"; printf "Operations: + - / * %\n"; printf "Commandes: print dup change pop int help list clear quit\n"; } function stack_in(n) { taille_pile += 1; pile[taille_pile] = n; } function stack_out() { taille_pile -= 1; return pile[taille_pile+1]; } function stack_get(n) { return pile[n]; } function stack_len() { return taille_pile; } function stack_list() { if (stack_len() > 0) { for (i=stack_len(); i>0; i--) printf " %20.8f\n", stack_get(i); } else printf "Pile vide!\n"; } function stack_print() { if (stack_len() > 0) printf " %20.8f\n", stack_get(stack_len()); else printf "Pile vide!\n"; } function stack_dup() { if (stack_len() > 0) stack_in(stack_get(stack_len())); else printf "Pile vide!\n"; } function stack_swap() { if (stack_len() > 1) { n1 = stack_out(); n2 = stack_out(); stack_in(n1); stack_in(n2); } else printf "Pas assez d'elements dans la pile!\n"; } function stack_pop() { if (stack_len() > 0) { printf " %20.8f\n", stack_out(); } else printf "Pile vide!\n"; } function stack_clear() { taille_pile = 0; } function stack_int() { if (stack_len() > 0) stack_in(int(stack_out())); else printf "Pile vide!\n"; } function stack_add() { if (stack_len() > 1) { n2 = stack_out(); n1 = stack_out(); stack_in(n1+n2); } else printf "Pas assez d'elements dans la pile!\n"; } function stack_sub() { if (stack_len() > 1) { n2 = stack_out(); n1 = stack_out(); stack_in(n1-n2); } else printf "Pas assez d'elements dans la pile!\n"; } function stack_mul() { if (stack_len() > 1) { n2 = stack_out(); n1 = stack_out(); stack_in(n1*n2); } else printf "Pas assez d'elements dans la pile!\n"; } function stack_div() { if (stack_len() > 1) { if (stack_get(stack_len()) != 0) { n2 = stack_out(); n1 = stack_out(); stack_in(n1/n2); } else printf "Division par 0!\n"; } else printf "Pas assez d'elements dans la pile!\n"; } function stack_mod() { if (stack_len() > 1) { if (stack_get(stack_len()) != 0) { if (stack_isint(stack_len()) && stack_isint(stack_len()-1)) { n2 = stack_out(); n1 = stack_out(); stack_in(n1%n2); } else printf "Modulo non entier!\n"; } else printf "Division par 0!\n"; } else printf "Pas assez d'elements dans la pile!\n"; } function stack_isint(n) { if (stack_get(n) == int(stack_get(n))) return 1; return 0; }