⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 一元稀疏表达式计算器 大学数据结构实验
💻 CPP
字号:
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include "ListOper.h"

using namespace std;

LinkList AnayString(char aString[], int aLength);

int main(int argc, char *argv[])    //-------------------------------
{
    LinkList L;
    char InStr[1024];
    int len;
    cout << "一元稀疏多项式计算器" << endl;
    cout << "Copyright@1999-2004, Gashero Liu." << endl;
    cout << "作者:刘晓明" << endl << endl;
    cout << "请输入一个1024个字符以内的稀疏多项式:";
    cin >> InStr;
    len=strlen(InStr);
    L=AnayString(InStr,len);
    SortList(L);
    OutPutList(L);
    FreeList(L);
    system("PAUSE");	
    return 0;
}

LinkList AnayString(char aString[], int aLength)    //---------------
//TODO: 字符串分析函数 
{
    LinkList L=NULL;
    Node *pos=NULL;
    Node *last;
    Node *head;
    CreateList(L);
    head=L;
    last=head;
    int c=0;
    int e=0;
    char temp[1];
    char tp;
    bool plus=true;
    char status='n';    //状态指示符,我省略了系数为负的情况
    /*
    n: 非运算状态
    c: 正在计算系数
    e: 正在计算指数 
    p: 指数为0 
    f: 完成了一个项目的输入
    */
    if(aLength==1)
    {
        if(isdigit(tp))
        {
            c=atoi(temp);
            e=1;
            status='f';
        }
        if(tp=='x')
        {
            c=1;
            e=1;
            status='f';
        }
    }            
    for(int i=0;i<aLength;i++)
    {
        temp[0]=aString[i];
        tp=temp[0];
        switch(status)
        {
            case 'n':
            {
                c=0;e=0;
                status='c';
                if(tp=='-')
                {
                    plus=false;
                    continue;
                }
                if(isdigit(tp))
                {
                    c=atoi(temp);
                    continue;
                }
                if(tp=='x')//多项式以x开头
                {
                    c=1;
                    e=0;
                    status='e';
                    continue;
                }
            }
            case 'c':
            {
                if(isdigit(aString[i]))
                {
                    if(plus)
                    {
                        c=c*10+atoi(temp);
                    }
                    else
                    {
                        c=c*10-atoi(temp);
                    }
                    continue;
                }
                if(tp=='x')
                {
                    if(c==0)
                    {
                        c=1;
                        e=0;
                    }
                    status='e';
                    e=0;
                    continue;
                }
                //此处考虑了常数项出现在其他位置的可能
                if(tp=='+')
                {
                    plus=true;
                    status='p';
                    continue;
                }
                if(tp=='-')
                {
                    plus=false;
                    status='p';
                    continue;
                }
                /*if(temp[0]=='^')
                {
                    status='e';
                    e=0;
                    continue;
                }*/ //此种情况不可能出现
                continue;
            }    //正在解析系数
            case 'e':
            {
                if(tp=='^')
                {
                    continue;
                }
                if(isdigit(tp))
                {
                    e=e*10+atoi(temp);
                    continue;
                }
                if(tp=='+')
                {
                    plus=true;
                    if(e==0)
                    {
                        e=1;
                    }    
                    status='f';
                    continue;
                }
                if(tp=='-')
                {
                    plus=false;
                    if(e==0)
                    {
                        e=1;
                    }
                    status='f';
                    continue;
                }
            }            //正在解析系数
            case 'p':
            {
                e=0;
                status='f';
                continue;
            }
            case 'f':
            {
                pos=CreateNode(e,c);
                last->next=pos;
                last=pos;
                c=0;e=0;
                status='c';
                i--;
                continue;
            }
        }
    }    
    pos=CreateNode(e,c);
    last->next=pos;
    return L;
}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -