| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 714 人关注过本帖
标题:我是菜鸟,这程序怎么运行不了,求帮助和完善,不胜感激!!!
只看楼主 加入收藏
你M在等我
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-6-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:9 
我是菜鸟,这程序怎么运行不了,求帮助和完善,不胜感激!!!
// 一元多项式运算.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include<iostream>
using namespace std;
#include<stdlib.h>
#include"stdio.h"


struct Term
{
    int exp;        //指数
    double coef;    //系数
};

struct Node
{
    Term data;      //结点内容,为TERM结构
    struct Node *next;
};

template <typename T>
class Polynomial                                            //多项式类
{
public:
    Polynomial(Node* p) :front(p),n(0)                      //构造函数
    {
        p->data.coef=-1;
        p->data.exp=-1;
        p->next=NULL;
    }
    ~Polynomial() { }                                       //析构函数
    void Print();                                           //显示
    void Delete(Node *p);                                   //销毁多项式值为0的项
    int Getlength();                                        //获取多项式长度
    void Setn();
    void Getfront();
    void Row();                                             //将多项式降序排序
    T Power(T n, T x);                                      //计算x^n,用于赋值
    void Plus(Polynomial &B,Polynomial &R);                 //相加
    void Minus(Polynomial &B,Polynomial &R);                //相减
    void Oppo();                                            //系数相反数,用于相减
    void Evalute(double x);                                 //赋值
    void Derivate(Polynomial &R);                           //求导
    void Multiply(Polynomial &B,Polynomial &R);             //相乘
    void destroyLink();                                     //销毁多项式
    void menuPrint();                                       //菜单
    void createLink(int n);                                 //创建多项式
    void Over();                                            //退出
    int n;
    double result;
private:
    Node *front;
};



//计算x^n,用于赋值
template<class T>
T Polynomial<T>::Power(T n, T x)
{
    double result=1;
    for(int i=1;i<=n;i++)
        result=result*x;
    return result;
}


//显示
template<class T>
void Polynomial<T>::Print()
{
    Row();
    if((front==NULL)||(front->next==NULL))
    {
        cout<<"一元多项式为空"<<endl;
    }
    else
    {
        Node *p=front->next;
        if(p->data.exp!=0)
            cout<<"Poly= "<<p->data.coef<<"x^"<<p->data.exp;
        else if(p->data.exp==0)
            cout<<"Poly= "<<p->data.coef;
        if(p->next!=NULL)
        {
            p=p->next;
            while(1)   
            {        
                if((p->data.coef<0)&&(p->data.exp!=0))   
                    cout<<p->data.coef<<"x^"<<p->data.exp;
                else if((p->data.coef>0)&&(p->data.exp!=0))
                    cout<<"+"<<p->data.coef<<"x^"<<p->data.exp;
                else if((p->data.coef<0)&&(p->data.exp==0))
                    cout<<p->data.coef;
                else if((p->data.coef>0)&&(p->data.exp==0))
                    cout<<"+"<<p->data.coef;
                if(p->next!=NULL)
                    p=p->next;
                else
                    break;
            }
        }
    }
    cout<<endl;
}



//销毁多项式值为0的项
template<class T>
void Polynomial<T>::Delete(Node *p)
{
        Node *q=front;
        while(q->next!=p)
            q=q->next;
        p=q;
        q=q->next;
        delete q;
}



//获取多项式长度
template<class T>
int Polynomial<T>::Getlength()
{   
    Node *p=front;
    if (p == NULL)
        return 0;
    while(p->next!=NULL)
    {
        n++;
        p=p->next;
    }
    return n;
}



template<class T>
void Polynomial<T>::Setn()
{
    n=Getlength();
}

template<class T>
void Polynomial<T>::Getfront()
{
    Node *front=new Node;
}



//将多项式降序排序与合并同类项
template<class T>
void Polynomial<T>::Row()
{
    Node *p=front->next;
    Node *q=front->next;
    Node *t=front->next;

    while(1)       //冒泡排序
    {
        p=t;
        q=p->next;
        while(q)
        {
            if(p->data.exp>q->data.exp)
            {
                if(q->next)
                    q=q->next;
                else
                    break;
            }
            else if(p->data.exp==q->data.exp)    //合并同类项
            {
                p->data.coef=p->data.coef+q->data.coef;
                Node *temp=front;
                while(temp->next!=q)
                    temp=temp->next;
                if(q->next)
                {
                    q=temp;
                    temp=temp->next;
                    q->next=temp->next;
                    delete temp;
                    q=q->next;
                }
                else
                {
                    q=temp;
                    temp=temp->next;
                    delete temp;
                    q->next=NULL;
                    break;
                }
            }
            else
            {
                Node *temp = new Node;
                temp->data=q->data;
                q->data=p->data;
                p->data=temp->data;
                delete temp;
                if(q->next)
                    q=q->next;
                else
                    break;
            }
        }
        if(t->next)
        {
            t=t->next;
        }
        else
            break;
    }
    Setn();
}



//系数相反数,用于相减
template<class T>
void Polynomial<T>::Oppo()
{
    Row();
    Node *p=front->next;
    while(1)
    {
        p->data.coef=0-p->data.coef;
        if(p->next)
            p=p->next;
        else
            break;
    }
}



//相减
template<class T>
void Polynomial<T>::Minus(Polynomial &B,Polynomial &R)
{
    B.Oppo();
    Plus(B,R);
    B.Oppo();
}



//求导
template<class T>
void Polynomial<T>::Derivate(Polynomial &R)
{
    Setn();
    Node *p=front->next;
    Node *r=R.front;
    while(1)
    {        
        r->next=new Node;
        r=r->next;
        r->data.coef=p->data.coef;
        r->data.exp=p->data.exp;
        r->next=NULL;
        if(p->next)
            p=p->next;
        else
            break;
    }
    r=R.front->next;
    while(1)
    {
        if(r->data.exp==0)
            Delete(p);
        else
        {
            r->data.coef=r->data.coef*r->data.exp;
            r->data.exp=r->data.exp-1;
        }
        if(r->next)
            r=r->next;
        else
            break;

    }
}



//相乘
template<class T>
void Polynomial<T>::Multiply(Polynomial &B,Polynomial &R)
{
    Node* a=front->next;
    Node* b=B.front->next;
    Node* r=R.front;
    if(R.front->next!=NULL)
        R.destroyLink();
    Row();
    B.Row();

    while(1)
    {
        while(1)
        {
            Node *rr=new Node;
            rr->data.exp=a->data.exp+b->data.exp;
            rr->data.coef=a->data.coef*b->data.coef;
            rr->next=NULL;
            r->next=rr;
            r=rr;
            if(b->next)
                b=b->next;
            else
                break;
        }
        if(a->next)
            a=a->next;
        else
            break;
        b=B.front->next;
        R.Row();
    }
}




//赋值运算
template<class T>
void Polynomial<T>::Evalute(double x)
{
    Setn();
    Node *p=front->next;
    double temp=0;
    while(1)
    {
        temp+=(p->data.coef)*Power(p->data.exp,x);
        if(p->next)
            p=p->next;
        else
            break;
    }
    cout<<temp<<endl;
}

//相加
template<class T>
void Polynomial<T>::Plus(Polynomial &B,Polynomial &R)
{
    Node* a=front->next;
    Node* b=B.front->next;
    Node* r=R.front;
    while(1)
    {
        r->next=new Node;
        r=r->next;
        r->next=NULL;
        r->data=a->data;
        if(a->next)
            a=a->next;
        else
            break;

    }
    while(1)
    {
        r->next=new Node;
        r=r->next;
        r->next=NULL;
        r->data=b->data;
        if(b->next)
            b=b->next;
        else
            break;

    }
    R.Row();
}



//销毁多项式
template<class T>
void Polynomial<T>::destroyLink()
{
    Node* a=front->next;
    if(!a) throw "一元多项式已为空!";//抛出异常

    while(1)
    {
        if(a->next)
        {
            Node* b=a;
            a=a->next;
            delete b;
        }
        else
        {
            delete a;
            break;
        }
    }
    front->next=NULL;
}




//菜单
template<class T>
void Polynomial<T>::menuPrint()
{
    cout<<"\t***********一元多项式的简单运算*************"<<endl;
    cout<<"\t\t 1创建要运算的两个一元多项式"<<endl;
    cout<<"\t\t 2将两个一元多项式相加"<<endl;
    cout<<"\t\t 3将两个一元多项式相减"<<endl;
    cout<<"\t\t 4将两个一元多项式相乘"<<endl;
    cout<<"\t\t 3将两个一元多项式求导"<<endl;
    cout<<"\t\t 6显示两个一元多项式"<<endl;
    cout<<"\t\t 7给两个一元多项式赋值"<<endl;
    cout<<"\t\t 8销毁所创建的二个多项式"<<endl;
    cout<<"\t\t 9退出"<<endl;
    cout<<"请输入你要进行的操作(1-9)"<<endl;
}



//创建多项式
template<class T>
void Polynomial<T>::createLink(int n)
{
    if(front->next!=NULL)
    {
        destroyLink();
    }
    Node *a=front;
    for(int i=1;i<=n;i++)
    {
        a->next=new Node;
        a=a->next;
        a->next=NULL;

        cout<<"请输入第"<<i<<"项的系数和指数:"<<endl;
        cout<<"系数:";
        cin>>(a->data).coef;
        cout<<"指数:";
        cin>>(a->data).exp;
        if(a->data.exp<0)
        {
            cout<<"您输入有误,指数不允许为负值!"<<endl;
            Delete(a);
            i--;
            continue;
        }
        if(a->data.coef==0)
        {
            cout<<"系数为零,重新输入!"<<endl;
            Delete(a);
            i--;
            continue;
        }
    }
}


//退出
template<class T>
void Polynomial<T>::Over()
{
    exit(0);
}


//主函数   
void main()
{
    Node *p=new Node;
    Node *q=new Node;
    Node *r=new Node;
    Polynomial<double> A(p);     //操作多项式A
    Polynomial<double> B(q);     //操作多项式B
    Polynomial<double> R(r);     //操作多项式R(存储结果)
    int m=0;
    int n=0;
    int choose;
    while(1)
    {  
        A.menuPrint();
        cin>>choose;
        switch(choose)
        {
        case 1:                                                               //创建多项式
            cout<<"请输入你要运算的第一个一元多项式的项数:"<<endl;
            cin>>m;
            A.createLink(m);
            cout<<"请输入你要运算的第二个一元多项式的项数:"<<endl;
            cin>>n;
            B.createLink(n);
            break;      
        case 2:                                                               //相加
            if(!m||!n)
            {
                cout<<"您的多项式创建有误,请重新选择……"<<endl;             //异常(以下省略)
                break;
            }
            A.Plus(B,R);
            cout<<"相加的两个一元多项式为:"<<endl;
            A.Print();
            B.Print();
            cout<<"相加后的结果为:"<<endl;
            R.Print();
            R.destroyLink();
            break;
        case 3:                                                               //相减
            if(!m||!n)
            {
                cout<<"您的多项式创建有误,请重新选择……"<<endl;
                break;
            }
            A.Minus(B,R);
            cout<<"相减的两个一元多项式为:"<<endl;
            A.Print();
            B.Print();
            cout<<"相减后的结果为:"<<endl;
            R.Print();
            R.destroyLink();
            break;     
        case 4:                                                               //相乘
            if(!n||!m)
            {
                cout<<"您的多项式创建有误,请重新选择……"<<endl;
                break;
            }
            A.Multiply(B,R);
            cout<<"相乘的两个一元多项式为:"<<endl;
            A.Print();
            B.Print();
            cout<<"相乘后的结果为:"<<endl;
            R.Print();
            R.destroyLink();
            break;
        case 5:                                                                //求导
            if(!m)
            {
                cout<<"您的多项式创建有误,请重新选择……"<<endl;
                break;
            }
            cout<<"求导的一元多项式为:"<<endl;
            A.Print();
            B.Print();
            cout<<"求导后的结果为:"<<endl;
            A.Derivate(R);
            R.Print();
            R.destroyLink();
            B.Derivate(R);
            R.Print();
            break;
        case 6:                                                                 //输出多项式
            if(!n||!m)
            {
                cout<<"您的多项式创建有误,请重新选择……"<<endl;
                break;
            }
            cout<<"第一个一元多项式为:"<<endl;
            A.Print();
            cout<<"第二个一元多项式为:"<<endl;
            B.Print();
            break;
        case 7:                                                                //多项式赋值
            if(!n||!m)
            {
                cout<<"您的多项式创建有误,请重新选择……"<<endl;
                break;
            }
            cout<<"第一个一元多项式为:"<<endl;
            A.Print();
            cout<<"第二个一元多项式为:"<<endl;
            B.Print();
            cout<<"您的X值为:"<<endl;
            double x;
            cin>>x;
            cout<<"第一个一元多项式值为:";
            A.Evalute(x);
            cout<<"第二个一元多项式值为:";
            B.Evalute(x);
            break;
        case 8:
            try                                                                //捕捉异常
            {
                A.destroyLink();
                B.destroyLink();   
            }
            catch(const char *s)
            {
                cout<<s<<endl;
                cout<<"请重新选择^^^"<<endl;
                continue;
            }
            cout<<"销毁成功!"<<endl;
            break;
        case 9:
            A.Over();                                                           //退出函数
        default:
            cout<<"您的输入有误,请重新选择操作……"<<endl;
            break;
        }
    }
}
搜索更多相关主题的帖子: 应用程序 double 多项式 控制台 
2011-06-29 12:17
CSharp_Go
Rank: 2
等 级:论坛游民
帖 子:6
专家分:13
注 册:2011-7-1
收藏
得分:10 
你该不是希望高手帮你慢慢看吧。。。
2011-07-01 15:54
lmm0629
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2011-7-4
收藏
得分:10 
写了这么多  这菜鸟的水平也忒高了
2011-07-04 14:09
deng520126
Rank: 2
等 级:论坛游民
帖 子:17
专家分:46
注 册:2011-7-20
收藏
得分:0 
饿 不懂。。。。
2011-07-22 21:32
upsetman
Rank: 1
来 自:浙江
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-5-21
收藏
得分:0 
太高深了
2011-07-26 11:16
l295051367
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-8-20
收藏
得分:0 
表示很不懂    表示很不懂
2011-08-20 14:52
l295051367
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-8-20
收藏
得分:0 
表示很不懂    表示很不懂
2011-08-20 14:52
ljw970243
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:43
专家分:105
注 册:2011-8-20
收藏
得分:0 
1 删除或注释#include <stdafx.h>,把 "stdio.h" 改为 <stdio.h>
//#include <stdafx.h>  //编译时找不到这个头文件,做什么用的?
#include <iostream> //尖括号前最好加个空格.
#include <stdlib.h>
#include <stdio.h>
2 void main() 改为 int main()
并在main()结束前加入一行
return 0; //不知做什么用的,人云亦云.
然后编译通过,简单测试正常.

[ 本帖最后由 ljw970243 于 2011-8-26 15:09 编辑 ]
2011-08-26 15:03
ljw970243
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:43
专家分:105
注 册:2011-8-20
收藏
得分:0 
该死的,竟然结贴了,而且没我的份.
呜呜...
2011-08-26 15:04
林枫???
Rank: 1
等 级:新手上路
帖 子:3
专家分:4
注 册:2011-9-9
收藏
得分:0 
好技术的菜鸟啊!!!我比你还菜啊!!!
2011-09-09 15:46
快速回复:我是菜鸟,这程序怎么运行不了,求帮助和完善,不胜感激!!!
数据加载中...
 
   



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

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