char input1[1000], input2[1000];
while(scanf("%s %s", input1, input2)!=EOF)
{
//input1 = -3x^2+5x-4
//input2 = x^2-1
}
在此之上,我想输入一个如下所示的多项式
// Create first list of -3x^2+5x-4
create_node(-3,2,&poly1);
create_node(5,1,&poly1);
create_node(-4,0,&poly1);
// Create second list of x^2-1
create_node(1,2,&poly2);
create_node(-1,0,&poly2);
我的问题是:(-3x ^ 2 + 5x-4)例如
我如何检测到-3、2、5,-4
create_node(-3,2,&poly1);
create_node(5,1,&poly1);
create_node(-4,0,&poly1);
我的想法是如何检测
//input 1 = -3x^2+5x-4
直到x的前面(将是-3),并检测到^的后面(将是2),依此类推。
但是我如何检测字符x和^等。
这是我的完整代码my code
谢谢
答案 0 :(得分:0)
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
char buffer[1000];
while(fgets(buffer, 1000, stdin)) {
char *p = buffer;
while (*p) {
while (*p && isspace(*p))
++p;
if (!*p)
break;
if (!isdigit(*p)) {
printf("Variable/operator: %c\n", *p++);
}
else {
char *end;
errno = 0;
long number = strtol(p, &end, 10);
if (errno == ERANGE) {
fputs("Value out of range!\n", stderr);
break;
}
else if(p != end) {
printf("Value: %ld\n", number);
p = end;
}
}
}
}
}
-3x^2+5x-4
Variable/operator: -
Value: 3
Variable/operator: x
Variable/operator: ^
Value: 2
Variable/operator: +
Value: 5
Variable/operator: x
Variable/operator: -
Value: 4
答案 1 :(得分:0)
create_node
28行的功能中,r
未初始化以下代码可以解决您的问题和两个问题。
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int coeff;
int pow;
struct Node *next;
};
// Function to create new node
void create_node(int x, int y, struct Node **temp)
{
struct Node *r = *temp;
if(r == NULL)
{
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
r->next = NULL;
*temp = r;
}
else
{
while (r->next != NULL)
r = r->next;
r = r->next = (struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
r->next = NULL;
}
}
// Function Adding two polynomial numbers
void polyadd(struct Node *poly1, struct Node *poly2, struct Node **poly3)
{
struct Node* poly;
while(poly1 && poly2)
{
// Dynamically create new node
if (*poly3 == NULL) {
poly = (struct Node *)malloc(sizeof(struct Node));
*poly3 = poly;
} else {
poly = poly->next = (struct Node *)malloc(sizeof(struct Node));
}
poly->next = NULL;
// If power of 1st polynomial is greater then 2nd, then store 1st as it is
// and move its pointer
if(poly1->pow > poly2->pow)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
// If power of 2nd polynomial is greater then 1st, then store 2nd as it is
// and move its pointer
else if(poly1->pow < poly2->pow)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
// If power of both polynomial numbers is same then add their coefficients
else
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff+poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
}
while(poly1 || poly2)
{
if (*poly3 == NULL) {
poly = (struct Node *)malloc(sizeof(struct Node));
*poly3 = poly;
} else {
poly = poly->next = (struct Node *)malloc(sizeof(struct Node));
}
poly->next = NULL;
if(poly1)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
}
}
void polySub(struct Node *poly1, struct Node *poly2, struct Node *poly)
{
}
// Display Linked list
void show(struct Node *node)
{
while(node != NULL)
{
if (node->coeff != 1 && node->pow != 0) {
printf("%dx^%d", node->coeff, node->pow);
} else if (node->coeff == 1 && node->pow != 0) {
printf("x^%d", node->pow);
} else if (node->coeff != 1 && node->pow == 0) {
printf("%d", node->coeff);
}
node = node->next;
if(node != NULL)
printf(" + ");
}
}
void detext_string(char *st, struct Node **poly) {
int coeff;
int pow;
while (*st != '\0') {
coeff = strtol(st, &st, 10);
if (coeff == 0)
coeff = 1;
if (*st == 'x') {
++st;
if (*st == '^') {
++st;
pow = strtol(st, &st, 10);
} else {
pow = 1;
}
} else {
pow = 0;
}
create_node(coeff, pow, poly);
}
}
void free_node(struct Node** p) {
struct Node* q = *p;
while (q != NULL) {
struct Node* temp = q;
q = q->next;
free(temp);
}
*p = NULL;
}
// Driver program
int main()
{
struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
char input1[1000], input2[1000];
while(scanf("%s %s", input1, input2) == 2)
{
detext_string(input1, &poly1);
detext_string(input2, &poly2);
printf("1st Number: ");
show(poly1);
printf("\n2nd Number: ");
show(poly2);
// Function add two polynomial numbers
polyadd(poly1, poly2, &poly);
// Display resultant List
printf("\nAdded polynomial: ");
show(poly);
printf("\n");
free_node(&poly1);
free_node(&poly2);
free_node(&poly);
}
return 0;
}