从文件中读入多项式的问题?请大神帮忙看看我的代码那出问题了?
在文件中保存,项数 系数 指数 系数 指数。。。。例: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");
}