| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 627 人关注过本帖
标题:从文件中读入多项式的问题?请大神帮忙看看我的代码那出问题了?
只看楼主 加入收藏
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
结帖率:57.14%
收藏
 问题点数:0 回复次数:1 
从文件中读入多项式的问题?请大神帮忙看看我的代码那出问题了?
在文件中保存,项数 系数 指数 系数 指数。。。。例:
3 1 0 1 1 1 2
3 2 0 3 2 7 9
3 90 0 78 4 8 8
要求从文件中读入,使得
a. 1+x+x^2
b. 2 +3*x^2+7*x^9
c. 2 +3*x^2+7*x^9
...

下面是我写的代码

#include <stdio.h>
#include <stdlib.h>

#define TURE 1
#define FALSE -1

typedef struct
{
     float coef;                        /*系数*/
    int expn;                         /*指数*/
}ElemType;
typedef struct LNode
{
     ElemType data;
    struct LNode *pNext;         
}LNode, *Ploynomial;

void menu ()
{
      printf ("************************************************************\n");
      printf ("------------------------------------------------------------\n");
      printf ("\t\t  多项式运算环境\t\t\n");
      printf ("------------------------------------------------------------\n");
     printf ("\t\t (1): 创建多项式\t\n");
     printf ("\t\t (2): 打印多项式\t\n");
     printf ("\t\t (3): 多项式相加\t\n");
     printf ("\t\t (4): 多项式相减\t\n");
     printf ("\t\t (5): 多项式相乘\t\n");
     printf ("\t\t (6): 多项式混合运算\t\n");
     printf ("\t\t (7): 退出多项式\t\n");
     printf ("************************************************************\n");
     printf ("请输入你的选择:  ");      
}

void ReadPloyn(Ploynomial Ployn, int n)                        /*从文件中读入数据*/
{
      FILE *fp;
      Ploynomial p = Ployn -> pNext;
     if ((fp = fopen("PloyIn.txt", "r")) == NULL)
     {
          printf ("无法读入文件!");
        exit (0);         
     }
           
     fscanf (fp, "%d ", n);
     if (p)
     {
          fscanf (fp, "%f %d ", p -> data.coef, p -> data.expn);
        p = p -> pNext;        
     }
     fclose (fp);
}

void InsertPloyn(Ploynomial Ployn1, Ploynomial Ployn2)        /*将Ployn1插入Ployn2中*/
{
      Ploynomial q1, q2;
      if (Ployn1 -> data.expn ==0 )
          free(Ployn1);   
     else
     {
           q1 = Ployn2;
          q2 = Ployn2 -> pNext;
          while (q2 && Ployn1 -> data.expn < q2 -> data.expn)        /*查找插入位置*/
         {
                q1 = q2;
              q2 = q2 -> pNext;        
         }
         
         if (q2 && Ployn1 -> data.expn == q2 -> data.expn)
         {
                q2 -> data.coef += Ployn1 -> data.coef;
              free(Ployn1);
              
              if (q2 -> data.coef == 0)
              {
                    free(q2);           
               }         
         }
         else
         {
                Ployn1 -> pNext = q2;
              q1 -> pNext = Ployn1;     
         }      
     }      
}
   
Ploynomial CreatePloyn(Ploynomial head, int n)            /*创造一个头结点为head,项数为m的一元多项式*/
{
     Ploynomial p, q;
    head = (Ploynomial ) malloc (sizeof (LNode));    /*创建头结点*/
    ReadPloyn(head, n);
    return head;      
}

int PrintPloyn(Ploynomial Ployn)                    /*输出多项式*/
{
    int count = 1;
    Ploynomial q;
    q = Ployn -> pNext;
    while (!q)
    {
          printf ("0\n");
          return 0;      
    }
    while (q)
    {
          if (q -> data.coef > 0 && count != 1)
                printf ("+");
          if (q -> data.coef != 1 && q -> data.coef != -1)
          {
                printf ("%.1f", q -> data.coef);
                if (q -> data.expn == 1)
                     printf ("X");            
                else
                     printf ("X^%d", q -> data.expn);   
          }
          else
          {
                if (q -> data.coef == 1)
                {
                      if (!q -> data.expn)
                         printf ("1");      
                      else if (q -> data.expn == 1)
                         printf ("X");   
                      else
                         printf ("X^%d", q -> data.expn);         
                }
                if (q -> data.coef == -1)
                {
                      if (!q -> data.expn)
                         printf ("-1");
                      else if (q -> data.expn == 1)
                         printf ("-X");
                      else
                         printf ("-X^%d", q -> data.expn);      
                }   
          }
          q = q -> pNext;
          count++;      
    }
    return count - 1;   
}

main()
{
     int n, choose;
     Ploynomial PloynA, PloynB, PloynC;
     while (1)
     {
         menu();
          scanf ("%d", &choose);
          switch (choose)
          {
              case 1 :
                   {
                        printf ("请输入多项式a.  ");
            PloynA = CreatePloyn(PloynA, n);
            PrintPloyn (PloynA);
                        printf ("请输入多项式b.  ");
                        PloynB = CreatePloyn(PloynB, n);
                        PrintPloyn (PloynB);
                        printf ("请输入回车键继续!\n ");
                        break;        
                   }           

              }
     }
     system ("pause");      
}
搜索更多相关主题的帖子: include 多项式 
2013-10-19 18:34
zc1992312
Rank: 2
等 级:论坛游民
帖 子:43
专家分:12
注 册:2013-3-20
收藏
得分:0 
有没有人啊?  
    调试好久叻, 还是不会啊?
    请求帮忙!!!
2013-10-20 16:41
快速回复:从文件中读入多项式的问题?请大神帮忙看看我的代码那出问题了?
数据加载中...
 
   



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

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