#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct
{
int coef; /*coef of pn*/
int exp; /*exp of pn*/
}PolyTerm;
typedef struct
{
int length;
PolyTerm *data;
}SqPoly;
float Evaluate(SqPoly pn,float x)
{
float sum=0;
int i;
for (i=0;i<pn.length;i++)
sum+=pn.data[i].coef*pow(x,pn.data[i].exp);
printf("The result is:%f",sum);
return 0;
}
int main(void)
{
float Evaluate(SqPoly pn,float x);
int i,n;
SqPoly pn;
PolyTerm data;
float x,f1;
clrscr();
printf("Please input the terms of number of the pn:\n");
scanf("%d",&n);
data.coef=(int*)malloc(sizeof(int)*n);
data.exp=(int*)malloc(sizeof(int)*n); 這里編譯器會提示兩個Non-portable pointer assignment in function main求解
pn.length=n;
printf("Please input the coef of the pn:\n");
for (i=0;i<n;i++)
scanf("%d",&pn.data[i].coef);
printf("Please input the exp of the pn:\n");
for (i=0;i<n;i++)
scanf("%d",&pn.data[i].exp);
printf("Please input the x to solve:");
scanf("%f",&x);
Evaluate(pn,x);
return 0;
}
最后還有一個警告是data is assigned as a value whichi is never used
這是在TC2.0的環境下的,初步學習
uj5u.com熱心網友回復:
pn.data = (PolyTerm *)malloc(sizeof(PolyTerm) * n);uj5u.com熱心網友回復:
float Evaluate(SqPoly pn,float x);int main(void)
{
int i,n;
SqPoly pn;
PolyTerm data;
float x,f1;
clrscr();
printf("Please input the terms of number of the pn:\n");
scanf("%d",&n);
pn.data = (PolyTerm*) malloc( sizeof(PolyTerm) * n );
pn.length=n;
printf("Please input the coef of the pn:\n");
for (i=0;i<n;i++)
scanf("%d",&pn.data[i].coef);
printf("Please input the exp of the pn:\n");
for (i=0;i<n;i++)
scanf("%d",&pn.data[i].exp);
printf("Please input the x to solve:");
scanf("%f",&x);
Evaluate(pn,x);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/133568.html
標籤:C語言
