标题:求助,求求你们帮帮我吧
只看楼主
foxsxt
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-6-2
 问题点数:0 回复次数:1 
求助,求求你们帮帮我吧
  各位高手能不能帮我写一个有关有理数四则混合运算的算法,快要考试啦,可是我还是不会做马求求你们行行好,帮帮我吧!!!
搜索更多相关主题的帖子: 混合 考试 有理数 
2005-06-08 21:19
热情依然
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 = 你输入x的次方为0时就可以实现你要的功能拉


c++/C + 汇编 = 天下无敌
2005-06-09 08:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-20413-1-1.html




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

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