interprete HQ9+

Stato
Discussione chiusa ad ulteriori risposte.

imported_lepa

Utente Silver
28 Settembre 2008
266
11
1
99
il mio (in c++)fa un po' schifo...anche perchè non ho capito cosa significava '+' :ipezziassurdi:....
per questo esercizio ho preso spunto dalla discussione di opocaj--> http://www.infernet-x.com/python/hq9-interpreter-t-13311.html


Codice:
#include <iostream>
using namespace std;
 
int main()
{
    int z;
    char x[z];
    cout << ">>";
    gets(x);
    for (int y=0; y<strlen(x); y++) {
        if ((x[y]=='h') || (x[y]=='H')) cout << "Hello World!\n";
        if ((x[y]=='q') || (x[y]=='Q')) cout << x << endl;
        if ((x[y]=='9'){
             for(int t=99; t>=0;){
                  cout << t << "bottles of beer\nyou take ";
                   if (t>0) cout << "one ";
                   else cout << "it ";
                   cout <<"down, pass it around,\n"; 
                   if (t>0)  cout << --t ;
                   else cout << "no more "; 
                   cout << "bottles of beer on the wall.\n\n";}}
        }
    return 0;
}
 
Bellissimo, mi è piaciuto un casino farlo!
PHP:
<?php
error_reporting (E_ERROR | E_USER_ERROR);
$code=($argv[1]) ? $argv[1] : $_GET['code'];
$code=strtoupper($code);
if (!$code) trigger_error ("Code not valid", E_USER_ERROR);
$code=htmlspecialchars($code,ENT_QUOTES);
$valid_codes = array ("H","Q","9","-");
$count = 0;
for ($i=0;$i<strlen($code);$i++) {
	if ($code[$i] == $valid_codes[0]) {
		print "Hello World.\n<br />\n";
	} elseif ($code[$i] == $valid_codes[1]) {
		print $code."\n<br />\n";
	} elseif ($code[$i] == $valid_codes[2]) {
		for ($a=99;$a>0;$a--) {
			$tmp = ($a==1) ? "no more bottles of beer on the wall." : ($a-1) . " bottles of beer on the wall.\n<br />\n";
			print "{$a} bottles of beer\n<br />\n
			you take one down, pass it around,\n<br />\n
			{$tmp}\n<br />\n";
		}
		$a = 99;
	} elseif ($code[$i] == $valid_codes[3]) {
		$count++;
	} else {
		trigger_error ("Code {$code[$i]} not valid", E_USER_ERROR);
	}
}
?>

PS: Invece di + dovete usare - perché il + nell'url viene considerato come uno spazio.
PS2: Attenzione perché al 99 Bottles of Beer quando arriva ad uno invece di fare 0 bottles of beer on the wall! fa no more bottles of beer on the wall., e sembra che tu non l'abbia il controllo, Lepa.
 
Copioni U.U
[Python]
Codice:
#!/usr/bin/python
import re
from sys import argv
struct=re.compile(r"[\w].ml")
code=ARGV[1]
if struct.findall(code):
    global inc
    inc=0
    source=open(code, "r").readline()
    source.split
    for i in range(0, len(source)):
        if source[i]=='H':
            print "Hello World"
        elif source[i]=='Q':
            print ''.join(source)
        elif source[i]=="9":
            for k in xrange(1, 101):
                j=str(100-k)
                if j!="0":
                    print j+" bottles of beer on the wall\n"+j+" bottles of beer!\n(You) take one down, and pass it around\n"+j+" bottles of beer on the wall!"
                else:
                    print "No more bottles of beer on the wall\nNo more bottles of beer!\n(You) take one down, and pass it around\nNo more bottles of beer on the wall!"

        elif source[i]=="+":
            inc+=1
        else:
            print ">> Code Error <<"
            exit
else:
    print ">> Regex Error <<"
 
Ragazzi io (dopo aver studiato una guida qualche giorno fa) ho provato a farlo in perl, ma presenta alcuni errori, funzionano solo i comandi H e 9. Come mai?
(OFF TOPIC: Buona vigilia di Natale a tutti!!!)

Codice:
#!/usr/bin/perl
$codice = @ARGV[0];
$codice = uc($codice);
$contatore = 0;
@cmdinseriti = split //, $codice;
for ($i=0;$i<($#cmdinseriti+1);$i++) {
	if ($cmdinseriti[$i] == "H") {
		print "Hello World.\n";
	}
	elsif ($cmdinseriti[$i] == "Q") {
		print "$codice\n";
	}
	elsif ($cmdinseriti[$i] == "9") {
		for ($m=99;$m>0;$m--) {
			if ($m==1) {
				$temp = "no more bottles of beer on the wall.\n";
			} else {
				$temp = ($m-1) . " bottles of beer on the wall.\n";
			}
			print "$m bottles of beer\n
            you take one down, pass it around,\n
            $temp\n";
		}
	}
	elsif ($cmdinseriti[$i] == "+") {
		$contatore = $contatore + 1;
	}
	else {
		print "Hai inserito un comando non valido!";
	}
}
 
L'ho fatto in Python ^^
Codice:
code=raw_input ("Inserisci il codice HQ9\n\n")
count = 0
for i in code:
	if i == "H":
		print "Hello World.\n"
	elif i == "Q":
		print code
	elif i == "9":
		for a in range (1,100):
			toprint = str(100 - a)
			if toprint == "1":
				phrase = "no more bottles of beer on the wall."
				s = ""
				one = "it"
			else:
				phrase = str(int(toprint)-1) + " bottles of beer on the wall."
				s = "s"
				one = "one"
			
			print  toprint + " bottle"+s+" of beer\nyou take "+one+" down, pass it around,\n"+phrase+"\n\n"
	elif i == "+":
		count += 1

PS: Ho notato anche che quando ci sono ancora bottiglie di birra fa bottles of beer you take one down, mentre quando ce ne una fa bottle of beer you take it down :p
 
Sto testando questo linguaggio figo e potente ( FALCON ):
Codice:
#!/usr/bin/falcon
count = 0
loop
	cmd = input()
	for i in cmd
		if i == 'H'
			> "Hello World."
		elif i == 'Q'
			> cmd
		elif i == '9'
			for j in [99:1]
				forfirst: > @ "$j bottles of beer on the wall\n$j bottles of beer!\ntake one down, and pass it around"
				formiddle: > @ "$j bottles of beer on the wall\n$j bottles of beer!\ntake one down, and pass it around"
				forlast: > @ "$j bottles of beer on the wall\n$j bottles of beer!\nif one of those bottles should happen to fall"
			end
			> "No bottles of beer on the wall!\nNo bottles of beer!\nGo to the store and buy 99 more"
		elif i == '+'
			count=count+1
		else
			> @ "$i not allowed"
		end
	end
end
P.S.: loop equivale ad un while(1) per cui è infinito :D
 
Mio in php:
Codice:
<?php
error_reporting("E_ALL &~ E_WARNING &~ E_NOTICE");
if($argc < 2)
	die("Usage: ".$argv[0]." <code>");
$argv[1] = strtoupper($argv[1]);
for($i=0;$i<strlen($argv[1]);$i++){
	if($argv[1][$i] == "H") {
		print "Hello, world!\n";
	} elseif($argv[1][$i] == "Q") {
		print $argv[1]."\n";
	} elseif($argv[1][$i] == "9") {
		for($j=99;$j>0;$j--)
			print $j." bottels of beer\nyou take one down, pass it around,\n".(($j == 1) ? "no more" : ($j-1))." bottles of beer on the wall\n\n";
	} elseif($argv[1][$i] == "+") {
		$c++;
	} else {
		print "Command not found";
	}
}
?>
non ho ben capito a cosa serve '+', c'è qualcuno che me lo spiega? Grazie
 
Ah no infatti pensavo di aver capito male perchè non serve assolutamente a nulla, allora avevo capito bene.
Grazie per il chiarimento
 
Stato
Discussione chiusa ad ulteriori risposte.