多项式导数计算器c++; 我正在为我的学校做一个项目,它是C++中的多项式求导计算器。 我有这些问题:
(多项式导数计算器c++; 我正在为我的学校做一个项目,它是C++中的多项式求导计算器。 我有这些问题:,c++,visual-studio,C++,Visual Studio,(^是指数运算) 当用户键入-x^n时,它不会计算结果中的-,例如:-x^4->4x^3 当用户第二次键入x^n或以多项式形式键入时,它是这样的:2x^5+x^4->10x^4+0x^3 intmain() { chars[100]; cout我建议您使用list来简化数据插入。此外,更好的结构化代码将有助于发现问题。您可以参考以下代码 typedef struct node { float coef; int expn; struct node* next; } P
^
是指数运算)
-x^n
时,它不会计算结果中的-
,例如:-x^4
->4x^3
x^n
或以多项式形式键入时,它是这样的:2x^5+x^4
->10x^4+0x^3
intmain()
{
chars[100];
cout我建议您使用list来简化数据插入。此外,更好的结构化代码将有助于发现问题。您可以参考以下代码
typedef struct node
{
float coef;
int expn;
struct node* next;
} PLOYList;
void insert(PLOYList *head, PLOYList *input)
{
PLOYList *pre, *now;
int signal = 0;
pre = head;
if (pre->next == NULL)
{
pre->next = input;
}
else
{
now = pre->next;
while (signal == 0)
{
if (input->expn < now->expn)
{
if (now->next = NULL)
{
now->next = input;
signal = 1;
}
else
{
pre = now;
now = pre->next;
}
}
else if (input->expn > now->expn)
{
input->next = now;
pre->next = input;
signal = 1;
}
else
{
now->coef = now->coef + input->coef;
signal = 1;
free(input);
if (now->coef == 0)
{
pre->next = now->next;
free(now);
}
}
}
}
}
PLOYList *create(char ch)
{
PLOYList *head, *input;
float x;
int y;
head = (PLOYList*)malloc(sizeof(PLOYList));
head->next = NULL;
scanf("%fx^ %d+", &x, &y);
while (x != 0)
{
input = (PLOYList*)malloc(sizeof(PLOYList));
input->coef = x;
input->expn = y;
input->next = NULL;
insert(head, input);
scanf("%fx^%d+", &x, &y);
}
return head;
}
PLOYList *def(PLOYList *head)
{
PLOYList *p;
p = head->next;
while (p)
{
p->coef = p->expn;
p->expn = p->expn--;
p = p->next;
}
return head;
}
void print(PLOYList *fun)
{
PLOYList *printing;
int flag = 0;
printing = fun->next;
if (fun->next == NULL)
{
printf("0\n");
}
while (flag == 0)
{
if (printing->coef > 0 && fun->next != printing)
printf("+");
if (printing->coef == 1);
else if (printing->coef == -1)
printf("-");
else
printf("%f", printing->coef);
if (printing->expn != 0)
printf("x^%d", printing->expn);
else if ((printing->coef == 1) || (printing->coef == -1))
printf("1");
if (printing->next == NULL)
flag = 1;
else
printing = printing->next;
}
printf("\n");
}
typedef结构节点
{
浮动系数;
int-expn;
结构节点*下一步;
}工作清单;
无效插入(POLYLIST*头,POLYLIST*输入)
{
前,现在;
int信号=0;
pre=头;
如果(预->下一步==NULL)
{
pre->next=输入;
}
其他的
{
现在=开始->下一步;
while(信号==0)
{
如果(输入->扩展<现在->扩展)
{
如果(现在->下一步=NULL)
{
现在->下一步=输入;
信号=1;
}
其他的
{
前=现在;
现在=开始->下一步;
}
}
else if(输入->扩展>现在->扩展)
{
输入->下一步=现在;
pre->next=输入;
信号=1;
}
其他的
{
现在->coef=现在->coef+输入->coef;
信号=1;
免费(输入);
如果(现在->系数==0)
{
前->下一步=现在->下一步;
免费(现在);
}
}
}
}
}
PLOYList*创建(char-ch)
{
列表*标题,*输入;
浮动x;
int-y;
head=(PLOYList*)malloc(sizeof(PLOYList));
head->next=NULL;
scanf(“%fx^%d+”、&x和&y);
而(x!=0)
{
输入=(PLOYList*)malloc(sizeof(PLOYList));
输入->系数=x;
输入->扩展=y;
输入->下一步=空;
插入(头,输入);
scanf(“%fx^%d+”、&x和&y);
}
回流头;
}
POLYLIST*def(POLYLIST*head)
{
表*p;
p=头部->下一步;
while(p)
{
p->coef=p->expn;
p->expn=p->expn--;
p=p->next;
}
回流头;
}
作废打印(列表*乐趣)
{
列表*打印;
int标志=0;
打印=乐趣->下一步;
如果(乐趣->下一步==NULL)
{
printf(“0\n”);
}
while(标志==0)
{
如果(打印->coef>0&&fun->下一步!=打印)
printf(“+”);
如果(打印->系数==1);
否则如果(打印->coef==-1)
printf(“-”);
其他的
printf(“%f”,printing->coef);
如果(打印->扩展!=0)
printf(“x^%d”,打印->扩展);
如果((打印->coef==1)| |(打印->coef==1))
printf(“1”);
如果(打印->下一步==NULL)
flag=1;
其他的
打印=打印->下一步;
}
printf(“\n”);
}
说真的,我会按原样提交。代码是完全非结构化的(例如,除了主循环之外,没有一个函数,过于复杂的循环)。如果在截止日期前进行修补,可能会使事情变得更糟。下次您必须编写一些代码时,请尝试将问题分解为更小的部分,并使用自己的函数解决每个部分。这样代码更易于管理。john是对的。您的代码中有许多块可能会被转换在函数、变量等方面,它很难读取,并且更容易出错。进行此检查将更容易发现问题,或许,使用更好的结构化代码,您可以自己解决问题。