以三元组表为存储结构实现矩阵相加
第一行输入t1,t2(0
输出三元组表C。
include
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"<
}
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 = https://www.it610.com/article/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 = https://www.it610.com/article/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 = https://www.it610.com/article/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 = https://www.it610.com/article/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 = https://www.it610.com/article/t;
}
else
{
p->data = https://www.it610.com/article/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 =https://www.it610.com/article/='[' || l1->top->data =https://www.it610.com/article/=']' || l1->top->data =https://www.it610.com/article/='(' || l1->top->data =https://www.it610.com/article/=')' || l1->top->data =https://www.it610.com/article/='{' || l1->top->data =https://www.it610.com/article/='}' )
{
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 <
【以三元组表为存储结构实现矩阵相加】}
推荐阅读
- 爱就是希望你好好活着
- 叙述作文
- 以读攻“毒”唤新活动曹彦斌打卡第二天
- 拉黑家人一整年之后,以为会快乐,最后却抑郁症!!
- 靈魂裡有香氣的人
- 抱着梦的无眠
- 数组常用方法一
- 你累吗
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- 试论化院的学生自组织