标题:以三元组表为存储结构实现矩阵相加
取消只看楼主
康明贤
Rank: 2
来 自:NWPU
等 级:论坛游民
帖 子:46
专家分:32
注 册:2017-10-23
结帖率:75%
 问题点数:0 回复次数:1 
以三元组表为存储结构实现矩阵相加
第一行输入t1,t2(0<t1,t2<100) ,t1和t2分别是矩阵A和B中非零元素的个数,后面t1+t2行分别输入A和B中的元素,用三元组表示。
C=A+B。
输出三元组表C。

[此贴子已经被作者于2018-5-27 11:48编辑过]

搜索更多相关主题的帖子: 三元 存储 结构 矩阵 相加 
2018-05-14 17:17
康明贤
Rank: 2
来 自:NWPU
等 级:论坛游民
帖 子:46
专家分:32
注 册:2017-10-23
得分:0 
#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;
}

千里之行,始于足下。
2018-05-27 11:47



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




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

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