私は最近、パイプを通る流量を計算し、行ごとに出力の散布図を生成するプログラムを作成しました。C に関する私の知識は (Python から始まった) 初歩的なものであり、コードを過度に複雑にしてしまったのではないかと感じています。そのため、以下のコードに代わるものがあるかどうかを尋ねています。コード構造などの批評も大歓迎です!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define PI 3.1415926
double
flow_rate(double diameter, double k, double slope){
double area, w_perimeter, hyd_rad, fr;
area = (PI*pow(diameter,2.0))/8.0;
w_perimeter = (PI*diameter)/2.0;
hyd_rad = area/w_perimeter;
fr = (1.0/k)*area*pow(hyd_rad,(2.0/3.0))*pow(slope,(1.0/2.0));
return fr;
}
int
main(int argc, char **argv) {
double avg_k=0.0312, min_slope=0.0008;
float s3_diameter;
int i=0, num=0, flow_array[6] ,rows, align=29;
char graph[] = " ";
char graph_temp[]= " ";
printf("\nFlow Rate (x 10^-3) m^3/s\n");
for (s3_diameter=0.50;s3_diameter>0.24;s3_diameter-=0.05){
flow_array[i] = (1000*(flow_rate(s3_diameter, avg_k, min_slope))+0.5);
i += 1;
}
for (rows=30;rows>0;rows--){
strcpy(graph_temp,graph);
for (num=0;num<6;num++){
if (rows==flow_array[num] && rows%5==0){
graph_temp[align] = '*';
printf("%d%s\n",rows,graph_temp);
align -= 5;
break;
}
else if (rows==flow_array[num]){
graph_temp[align] = '*';
printf("|%s\n",graph_temp);
align -= 5;
break;
}
else {
if (rows%5==0 && num==5){
printf("%d%s\n",rows,graph_temp);
}
else if (rows%5!=0 && num==5){
printf("|%s\n",graph_temp);
}
}
}
}
printf("|----2----3----3----4----4----5----\n");
printf(" 5 0 5 0 5 0\n");
printf(" Diameter (x 10^-2) m\n");
return 0;
}
以下のように出力します。
Flow Rate (x 10^-3) m^3/s
30
|
|
|
|
25
|
|
| *
|
20
|
|
| *
|
15
|
|
| *
|
10
| *
|
|
| *
5
| *
|
|
|
|----2----3----3----4----4----5----
5 0 5 0 5 0
Diameter (x 10^-2) m