C语言三元组表加法,以三元组表为存储结构实现矩阵相加

#include

#include

#include

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<

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 <

return 0;

}

6f83fddf9cb9ff6843fffd45e1807199.gif

千里之行,始于足下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值