第一行输入t1,t2(0<t1,t2<100) ,t1和t2分别是矩阵A和B中非零元素的个数,后面t1+t2行分别输入A和B中的元素,用三元组表示。
C=A+B。
输出三元组表C。

include<iostream>

include<stdlib.h>

include<stdio.h>

using namespace std;
int FLAGl2 = 0,FLAGl1 = 0;
struct node
{

struct node *next;
char data;

};
struct stackchar
{

struct node *top;

}*l1;
struct digi
{

struct digi *next;
struct datascore *data;

};
struct stackstr
{

struct digi *top;

}*l2;
int error1()
{

cout<<"no"<<endl;
return 0;

}
struct datascore
{

struct datascore *last;
char data;
struct datascore *next;

};
char judge(char y,char x)//y?x;
{

if(x == y)
{
    if(x == '+'||x == '-'||x == '*'||x == '/')
    return '<';
    else return '>';
}
if((x == '[' && y == ']') || (x == '(' && y == ')') || (x == '{' && y == '}'))
{
    return '=';
}
if(x == '*' || x == '/')
{
    if(y == '(' || y == '[' || y == '{')
    {
        return '>';
    }
    else
    {
        return '<';
    }
}
if(x == '+' || x == '-')
{
    if(y == ']' || y == '}' || y == ')')
    {
        return '<';
    }
    else return '>';
}
if(x == '(')
{
    if(y == ']' || y == '}')
    {
        return '?';
    }
    else return '>';
}
if(x == '[')
{
    if(y == ')' || y == '}')
    {
        return '?';
    }
    else return '>';
}
if(x == '{')
{
    if(y == ')' || y == ']')
    {
        return '?';
    }
    else return '>';
}

}
void init()
{

l1 = (struct stackchar *)malloc(sizeof(struct stackchar));
l1->top = NULL;
l2 = (struct stackstr *)malloc(sizeof(struct stackstr));
l2->top = NULL;

}
void popl1(stackchar *m)
{

if(m->top == NULL)
{
    return;
}
else if(m->top->next == NULL)
{
    m->top = NULL;
}
else
{
    m->top = m->top->next;
}

}
struct datascore *createstring(char m)
{

struct datascore *w;
w = (struct datascore *)malloc(sizeof(struct datascore));
w->next = NULL;
w->data = m;
w->last = w;
return w;

}
void popl2(stackstr *m)
{

if(m->top == NULL)
{
    return;
}
else if(m->top->next == NULL)
{
    m->top = NULL;
}
else
{
    m->top = m->top->next;
}

}
void pushl1(struct stackchar *m,char x)
{

struct node *y;
y = (struct node *)malloc(sizeof(struct node));
y->data = x;
y->next = NULL;
if(l1->top)
{
    y->next = m->top;
    m->top = y;
}
else
{
    m->top = y;
}

}
void pushl2(struct stackstr o,struct datascore x)
{

struct digi *y;
y = (struct digi *)malloc(sizeof(struct digi));
y->data = x;
y->next = NULL;
if(l2->top)
{
    y->next = o->top;
    o->top = y;   
}
else
{
    o->top = y;
}

}
struct datascore unionstring(struct datascore x1,struct datascore *y1,char l)
{

struct datascore *q;
q = (struct datascore *)malloc(sizeof(struct datascore));
q->data = l;
x1->last->next = y1;
y1->last->next = q;
x1->last = q;
return x1;//将字符串链接到x1上并返回

}
struct datascore *operate(char m)
{

struct datascore *t1,*t2,*sh;
t1 = l2->top->data;
popl2(l2);
t2 = l2->top->data;
popl2(l2);
sh = unionstring(t2,t1,m);//合并两个字符串,后缀符号
return sh;

}
bool isdigi(char m)
{

if(m >= 'a' && m <= 'z')
{
    return true;
}
else return false;

}

int main()
{

char t;
int flag = 0;
struct node *head, *p, *q;
struct datascore *k,*tt;
head = NULL;
init();
while(1)
{
    t = getchar();
    if(t == '\n') break;
    else
    {
        p = (struct node *)malloc(sizeof(struct node));
        p->next = NULL;
        if(!head)
        {
            head = p;
            p->data = t;
        }
        else
        {
            p->data = t;
            q->next = p;
           
        }
        q = p;
    }
}
q = head;
while(q)
{
        if(isdigi(q->data))
        {
            k = createstring(q->data);//将每个字母用单个链表存储,k包含头和尾结点(头结点用k表示)
            pushl2(l2,k);
        }
        else
        {
            flag = 0;
            if(l1->top == NULL)
            {
                pushl1(l1,q->data);
            }
            else switch(judge(q->data,l1->top->data))//栈顶操作符优先级判断
            {
                case '>':
                    pushl1(l1,q->data);
                    break;
                case '<':
                    {
                        while(l1->top && judge(q->data,l1->top->data) == '<')
                        {
                                pushl2(l2,operate(l1->top->data));
                                popl1(l1);
                        }
                        if(l1->top && judge(q->data,l1->top->data) == '=')
                        {
                            popl1(l1);
                        }
                        else if(l1->top && judge(q->data,l1->top->data) == '?')
                        {
                            return error1();
                        }
                        else pushl1(l1,q->data);
                        break;
                    }
                case '?':
                    return error1();
            }
        }
        q = q->next;
}
while(l1->top)
{
    if(l1->top->data == '[' || l1->top->data == ']' || l1->top->data == '(' || l1->top->data == ')' || l1->top->data == '{' || l1->top->data == '}' )
    {
        return error1();
    }
    pushl2(l2,operate(l1->top->data));
    popl1(l1);
}
tt = l2->top->data;
while(tt != l2->top->data->last)//
{
    cout << tt->data;
    tt = tt->next;
}
cout << tt->data <<endl;
return 0;

}


已注销
4 声望0 粉丝

引用和评论

0 条评论