#include <stdio.h> typedef struct { int year; float salary; } Data;//定义数据结构体,存储年份和平均工资的数据 int main(int argc, char* argv[]) { // Sample data Data sample_data[] = { {2012, 3450}, {2013, 3785}, {2014, 4380}, {2015, 4580}, {2017, 5425}, {2018, 6060}, {2019, 6320}, {2020, 6880}, {2021, 7120}, {2022, 7360}, }; int sample_size = sizeof(sample_data) / sizeof(Data); //定义样本数据数组,并初始化 // 开始绘制散点图 FILE* gp = _popen("gnuplot -persist", "w"); fprintf(gp, "set title 'Average Salary of Graduates'\n"); fprintf(gp, "set xlabel 'Year'\n"); fprintf(gp, "set ylabel 'Salary'\n"); fprintf(gp, "plot '-' with points pointtype 6 pointsize 1.5 title 'Sample Data'\n"); for (int i = 0; i < sample_size; i++) { fprintf(gp, "%d %f\n", sample_data[i].year, sample_data[i].salary); } fprintf(gp, "e\n"); fflush(gp); // 使用最小二乘法拟合曲线 float sx = 0, sy = 0, sxx = 0, sxy = 0; for (int i = 0; i < sample_size; i++) { sx += sample_data[i].year; sy += sample_data[i].salary; sxx += sample_data[i].year * sample_data[i].year; sxy += sample_data[i].year * sample_data[i].salary; } float a = (sxy * sample_size - sx * sy) / (sxx * sample_size - sx * sx); float b = (sxx * sy - sx * sxy) / (sxx * sample_size - sx * sx); // 将拟合曲线绘制到散点图上 fprintf(gp, "set xrange [%d:%d]\n", sample_data[0].year, sample_data[sample_size - 1].year + 1); fprintf(gp, "f(x) = %f * x + %f\n", a, b); fprintf(gp, "plot '-' with points pointtype 6 pointsize 1.5 title 'Sample Data', f(x) with lines linewidth 2 title 'Fitted Curve'\n"); for (int i = 0; i < sample_size; i++) { fprintf(gp, "%d %f\n", sample_data[i].year, sample_data[i].salary); } fprintf(gp, "e\n"); fflush(gp); // 预测明年的平均工资 //int next_year = sample_data[sample_size - 1].year + 1; //float next_salary = a * next_year + b; //printf("预计 %d 年毕业生的平均工资是 %.2f\n", next_year, next将这段代码修改成数据是即时输入的
时间: 2024-01-09 20:04:44 浏览: 73
将这段代码修改成数据是即时输入的,可以将样本数据的部分改为从用户输入获取,修改后的代码如下:
```
#include <stdio.h>
typedef struct {
int year;
float salary;
} Data;
int main(int argc, char* argv[]) {
// Sample data
Data sample_data[100];
printf("请输入样本数据的个数:");
int sample_size;
scanf("%d", &sample_size);
printf("请依次输入样本数据的年份和平均工资:\n");
for (int i = 0; i < sample_size; i++) {
scanf("%d%f", &sample_data[i].year, &sample_data[i].salary);
}
// 绘制散点图
FILE* gp = _popen("gnuplot -persist", "w");
fprintf(gp, "set title 'Average Salary of Graduates'\n");
fprintf(gp, "set xlabel 'Year'\n");
fprintf(gp, "set ylabel 'Salary'\n");
fprintf(gp, "plot '-' with points pointtype 6 pointsize 1.5 title 'Sample Data'\n");
for (int i = 0; i < sample_size; i++) {
fprintf(gp, "%d %f\n", sample_data[i].year, sample_data[i].salary);
}
fprintf(gp, "e\n");
fflush(gp);
// 使用最小二乘法拟合曲线
float sx = 0, sy = 0, sxx = 0, sxy = 0;
for (int i = 0; i < sample_size; i++) {
sx += sample_data[i].year;
sy += sample_data[i].salary;
sxx += sample_data[i].year * sample_data[i].year;
sxy += sample_data[i].year * sample_data[i].salary;
}
float a = (sxy * sample_size - sx * sy) / (sxx * sample_size - sx * sx);
float b = (sxx * sy - sx * sxy) / (sxx * sample_size - sx * sx);
// 将拟合曲线绘制到散点图上
fprintf(gp, "set xrange [%d:%d]\n", sample_data[0].year, sample_data[sample_size - 1].year + 1);
fprintf(gp, "f(x) = %f * x + %f\n", a, b);
fprintf(gp, "plot '-' with points pointtype 6 pointsize 1.5 title 'Sample Data', f(x) with lines linewidth 2 title 'Fitted Curve'\n");
for (int i = 0; i < sample_size; i++) {
fprintf(gp, "%d %f\n", sample_data[i].year, sample_data[i].salary);
}
fprintf(gp, "e\n");
fflush(gp);
// 预测明年的平均工资
int next_year;
float next_salary;
printf("请输入要预测的年份:");
scanf("%d", &next_year);
next_salary = a * next_year + b;
printf("预计 %d 年毕业生的平均工资是 %.2f\n", next_year, next_salary);
return 0;
}
```
修改后,用户可以输入实时的数据,程序会实时绘制散点图并进行拟合,同时用户可以输入要预测的年份并得到预测的平均工资。
阅读全文