Advertisement
ninto1

The whole code

Jan 22nd, 2021
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. //Code by Ninto
  7.  
  8. int main()
  9. {
  10. int duration = 10; //Introducing variables
  11. int hr, min, sec, r, togo = 0;
  12. int hours, minutes, seconds, day, month, year;
  13. int cycles = 0;
  14. int plus = 0;
  15. int minus = 0;
  16. int plusminus = 0;
  17. char fileSpec[255];
  18.  
  19. //TIME for file name
  20. time_t now;
  21. time(&now);
  22. printf("Today is : %s", ctime(&now));
  23. struct tm *local = localtime(&now);
  24.  
  25. hours = local->tm_hour;
  26. minutes = local->tm_min;
  27. seconds = local->tm_sec;
  28.  
  29. day = local->tm_mday;
  30. month = local->tm_mon + 1;
  31. year = local->tm_year + 1900;
  32.  
  33. //Time block end
  34.  
  35. //create file
  36. FILE *fPointer;
  37. snprintf(fileSpec, "%04d_%02d_%02d_%02d:%02d.txt", year, month, day, hours, minutes); //From another post. I do not exactly know the syntax
  38. fPointer = fopen(fileSpec, "w");
  39. fprintf(fPointer, "Date and Time of creation: %d/%d/%d %d:%d:%d\n", day, month, year, hours, minutes, seconds);
  40. for (int i = 0; i < duration; i++) //Main calculating block
  41. {
  42. srand(time(NULL));
  43. r = 10000 - rand() % 20000; //random number + adding to plus/minus
  44. if (r > 0)
  45. {
  46. plus++;
  47. plusminus--;
  48. }
  49. else if (r < 0)
  50. {
  51. minus++;
  52. plusminus--;
  53. }
  54.  
  55. printf("Current number:%d\n", r); //output and ETA calculation
  56. fprintf(fPointer, "%d\n", r);
  57. togo = duration - cycles;
  58. hr = (int)(togo / 3600);
  59. min = ((int)(togo / 60)) % 60;
  60. sec = (int)(togo % 60);
  61. printf("ETA: %02d:%02d:%02d\n", hr, min, sec);
  62. hr = 0;
  63. min = 0;
  64. sec = 0;
  65. r = 0;
  66. cycles++;
  67. sleep(1);
  68. }
  69.  
  70. printf("\n\n\n\nDONE\nDONE\n\n"); //Final result
  71. printf("difference: %d\n", plusminus);
  72. printf("positive: %d\n", plus);
  73. printf("negative: %d\n", minus);
  74. printf("cycles: %d", cycles);
  75.  
  76. //Final result in file
  77. fprintf(fPointer, "difference: %d\n", plusminus);
  78. fprintf(fPointer, "positive: %d\n", plus);
  79. fprintf(fPointer, "negative: %d\n", minus);
  80. fprintf(fPointer, "cycles: %d", cycles);
  81. printf("\nFile written\n");
  82. fclose(fPointer);
  83. while (1) //endless loop
  84. {
  85. r = r;
  86. }
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement