fork download
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #define distance(xi,xj,yi,yj) sqrt(pow((xi)-(xj),2.0)+pow((yi)-(yj),2.0))
  6. #define MaxN 100
  7.  
  8. FILE *fp1;
  9. FILE *fp2;
  10. FILE *fp3;
  11.  
  12. struct CityInfo
  13. {
  14. int citynumber;
  15. double x;
  16. double y;
  17. };
  18.  
  19. int main(void){
  20. int N;
  21. int n;
  22. int city_number;
  23. double a, b;
  24. double ux;
  25. double uy;
  26.  
  27. struct CityInfo cityinfo[MaxN]{{ 0, 0.0, 0.0 }};
  28.  
  29.  
  30. printf("訪問都市の位置座標x座標とy座標の範囲(下限a,上限b)の設定について\n");
  31. printf("範囲の下限aを指定して下さい____> "); scanf_s("%lf", &a);
  32. printf("範囲の上限bを指定して下さい____> "); scanf_s("%lf", &b);
  33. printf("訪問都市数Nを指定して下さい____>"); scanf_s("%d", &N);
  34.  
  35. errno_t err, err_r;
  36. err = fopen_s(&fp1, "citydata_info.csv", "w");
  37. if (err == 1)
  38. {
  39. printf("The file 'citydata_info.csv'was not opened\n\n");
  40. }
  41.  
  42. srand((unsigned int)time(NULL));
  43.  
  44. printf("\n都市番号\tx座標\t\t\ty座標\n");
  45.  
  46. for (n = 1; n <= N; n++) {
  47.  
  48. city_number = n;
  49.  
  50. ux = (double)rand() / (double)RAND_MAX;
  51. cityinfo[n].x = a + (b - a) * ux;
  52. uy = (double)rand() / (double)RAND_MAX;
  53. cityinfo[n].y = a + (b - a) * uy;
  54.  
  55.  
  56. printf("%d\t\t%lf\t\t%lf\n", n, cityinfo[n].x, cityinfo[n].y);
  57. fprintf(fp1, "%d\t%lf\t%lf\n", n, cityinfo[n].x, cityinfo[n].y);
  58.  
  59. }
  60.  
  61. if (fp1)
  62. {
  63. err = fclose(fp1);
  64. if (err == 1)
  65. {
  66. printf("The file 'citydata_info.csv'was not closed\n\n");
  67. }
  68. }
  69.  
  70.  
  71. struct CityInfo *cityinfodata;
  72.  
  73. cityinfodata = (struct CityInfo *)malloc((N + 1) *sizeof(struct CityInfo ));
  74. if (cityinfodata == NULL){
  75. printf(" メモリが確保できません。\n"); exit(1);
  76. }
  77.  
  78. for (n = 0; n <= N; n++){
  79. cityinfo[n].citynumber = 0;
  80. cityinfo[n].x = 0.0;
  81. cityinfo[n].y = 0.0;
  82. }
  83. if (err == 1){
  84. printf("The file 'citydata_info.txt' was not opened as read mode.\n\n");
  85. }
  86. err_r = fopen_s(&fp2, "citydata_info.csv", "w");
  87.  
  88. if (err_r == 1){
  89. printf("The file 'citydata_info.csv' was not opened as read mode.\n\n");
  90. }
  91.  
  92. n = 0;
  93.  
  94. while ((fscanf_s(fp1, "%d,%lf,%lf", &cityinfo[n].citynumber, &cityinfo[n].x, &cityinfo[n].y)) == 3){
  95.  
  96. fprintf_s(fp2, "%d,%lf,%lf\n", cityinfo[n].citynumber, cityinfo[n].x, cityinfo[n].y);
  97.  
  98. n++;
  99.  
  100. }
  101.  
  102.  
  103. if (fp1){
  104. err = fclose(fp1);
  105. if (err == 1){ printf("The file 'citydata_info.txt' was not closed.\n\n"); }
  106. }
  107. if (fp2){
  108. err_r = fclose(fp2);
  109. if (err_r == 1){ printf("The file 'citydata_info.csv' was not closed.\n\n"); }
  110. }
  111.  
  112. err_r = fopen_s(&fp2, "citydata_info.csv", "r");
  113.  
  114. if (err_r == 1){
  115. printf("The file 'citydata_info.csv' was not opened as read mode.\n\n");
  116. }
  117.  
  118. errno_t err3;
  119. err3 = fopen_s(&fp3, "qwerty.csv", "w");
  120. if (err3 == 1)
  121. {
  122. printf("The file qwerty.csv' was not opened as read mode.\n\n");
  123. }
  124. fprintf_s(fp3, "都市番号,x座標,y座標\n");
  125.  
  126. while ((fscanf_s(fp3, "%d,%lf,%lf", &cityinfo[n].citynumber, &cityinfo[n].x, &cityinfo[n].y)) == 3){
  127.  
  128. n++;
  129.  
  130. }
  131.  
  132. int *z;
  133. z = (int *)malloc((N + 1) *sizeof(int));
  134. if (z == NULL){
  135. printf("一次元配列 z のメモリを確保できません。\n"); exit(1);
  136. }
  137.  
  138. for (n = 0; n <= N; n++){
  139. z[n] = 0;
  140. }
  141.  
  142. int *zz;
  143. zz = (int *)malloc((N + 1) *sizeof(int));
  144. if (zz == NULL){
  145. printf("一次元配列 zz のメモリを確保できません。\n"); exit(1);
  146. }
  147.  
  148. for (n = 0; n <= N; n++){
  149. zz[n] = 0;
  150. }
  151.  
  152. clock_t start_time, end_time;
  153. int start;
  154. int i;
  155. int j;
  156. int k;
  157. int next;
  158. double min;
  159. double disij;
  160. double tdis=0.0;
  161.  
  162. start = 0;
  163. printf("\n最初の出発都市startを都市番号1~%dより指定してください。-->", N); scanf_s("%d", &start);
  164.  
  165. start_time = clock();
  166. i = start;
  167.  
  168. printf("k\ti\tnext\tmin\ttdis\n");
  169.  
  170. k = 1;
  171.  
  172.  
  173. z[i] = k;
  174. zz[k] = i;
  175.  
  176. for (k = 2; k <= N; k++) {
  177. min = 10000.0;
  178. for (j = 1; j <= N ; j++) {
  179. if (z[j] == 0) {
  180. disij = distance(cityinfo[i].x, cityinfo[j].x, cityinfo[i].y, cityinfo[j].y);
  181.  
  182. if (disij<min) {
  183. next = j;
  184. min = disij;
  185. }
  186. }
  187. }
  188.  
  189. z[next] = k;
  190. zz[k] = next;
  191. tdis = tdis + disij;
  192. i = next;
  193.  
  194. printf("%d\t%d\t%d\t%lf\t%lf\n", k, i, next, min, tdis);
  195. fprintf(fp3, "%d,%d,%d,%lf,%lf\n", k, i, next, min, tdis);
  196.  
  197. }
  198.  
  199. end_time = clock();
  200.  
  201. double ldis= 0.0;
  202. ldis = sqrt(pow(cityinfo[start].x - cityinfo[next].x, 2.0) + pow(cityinfo[start].y - cityinfo[next].y, 2.0));
  203. tdis = tdis+ldis;
  204.  
  205. printf("\t%d\t%d\t%lf\t%lf\n", next, start, ldis, tdis);
  206. fprintf(fp3, ",%d,%d,%lf,%lf\n", next, start, ldis, tdis);
  207.  
  208. double searching_time;
  209. searching_time = (double)(end_time - start_time) / (double)CLOCKS_PER_SEC;
  210.  
  211. printf("\n最近隣法による最短巡回経路の総移動距離は%lfです。\n", tdis);
  212. fprintf_s(fp3, "\n最近隣法による最短巡回経路の総移動距離は%lfです。\n", tdis);
  213.  
  214. printf("探索処理時間%lf秒\n", searching_time);
  215. fprintf_s(fp3, "探索処理時間%lf秒\n", searching_time);
  216.  
  217. free(cityinfo);
  218. free(z);
  219. free(zz);
  220.  
  221. return 0;
  222. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:27:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
     struct CityInfo cityinfo[MaxN]{{ 0, 0.0, 0.0 }};
                                   ^
prog.c:27:39: warning: left-hand operand of comma expression has no effect [-Wunused-value]
     struct CityInfo cityinfo[MaxN]{{ 0, 0.0, 0.0 }};
                                       ^
prog.c:27:44: warning: left-hand operand of comma expression has no effect [-Wunused-value]
     struct CityInfo cityinfo[MaxN]{{ 0, 0.0, 0.0 }};
                                            ^
prog.c:27:38: warning: statement with no effect [-Wunused-value]
     struct CityInfo cityinfo[MaxN]{{ 0, 0.0, 0.0 }};
                                      ^
prog.c:27:50: error: expected ‘;’ before ‘}’ token
     struct CityInfo cityinfo[MaxN]{{ 0, 0.0, 0.0 }};
                                                  ^
prog.c:31:5: warning: implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]
     printf("範囲の下限aを指定して下さい____> "); scanf_s("%lf", &a);
     ^
prog.c:35:5: error: unknown type name ‘errno_t’
     errno_t err, err_r;
     ^
prog.c:36:5: warning: implicit declaration of function ‘fopen_s’ [-Wimplicit-function-declaration]
     err = fopen_s(&fp1, "citydata_info.csv", "w");
     ^
prog.c:51:9: error: ‘cityinfo’ undeclared (first use in this function)
         cityinfo[n].x = a + (b - a) * ux;
         ^
prog.c:51:9: note: each undeclared identifier is reported only once for each function it appears in
prog.c:94:5: warning: implicit declaration of function ‘fscanf_s’ [-Wimplicit-function-declaration]
     while ((fscanf_s(fp1, "%d,%lf,%lf", &cityinfo[n].citynumber, &cityinfo[n].x, &cityinfo[n].y)) == 3){
     ^
prog.c:96:9: warning: implicit declaration of function ‘fprintf_s’ [-Wimplicit-function-declaration]
         fprintf_s(fp2, "%d,%lf,%lf\n", cityinfo[n].citynumber, cityinfo[n].x, cityinfo[n].y);
         ^
prog.c:118:5: error: unknown type name ‘errno_t’
     errno_t err3;
     ^
prog.c:22:5: warning: variable ‘city_number’ set but not used [-Wunused-but-set-variable]
 int city_number;
     ^
stdout
Standard output is empty