简体   繁体   中英

Alternatives for creating graphs in C

I recently created a program that calculates flow rate through a pipe and generates, line by line, a scatter graph of the output. My knowledge of C is rudimentary (started with python) and I get the feeling that I may have made the code overly complicated. As such, I am asking if anyone has any alternatives to the code below. Critiques of code structure etc. are also welcome!

#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;
}

Output as below.

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 is by far the simplest way to draw graph in C. It can draw from simple plotting to complex 3d graph, and even provides an ASCII Art output (if ASCII output is really required)

You can find more information on how to use GNUPlot in a C program here: http://ndevilla.free.fr/gnuplot/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM