Você está na página 1de 3

// Project4.cpp : Defines the entry point for the console application.

//
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <sstream>
#include "parser.h"

double* FindConstants(double[], double[], double[]);


std::string DoubleToString(double);
//Template for converting double to string
template<typename T>
std::string ToString(const T& t)
{
std::ostringstream s;
s << t;
return s.str();
}
int numElements = 5;

int _tmain(int argc, _TCHAR* argv[])


{
double xVals[5] = {1.0,2.0,3.0,-4.0,5.0}; //x values
double yVals[5] = {2,48,272,1182,2262}; //y values
std::string string = " "; //polyno
mial stored as a string in Newton form
double result;
//create and initialize array for values for constants (Newton form) to
0.
double C[5];
for (int i = 0; i <= numElements; i++)
C[i] = 0;
double *cPointer = &(C[0]);
FindConstants(xVals, yVals, cPointer); //returns constants for
given data as an array of doubles
//construct resulting polynomial string
for(int x=0; x < numElements; x++) //repeat for eac
h element in C array
{
string = string + ToString(C[x]);
for (int i=0; i < x; i++)
string = string + "(x-" + ToString(xVals[i]) + ")";
//add (x-x0), (x-x0)(x-x1), ... to string
if (x != numElements-1)
string = string + " + ";
}
//print the string
std::cout << "The interpolating polynomial in Newton form is:\n\n" << st
ring << std::endl;
//Change string to allow the parser to work correctly
//Original string as char array
char buffer[150];
char temp[150] = {' '}; //temp string as char array
char *bufferP = &buffer[0];
char *tempP = &temp[0];
int tempPos = 0;
int i = 0;
char currChar = string.at(i);;
for(i = 0; currChar != '\0' && i < string.length(); i++)
{
currChar = string.at(i);
*(bufferP+i) = currChar;
}
*(bufferP+i) = '\0'; //null terminate the character array
for(i = 0; *(bufferP+i) != '\0'; i++)
{
*(tempP + i + tempPos) = *(bufferP + i); //write
current character
//(x-x0)(x-x1) -> (x-x0)(x-x1) OR #( -> #*(
if ( ( *(bufferP+i)==')' && *(bufferP+i+1) =='(' ) || ((int)*(bufferP+i
) > 47 && (int)*(bufferP+i) < 58 && *(bufferP+i+1) =='(' ) )
{
*(tempP+i+tempPos+1) = '*';
tempPos++;
}
}
//Evaluate p(-1)
Parser p ((std::string) tempP);
p ["x"] = -1;
result = p.Evaluate (temp);
std::cout << "\n p(-1) = " << result << std::endl; //Print result
return 0;
}

//Finds constant values


//Input: xVals - Array of x values
// yVals - Array of y values
//Output: returns pointer to array of calculated constant values
double* FindConstants(double xVals[],double yVals[], double *C)
{
int i; //counters
int x;
int j; //Iterations
double xPart;
double previousSum;
double divisor;
C[0] = yVals[0];
for(x=1; x < numElements; x++) //do for each pair
{
previousSum = 0;
divisor = 1;
for (j=0; j < x; j++) //Sum previous iterations
{
xPart = 1; //Reset total from xvalu
e multiplication to 1 for each part.
for (i=0; i < j; i++)
xPart = xPart * (xVals[x] - xVals[i]); //first
iteration = 1 * (x[1] - x[0])
previousSum = previousSum + (C[j] * xPart); //sum o
f values to left of C variable (to be subtracted) from y[x]
}
if (j != 0)
divisor = xPart * (xVals[x] - xVals[i]); //sum of valu
es to right of C (to be the divisor)
C[x] = (yVals[x] - previousSum) / divisor;
}
return C;
}

Você também pode gostar