Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. /*=======================================================*/
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <float.h>
  6.  
  7. #define MAX_NAME_LEN 80
  8. #define MAX_NUM_TRIALS 20
  9. #define MAX_SUBJECTS 500
  10. #define MAX_TRIALS (MAX_SUBJECTS * MAX_NUM_TRIALS)
  11.  
  12. #define TRUE 1
  13. #define FALSE 0
  14.  
  15. /*=======================================================*/
  16. typedef struct
  17. {
  18. char name[MAX_NAME_LEN];
  19. double err;
  20. double sec;
  21. int wu;
  22. int condition_n;
  23. int valid;
  24. } trial_t;
  25.  
  26. /*-------------------------------------------------------*/
  27. typedef struct
  28. {
  29. char name[MAX_NAME_LEN];
  30. int condition_n;
  31. } trial_key_t;
  32.  
  33. /*-------------------------------------------------------*/
  34. typedef struct
  35. {
  36. int total_trials;
  37. int total_valid_trials;
  38. double avg_error;
  39. trial_t *lowest_error;
  40. } stats_t;
  41.  
  42. /*=======================================================*/
  43. FILE *scan_trial_filename();
  44. int scan_trials(FILE *infile, trial_t trials[]);
  45. trial_t scan_trial(FILE *infile);
  46. void print_trials(trial_t trials[], int num_trials);
  47. void print_trial(trial_t trial);
  48. void process_invalidations(trial_t trials[], int num_trials);
  49. trial_t *find_trial(trial_t trials[], int num_trials, trial_key_t *key);
  50. stats_t calc_stats(trial_t trials[], int num_trials);
  51. void print_summary(stats_t results);
  52.  
  53. /*=======================================================*/
  54. int main(void)
  55. {
  56. int num_trials;
  57. FILE *trial_file;
  58. trial_t trials[MAX_TRIALS];
  59. stats_t results;
  60.  
  61. /* Open the trial data file */
  62. trial_file = scan_trial_filename();
  63. if (trial_file == NULL)
  64. return 0; /* Bail out early! */
  65.  
  66. /* Read in the trial data then close the trial file*/
  67. num_trials= scan_trials(trial_file, trials);
  68. fclose(trial_file);
  69.  
  70. /* Process requests to invalidate certain trials */
  71. process_invalidations(trials, num_trials);
  72. print_trials(trials, num_trials);
  73.  
  74. /* Calculate statistics on valid trials */
  75. results = calc_stats(trials, num_trials);
  76.  
  77. /* Print Summary */
  78. print_summary(results);
  79. return 0;
  80. }
  81.  
  82. /*=======================================================*/
  83. FILE *scan_trial_filename()
  84. {
  85. char trial_filename[MAX_NAME_LEN];
  86. FILE *trial_file;
  87.  
  88. scanf("%s", trial_filename);
  89. trial_file = fopen(trial_filename, "r");
  90. if (trial_file == NULL)
  91. {
  92. printf("Unable to open %s\n", trial_filename);
  93. printf("Exiting!\n");
  94. }
  95. return trial_file;
  96. }
  97.  
  98. /*=======================================================*/
  99. int scan_trials(FILE *infile, trial_t trials[])
  100. {
  101. int current_trial = 0;
  102.  
  103. while (current_trial < MAX_TRIALS && !feof(infile))
  104. {
  105. trials[current_trial] = scan_trial(infile);
  106. current_trial++;
  107. }
  108.  
  109. return current_trial - 1;/* last trial is bogus */
  110. }
  111.  
  112. /*=======================================================*/
  113. trial_t scan_trial(FILE *infile)
  114. {
  115. trial_t result;
  116.  
  117. result.valid = TRUE;
  118. /* Replace variables that were above with parts of result */
  119. fscanf(infile, "%s%d%lf%lf%d", result.name, &result.condition_n,
  120. &result.err, &result.sec, &result.wu);
  121. return result;
  122. }
  123.  
  124. /*=======================================================*/
  125. void print_trials(trial_t trials[], int num_trials)
  126. {
  127. int i = 0;
  128.  
  129. for (i = 0; i < num_trials; i++)
  130. {
  131. printf("==============\n");
  132. print_trial(trials[i]);
  133. }
  134. printf("==============\n");
  135. }
  136.  
  137. /*=======================================================*/
  138. void print_trial(trial_t trial)
  139. {
  140. printf("Subject: %s\n", trial.name);
  141. printf("N cond.: %d\n", trial.condition_n);
  142. printf(" Errors: %f\n", trial.err);
  143. printf(" Time: %f\n", trial.sec);
  144. printf("W-Units: %d\n", trial.wu);
  145. printf(" Valid: %d\n", trial.valid);
  146. }
  147.  
  148. /*=======================================================*/
  149. void process_invalidations(trial_t trials[], int num_trials)
  150. {
  151. trial_key_t key;
  152. trial_t *found = NULL;
  153.  
  154. while (scanf("%s%d", key.name, &key.condition_n) != EOF)
  155. {
  156. found = find_trial(trials, num_trials, &key);
  157. if (found)
  158. found->valid = FALSE;
  159. else
  160. printf("Could not find %s(%d)\n",
  161. key.name, key.condition_n);
  162. }
  163. }
  164.  
  165. /*=======================================================*/
  166. trial_t *find_trial(trial_t trials[], int num_trials, trial_key_t *key)
  167. {
  168.  
  169. int i;
  170. int v;
  171.  
  172. for (i=0; i<num_trials; i++)
  173. {
  174. v = strcmp(trials[i].name, key->name);
  175. if (v == 0 && trials[i].condition_n == key->condition_n)
  176. {
  177. return (&trials[i]);
  178. }
  179.  
  180. }
  181.  
  182.  
  183. return NULL;
  184. }
  185.  
  186. /*=======================================================*/
  187. stats_t calc_stats(trial_t trials[], int num_trials)
  188. {
  189. stats_t results = {0};
  190.  
  191. trial_t *cur_trial = NULL;
  192. int i;
  193. int valid = 0;
  194.  
  195. double error_sum = 0.0;
  196. double eff_sum = 0.0;
  197. double min_error = FLT_MAX;
  198. /*double efficiency = 0.0;
  199. double max_eff = -1.0; */
  200.  
  201. /*Total valid trials*/
  202. for (i = 0; i < num_trials; i++)
  203. {
  204. cur_trial = &trials[i];
  205. if (cur_trial->valid != FALSE)
  206. valid++;
  207. }
  208.  
  209. /*Sum error*/
  210. for (i = 0; i < num_trials; i++)
  211. {
  212. cur_trial = &trials[i];
  213. if (cur_trial->valid != FALSE)
  214. error_sum += cur_trial->err;
  215. }
  216.  
  217. /*Find lowest error*/
  218. for(i = 0; i <=num_trials; i++)
  219. {
  220. cur_trial = &trials[i];
  221. if (cur_trial->valid != FALSE)
  222. cur_trial->err;
  223.  
  224. if ((cur_trial->err) < min_error)
  225. {
  226. min_error = cur_trial->err;
  227. }
  228. }
  229.  
  230.  
  231.  
  232.  
  233. results.total_trials=num_trials;
  234. results.total_valid_trials = valid;
  235. results.avg_error = error_sum/results.total_valid_trials;
  236.  
  237.  
  238. if (num_trials <= 0)
  239. return results;
  240.  
  241. return results;
  242. }
  243.  
  244. /*=======================================================*/
  245. void print_summary(stats_t results)
  246. {
  247. printf("=====Results=====\n");
  248. printf(" Total trials: %d\n", results.total_trials);
  249. printf(" Total valid: %d\n", results.total_valid_trials);
  250. printf(" Avg error: %f\n", results.avg_error);
  251. if (results.lowest_error)
  252. {
  253. printf("\n==>Trial showing lowest error<==\n");
  254. print_trial(*results.lowest_error);
  255. }
  256. if (results.best.efficiency)
  257. {
  258. printf("\n==>Trial showing lowest error<==\n");
  259. print_trial(*results.lowest_error);
  260. }
  261.  
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement