题目
计算多项式乘法,按照x的递减,y的递增序输出。
分析
模拟。这里分成三个部分:1.将字符串分解成不同的单元(axmyn为一个单元)构成的多项式;2.计算乘法,并且排序,合并同类项;3.按照特定格式输出。
说明
注意系数和指数为0的情况。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int _max_item_size = 1000;
typedef struct _item
{
int number;
int x_power;
int y_power;
_item *next;
}item;
item Item[_max_item_size];
int item_size = 0;
item* get_item(char buf[], int &index)
{
if (!buf[index]) return NULL;
int sign = 1;
if (buf[index] == '-') {
sign = -1;
index ++;
} else if (buf[index] == '+')
index ++;
Item[item_size].number = 0;
while (buf[index] >= '0' && buf[index] <= '9')
Item[item_size].number = Item[item_size].number*10 + buf[index ++] - '0';
Item[item_size].number = Item[item_size].number * sign;
Item[item_size].x_power = 0;
Item[item_size].y_power = 0;
for (int k = 0; k < 2; ++ k) {
if (buf[index] == 'x' || buf[index] == 'y') {
char ch = buf[index ++];
int number = 0;
while (buf[index] >= '0' && buf[index] <= '9')
number = number*10 + buf[index ++] - '0';
if (number == 0 && buf[index-1] != '0')
number = 1;
if (ch == 'x')
Item[item_size].x_power = number;
else if (ch == 'y')
Item[item_size].y_power = number;
} else break;
if (Item[item_size].number == 0)
Item[item_size].number = 1 * sign;
}
Item[item_size].next = NULL;
return &Item[item_size ++];
}
typedef struct _polynomial
{
item *item_list;
int size;
}polynomial;
polynomial Polynomial[3];
int polynomial_size;
polynomial* get_polynomial(char buf[])
{
int index = 0;
polynomial* pol = &Polynomial[polynomial_size ++];
pol->size = 0;
pol->item_list = NULL;
item *pi = NULL;
while (pi = get_item(buf, index)) {
pi->next = pol->item_list;
pol->item_list = pi;
pol->size ++;
}
return pol;
}
void swap(int &a, int &b)
{
int c = a;
a = b;
b = c;
}
polynomial* mul_polynomial(polynomial* A, polynomial* B)
{
polynomial* pol = &Polynomial[polynomial_size ++];
pol->size = 0;
pol->item_list = NULL;
for (item* pi = A->item_list; pi; pi = pi->next)
for (item* qi = B->item_list; qi; qi = qi->next) {
Item[item_size].number = pi->number * qi->number;
Item[item_size].x_power = pi->x_power + qi->x_power;
Item[item_size].y_power = pi->y_power + qi->y_power;
Item[item_size].next = pol->item_list;
pol->item_list = &Item[item_size ++];
pol->size ++;
}
// sort
for (item* pi = pol->item_list; pi; pi = pi->next)
for (item* qi = pi->next; qi; qi = qi->next)
if (pi->next && (pi->x_power < qi->x_power
|| (pi->x_power == qi->x_power
&& pi->y_power > qi->y_power))) {
swap(pi->number, qi->number);
swap(pi->x_power, qi->x_power);
swap(pi->y_power, qi->y_power);
}
// combine
for (item* pi = pol->item_list; pi; pi = pi->next)
if (pi->next && pi->x_power == pi->next->x_power
&& pi->y_power == pi->next->y_power) {
pi->number += pi->next->number;
pi->next = pi->next->next;
}
return pol;
}
char save_number[81];
void insert_number(int number, char buf[], int &index)
{
int count = 0;
while (number) {
save_number[count ++] = number%10 + '0';
number /= 10;
}
while (count > 0)
buf[index ++] = save_number[-- count];
}
void output_polynomial(polynomial* A)
{
char line1[81], line2[81];
for (int i = 0; i < 80; ++ i)
line1[i] = line2[i] = ' ';
int index = 0, number = 0;
for (item* pi = A->item_list; pi; pi = pi->next) {
if (!pi->number)
continue;
number = pi->number;
if (pi != A->item_list)
line2[index ++] = ' ';
if (number < 0) {
line2[index ++] = '-';
number = number*-1;
} else if (pi != A->item_list)
line2[index ++] = '+';
if (pi != A->item_list)
line2[index ++] = ' ';
if (number == 1) {
if (pi->x_power == 0 && pi->y_power == 0)
line2[index ++] = '1';
} else
insert_number(number, line2, index);
if (pi->x_power > 0) {
line2[index ++] = 'x';
if (pi->x_power != 1)
insert_number(pi->x_power, line1, index);
}
if (pi->y_power > 0) {
line2[index ++] = 'y';
if (pi->y_power != 1)
insert_number(pi->y_power, line1, index);
}
}
if (index == 0) {
line1[index] = 0;
line2[index ++] = '0';
}
line1[index] = line2[index] = 0;
puts(line1);
puts(line2);
}
int main()
{
char buf[256];
while (~scanf("%s", buf) && strcmp(buf, "#")) {
item_size = 0;
polynomial_size = 0;
polynomial* A = get_polynomial(buf);
scanf("%s", buf);
polynomial* B = get_polynomial(buf);
polynomial* C = mul_polynomial(A, B);
output_polynomial(C);
}
return 0;
}