Advertisement
xlujiax

gg

May 4th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <limits.h>
  4. #include "funcionesAuxiliares.h"
  5. #include "ordenacion.h"
  6. #define EOF -1
  7. #define MAX_CARACTERES 30
  8. #define MAXDATOS 500
  9.  
  10. using namespace std;
  11.  
  12. int main(int argc, char** argv) {
  13. char a,b,c, tipoClima, tipoT, tipoH, tipoP;
  14. int codigo, cantCarac, dd, mm, aa;
  15. double temp, humedad, precip, vol;
  16. int FECHAS[MAXDATOS], cantDatos, indices[MAXDATOS];
  17. double CELSIUS[MAXDATOS], FAHRENHEIT[MAXDATOS], PRECIPITACIONES[MAXDATOS], HUMEDAD[MAXDATOS], VOLUMEN[MAXDATOS];
  18.  
  19. //La primera linea indica como debe estar cordenado el reporte
  20. //Contiene uno o dos caracteres
  21. cin >> a;
  22. b = cin.peek();
  23. if(c != '\n')
  24. cin >> b;
  25.  
  26. impEncabePrincipal(a);
  27. //El archivo
  28. while(1){
  29. impC('=',118);
  30. cout << endl;
  31. //Leo e nombre de la cuidad y el codigo
  32. cantCarac = 0;
  33. while(!(cin >> codigo)){
  34. cin.clear();
  35. while((c = cin.get()) != ' '){
  36. cantCarac++;
  37. cout.put(c);
  38. }
  39. cantCarac++;
  40. cout.put(c);
  41. }
  42. impC(' ',MAX_CARACTERES - cantCarac);
  43. cout << codigo;
  44. cantDatos = 0;
  45.  
  46. while(1){
  47.  
  48. for(int i=0; i<3; i++){
  49. cin >> tipoClima;
  50. if(tipoClima == 'T'){
  51. if(cin >> temp){
  52. tipoT = cin.get();
  53. if(tipoT == 'C'){
  54. CELSIUS[cantDatos] = temp;
  55. FAHRENHEIT[cantDatos] = temp*9/5 + 32;
  56. }
  57. else{
  58. FAHRENHEIT[cantDatos] = temp;
  59. CELSIUS[cantDatos] = (temp - 32)*5/9;
  60. }
  61. }
  62. else{
  63. cin.clear();
  64. CELSIUS[cantDatos] = INT_MIN;
  65. FAHRENHEIT[cantDatos] = INT_MIN;
  66. tipoT = cin.get();
  67. }
  68. }
  69. else if(tipoClima == 'H'){
  70. if(cin >> humedad){
  71. tipoH = cin.get();
  72. if(tipoH != '%')
  73. HUMEDAD[cantDatos] = humedad*100;
  74. else
  75. HUMEDAD[cantDatos] = humedad;
  76. }
  77. else{
  78. HUMEDAD[cantDatos] = INT_MIN;
  79. cin.clear();
  80. tipoH = cin.get();
  81. }
  82. }
  83. else if(tipoClima == 'P'){
  84. if(cin >> precip){
  85. tipoP = cin.get();
  86. if(tipoP == 'H')
  87. precip *= 60;
  88. else if(tipoP == 'S')
  89. precip /= 60;
  90. cin >> vol;
  91. PRECIPITACIONES[cantDatos] = vol/precip;
  92. }
  93. else{
  94. cin.clear();
  95. PRECIPITACIONES[cantDatos] = INT_MIN;
  96. tipoP = cin.get();
  97. }
  98. }
  99. }//fin del for
  100. //Leo la fecha
  101. dd = 0;
  102. mm = 0;
  103. aa = 0;
  104. cin >> aa;
  105. c=cin.peek();
  106. if(c == '/'){
  107. cin.get();
  108. mm = aa;
  109. cin >> aa;
  110. c = cin.peek();
  111. if(c == '/'){
  112. cin.get();
  113. dd = mm;
  114. mm = aa;
  115. cin >> aa;
  116. }
  117. }
  118. int fecha = aa*10000 + mm*100 + dd;
  119. FECHAS[cantDatos] = fecha;
  120.  
  121. if(cin.get() == '\n') break;
  122. cantDatos++;
  123.  
  124. }//fin del while(1)
  125.  
  126. //Termino de leer todos los datos por cuidad
  127. //Toca ordenar
  128. inicializacionIndices(indices, cantDatos);
  129. if(a == 'T'){
  130. if(b == 'C')
  131. ordenar(CELSIUS, indices, cantDatos);
  132. else
  133. ordenar(FAHRENHEIT, indices, cantDatos);
  134. }
  135. else if(a == 'F')
  136. ordenar(FECHAS, indices, cantDatos);
  137. else if(a == 'H')
  138. ordenar(HUMEDAD, indices, cantDatos);
  139. else
  140. ordenar(PRECIPITACIONES, indices, cantDatos);
  141.  
  142. impresion(FECHAS, CELSIUS, FAHRENHEIT, HUMEDAD, PRECIPITACIONES, cantDatos, indices);
  143. impC('-',118);
  144. cout << endl << setw(40) << "Promedio: " << setw(25) << " ";
  145. cout.precision(2);
  146. cout << fixed << setw(5) << promedio(CELSIUS, cantDatos);
  147. cout << fixed << setw(12) << promedio(FAHRENHEIT, cantDatos);
  148. cout << fixed << setw(12) << promedio(HUMEDAD, cantDatos);
  149. cout << fixed << setw(12) << promedio(PRECIPITACIONES, cantDatos);
  150. cout << endl;
  151. c = cin.peek();
  152. if(c == EOF) break;
  153. }
  154.  
  155. return 0;
  156. }
  157.  
  158. #ifndef FUNCIONESAUXILIARES_H //FuncionAuxiliar.h
  159. #define FUNCIONESAUXILIARES_H
  160.  
  161. void impEncabePrincipal(char a);
  162. void impC(char , int);
  163. void impresion(int [], double [], double [], double [], double [], int , int []);
  164. double promedio(double [], int );
  165.  
  166. #endif /* FUNCIONESAUXILIARES_H */
  167.  
  168. #include <iomanip> //FuncionAuxiliar.cpp
  169. #include <iostream>
  170. #include <limits.h>
  171. #include "funcionesAuxiliares.h"
  172.  
  173. using namespace std;
  174.  
  175. void impEncabePrincipal(char a){
  176. cout << setw(65) << "REGISTRO CLIMATICO DEL PAIS" << endl;
  177. cout << setw(55) << "ORDENADO POR ";
  178. if( a == 'F') cout << "FECHA" << endl;
  179. else if( a == 'T') cout << "TEMPERATURA" << endl;
  180. else if( a == 'H') cout << "HUMEDAD" << endl;
  181. else if( a == 'P') cout << "PRECIPITACIONES" << endl;
  182. impC('=',118);
  183. cout << endl;
  184. cout << setw(12) << "CUIDAD";
  185. cout << setw(24) << "CODIGO";
  186. cout << setw(20) << "FECHA";
  187. cout << setw(23) << "TEMPERATURA";
  188. cout << setw(16) << "HUMEDAD";
  189. cout << setw(15) << "PRECIPITACION";
  190. cout << endl;
  191.  
  192. cout << setw(80) << "C F";
  193. cout << setw(12) << "%";
  194. cout << setw(23) << "CANTIDAD/TIEMPO(min)";
  195. cout << endl;
  196. }
  197.  
  198. void impC(char c, int n){
  199. for(int i=0; i<n; i++)
  200. cout.put(c);
  201. }
  202.  
  203. void imprimirFecha(int fecha){
  204. int aa, mm, dd;
  205. aa = fecha/10000;
  206. mm = (fecha - aa*10000)/100;
  207. dd = fecha - (aa*10000 + mm*100);
  208. cout.fill('0');
  209.  
  210. if(dd == 0)
  211. cout << "--/";
  212. else
  213. cout << setw(2) << dd << "/";
  214.  
  215. if(mm == 0)
  216. cout << "--/";
  217. else
  218. cout << setw(2) << mm << "/";
  219.  
  220. cout << setw(4) << aa;
  221. cout.fill(' ');
  222. }
  223.  
  224. void imprimirDouble(double a){
  225. cout.precision(2);
  226. if(a == INT_MIN)
  227. cout << setw(12) << "--.--";
  228. else
  229. cout << fixed << setw(12) << a;
  230. }
  231.  
  232. void impresion(int FECHAS[], double CELSIUS[], double FAHRENHEIT[], double HUMEDAD[], double PRECIPITACIONES[], int cantDatos, int indices[]){
  233. int dd, mm, aa;
  234.  
  235. cout << setw(14) << " ";
  236. imprimirFecha(FECHAS[indices[0]]);
  237. imprimirDouble(CELSIUS[indices[0]]);
  238. imprimirDouble(FAHRENHEIT[indices[0]]);
  239. imprimirDouble(HUMEDAD[indices[0]]);
  240. imprimirDouble(PRECIPITACIONES[indices[0]]);
  241. cout << endl;
  242.  
  243. for(int i=1; i<cantDatos; i++){
  244. //Imprimo la fecha
  245. cout << setw(48) << " ";
  246. imprimirFecha(FECHAS[indices[i]]);
  247. imprimirDouble(CELSIUS[indices[i]]);
  248. imprimirDouble(FAHRENHEIT[indices[i]]);
  249. imprimirDouble(HUMEDAD[indices[i]]);
  250. imprimirDouble(PRECIPITACIONES[indices[i]]);
  251. cout << endl;
  252. }
  253. }
  254.  
  255. double promedio(double X[], int n){
  256. double suma = 0.0;
  257. for(int i=0; i<n ; i++)
  258. if(X[i] != INT_MIN)
  259. suma += X[i];
  260. return suma/n;
  261. }
  262.  
  263. #ifndef ORDENACION_H //Ordenacion.H
  264. #define ORDENACION_H
  265.  
  266. void inicializacionIndices(int [], int );
  267.  
  268. template <typename T>
  269. void ordenar(T [], int [], int);
  270.  
  271. template <typename T>
  272. void quickSort(T [], int [], int, int);
  273.  
  274. void swap(int [], int , int);
  275.  
  276. #endif /* ORDENACION_H */
  277.  
  278. #include "ordenacion.h" //Ordenacion.cpp
  279.  
  280. void inicializacionIndices(int X[], int n){
  281. for(int i=0; i<n; i++)
  282. X[i] = i;
  283. }
  284.  
  285. template <typename T>
  286. void ordenar(T X[], int ind[], int n){
  287. quickSort(X, ind, 0, n-1);
  288. }
  289. template void ordenar(int [], int [], int );
  290. template void ordenar(double [], int [], int );
  291.  
  292. template <typename T>
  293. void quickSort(T X[], int ind[], int izq, int der){
  294. int limite,i;
  295.  
  296. if(izq>=der) return; //Condición de salida
  297.  
  298. swap(ind,izq,(izq+der)/2); //Con el pivote se intercambia
  299.  
  300. limite = izq;
  301.  
  302. for(i=izq+1;i<=der;i++)
  303. if(X[ind[izq]] > X[ind[i]])
  304. swap(ind,++limite,i); //Condición de intercambio
  305.  
  306. swap(ind,izq,limite);
  307. quickSort(X, ind, izq, limite-1);
  308. quickSort(X, ind, limite+1, der);
  309. }
  310. template void quickSort(int [], int [], int , int );
  311. template void quickSort(double [], int [], int , int );
  312.  
  313. void swap(int X[], int i, int j){
  314. int temp = X[i];
  315. X[i] = X[j];
  316. X[j] = temp;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement