Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1413954
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2014-03-10 15:42:14

题目:
两输入字符串,多项式,得出多项式乘积的结果。

代码:

点击(此处)折叠或打开

  1. /******************************************************************************************************
  2. Description : 对两个输入的一元多项式,进行多项式乘法运算,输出结果一元多项式
  3. Prototype : void PolynomialMultiple (char* InputString1, char* InputString2, char* OutputString)
  4. Input Param : char* InputString1 乘数1多项式字符串
  5.                   char* InputString2 乘数2多项式字符串
  6. Output Param : char* OutputString 乘积多项式字符串
  7. Return Value : void

  8. ********************************************************************************************************/
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <malloc.h>

  12. #define OK 1
  13. #define Error 0

  14. typedef struct Node
  15. {
  16.     int top;
  17.     int front;
  18.     struct Node* next;
  19. }Node;

  20. int InitList(Node **L)
  21. {
  22.     *L=(Node*)malloc(sizeof(Node)); /* 产生头结点,并使L指向此头结点 */
  23.     if(!(*L)) /* 存储分配失败 */
  24.             return Error;
  25.     (*L)->next=NULL; /* 指针域为空 */

  26.     return OK;
  27. }

  28. int insert_list(Node** link_list,int para_top,int para_front)
  29. {
  30.     int j=1;
  31.     Node* p;
  32.     Node* insert_node = NULL;
  33.     Node* pNext;
  34.     p = *link_list;
  35.     pNext = p->next;

  36.     while(pNext != NULL)
  37.     {
  38.         if (pNext->top == para_top)
  39.         {
  40.             pNext->front += para_front;
  41.             return OK;
  42.         }
  43.         else if (pNext->top > para_top)
  44.         {
  45.             p = pNext;
  46.             pNext = p->next;
  47.         }
  48.         else
  49.         {
  50.             insert_node = (Node*)malloc(sizeof(Node));
  51.             insert_node->next = pNext;
  52.             insert_node->top = para_top;
  53.             insert_node->front = para_front;
  54.             p->next = insert_node;
  55.             return OK;
  56.         }
  57.     }

  58.     insert_node = (Node*)malloc(sizeof(Node));
  59.     insert_node->next = pNext;
  60.     insert_node->top = para_top;
  61.     insert_node->front = para_front;
  62.     p->next = insert_node;
  63.     return OK;
  64. }

  65. int insertData(char* InputString,Node** pHeadInput)
  66. {
  67.     if (NULL==InputString || NULL==pHeadInput)
  68.     {
  69.         return OK;
  70.     }
  71.     char* pInput = InputString;
  72.     size_t len = strlen(InputString);
  73.     int para_top,para_front;
  74.     unsigned int i,j,k;
  75.     int flag = 1;
  76.     for(i=0; i<len; i++)
  77.     {
  78.         if (pInput[i]=='X' && pInput[i+1]=='^')
  79.         {
  80.             // 获取指数
  81.             para_top=0,para_front=0;
  82.             for(j=i+2;j<len;j++)
  83.             {
  84.                 if (pInput[j]>='0' && pInput[j]<='9')
  85.                 {
  86.                     para_top = para_top*10 + pInput[j]-'0';
  87.                 }
  88.                 else
  89.                 {
  90.                     break;
  91.                 }
  92.             }
  93.             //获取系数
  94.             k=i-1;
  95.             while (k>=0 && (pInput[k]>='0'&& pInput[k]<='9'))
  96.             {
  97.                 k--;
  98.             }
  99.             if (k<0)
  100.             {
  101.                 flag = 1;
  102.             }
  103.             else
  104.             {
  105.                 if (pInput[k] == '-')
  106.                 {
  107.                     flag = -1;
  108.                 }
  109.                 else
  110.                 {
  111.                     flag = 1;
  112.                 }

  113.             }
  114.             k++;
  115.             for (;k<i;k++)
  116.             {
  117.                 if (pInput[k]>='0' && pInput[j]<='9')
  118.                 {
  119.                     para_front = para_front*10 + pInput[k]-'0';
  120.                 }
  121.             }

  122.             insert_list(pHeadInput,para_top,para_front*flag);
  123.         }

  124.     }
  125.     return OK;
  126. }

  127. Node* getData(Node* pHeadInput)
  128. {
  129.     if (NULL == pHeadInput)
  130.     {
  131.         return NULL;
  132.     }
  133.     Node* data = (Node*)malloc(sizeof(Node));
  134.     data->front = pHeadInput->front;
  135.     data->top = pHeadInput->top;
  136.     data->next = pHeadInput->next;
  137.     return data;
  138. }

  139. void cpdata2str(char* str, int data)
  140. {
  141.     char* end = str;
  142.     char* begin = str;
  143.     while (data != 0)
  144.     {
  145.         *end = '0' + data%10;
  146.         end++;
  147.         data = data/10;
  148.     }
  149.     end--;
  150.     char tmp;
  151.     while (begin < end)
  152.     {
  153.         tmp = *begin;
  154.         *begin = *end;
  155.         *end = tmp;

  156.         begin++;
  157.         end--;
  158.     }
  159. }

  160. void PolynomialMultiple (char* InputString1, char* InputString2, char* OutputString)
  161. {
  162.     /*在这里实现功能*/
  163.     if (NULL==InputString1 || NULL==InputString2 || NULL == OutputString)
  164.     {
  165.         return;
  166.     }
  167.     Node* pHeadInput1 = NULL;
  168.     Node* pHeadInput2 = NULL;
  169.     Node* pOutput = NULL;
  170.     char* pInput1 = InputString1;
  171.     char* pInput2 = InputString2;
  172.     char* pOutputstr = OutputString;
  173.     InitList(&pHeadInput1);
  174.     InitList(&pHeadInput2);
  175.     InitList(&pOutput);

  176.     insertData(pInput1,&pHeadInput1);
  177.     insertData(pInput2,&pHeadInput2);
  178.     
  179.     Node* pHead1 = NULL;
  180.     Node* pHead2 = NULL;
  181.     Node* pTmp1 = pHeadInput1;
  182.     Node* pTmp2;
  183.     while ( (pHead1 = getData(pTmp1->next)) != NULL )
  184.     {
  185.         pTmp2 = pHeadInput2;
  186.         while ( (pHead2 = getData(pTmp2->next)) != NULL)
  187.         {
  188.             insert_list(&pOutput,pHead1->top+pHead2->top,pHead1->front*pHead2->front);
  189.             pTmp2 = pHead2;
  190.         }
  191.         pTmp1 = pHead1;
  192.     }
  193.     
  194.     char str_data[10]={0};
  195.     Node* pHead = NULL;
  196.     int i = 0;
  197.     size_t len=0;
  198.     while ( (pHead = getData(pOutput->next)) != NULL )
  199.     {
  200.         if (pHead->front > 0)
  201.         {
  202.             cpdata2str(str_data, pHead->front);
  203.             len = strlen(str_data);
  204.             if (i!=0)
  205.             {
  206.                 *pOutputstr++ = '+';
  207.             }
  208.             strncpy(pOutputstr,str_data,len);
  209.             pOutputstr += len;
  210.             strncpy(pOutputstr,"X^",2);
  211.             pOutputstr += 2;

  212.             memset(str_data,'\0',10);
  213.             cpdata2str(str_data, pHead->top);
  214.             len = strlen(str_data);
  215.             if(len == 0)
  216.             {
  217.                 *pOutputstr = '0';
  218.                 pOutputstr ++;
  219.             }
  220.             else
  221.             {
  222.                 strncpy(pOutputstr,str_data,len);
  223.                 pOutputstr += len;            
  224.             }
  225.         }
  226.         else if(pHead->front < 0)
  227.         {
  228.             cpdata2str(str_data, -(pHead->front));
  229.             len = strlen(str_data);
  230.             *pOutputstr++ = '-';
  231.             strncpy(pOutputstr,str_data,len);
  232.             pOutputstr += len;
  233.             strncpy(pOutputstr,"X^",2);
  234.             pOutputstr += 2;

  235.             memset(str_data,'\0',10);
  236.             cpdata2str(str_data, pHead->top);
  237.             len = strlen(str_data);
  238.             if(len == 0)
  239.             {
  240.                 *pOutputstr = '0';
  241.                 pOutputstr ++;
  242.             }
  243.             else
  244.             {
  245.                 strncpy(pOutputstr,str_data,len);
  246.                 pOutputstr += len;            
  247.             }
  248.         }
  249.         pOutput = pHead;
  250.         memset(str_data,'\0',10);
  251.         i++;
  252.     }
  253.     *pOutputstr = '\0';
  254.     return;
  255. }

阅读(2726) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~