c編程多項式
㈠ 急!c語言 計算多項式的程序
#include
#include
void
main(
)
{
double
coe[20],
x,
sum
=
0;
int
i,
n;
printf("請輸入總項數:
");
scanf("%d",
&n);
printf("請按指數從高到低的順序輸入各項系數:
");
for(i
=
n;
i
>=
0;
i--)
scanf("%lf",
&coe[i]);
printf("請輸入變數x的值:
");
scanf("%lf",
&x);
for(i
=
n;
i
>=
0;
i--)
sum
=
sum
*
x
+
coe[i];
printf("\n結果為:
%lf\n",
sum);
}
㈡ C語言多項式
#include <stdio.h>
#define DEGREE_MAX 8
void get_poly(double coeff[], int *degree)
{
int i;
printf("please enter the biggest degree:");
scanf("%d", degree);
for (i = *degree; i >= 0; i--) {
printf("enter %d `s Coefficient:", i);
scanf("%lf", &coeff[i]);
}
printf("\nyour polynomial is:\n");
for (i = *degree; i >= 0; i--) {
printf("%.2lfX^%d%c", coeff[i], i, (i > 0?' ' : '\n'));
}
}
double eval_poly(const double coeff[], int degree, double x)
{
int i;
double m = x, ret = 0;
ret += coeff[0];
for (i = 1; i <= degree; i++) {
ret += coeff[i]*m;
m *= x;
}
return ret;
}
int main() {
double coeff[DEGREE_MAX],x;
int degree;
get_poly(coeff, °ree);
printf("\nplease enter X value:");
scanf("%lf", &x);
printf("the answer is %.2lf\n", eval_poly(coeff, degree, x));
return 0;
}
㈢ 編寫一段c程序,實現多項式的計算,誰能幫我呀,
分類: 電腦/網路 >> 程序設計 >> 其他編程語言
問題描述:
急需解決!!!!!!!急需解決!!!!!!!
解析:
我可以寫個簡單的只有+ - * / 冪和括弧的多項式的計算
/*
主要是堆棧的應運,把多項式轉換為後綴表達式,再計算後綴表達式的值
*/
中綴表達式轉化為後綴表達式的程序
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
typedef struct node
{
char data; int code; int pri;
struct node *link;
}NODE;
struct Tb1
{
char data; int code; int pri;
}opchTb1[]={{'*',1,4},{'/',2,4},{'+',3,2},{'-',4,2},{'(',5,5},{')',6,1},{'\0',7,0},{'#',-1,0}};
NODE *optop;
char num[200], *numtop;
char expStr[200];
void push(char x,int c,int p,NODE **toppt)
{
NODE *q=(NODE *)malloc(sizeof(NODE));
q->data=x;
q->code=c;
q->pri=p;
q->link=*toppt;
*toppt=q;
}
int pop(char *op,int *cp, NODE **toppt)
{
NODE *q=*toppt;
if(*toppt==NULL) return 1;
*op=q->data;
*cp=q->code;
*toppt=q->link;
free(q);
return 0;
}
int expr(char *pos)
{
struct Tb1 *op;
char sop;
int type,code,n,m,i,c;
optop=NULL;
numtop=num;
n=m=0;
c=' ';
push('#',0,0,*optop);
while(1){
while(c==' '||c=='\t') c=*pos++;
if(isalpha(c)){
*numtop++=' ';
while(isalpha(c)||isdigit(c)) {*numtop++=c;c=*pos++;}
if(m) return 1;
m=1;
continue;
}
else {
for(i=0;opchTb1[i].code!=-1&&opchTb1[i].data!=c;i++)
if(opchTb1[i].code==-1) return 3;
op=&opchTb1.[i];
type=opchTb1.[i].code;
c=*pos++;
}
if(type<5){
if(m!=1) return 1;
m=0;
}
if(type==5) n++;
if(type==6){
if(n--==0) return 2;
if(op->pri>optop->pri)
if(op->data=='(') push(op->code,1,*optop);
else push(op->data,op->code,op->pri,*optop);
else{
while(optop!=NULL&&op->pri<=optop->pri) {
pop(&sop,&code,&optop);
if(code<5&&code>0) {
*numtop++=' ';
*numtop++=sop;
}
}
if(op->data=='\0') return(n!=0||(m!=1&&numtop>num))?4:(*numtop='\0');
else if(op->data!=')') push(op->data,op->code,op->pri,&optop);
}
}
}
void main()
{
int d;
printf("please input the string!\n");
gets(expStr);
if((d=expr(expStr))==0) printf("the postfix string is:%s\n",num);
else printf("The string error! the error is:%d\n",d);
getch();
}
後綴表達式的計算
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXCOLS 80
#define TRUE 1
#define FLASE 0
double eval(char[]);
double pop(struct stack *ps);
void push(struct stack *ps,double x);
int empty(struct stack *ps);
int isdigit(char);
double oper(int,double,double);
void main()
{
char expr[MAXCOLS];
int position=0;
printf("\nPlease input the string:");
while((expr[position++]=getchar())!='\n');
expr[--position]='\0';
printf("%s%s","the original postfix expression is",expr);
printf("\n%f",eval(expr));
getch();
} /*end main*/
/*程序的主要部分eval函數,這個函數只是計算演算法的C語言實現,同時考慮了特定的環境和數據的輸入*/
/*輸出格式。eval調用了一個isdigit函數,它用來判斷其參數是不是一個操作數。在函數eval及其調用的*/
/*pop和push常式中都使用了下面的堆棧說明。eval函數在聲明後給出*/
struct stack{
int top;
double items[MAXCOLS];
};
double eval(char expr[])
{
int c,position;
double opnd1,opnd2,value;
struct stack opndstk;
opndstk.top=-1;
for(position=0;(c=expr[position])!='\0';position++)
if(isdigit(c)) /*operand--convert the character representation of the digit into double and*/
/*push it onto the stack*/
push(&opndstk,(double)(c-'0'));
else{ /*operator*/
opnd2=pop(&opndstk);
opnd1=pop(&opndstk);
value=oper(c,opnd1,opnd2);
push(&opndstk,value);
} /*end else*/
return(pop(&opndstk));
}/*end eval*/
/*下面的函數在許多C系統中都被預定義為一個宏*/
int isdigit(char symb)
{
return(symb>='0'&&symb<='9');
}
/*函數oper首先檢查它的第一個參數是不是一個合法的運算符,如果是,則用另外兩個參數來決定運算結果*/
/*對於求冪運算,使用了math.h中定義的函數pow(op1,op2)。*/
double oper(int symb,double op1,double op2)
{
switch(symb){
case '+' : return(op1+op2);
case '-' : return(op1-op2);
case '*' : return(op1*op2);
case '/' : return(op1/op2);
case '$' : return(pow(op1,op2));
default:printf("%s","illegal operation");
exit(1);
}/*end switch*/
}/*end oper*/
double pop(struct stack *ps)
{
if(empty(ps)){
printf("%s","stack underflow");
exit(1);
} /*end if*/
return(ps->items[ps->top--]);
}/*end pop*/
void push(struct stack *ps,double x)
{
ps->items[++(ps->top)]=x;
return;
} /*end push*/
int empty(struct stack *ps)
{
return(ps->top==-1);
}/*end empty*/