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

#include "term.h"



double poly_evaluate(struct term *poly, double x) {
    // TODO: implement this function
    (void) poly;
    (void) x;
    return 0;
}

struct term *poly_add(struct term *poly1, struct term *poly2) {
    // TODO: implement this function
    (void) poly1;
    (void) poly2;
    return NULL;
}


static struct term *read_poly(void) {
    struct term *head = NULL;
    struct term *tail = NULL;
    char tok[50];
    while (scanf("%49s", tok) == 1 && strcmp(tok, ",") != 0) {
        int exp;
        if (scanf("%d", &exp) != 1) { break; }
        struct term *t = malloc(sizeof(struct term));
        t->coefficient = atoi(tok);
        t->exponent = exp;
        t->next = NULL;
        if (tail == NULL) { head = t; } else { tail->next = t; }
        tail = t;
    }
    return head;
}

int main(void) {
    double x;
    if (scanf("%lf", &x) != 1) { x = 0; }
    struct term *poly1 = read_poly();
    struct term *poly2 = read_poly();
    printf("%g\n", poly_evaluate(poly1, x));
    struct term *sum = poly_add(poly1, poly2);
    if (sum == NULL) {
        printf("0 (the zero polynomial)\n");
    } else {
        for (struct term *t = sum; t != NULL; t = t->next) {
            printf("%dx^%d", t->coefficient, t->exponent);
            if (t->next != NULL) { printf(" + "); }
        }
        printf("\n");
    }
    return 0;
}
