练习43

  1. /***********************************************************************************
  2.   43. 对于次数很高,但项目很少的多项式,可用链表来表示。
  3.   例如:X^1000-76*X^76+3*X^3-7可表示为
  4.   ┌─┬──┬─┐  ┌──┬─┬─┐   ┌─┬─┬─┐  ┌─┬─┬──┐
  5.   │1 │1000│  ┼→│-76 │78│  ┼→ │3 │3 │  ┼→│-7│0 │ NIL│
  6.   └─┴──┴─┘  └──┴─┴─┘   └─┴─┴─┘  └─┴─┴──┘
  7.  在此方式下,编程完成两个多项式的加法与乘法。
  8.   *********************************************************************************/
  9. #include <stdio.h>
  10. #include <malloc.h>
  11. typedef struct tagNode
  12. {
  13.     int c;
  14.     int p;
  15.     tagNode *next;
  16. } Node;
  17. FILE *fp;
  18. Node* CreateLink()
  19. {
  20.     int c,p;
  21.     Node *head = NULL;
  22.     Node *tail = NULL;
  23.     Node *temp = NULL;
  24.     fscanf(fp,"%d",&c);
  25.     while(c != 0)
  26.     {
  27.         fscanf(fp,"%d",&p);
  28.         temp = (Node*)malloc(sizeof(Node));
  29.         temp->c = c;
  30.         temp->p = p;
  31.         temp->next = NULL;
  32.         if(!head)
  33.         {
  34.             tail = head = temp;
  35.         }
  36.         else 
  37.         {
  38.             tail->next = temp;
  39.             tail = tail->next;
  40.         }
  41.         fscanf(fp,"%d",&c);
  42.     }
  43.     return head;
  44. }
  45. Node* AddLink(Node *la, Node *lb)
  46. {
  47.     Node *head=NULL,*tail=NULL,*temp;
  48.     Node *s,*t;
  49.     s = la;
  50.     t = lb;
  51.     while(s && t)
  52.     {
  53.         temp = (Node*)malloc(sizeof(Node));
  54.         temp->next = NULL;
  55.         if(s->p == t->p)
  56.         {
  57.             temp->c = s->c + t->c;
  58.             temp->p = s->p;
  59.             s = s->next;
  60.             t = t->next;
  61.         }
  62.         else if(s->p > t->p)
  63.         {
  64.             temp->c = s->c;
  65.             temp->p = s->p;
  66.             s = s->next;
  67.         }
  68.         else 
  69.         {
  70.             temp->c = t->c;
  71.             temp->p = t->p;
  72.             t = t->next;
  73.         }
  74.         if(!head)
  75.         {
  76.             head = tail = temp;
  77.         }
  78.         else 
  79.         {
  80.             tail->next = temp;
  81.             tail = tail->next;
  82.         }
  83.     }
  84.     if(!t)
  85.         s = t;
  86.     while(s)
  87.     {
  88.         temp = (Node*)malloc(sizeof(Node));
  89.         temp->c = s->c;
  90.         temp->p = s->p;
  91.         s = s->next;
  92.         if(!head)
  93.         {
  94.             head = tail = temp;
  95.         }
  96.         else 
  97.         {
  98.             tail->next = temp;
  99.             tail = tail->next;
  100.         }
  101.     }
  102.     return head;
  103. }
  104. Node* MulLink(Node *la, Node *lb)
  105. {
  106.     Node *rs=NULL;
  107.     Node *s,*t,*pr,*qr;
  108.     int c,p;
  109.     for(s=la; s; s=s->next)
  110.     {
  111.         for(t= lb; t; t=t->next)
  112.         {
  113.             c = s->c * t->c;
  114.             p = s->p + t->p;
  115.             for(qr=rs,pr=NULL; qr; pr=qr,qr=qr->next)
  116.             {
  117.                 if(p >= qr->p)
  118.                     break;
  119.             }
  120.             if(qr && p == qr->p)
  121.                 qr->c += c;
  122.             else
  123.             {
  124.                 Node *temp;
  125.                 temp = (Node*)malloc(sizeof(Node));
  126.                 temp->c = c;
  127.                 temp->p = p;
  128.                 temp->next = NULL;
  129.                 if(!qr)
  130.                 {
  131.                     if(!pr)
  132.                         rs = temp;
  133.                     else pr->next = temp;
  134.                 }
  135.                 else
  136.                 {
  137.                     temp->next = pr->next;
  138.                     pr->next = temp;
  139.                 }
  140.             }
  141.         }
  142.     }
  143.     
  144.     return rs;
  145. }
  146. void PrintLink(Node* l)
  147. {
  148.     Node* cur = l;
  149.     while(cur)
  150.     {
  151.         if(cur->c > 0 && cur != l)
  152.             printf("+"); 
  153.         if(cur->p != 0)
  154.         {
  155.             if(cur->p != 1)
  156.             {
  157.                 if(cur->c != 1)
  158.                 {
  159.                     if(cur->c != -1)
  160.                         printf("%dX^%d",cur->c,cur->p);
  161.                     else printf("-X^%d",cur->p);
  162.                 }
  163.                 else
  164.                     printf("X^%d",cur->p);
  165.             }
  166.             else
  167.             {
  168.                 if(cur->c != 1)
  169.                 {
  170.                     if(cur->c != -1)
  171.                         printf("%dX",cur->c);
  172.                     else printf("-X");
  173.                 }
  174.                 else
  175.                     printf("X");
  176.             }
  177.         }
  178.         else printf("%d",cur->c);
  179.         cur = cur->next;
  180.     }
  181.     printf("/n");
  182. }
  183. void main()
  184. {
  185.     Node *la,*lb,*lc;
  186.     fp = fopen("data.txt","r");
  187.     if(!fp)
  188.     {
  189.         printf("Cannot open data file!/n");
  190.         return;
  191.     }
  192.     la = CreateLink();
  193.     PrintLink(la);
  194.     lb = CreateLink();
  195.     PrintLink(lb);
  196.     lc = AddLink(la,lb);
  197.     PrintLink(lc);
  198.     lc = MulLink(la,lb);
  199.     PrintLink(lc);
  200.     fclose(fp);
  201. }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
矩阵键盘是一种常用的输入设备,它主要由多个行和列组成,通过按下不同的组合键来输出不同的字符或指令。Arduino作为一种常用的微控制器,可以轻松地与矩阵键盘进行连接,实现输入输出控制。对于初学者而言,用Arduino控制矩阵键盘是一种很好的习题,因为它涉及到数字输入、数组存储、按键扫描及字符显示等多方面的编程知识。 Arduino矩阵键盘练习可以分为以下几个步骤: 1.准备工作:连接矩阵键盘和Arduino开发板,并下载安装相关的Arduino开发软件。 2.键盘扫描:通过使用Arduino或外部库提供的键盘扫描函数,可以获取用户输入的按键所对应的列、行信息。通常情况下,矩阵键盘为4*4或3*4的矩形方阵,需要定义对应的数组用于存储扫描结果。 3.字符映射:获取按键的列、行信息后,需要对应到相应的字符,在Arduino中可以使用switch case语句或者数组查表法来实现。例如,当按下数字“1”的时候,我们需要输出ASCII码值为49的字符“1”,而当按下符号“+”时,则需要输出ASCII码值为43的字符“+”。 4.输出结果:最后,将字符输出到LCD显示器或串口终端上,供用户查看和操作。 通过实际进行Arduino矩阵键盘练习,可以加深对数字信号输入和处理的理解,提高编程技巧和代码逻辑思维能力。同时,还可以为实际项目中的数据采集和控制提供基础支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值