gpt4 book ai didi

c - 在 C 中创建图形的替代方法

转载 作者:太空宇宙 更新时间:2023-11-04 00:08:42 24 4
gpt4 key购买 nike

我最近创建了一个程序来计算通过管道的流量并逐行生成输出的散点图。我对 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

最佳答案

GNUPlot 是迄今为止用 C 语言绘制图形的最简单方法。它可以绘制从简单的绘图到复杂的 3d 图形,甚至提供 ASCII 艺术输出(如果确实需要 ASCII 输出)

您可以在此处找到有关如何在 C 程序中使用 GNUPlot 的更多信息:http://ndevilla.free.fr/gnuplot/

关于c - 在 C 中创建图形的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com