百韵网 >>  正文

编写一段c程序,实现多项式的计算,谁能帮我呀, 谁能帮我用c语言帮我写一个一元多项式程序。

来源:www.baiyundou.net   日期:较早时间
我可以写个简单的只有+ - * / 幂和括号的多项式的计算
/*
主要是堆栈的应运,把多项式转换为后缀表达式,再计算后缀表达式的值
*/

//中缀表达式转化为后缀表达式的程序

#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*/

我可以写个简单的只有+
-
*
/
幂和括号的多项式的计算
/*
主要是堆栈的应运,把多项式转换为后缀表达式,再计算后缀表达式的值
*/
//中缀表达式转化为后缀表达式的程序
#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*/

谁能帮我用c语言帮我写一个一元多项式程序。~

我记得数据结构书上有源代码的啊......还是帮你写了一个,整整一个小时啊,累死我了

#include
#include
#include

typedef struct _List List;
struct _List
{
int c;
int e;
List* next;
};

#define OPER_PLUS 0
#define OPER_MINUS 1

static List* list_oper(List* list1, List* list2, int oper);

/* order by e dec */
List* add_list(List* list, int c, int e);
void print_list_member(List* list);
List* list_plus(List* list1, List* list2);
List* list_minus(List* list1, List* list2);

List* list_plus(List* list1, List* list2)
{
return list_oper(list1, list2, OPER_PLUS);
}

List* list_minus(List* list1, List* list2)
{
return list_oper(list1, list2, OPER_MINUS);
}

static List* list_oper(List* list1, List* list2, int oper)
{
assert(list1 != NULL && list2 != NULL);

List* result_list = (List*) malloc(sizeof(List));
result_list->c = 0;
result_list->e = 0;

List* list1_temp = list1->next;
List* list2_temp = list2->next;

List* result_temp = result_list;
result_list->next = NULL;

while(list1_temp != NULL && list2_temp != NULL)
{
if(list1_temp->e != list2_temp->e)
{
List* add_list = (List*) malloc(sizeof(List));
add_list->next = NULL;

if(list1_temp->e > list2_temp->e)
{
add_list->e = list1_temp->e;
add_list->c = list1_temp->c;

list1_temp = list1_temp->next;
}
else
{
add_list->e = list2_temp->e;
add_list->c = list2_temp->c;

list2_temp = list2_temp->next;
}

result_temp->next = add_list;
result_temp = result_temp->next;
}
else
{
if(list1_temp->c + list2_temp->c != 0)
{
List* add_list = (List*) malloc(sizeof(List));
add_list->next = NULL;

add_list->e = list1_temp->e;
if(oper == OPER_PLUS)
{
add_list->c = list1_temp->c + list2_temp->c;
}
else
{
add_list->c = list1_temp->c - list2_temp->c;
}

result_temp->next = add_list;
result_temp = result_temp->next;
}

list1_temp = list1_temp->next;
list2_temp = list2_temp->next;
}
}/* end while */

while(list1_temp != NULL)
{
List* add_list = (List*) malloc(sizeof(List));
add_list->next = NULL;

add_list->e = list1_temp->e;
add_list->c = list1_temp->c;

result_temp->next = add_list;
result_temp = result_temp->next;
list1_temp = list1_temp->next;
}

while(list2_temp != NULL)
{
List* add_list = (List*) malloc(sizeof(List));
add_list->next = NULL;

add_list->e = list2_temp->e;
add_list->c = list2_temp->c;

result_temp->next = add_list;
result_temp = result_temp->next;
list2_temp = list2_temp->next;
}

return result_list;

}


void print_list_member(List* list)
{
assert(list != NULL);

List* print_list = list->next;
int count = 0;

while(print_list != NULL)
{
count++;
print_list = print_list->next;
}
printf("count = %d, ", count);

print_list = list->next;
count = 0;
while(print_list != NULL)
{
printf("c%d = %d, e%d = %d, ", count, print_list->c, count, print_list->e);
count++;
print_list = print_list->next;
}

printf("
");
}


List* add_list(List* list, int c, int e)
{
assert(e > 0);

List* list_temp = NULL;
List* list_add = NULL;

/* create new node */
list_add = (List*)malloc(sizeof(List));
list_add->c = c;
list_add->e = e;

/* first add */
if(list == NULL)
{
/* create head */
list = (List*)malloc(sizeof(List));
list->c = 0;
list->e = 0;

list->next = list_add;
list_add->next = NULL;
return list;
}

list_temp = list->next;
List* equel_list = list;

while(list_temp != NULL)
{
assert(list_temp->e != e);

if(list_temp->e > e)
{
equel_list = list_temp;
list_temp = list_temp->next;

if(list_temp == NULL)
{
list_add->next = NULL;
equel_list->next = list_add;
return list;
}
}
else
{
list_add->next = list_temp;
equel_list->next = list_add;
return list;
}
}

return list;
}

int main()
{
/* test */
List* list1 = NULL;
List* list2 = NULL;
list1 = add_list(list1, 3, 2);
list1 = add_list(list1, 9, 4);
list1 = add_list(list1, 1, 5);
list1 = add_list(list1, -3, 1);
list1 = add_list(list1, -1, 9);

list2 = add_list(list2, 5, 2);
list2 = add_list(list2, 2, 3);
list2 = add_list(list2, -9, 8);
list2 = add_list(list2, 1, 9);
list2 = add_list(list2, -8, 5);

// print_list_member(list1);
// print_list_member(list2);

List* list3 = list_plus(list1, list2);
List* list4 = list_minus(list1, list2);

print_list_member(list3);
print_list_member(list4);

}

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct node{
int arr;
struct node *next;
};
typedef struct node Node;
typedef Node *Stack;
struct stacktop{
Stack top;
};
typedef struct stacktop *Top;
int jisuan(int sr1,int sr2,int sr3);
int opinion4(char *ptr,int *num);
int opinion3(char ch);
int opinion2(char ch);
int opinion(char ch);
void StackPush(int ch,Top ptop);
void StackPop(int *pi,Top ptop);
int main(void)
{

int value1;
int value2;
int value3;
char ch[100];
int R,a;
int i=0;
Top svalue;
Top sctype;
svalue=(Top)malloc(sizeof(struct stacktop));
svalue->top=NULL;
sctype=(Top)malloc(sizeof(struct stacktop));
sctype->top=NULL;
StackPush('a',sctype);
printf("请输入一个表达式:
-->");
gets(ch);
while(ch!=NULL&&ch[i]!='\0')
{
if(opinion(ch[i])==1)//如果是操作符
{
if(opinion2(ch[i])top->arr))
{
StackPop(&value1,svalue);
StackPop(&value2,svalue);
StackPop(&value3,sctype);

R=jisuan(value1,value2,value3);
StackPush(R,svalue);
}
StackPush(ch[i],sctype);
}
else if(opinion3(ch[i])==1)//不是操作符,是操作数
{
a=opinion4(ch,&i);
StackPush(a,svalue);
}
else
{
break;
}
i++;
}

while(sctype->top->arr!='a')
{
StackPop(&value1,svalue);
StackPop(&value2,svalue);
StackPop(&value3,sctype);
R=jisuan(value1,value2,value3);
StackPush(R,svalue);
}
printf("%d",R);
return 0;
}

int jisuan(int sr1,int sr2,int sr3)
{
switch((char)sr3)
{
case '+':return sr2+sr1;
case '-':return sr2-sr1;
case '*':return sr2*sr1;
case '/':return sr2/sr1;
}
}


/*将字符转换成数字*/
int opinion4(char *ptr,int *num)
{
int x;
int b=*num;
int c=0;
int d,e,f;
int brr[50];
int fang=1;
int total=0;
x=strlen(ptr);
while((opinion3(ptr[b])==1)&&b<x)
{
brr[c]=ptr[b]-48;
b++;
c++;
}

f=c-1;
if(c==1)
{
*num=b-1;
return brr[0];
}
else
{
for(d=0;d<c;d++)
{
for(e=f;e>0;e--)
fang=fang*10;
total=total+brr[d]*fang;
f--;
fang=1;
}
*num=b-1;
return total;
}
}

/*判断是否为数字*/
int opinion3(char ch)
{
if(ch>='0'&&ch<='9')
return 1;
else
return 0;
}

/*判断运算符的优先级*/
int opinion2(char ch)
{
switch(ch)
{
case '*':
case '/':return 2;
case '+':
case '-':return 1;
default:return 0;
}
}

void StackPop(int *pi,Top ptop)
{
Stack pnew;
pnew=ptop->top;
*pi=pnew->arr;
ptop->top=pnew->next;
free(pnew);
}

void StackPush(int ch,Top ptop)
{
Stack pnew;
pnew=(Stack)malloc(sizeof(Node));
if(pnew==NULL)
exit(1);
pnew->arr=ch;
if(ptop->top==NULL)
pnew->next=NULL;
else
pnew->next=ptop->top;
ptop->top=pnew;

}

/*判断是否为运算符*/
int opinion(char ch)
{
switch(ch)
{
case '+':
case '-':
case '*':
case '/': return 1;
default:
return 0;
}
}


如果有不会的加我百度HI
有什么问题可以随时问我。
yinatea@163.com

相关要点总结:

15060255288:编写一段c程序,实现多项式的计算,谁能帮我呀,
边湛答: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 ...

15060255288:c语言设计 计算多项式a0+a1*x+a2*x*x+...+a9*x*x*x*x*x*x*x*x*x的...
边湛答:{ double s=0.0;double s1=1.0;double x;int i;int a[10];printf("please input x:");scanf("%f",&x);printf("please input a[1] ~a[10]:");for(i=0;i<10;i++)scanf("%d",&a[i]);for(i=0;i<10;i++){ s+=a[i]*s1;s1*=x;} printf("s=%f",s);} ...

15060255288:编写程序求多项式ax^3+bx^2+c的值
边湛答:int main() { float a, b, c, x, result;// 输入多项式系数和自变量 printf("请输入多项式系数a:");scanf("%f", &a);printf("请输入多项式系数b:");scanf("%f", &b);printf("请输入多项式系数c:");scanf("%f", &c);printf("请输入自变量x的值:");scanf("%f", &x);// ...

15060255288:多项式求和的c语言程序
边湛答:int Fluction(int);//声明实现多项式 1-1/2+1/3-1/4+1/5-1/6+...的功能函数 double sum;//定义全局变量(其实一般不推荐定义全局变量)int main(){ int m,n;//m个测试实例,求前 n项和 while(scanf("%d",&m)!=EOF){ for(int i=1;i<=m;i++)//输入 m个测试实例,所以循环...

15060255288:c语言编程求解多项式ax^3+bx^2+c的值
边湛答:以下是一个计算多项式ax^3+bx^2+c值的C语言代码:include <stdio.h> int main() { float a, b, c, x, result;printf("请输入多项式系数 a, b, c:");scanf("%f %f %f", &a, &b, &c);printf("请输入 x 的值:");scanf("%f", &x);result = a * x * x * x + b *...

15060255288:C语言程序题:编写程序实现多项式计算
边湛答:include <stdio.h>#include <stdlib.h>#include <math.h>#define EPS 1E-6typedef struct item {double coefficient;int power;struct item *next;} *POLYNOMIAL,*pItem;POLYNOMIAL Create() { // 创建多项式pItem head,p;double coe;int pwr;head = p = (pItem)malloc(sizeof(item));while...

15060255288:C语言写一个程序,用链表实现多项式相加相减
边湛答://要求:多项式按降幂排列#include<stdafx.h>#include<iostream.h>struct node{int coef;//系数int exp;//指数node * next;};node * h1=NULL;//第一个多项式的头指针node * h2=NULL;//第二个多项式的头指针node * insert(int c,int e)//创建一个系数为c,指数为e的结点,并返回其地址{...

15060255288:C程序 输入一个实数x,计算多项式x+(x^2)/2!+(x^3)/3!+...的和,直到末...
边湛答:include<stdio.h> int main(){ double x,s=0,t;int n=2;scanf("%lf",&x);t = x;while(t>1e-5){ s+=t;t = t*x/n;n++;} printf("%.3lf\n",s);return 0;}

15060255288:C语言程序 编写一个函数,计算并输出下列多项式的值:s=1+1/1!+1/2...
边湛答:include <stdio.h> int main(void){ int n;int i, j;float sum = 1.0;float dex = 1.0;printf("输入一个数:");scanf("%d", &n);for (i = 2; i <= n; ++i){ for (j = i; j > 0; --j){ dex*=j;} sum+=1/dex;dex = 1.0;} printf("%.2f\n", sum);re...

15060255288:C语言求多项式乘法
边湛答:多项式乘法的实现,已知如下两个多项式P(x)=PX+PX+...+PX+PQ(x)=qX+qX+...+qX+q求它们的乘积多项式S(x)=sX+...+sX+s。可以定义两个一维数组p,q,按照次数从高到低存储两个多项式的系... 多项式乘法的实现,已知如下两个多项式 P(x)=P X + P X +...+ P X+P Q(x)=q X +q X +.....

(编辑:本站网友)
相关推荐
关于我们 | 客户服务 | 服务条款 | 联系我们 | 免责声明 | 网站地图
@ 百韵网