如何用C画图

 

       请问各位高人,如何用C 来画图呢?

       我在网上下载了TC2.01存在U盘里,每次用的时候就把它装到电脑上.我想写一个画图的程序,但搞不懂initgraph()函数该怎样用?运行后出现的结果总是BGI ERROR:~~~~~~~~,不知道为什么呢?是不是跟我临时装的TC有关?是不是装的时候没有带上图形函数呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,如果要使用C语言绘制图表,可以使用GNU plot库。需要安装GNU plot库后,在C语言中使用`popen`函数来调用GNU plot命令。以下是一个简单的例子: ``` #include <stdio.h> int main() { FILE *gnuplot = popen("gnuplot", "w"); fprintf(gnuplot, "plot sin(x)\n"); fflush(gnuplot); getchar(); pclose(gnuplot); return 0; } ``` 这个例子会绘制正弦函数的图像,并在按下回车键后关闭GNU plot窗口。 我们可以根据上面Python的代码,将每个流接收到的总字节和时间输出到文件中,然后使用GNU plot绘制图表。以下是示例代码: ``` #include <stdio.h> #include <stdlib.h> struct PacketInfo { double timestamp; int flowID; int size; }; int main() { // 读取数据包信息 FILE *fp = fopen("packet_info.txt", "r"); if (fp == NULL) { perror("fopen"); exit(1); } struct PacketInfo packet; int flow_count = 0; int *flow_bytes = NULL; double *flow_time = NULL; while (fscanf(fp, "%lf %d %d\n", &packet.timestamp, &packet.flowID, &packet.size) == 3) { if (packet.flowID >= flow_count) { flow_count = packet.flowID + 1; flow_bytes = realloc(flow_bytes, flow_count * sizeof(int)); flow_time = realloc(flow_time, flow_count * sizeof(double)); for (int i = 0; i < flow_count; i++) { flow_bytes[i] = 0; flow_time[i] = -1; } } if (flow_time[packet.flowID] < 0) { flow_time[packet.flowID] = packet.timestamp; } flow_bytes[packet.flowID] += packet.size; } fclose(fp); // 输出到文件 fp = fopen("plot_data.txt", "w"); if (fp == NULL) { perror("fopen"); exit(1); } for (int i = 0; i < flow_count; i++) { if (flow_time[i] < 0) { continue; } fprintf(fp, "# Flow %d\n", i); int bytes = 0; for (double t = 0; t < flow_time[i] + 10; t += 0.1) { while (bytes < flow_bytes[i] && flow_time[0] + t >= flow_time[i] + (double)bytes / flow_bytes[i]) { bytes++; } fprintf(fp, "%lf %d\n", t, bytes); } fprintf(fp, "\n"); } fclose(fp); // 绘制图表 FILE *gnuplot = popen("gnuplot", "w"); fprintf(gnuplot, "set title 'Bytes Received'\n"); fprintf(gnuplot, "set xlabel 'Time (s)'\n"); fprintf(gnuplot, "set ylabel 'Total Bytes Received'\n"); fprintf(gnuplot, "plot 'plot_data.txt' every ::1 using 1:2 with lines title 'Flow 0'"); for (int i = 1; i < flow_count; i++) { fprintf(gnuplot, ", '' every ::%d using 1:2 with lines title 'Flow %d'", i * 2 - 1, i); } fprintf(gnuplot, "\n"); fflush(gnuplot); getchar(); pclose(gnuplot); // 释放内存 free(flow_bytes); free(flow_time); return 0; } ``` 这个例子会读取之前输出到文件中的数据包信息,然后输出到另一个文件中,并使用GNU plot绘制图表。需要注意的是,为了使每个流的图像不重叠,我们在每个流的数据之间添加了一个空行,这样GNU plot会自动将它们分开。另外,由于GNU plot不支持在图例中使用变量,我们需要手动为每个流设置一个标题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值