本程序实现把计算结果保存到文件

一、用C程序计算
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define pi 3.1415926
int main()
{
	//  打开文件
    FILE *fp;
    if((fp = fopen("sin.dat","w+"))==NULL) // "w+"在没有sin.dat存在时,新建一个
    {
        printf("无法打开文件\n");
        exit(0);
    }
    // 计算结果
    for(int i=0;i<361;i++)
    {
        fprintf(fp,"%.2f %.2f\n",(float)i,sin(i*(pi/180))); // 保留两位小数
    }
    fclose(fp);
    return 0;
}

计算结果:
C程序计算结果并写入文件

二、将计算得到的结果用python画图
import numpy as np
import matplotlib.pyplot as plt
a = np.loadtxt("F:\\Code\\C_程序\\sin.dat")
x = a[:, 0]
y = a[:, 1]
plt.plot(x, y)
plt.show()

C程序计算结果并写入文件

三、用gnuplot画图

cmd进入“sin.dat”目录
plot "sin.dat" with lines
C程序计算结果并写入文件

相关文章:

  • 2021-11-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-02
  • 2021-06-27
  • 2021-07-07
  • 2022-12-23
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案