⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wyio.cpp

📁 这是一个计算曲线拟合的程序,用户在记事本中输入x,y值,然后程序会根据输入的数值拟合出相近的曲线.
💻 CPP
字号:
/*
Department of Electronic Engineering, University of Liverpool
Module:Pregramme Development
Tutor:J D Yan
Designer: Wang Yao
Student ID: 200478881
Major task: Curve fitting
2006-11-17
*/

#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>

using std::ifstream;
#include <cstdlib> // for exit function
// This program reads values from the file 'example.dat'
// and echoes them to the display until a negative value
// is read.

using namespace std;
void straightline(float arg1[],float arg2[],int t,float &at,float &bt);   // function to fit a line
void second_order_polynomial(float arg1[],float arg2[],int t,float &at,float &bt,float &ct);  // function to fit curve
float third_determinant(float third[3][3]);  // function to calcualte the value of a third order determinant                                                     
float input(float *xy,float *x,float *y,char *stro,char *str);  //function to read in data from a file
float out(char &ch,int &n,float &a,float &b,float &c,float &z,float *x,float *y,char *stro);  // function to output data to a file

int main()
{
   char num[100];
   int n=0;
   
   int i=0;
   float a,b,z,c;
   char ch,str[100];
char stro[100];
     cout<<"Please input the path of input file...." <<endl;
   cout<<"The path should follow the format like this: d:\\file1\\file2\\..."<<endl;
   cout<<"The path is:  ";
   gets(str);
   cout<<"Please input the output file path:  ";
   gets(stro);
   cout<<"Which do you want to curve? Straight line or Second order polynomial?"<<endl;
   cout<<"If you want to curve straith line,please input 'S';"<<endl<<"If you want to curve second order polynomial,please input 'P'"<<endl;
   cout<<"Please input your choice:";
   cin>>ch;
   	ifstream indata;
	indata.open(str);
	if(!indata)    // check whether can open the input file
   { // file couldn't be opened
      cerr << "Error: Input file could not be opened" << endl;
      exit(1);
   }
		while ( !indata.eof() )   // count the number of the data in the input file
   { // keep reading until end-of-file
	    indata>>num;	
	     if(*num!=',' && *num!= NULL) 
		 {

		
		n++;}

		 // sets EOF flag if no value found
   }
indata.close();
float *xy=new float[n],*x=new float[n],*y=new float[n];  // danymic memory allocation

input(xy,x,y,stro,str);  // call input function to read in data from input file

 cout<<stro<<endl;                   // used for designer test, trail the value of variable
   cout<<xy[0]<<xy[1]<<xy[2]<<endl;
 
 
   if(ch=='S')   // if user choose s, then fit a line
  {
	straightline(x,y,n,a,b);

    cout<<endl<<endl<<a<<"  "<<b<<endl;
	 
	for(i=0;i<=n/2-1;i++)  // used for designer test, observe the value of output
	 {
		z=a*x[i]+b;
		cout<<"original X "<<x[i]<<",   "<<"original Y "<<y[i]<<",   "<<"calculcated Cy "<<i<<" is:  "<<z<<endl; 
	 }
  }

else if(ch=='P')  // if user choose p, then fit curve
   {
	second_order_polynomial(x,y,n,a,b,c);
	
    cout<<endl<<endl<<a<<"  "<<b<<"		"<<c<<endl;
	 
	for(i=0;i<=n/2-1;i++)  // used for test , observe output immediately
	 {
		z=a*x[i]*x[i]+b*x[i]+c;
		cout<<"original X "<<x[i]<<",   "<<"original Y "<<y[i]<<",   "<<"calculcated Cy "<<i<<" is:  "<<z<<endl; 
	 }

   }
   else    // if user input wrong character,output error message
	   cout<<"You must input the correct character 'S' or 'P'."<<endl;
       cout<<"Please restart programme to choose again."<<endl;

    out(ch,n,a,b,c,z,x,y,stro);  //call fucntion to write result to an output file
	
    delete []xy;
	delete []x;
	delete []y;
}

 float input(float *xy,float *x,float *y,char *stro,char *str)
  {
	   
   int i=0,j=0;  
  
   char num[100];  // contain instant character read from input file
   // variable for input value

   
 
   ifstream indata; // indata is like cin
  
   indata.open(str); // opens the file
   
   if(!indata) 
   { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }

 

   while ( !indata.eof() )
   { // keep reading until end-of-file
	    indata>>num;
		cout<<"*******"<<num<<"*******"<<endl;
	   if(*num!=',' && *num!= NULL) // if character is not comma or null, convert character and assign value to array xy[n]
		 {cout << "The next number is " << atof(num) <<"	"<<"J before use: "<<j<<endl;

		xy[j]=atof(num);
		
		j++;}

		 cout<<"J now is : "<<j<<endl;
		 // sets EOF flag if no value found
   }
 
   for(i=0;i<=j/2-1;i++)// extract value from array xy[n] and assign value to x[n] and y[n]
   {
	   x[i]=xy[2*i];
       y[i]=xy[2*i+1];
   }

   indata.close();  //close file

   cout << "End-of-file reached.." << endl;
   
   for(i=0;i<=j/2-1;i++)  // used for test 
    {
	cout<<x[i]<<"   "<<y[i]<<endl;
    }
 }


void straightline(float arg1[],float arg2[],int t,float &at,float &bt)  // fitting a line
{
	int i;
	float a1,a2,b1,b2,d1,d2;
cout<<arg1[0]<<"*******"<<arg1[1]<<" *********"<<arg1[2]<<endl;
	a1=0;
	a2=0;
	d1=0;
	d2=0;
	for(i=0;i<=t/2-1;i++)  // calculate value
	{
		a1=a1+arg1[i]*arg1[i];
		a2=a2+arg1[i];
		
		d1=d1+arg1[i]*arg2[i];
		d2=d2+arg2[i];
    }
	
		b1=a2;
		b2=t/2;
		d1=-d1;
		d2=-d2;

		at=(b1*d2-d1*b2)/(a1*b2-b1*a2);  //calculate at and bt 
		bt=-(a1*d2-a2*d1)/(a1*b2-a2*b1);
		
		
			cout<<a1<<"	"<<b1<<"	"<<d1<<endl;
				cout<<a2<<"	"<<b2<<"	"<<d2<<endl;
		
	
}

void second_order_polynomial(float arg1[],float arg2[],int t,float &at,float &bt,float &ct)  // fitting curve
{
	float a[3]={0},b[3]={0},c[3]={0},d[3]={0};
	int i;
	
	for(i=0;i<=t/2-1;i++)  // calculate element value of array a[3],b[3].c[3],d[3]
	{
		a[0]=a[0]+arg1[i]*arg1[i]*arg1[i]*arg1[i];
		a[1]=a[1]+arg1[i]*arg1[i]*arg1[i];
		a[2]=a[2]+arg1[i]*arg1[i];

		b[0]=b[0]+arg1[i]*arg1[i]*arg1[i];
		b[1]=b[1]+arg1[i]*arg1[i];
		b[2]=b[2]+arg1[i];

		c[0]=c[0]+arg1[i]*arg1[i];
		c[1]=c[1]+arg1[i];

		d[0]=d[0]+arg1[i]*arg1[i]*arg2[i];
		d[1]=d[1]+arg1[i]*arg2[i];
		d[2]=d[2]+arg2[i];
		
	}
	c[2]=t/2;
	d[0]=-d[0];
	d[1]=-d[1];
	d[2]=-d[2];

	float A1[3][3]={                             // initialize third order determinant
					{b[0],c[0],d[0]},
					{b[1],c[1],d[1]},
					{b[2],c[2],d[2]}
					};
		  
	float	  A2[3][3]={
					a[0],b[0],c[0],
					a[1],b[1],c[1],
					a[2],b[2],c[2]
					};
		  
	float	  B1[3][3]={
					{a[0],c[0],d[0]},
					{a[1],c[1],d[1]},
					{a[2],c[2],d[2]}
					};

	float	 C1[3][3]={
					{a[0],b[0],d[0]},
					{a[1],b[1],d[1]},
					{a[2],b[2],d[2]}
                  };

		 at=-(third_determinant(A1)/third_determinant(A2)); // call a function to calculate at, bt and ct
		 bt=third_determinant(B1)/third_determinant(A2);
		 ct=-(third_determinant(C1)/third_determinant(A2));	
		
		 for(i=0;i<=t/2-1;i++)   // used for test
		{
			cout<<a[i]<<"	"<<b[i]<<"	"<<c[i]<<"		"<<d[i]<<endl;
		}

		cout<<at<<"		"<<bt<<"	"<<ct<<endl;
		cout<<third_determinant(A1)<<endl<<third_determinant(A2)<<endl<<third_determinant(B1)<<endl<<third_determinant(C1)<<endl;
}

  float third_determinant(float third[3][3])   // calculate the value of a third order determinant
{
	float val;

	val=third[0][0]*(third[1][1]*third[2][2]-third[1][2]*third[2][1])-third[0][1]*(third[1][0]*third[2][2]-third[1][2]*third[2][0])+
		third[0][2]*(third[1][0]*third[2][1]-third[1][1]*third[2][0]);

	return val;
}


float out(char &ch,int &n,float &a,float &b,float &c,float &z,float *x,float *y,char *stro) // write data to output file
{   
	int i=0,j=0;
	ofstream out;
	cout<<"***************"<<stro<<endl;
	out.open(stro);   // open file
	if(ch=='S')  // output data to a file for line
  {
	
	 
	for(i=0;i<=n/2-1;i++)
	 {
		z=a*x[i]+b;   // calculate new y value
	 }
	
	 if(!out)
	{
		cout<<"cannot open output file.\n";  //check whether can open the output file
		return 1;
	}
	  
	  out<<"You choose to fit a Line"<<endl;
	  out<<"The coefficients of the derived equation   ";
	  out<<"a is "<<a<<"  ,  "<<"b is "<<b<<endl;
	   
	for(i=0;i<=n/2-1;i++)   // write data to the output file
	 {
		z=a*x[i]+b;
		out<<"original X "<<x[i]<<",   "<<"original Y "<<y[i]<<",   "<<"calculcated Cy "<<" is:  "<<z<<endl; 
	 }
  }

else if(ch=='P')      // write data to a file if user choose to fit curve
   {
	
	   for(i=0;i<=n/2-1;i++)
	   {
			z=a*x[i]*x[i]+b*x[i]+c;  // calculate new y
	   }
 	
	   if(!out)
	   {
			cout<<"cannot open output file.\n";
			return 1;
	    }
	
	   out<<"You choose to fit polynomial curve"<<endl;
	   out<<"The coefficients of the derived equation   ";
	   out<<"a is "<<a<<"  ,  "<<"b is "<<b<<"  ,  "<<"c is "<<c<<endl;
     
		for(i=0;i<=n/2-1;i++)   // output data 
		{
			z=a*x[i]*x[i]+b*x[i]+c;
			out<<"original X "<<x[i]<<",   "<<"original Y "<<y[i]<<",   "<<"calculcated Cy "<<" is:  "<<z<<endl; 
		}
  
   } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -