PTA.7-1 一元多项式求导 (20 分)

#include
#include

using namespace std;

//整形n用于判断输入的是指数还是系数

typedef struct node
{
	int* b;
	int* t;
}*lnode;

void inl(lnode& l, int x)
{
	*(l->t) = x;
	l->t++;
}

void inl_(lnode& l, int x) 
{

	//求导
	*(--(l->t)) *= x;

    //存指数
	*(++(l->t)) = x-1;
	l->t++;
}

void outl(lnode& l)
{
	l->t--;
}

void print(lnode& l)
{
	int* temp = l->b;
	if (!(l->b == l->t))
	{
		cout << *temp;
		temp++;
		while (temp != l->t)
		{
			cout << " " << *temp;
			temp++;
		}
	}
    else  //及特殊情况,即求导为零,真该死啊  eg.输入 6 0
        cout<<"0 0"<b = new int[4000];
	l->t = l->b;
	int num, n = 1;
	while (cin >> num)
	{
		if (n % 2)
		{
				inl(l, num);//存系数
		}
		else
		{
			if (num == 0)  //因为指数是0,所以求导后系数也是零,直接出栈
				outl(l);  
			else
				inl_(l, num);//指数进栈并进行求导运算
		}
		n++;
	}
	print(l);
	return 0;
}

你可能感兴趣的:(c++,数据结构)