| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 938 人关注过本帖
标题:[求助]帮帮忙!我做的一个关于多项式加减的程序出错了
只看楼主 加入收藏
幽幽001
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-6-8
收藏
 问题点数:0 回复次数:2 
[求助]帮帮忙!我做的一个关于多项式加减的程序出错了
程序如下 题目 一元多项式加减运算 运行的时候说 画红线的地方错了 请好心人帮我改改 要是你们有做好的发一个上来好吗
#include <stdio.h> #include <stdlib.h> #include <conio.h> typedef struct pnode { float coef; int expn; struct pnode * next; } pnode; pnode * creatpolyn(char ch) { pnode * p, *s,*r; float x; int y; p=(pnode *)malloc(sizeof(pnode)); p->next=NULL; printf("shuru\n",ch); scanf("%f %d",&x,&y); while(x!=0) { s=(pnode *)malloc(sizeof(pnode)); s->coef=x; s->expn=y; s->next=NULL; if(p->next==NULL) { p->next=s; r=s; } else { r->next=s; r=s; } scanf("%f %d",&x,&y); } return p; } pnode *addpolyn(pnode * f,pnode * g) { pnode * fg; pnode *t,*q,*s,*r; float m; t=f->next; q=g->next; fg=r=(pnode*)malloc(sizeof(pnode)); fg->next=NULL; while(t&&q) { if(t->expn==q->expn) { m=t->coef+q->coef; if(m!=0) { s=(pnode *)malloc(sizeof(pnode)); s->coef=m; s->expn=t->expn; s->next=NULL; } t=t->next; q=q->next; } else if(t->expn<q->expn) { s=(pnode *)malloc(sizeof(pnode)); s->coef=t->coef; s->expn=t->expn; s->next=NULL; t=t->next; } else { s=(pnode *)malloc(sizeof(pnode)); s->coef=q->coef; s->expn=q->expn; s->next=NULL; q=q->next; } if(fg->next==NULL) { fg->next=s; r=s; } else { r->next=s; r=s; } } r->next=t?t:q; return fg; } void outpolyn(pnode * f) { pnode *t; t=f->next; if(!f->next){ printf("0\n"); return; } while(t) { if(t->coef>0&&f->next!=t) printf("+"); if(t->expn==0) printf("%f",t->coef); else printf("%f*X^%d",t->coef,t->expn); t=t->next; } printf("\n"); } /*一元多项式系数取反*/ pnode * opposite (phone f) pnode f ; p=f.head; while(p->next) { p=p->next; p->data.coef*=-1; } } /*多项式减法*/ pnode * subtractpolyn(pnode *f,pnode *g) { opposite(g) addpolyn(f,g) } void main() { open(); pnode * f,* g,* fg; int i=-1; while(i!=0) { scanf("%d",&i); getchar(); switch(i) { case 0: return; case 1: printf("duoxiangshi xiangjia:\n"); f=createpolyn('A'); printf("A="); outpolyn(f); g=createpolyn('B'); printf("B="); outpolyn(g); printf("A+B="); fg=addpolyn(f,g); outpolyn(fg); i=-1; break; case 2: printf("duoxiangshi xiangjian:\n"); f=createpolyn('A'); printf("A="); outpolyn(f); g=createpolyn('B'); printf("B="); outpolyn(g); printf("A-B="); fg=subtractpolyn(f,g); outpolyn(fg); i=-1; break; case 3: i=-1; break; default: printf("shuruyouwu,congxinshuru\n"); } } }

[此贴子已经被作者于2005-6-8 23:57:44编辑过]

搜索更多相关主题的帖子: 多项式 
2005-06-08 23:56
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
收藏
得分:0 

我自己做了一个,是用栈来做的,用中缀表达式转换成后缀表达式再计算。 //Queue.h class Queue;

class Node{

private: Node *next; int data; int ndata;

public: Node(int d1,int n1,Node *l=NULL):data(d1),ndata(n1),next(l){}; friend class Queue;

}; class Queue{

public: Queue(); ~Queue(); void EnQue(int d,int n); void DeQue(int &d,int &n); bool IsEmpty(){return rear==front;}

void MakeEmpty(); int getlong(){return count;}

private:

Node *rear,*front; int count; };

Queue::Queue() { rear=front=NULL; count=0; }

Queue::~Queue() { MakeEmpty(); }

void Queue::EnQue(int d,int n) { Node *current=new Node(d,n,NULL);

if(rear==NULL)

rear=front=current;

else{ rear->next=current;

rear=current; } }

void Queue::DeQue(int &d,int &n) { Node *current;

assert(!IsEmpty());

current=front;

d=current->data;

n=current->ndata;

front=front->next;

delete current; }

void Queue::MakeEmpty() { Node *current;

while(front) { current=front;

front=front->next;

delete current; } } //stack.h template<typename T>class LinStack;

template<typename T>class StackNode{ private:

StackNode<T> *next;

T data; public:

StackNode(const T &item,StackNode<T> *perNext=NULL);

friend class LinStack<T>; };

template<typename T>StackNode<T>::StackNode(const T &item,StackNode<T> *perNext=NULL){ data=item; next=perNext; }

template<typename T>class LinStack{ private: StackNode<T> *top; public: LinStack(); ~LinStack(); void MakeEmpty(); void Push(const T &item); bool IsEmpty(){return top==NULL;} T pop() ; T Gettop(){ return top->data;}

void ClerStack();

}; template<typename T>LinStack<T>::LinStack(){ top=NULL; } template<typename T>LinStack<T>::~LinStack(){ MakeEmpty(); } template<typename T>void LinStack<T>::MakeEmpty(){ StackNode<T> *temp; while(top!=NULL){ temp=top; top=top->next; delete temp; } } template<typename T>void LinStack<T>::Push(const T &item){ StackNode<T> *newNode; newNode=new StackNode<T>(item,top); top=newNode; } template<typename T>T LinStack<T>::pop() {

assert(!IsEmpty());

StackNode<T> *temp; temp=top; T data=temp->data; top=top->next; delete temp; return data; } #include<iostream> #include<assert.h> #include"Queue.h" #include"stack.h" using namespace std;

typedef struct i{ int data;

int ndata; }Item;

int perence(char op) { switch(op) { case '+': case '-': return 1; case '*': case '/': return 2; default: return 0; } } void expersion(char *exp,LinStack<char> s,char *s1) { char *c=exp;

s.Push('@'); while(*c) { if(*c==' ')

c++;

else if(*c=='(') { s.Push(*c);

c++; }

else if(*c==')') { while(s.Gettop()!='(')

*s1++=s.pop();

s.pop();

c++; } else if(*c=='+'||*c=='-'||*c=='*'||*c=='/') { while(perence(*c)<perence(s.Gettop()))

*s1++=s.pop();

s.Push(*c);

c++; } else{ while(*c>='0'&&*c<='9')

*s1++=*c++; } *s1++=' '; } *s1++=' ';

char ch=s.pop();

while(ch!='@') { if(ch=='(')

exit(1); else { *s1++=ch;

*s1++=' ';

ch=s.pop(); } }

*s1='\0'; }

void calculation(char *s1,LinStack<double> sd) { double num1,num2;

char *c=s1;

double item=0.0;

while(*c) { switch(*c) { case '+': num1=sd.pop();

num2=sd.pop();

sd.Push(num2+num1);

c++;

break; case '-': num1=sd.pop();

num2=sd.pop();

sd.Push(num2-num1);

c++; break; case '*': num1=sd.pop();

num2=sd.pop();

sd.Push(num2*num1);

c++;

break; case '/': num1=sd.pop();

num2=sd.pop();

if(num2==0)

exit(1); else sd.Push(num2/num1);

c++; break; case ' ': c++; break; default: while(*c>='0'&&*c<='9') { item=item*10+*c-'0';

c++; } sd.Push(item); item=0.0;

break; } } if(!sd.IsEmpty())

cout<<"答案是:"<<sd.pop()<<endl; } void main() { Queue q;

LinStack<char> s;

LinStack<double> sd;

Item *item;

int d,n,size,i,j; char exp[255],s1[255];

long int sum,x,x1=0;

cout<<"确定多项式数组大小:"<<endl;

cin>>size;

item=new Item[size]; for(i=0;i<size;i++) { cout<<"输入系数:"<<endl; fflush(stdin); cin>>item[i].data;

cout<<"输入x的次方:"<<endl;

fflush(stdin);

cin>>item[i].ndata; }

for(i=0;i<=size;i++)

q.EnQue(item[i].data,item[i].ndata);

delete item;

cout<<"确认未知数x的值:"<<endl;

cin>>x;

x1=x;

cout<<"依照以下的数字组成中缀表达式:"<<endl; for(i=0;i<size;i++) { q.DeQue(d,n);

if(n==0) { sum=d*x;

cout<<sum<<'\t'; }

else{ for(j=0;j<n-1;j++) x*=x;

sum=d*x;

x=x1;

cout<<sum<<'\t'; }

} cout<<endl;

cout<<"输入中缀表达式:"<<endl; for(i=0;i<255;i++) { cin>>exp[i];

if(exp[i]=='=')

break; } exp[i]='\0'; expersion(exp,s,s1);

cout<<"输出后缀表达式:"<<endl;

cout<<s1<<endl;

calculation(s1,sd); } 输入格式:

例如我要计算 4x^3+2x^3+2x^2 当x=3时的值 (^3表示3次方,^2表示平方,依此类推)

确定多项式数组大小: 3 输入系数: 4 输入x的次方: 3 输入系数: 2 输入x的次方: 3 输入系数: 2 输入x的次方: 2 确认未知数x的值: 3 依照以下的数字组成中缀表达式: 324 162 18 输入中缀表达式: 324 - 162 + 18 =


c++/C + 汇编 = 天下无敌
2005-06-09 08:27
幽幽001
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-6-8
收藏
得分:0 
可以做个C语言的吗???
在TURBOC下运行
谢谢了
2005-06-09 17:10
快速回复:[求助]帮帮忙!我做的一个关于多项式加减的程序出错了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018071 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved