Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string>
- using namespace std;
- void get_params (string qry, int &par1, int &par2, string &par3)
- {
- string oper1; // Operando #1 (ASCII).
- string oper2; // Operando #2 (ASCII).
- string option; // Operador.
- int pos1; // Estas dos variables se emplean para ir seleccionando los
- int pos2; // substrings de datos dentro del QUERY_STRING.
- // Sacamos substring. Operando #1:
- pos1 = qry.find("=", 0);
- pos2 = qry.find("&", pos1);
- pos1++;
- oper1 = qry.substr(pos1, pos2 - pos1);
- // Para pasar de ASCII a integer hay que tratar el substring como un c_str.
- par1 = atoi(oper1.c_str());
- // Sacamos otro substring. Operando #2:
- pos1 = qry.find("=", pos2);
- pos2 = qry.find("&", pos1);
- pos1++;
- oper2 = qry.substr(pos1, pos2 - pos1);
- par2 = atoi(oper2.c_str());
- // Sacamos otro substring. Operador:
- pos1 = qry.find("=", pos2);
- par3 = qry.substr(pos1 + 1);
- }
- int main ()
- {
- int longitud;
- string method = "GET"; // String para identificar el método empleado por el navegador.
- string query; // String que contiene la información del formulario.
- int pos1; // Estas dos variables se emplean para ir seleccionando los
- int pos2; // substrings de datos dentro del QUERY_STRING.
- string option; // Operador.
- int op1; // Operando #1 (integer).
- int op2; // Operando #2 (integer).
- int res; // Resultado de la operación.
- float div; // Resultado de división con decimales.
- if ((getenv("REQUEST_METHOD")) == method)
- {
- // Método GET.
- query = getenv("QUERY_STRING");
- get_params(query, op1, op2, option);
- }
- else
- {
- // Método POST.
- longitud = atoi(getenv("CONTENT_LENGTH"));
- cin >> query;
- get_params(query, op1, op2, option);
- }
- cout << "Content-Type: text/html" << endl << endl;
- cout << "<html><head>" << endl;
- cout << "<title>Resultado de la operación</title>" << endl;
- cout << "</head><body>" << endl;
- cout << "<h1>Resultado:</h1>" << "<br /><br />" << endl;
- if (option == "sum")
- {
- res = op1 + op2;
- cout << op1 << " + " << op2 << " = <strong>" << res << "</strong>" << endl;
- }
- else if (option == "res")
- {
- res = op1 - op2;
- cout << op1 << " - " << op2 << " = <strong>" << res << "</strong>" << endl;
- }
- else if (option == "mul")
- {
- res = op1*op2;
- cout << op1 << " * " << op2 << " = <strong>" << res << "</strong>" << endl;
- }
- else if (option == "div")
- {
- div = (float)(op1/op2);
- cout.setf(ios::showpoint);
- cout.precision(5);
- cout << op1 << " / " << op2 << " = <strong>" << div << "</strong>" << endl;
- res = op1%op2;
- cout << "El resto de la división es: " << "<strong>" << res << "</strong>" << endl;
- }
- cout << "</body></html>" << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment