Last active
February 11, 2017 14:53
-
-
Save Alynva/5ad692d11df8cdacd2c1b10f85473f72 to your computer and use it in GitHub Desktop.
Programa em CGI para converter unidades de medida
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
char* dados = NULL; | |
int input1 = 0, convert = 0, i; | |
double result1 = 0.0; | |
char erro[50] = {'\n'}; | |
char select[7][70] = {'\n'}; | |
for (i = 0; i < 7; i++) snprintf(select[i], sizeof select[i], ""); | |
dados = getenv("QUERY_STRING"); | |
if (dados == NULL) { | |
snprintf(select[0], sizeof select[0], "<option disabled selected value>Selecione uma conversão</option>"); | |
snprintf(erro, sizeof erro, "Erro durante a extra��o de dados do formul�rio."); | |
} else if (sscanf(dados, "convert=%d&input1=%d", &convert, &input1) != 2) { | |
snprintf(select[0], sizeof select[0], "<option disabled selected value>Selecione uma conversão</option>"); | |
snprintf(erro, sizeof erro, "Entrada inválida!"); | |
} else { | |
if (convert == 11) { // Metros > Milhas | |
result1 = input1 * 0.000621371; | |
snprintf(select[1], sizeof select[0], "selected"); | |
} else if (convert == 12) { // Milhas > Metros | |
result1 = input1 / 0.000621371; | |
snprintf(select[2], sizeof select[1], "selected"); | |
} else if (convert == 21) { // Litros > Gal�es | |
result1 = input1 * 0.264172; | |
snprintf(select[3], sizeof select[2], "selected"); | |
} else if (convert == 22) { // Gal�es > Litros | |
result1 = input1 / 0.264172; | |
snprintf(select[4], sizeof select[3], "selected"); | |
} else if (convert == 31) { // Quilogramas > Libras | |
result1 = input1 * 2.20462; | |
snprintf(select[5], sizeof select[4], "selected"); | |
} else if (convert == 32) { // Libras > Quilogramas | |
result1 = input1 / 2.20462; | |
snprintf(select[6], sizeof select[5], "selected"); | |
} | |
} | |
printf("%s%c%c\n","Content-Type:text/html;charset=UTF-8",13,10); | |
printf("<html>"); | |
printf("<head>"); | |
printf("<meta charset=\"utf-8\">"); | |
printf("<title>Mini avaliação - Converções de medidas</title>"); | |
printf("<link rel=\"stylesheet\" href=\"../converMedidas.css\">"); | |
printf("</head>"); | |
printf("<body>"); | |
printf("<div>"); | |
printf("<form action=\"main.cgi\" method=\"get\">"); | |
printf("<select name=\"convert\">"); | |
printf("%s", select[0]); | |
printf("<optgroup label=\"Converter distância\">"); | |
printf("<option value=\"11\" %s>Metros → Milhas</option>", select[1]); | |
printf("<option value=\"12\" %s>Milhas → Metros</option>", select[2]); | |
printf("</optgroup>"); | |
printf("<optgroup label=\"Converter volume\">"); | |
printf("<option value=\"21\" %s>Litros → Galões</option>", select[3]); | |
printf("<option value=\"22\" %s>Galões → Litros</option>", select[4]); | |
printf("</optgroup>"); | |
printf("<optgroup label=\"Converter massa\">"); | |
printf("<option value=\"31\" %s>Quilogramas → Libras</option>", select[5]); | |
printf("<option value=\"32\" %s>Libras → Quilogramas</option>", select[6]); | |
printf("</optgroup>"); | |
printf("</select>"); | |
printf("<br>"); | |
printf("<br>"); | |
printf("<div><input type=\"number\" name=\"input1\" value=\"%d\"> → <input type=\"text\" value=\"%.1lf\" disabled></div>", input1, result1); | |
printf("<br>"); | |
printf("<input type=\"submit\" value=\"Calcular\">"); | |
printf("</form>"); | |
printf("<span>%s</span>", erro); | |
printf("</div>"); | |
printf("</body>"); | |
printf("</html>"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* { | |
font-family: monospace; | |
font-size: 1.1em; | |
text-align: center; | |
} | |
body { | |
background-color: rgb(40,80,150); | |
color: white; | |
margin: 0; | |
} | |
body > div { | |
position: absolute; | |
top: 0; | |
right: 0; | |
bottom: 0; | |
left: 0; | |
margin: auto; | |
/*width: 1200px;*/ | |
width: 300px; | |
height: 138px; | |
} | |
div > div { | |
width: 470px; | |
display: inline-block; | |
} | |
input, select { | |
border: 0; | |
background-color: white; | |
color: black; | |
outline: none; | |
transition: color .1s, background-color .4s; | |
border-bottom: 3px solid; | |
} | |
opgroup { | |
font-size: .9em; | |
color: grey; | |
} | |
input[type='number'] { | |
border-bottom-color: red; | |
max-width: 80px; | |
} | |
input[type='text'] { | |
border-bottom-color: yellow; | |
max-width: 100px; | |
} | |
input[type='submit'] { | |
border-bottom-color: green; | |
cursor: pointer; | |
max-width: none; | |
} | |
input:focus, input:hover, form > div:hover *, select:hover > #optgroup-label { | |
color: white; | |
background-color: transparent; | |
} | |
select:hover * { | |
color: inherit; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Mini avaliação - Converções de medidas</title> | |
<link rel="stylesheet" href="converMedidas.css"> | |
</head> | |
<body> | |
<div> | |
<form action="cgi-bin/converMedidas.cgi" method="get"> | |
<select name="convert" required> | |
<option disabled selected value>Selecione uma conversão</option> | |
<optgroup label="Converter distância"> | |
<option value="11">Metros → Milhas</option> | |
<option value="12">Milhas → Metros</option> | |
</optgroup> | |
<optgroup label="Converter volume"> | |
<option value="21">Litros → Galões</option> | |
<option value="22">Galões → Litros</option> | |
</optgroup> | |
<optgroup label="Converter massa"> | |
<option value="31">Quilogramas → Libras</option> | |
<option value="32">Libras → Quilogramas</option> | |
</optgroup> | |
</select> | |
<br> | |
<br> | |
<div><input type="number" name="input1" value="0"> → <input type="text" value="?" disabled></div> | |
<br> | |
<input type="submit" value="Calcular"> | |
</form> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment