Keine Beschreibung

main.cpp 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cstdlib>
  7. #include <sstream>
  8. using namespace std;
  9. // //
  10. // SOLUTION EXERCISE 2//
  11. // //
  12. // Function to obtain the first digit of a an integer //
  13. int firstDigit(int passenger){
  14. int quotient;
  15. quotient=passenger/10;
  16. while(quotient > 9){
  17. quotient=quotient/10;
  18. }
  19. return quotient;
  20. }
  21. int main(int argc, char *argv[])
  22. {
  23. QApplication a(argc, argv);
  24. MainWindow w;
  25. w.setWindowTitle("Benford\'s Law");
  26. w.show();
  27. // Here is a simple example to help you understand how to
  28. // invoke the histo method.
  29. // Aqui esta un ejemplo simple para ayudar a entender como
  30. // invocar la funcion histo.
  31. // This is an array of strings
  32. // Esto es un arreglo de cadenas de caracteres
  33. // string histoNames[4] = {"Rosa", "Pepin", "Lobo", "Mota"};
  34. // This is an array of corresponding values
  35. // Esto es un arreglo de valores correspondientes
  36. // double histoValues[4] = {.21, .26, .05, .48};
  37. // We pass the array of strings, the array of values, the size of the
  38. // arrays, and the names of the x and y axis.
  39. // Pasamos el arreglo de cadenas de caracteres, el arreglo de valores,
  40. // tamano de los arreglos y los nombres de los axis x y y.
  41. // w.histo(histoNames, histoValues, 4, "personaje", "frecuencia");
  42. // //
  43. // SOULTION EXERCISE 2 (CONT) //
  44. // //
  45. string bus;
  46. int numPassenger;
  47. double histoValues[10]={0,0,0,0,0,0,0,0,0,0};
  48. string histoNames[9] = {"1","2","3","4","5","6","7","8","9"};
  49. // read data from file; NOTE path has to be modified to your own path //
  50. ifstream inputFile;
  51. inputFile.open("/Users/Ivelisse/Dropbox/eip/EIP-REVISION-VERANO/Arrays/BenfordsLaw/benfordslaw-WorkingFolder-WithSolution-July-2015/data/cta-a.txt");
  52. while (inputFile >> bus){
  53. inputFile >> numPassenger;
  54. histoValues[firstDigit(numPassenger)-1]= histoValues[firstDigit(numPassenger)-1]+1;
  55. }
  56. inputFile.close();
  57. // compute frequency of occurrence //
  58. int sum=0;
  59. for(int i=0; i<9; ++i)
  60. sum=sum+histoValues[i];
  61. for(int i=0; i<9; ++i)
  62. histoValues[i]=histoValues[i]/sum;
  63. // display histogram //
  64. w.histo(histoNames, histoValues, 9, "Digit", "Frequency");
  65. return a.exec();
  66. }