[英]Problem in a polynomial derivative calculator c++
I am doing a project for my school its a polynomial derivative calculator in c++ and I am having these problems:我正在为我的学校做一个项目,它是 c++ 中的多项式导数计算器,我遇到了这些问题:
( ^
is Exponentiation) ( ^
是指数)
when user types -x^n
it does not calculate the -
in the outcome ex: -x^4
-> 4x^3
当用户键入-x^n
时,它不会计算结果中的-
例如: -x^4
-> 4x^3
when user types x^n
for a second time, or in a polynomial, it goes like this: 2x^5+x^4
-> 10x^4+0x^3
当用户第二次或在多项式中键入x^n
时,它是这样的: 2x^5+x^4
-> 10x^4+0x^3
int main()
{
char s[100];
cout << " enter your Polynomial : (use ^ for Exponentiation) ";
cin.getline(s, 100, '\n');
int i = 0;
char za[20], t[20], z[20];
while (true) {
if (s[i] == '\0') {
break;
}
if (s[i] == '-' && s[i + 1] == 'x') {
i += 2;
if (s[i] == '^') {
i++;
int j = 0;
if (s[i] == '-') {
for (j, i; s[i] != '+' && s[i] != '\0'; i++, j++) {
if (j != 0) {
if (s[i] == '-')
break;
}
t[j] = s[i];
}
t[j] = '\0';
}
else {
for (j, i; s[i] != '+' && s[i] != '-' && s[i] != '\0'; i++, j++) {
t[j] = s[i];
}
t[j] = '\0';
}
float tavan = atof(t);
if (s[(i - j) - 3] == '+') {
cout << '+' << tavan << "x^" << tavan - 1;
}
else {
cout << tavan << "x^" << tavan - 1;
}
continue;
}
else {
cout << "-1";
continue;
}
}
if (s[i] == 'x') {
i++;
if (s[i] == '^') {
i++;
int j = 0;
if (s[i] == '-') {
for (j, i; s[i] != '+' && s[i] != '\0'; i++, j++) {
if (j != 0) {
if (s[i] == '-')
break;
}
t[j] = s[i];
}
t[j] = '\0';
}
else {
for (j, i; s[i] != '+' && s[i] != '-' && s[i] != '\0'; i++, j++) {
t[j] = s[i];
}
t[j] = '\0';
}
float tavan = atof(t);
if (s[(i-j)-2] == '+') {
cout << '+' << tavan << "x^" << tavan - 1;
}
else {
cout << tavan << "x^" << tavan - 1;
}
continue;
}
else {
if (s[i - 2] == '+') {
cout << '+' <<"1";
}
else {
cout << "1";
}
continue;
}
}
else {
int x = 0;
for (x, i; s[i] != '^'; i++, x++) {
z[x] = s[i];
}
z[x] = '\0';
i++;
int j = 0;
if (s[i] == '-') {
for (j, i; s[i] != '+' && s[i] != '\0'; i++, j++) {
if (j != 0) {
if (s[i] == '-')
break;
}
t[j] = s[i];
}
t[j] = '\0';
}
else {
for (j, i; s[i] != '+' && s[i] != '-' && s[i] != '\0'; i++, j++) {
t[j] = s[i];
}
t[j] = '\0';
}
int k = 0;
for (k; z[k] != 'x'; k++) {
za[k] = z[k];
}
za[k] = '\0';
float zarib = atof(za);
float tavan = atof(t);
zarib = tavan * zarib;
tavan = tavan - 1;
if (z[0] == '+') {
cout << '+' << zarib << "x^" << tavan;
}
else {
cout << zarib << "x^" << tavan;
}
}
}
return 0;
}
I recommend that you use list to make it easier to insert data.我建议您使用列表来更容易插入数据。 Also, a better structured code will be helpful to find problems.此外,更好的结构化代码将有助于发现问题。 You could refer to the following code;您可以参考以下代码;
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.