Guest User

Untitled

a guest
May 25th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void get_params (string qry, int &par1, int &par2, string &par3)
  8. {
  9. string oper1; // Operando #1 (ASCII).
  10. string oper2; // Operando #2 (ASCII).
  11. string option; // Operador.
  12. int pos1; // Estas dos variables se emplean para ir seleccionando los
  13. int pos2; // substrings de datos dentro del QUERY_STRING.
  14.  
  15. // Sacamos substring. Operando #1:
  16. pos1 = qry.find("=", 0);
  17. pos2 = qry.find("&", pos1);
  18. pos1++;
  19. oper1 = qry.substr(pos1, pos2 - pos1);
  20. // Para pasar de ASCII a integer hay que tratar el substring como un c_str.
  21. par1 = atoi(oper1.c_str());
  22. // Sacamos otro substring. Operando #2:
  23. pos1 = qry.find("=", pos2);
  24. pos2 = qry.find("&", pos1);
  25. pos1++;
  26. oper2 = qry.substr(pos1, pos2 - pos1);
  27. par2 = atoi(oper2.c_str());
  28. // Sacamos otro substring. Operador:
  29. pos1 = qry.find("=", pos2);
  30. par3 = qry.substr(pos1 + 1);
  31. }
  32.  
  33. int main ()
  34. {
  35. int longitud;
  36. string method = "GET"; // String para identificar el método empleado por el navegador.
  37. string query; // String que contiene la información del formulario.
  38. int pos1; // Estas dos variables se emplean para ir seleccionando los
  39. int pos2; // substrings de datos dentro del QUERY_STRING.
  40. string option; // Operador.
  41. int op1; // Operando #1 (integer).
  42. int op2; // Operando #2 (integer).
  43. int res; // Resultado de la operación.
  44. float div; // Resultado de división con decimales.
  45.  
  46.  
  47. if ((getenv("REQUEST_METHOD")) == method)
  48. {
  49. // Método GET.
  50. query = getenv("QUERY_STRING");
  51. get_params(query, op1, op2, option);
  52. }
  53. else
  54. {
  55. // Método POST.
  56. longitud = atoi(getenv("CONTENT_LENGTH"));
  57. cin >> query;
  58. get_params(query, op1, op2, option);
  59.  
  60. }
  61.  
  62.  
  63. cout << "Content-Type: text/html" << endl << endl;
  64. cout << "<html><head>" << endl;
  65. cout << "<title>Resultado de la operaci&oacute;n</title>" << endl;
  66. cout << "</head><body>" << endl;
  67. cout << "<h1>Resultado:</h1>" << "<br /><br />" << endl;
  68.  
  69. if (option == "sum")
  70. {
  71. res = op1 + op2;
  72. cout << op1 << " + " << op2 << " = <strong>" << res << "</strong>" << endl;
  73.  
  74. }
  75. else if (option == "res")
  76. {
  77. res = op1 - op2;
  78. cout << op1 << " - " << op2 << " = <strong>" << res << "</strong>" << endl;
  79.  
  80. }
  81. else if (option == "mul")
  82. {
  83. res = op1*op2;
  84. cout << op1 << " * " << op2 << " = <strong>" << res << "</strong>" << endl;
  85.  
  86. }
  87. else if (option == "div")
  88. {
  89. div = (float)(op1/op2);
  90. cout.setf(ios::showpoint);
  91. cout.precision(5);
  92. cout << op1 << " / " << op2 << " = <strong>" << div << "</strong>" << endl;
  93. res = op1%op2;
  94. cout << "El resto de la divisi&oacute;n es: " << "<strong>" << res << "</strong>" << endl;
  95. }
  96.  
  97. cout << "</body></html>" << endl;
  98.  
  99. return 0;
  100. }
Add Comment
Please, Sign In to add comment