多项式计算

来源:互联网 发布:ip 连不上数据库mysql 编辑:程序博客网 时间:2024/05/01 16:16

 计算多项式的值 3x2+1

输入:多项式系数的个数,x的值

输出:表达式的值

 

 

 

 

 

 

 

 

 

 

 

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

#include "stdafx.h"
#include"iomanip.h"
#include "iostream.h"
 
#define MAXSIZE 100

 

//此多项式是的系数是按照从a0的系数开始输入的;当x^n前的系数为0时,就输入0;
float polynomial(float a[],float x,float n)
{
   int i;
   for(i = n;i > 0;i--)
   {
       a[i-1] = a[i] * x + a[i-1];
   }
   return a[0];

int main(int argc, char* argv[])
{
 float a[MAXSIZE];
 int n,i,x;
   cout<<"please input the numbers of array"<<endl;
   cin>>n;
   cout<<"input x:"  ;
   cin>>x;
   cout<<"please input every value of element"<<endl;
   for(i = n-1;i >=0;i--)
   {
    cout<<"please input the"<<setw(5)<<i<<setw(35)<<"element(enter Enter to switch):"<<endl;
    cin>>a[i];
   }
   cout<<polynomial(a,x,n-1);
 return 0;
}

 

 

运行结果如图: