📄 work1.cpp
字号:
// work1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "iostream.h"
#define MAX_N 25
struct point_type
{
double x;
double y;
double weight;
}point[MAX_N];
int main(int argc, char* argv[])
{
int n;
int i;
static double u11,u12,u21,u22,c1,c2;
double a,b;
//
//输入数据的个数
cout<<"输入点个数n:"<<endl;
cin>>n;
//确认输入的数在正确输入范围
while ((n>MAX_N)||(n<0))
{
cout<<"输入点个数必须在范围 1 到"<<MAX_N<<endl;
cout<<"重新输入n:";
cin>>n;
}
//输入数据
cout<<"输入x_i,y_i,weight_i,i=0.."<<n-1<<": "<<endl;
for (i=0;i<n;i++)
{
cin >>point[i].x>>point[i].y >>point[i].weight;
}
cout<<"输出x_i,y_i,weight_i,i=0.."<<n-1<<": "<<endl;
for (i=0;i<n;i++)
{
cout<<point[i].x<<" "<<point[i].y <<" "<<point[i].weight<<endl;
}
//方程组的求解
u11=u12=0;
u22=0;
c1=c2=0;
for (i=0;i<n;i++)
{
u11=u11+point[i].weight;
u12=u12+point[i].weight*point[i].x;
u22=u22+point[i].x*point[i].x*point[i].weight;
c1=c1+point[i].y*point[i].weight;
c2=c2+point[i].x*point[i].y*point[i].weight;
}
u21=u12;
//求出线性函数系数
a=(c1*u22-c2*u12)/(u11*u22-u12*u21);
b=(c1*u21-c2*u11)/(u21*u12-u22*u11);
cout<<"解为 p(x):"<<a<<"+"<<b<<"x"<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -