#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
const int maxn=1e4+5;
struct Node
{
int temp[maxn];
int summary=0;
};
struct Node a,b;
int true_or_false;
void Add(){
printf("C(x)=");
Node s;
int flag=0;
int ma = max(a.summary,b.summary);
for(int i=0;i<maxn;i++){
s.temp[i]=0;
}
for(int i=0;i<=ma;i++){
s.temp[i]=a.temp[i]+b.temp[i];
}
for(int i=0;i<=ma;i++){
if(s.temp[i]!=0){
flag=1;
break;;
}
}
if(!flag)printf("0\n");
else{
true_or_false=0;
for(int i=0;i<=ma;i++){
if(s.temp[i]!=0) {
if (true_or_false == 0) {
true_or_false = 1;
if (s.temp[i] < 0) {
if (i == 0)printf("%d", s.temp[i]);
else if (i == 1)printf("%dx", s.temp[i]);
else {
if (s.temp[i] == -1) {
printf("-");
printf("x^%d", i);
}
else printf("%dx^%d", s.temp[i], i);
}
}
else {
if (i == 0)printf("%d", s.temp[i]);
else if (i == 1)printf("%dx", s.temp[i]);
else {
if (s.temp[i] == 1)printf("x^%d", i);
else printf("%dx^%d", s.temp[i], i);
}
}
}
else{
if(s.temp[i]<0){
if(i==1)printf("%dx",s.temp[i]);
else{
if(s.temp[i]==-1){
printf("-");
printf("x^%d",i);
}
else printf("%dx^%d",s.temp[i],i);
}
}
else{
if(i==1){
printf("+");
printf("%dx",s.temp[i]);
}
else{
if(s.temp[i]==1){
printf("+");
printf("x^%d",i);
}
else{
printf("+");
printf("%dx^%d",s.temp[i],i);
}
}
}
}
}
}
printf("\n");
}
}
void Sub(){
printf("C(x)=");
Node s;
int flag=0;
int ma = max(a.summary,b.summary);
for(int i=0;i<maxn;i++){
s.temp[i]=0;
}
for(int i=0;i<=ma;i++)s.temp[i]=a.temp[i]-b.temp[i];
for(int i=0;i<=ma;i++){
if(s.temp[i]!=0){
flag=1;
break;
}
}
if(flag==0)printf("0");
else{
true_or_false=0;
for(int i=0;i<=ma;i++){
if(s.temp[i]!=0){
if(true_or_false==0){
true_or_false=1;
if(s.temp[i]<0){
if(i==0)printf("%d",s.temp[i]);
else if(i==1)printf("%dx",s.temp[i]);
else{
if(s.temp[i]==-1) {
printf("-");
printf("x^%d", i);
}
else printf("%dx^%d",s.temp[i],i);
}
}
else{
if(i==0)printf("%d",s.temp[i]);
else if(i==1)printf("%dx",s.temp[i]);
else{
if(s.temp[i]==1)printf("x^%d",i);
else printf("%d^%d",s.temp[i],i);
}
}
}
else{
if(s.temp[i]<0){
if(i==1){
printf("%dx",s.temp[i]);
}
else{
if(s.temp[i]==-1){
printf("-");
printf("x^%d",i);
}
else{
printf("%dx^%d",s.temp[i],i);
}
}
}
else{
printf("+");
if(s.temp[i]==1){
printf("x^%d",i);
}
else{
if(s.temp[i]==1)printf("x^%d",i);
else{
printf("%dx^%d",s.temp[i],i);
}
}
}
}
}
}
printf("\n");
}
}
void Muti(){
printf("C(x)=");
Node s;
int flag=0;
int muti=a.summary*b.summary;
for(int i=0;i<maxn;i++){
s.temp[i]=0;
}
for(int i=0;i<=a.summary;i++){
for(int j=0;j<=b.summary;j++){
s.temp[i+j]+=a.temp[i]*b.temp[j];
}
}
for(int i=0;i<=muti;i++){
if(s.temp[i]!=0){
flag=1;
break;
}
}
if(!flag)printf("0");
else{
true_or_false=0;
for(int i=0;i<=muti;i++){
if(s.temp[i]!=0){
if(true_or_false==0){
true_or_false=1;
if(s.temp[i]<0){
if(i==0)printf("%d",s.temp[i]);
else if(i==1){
printf("%dx",s.temp[i]);
}
else{
if(s.temp[i]==-1){
printf("-");
printf("x^%d",i);
}
else printf("%dx^%d",s.temp[i],i);
}
}
else{
if(i==0)printf("%d",s.temp[i]);
else if(i==1)printf("%dx",s.temp[i]);
else{
if(s.temp[i]==1)printf("x^%d",i);
else{
printf("%dx^%d",s.temp[i],i);
}
}
}
}
else{
if(s.temp[i]<0) {
if (i == 1)printf("%dx", s.temp[i]);
else {
if (s.temp[i] == -1) {
printf("-");
printf("x^%d", i);
} else {
printf("%dx^%d", s.temp[i], i);
}
}
}
else{
if(i==1){
printf("+");
printf("%dx",s.temp[i]);
}
else{
if(s.temp[i]==1){
printf("+");
printf("x^%d",i);
}
else{
printf("+");
printf("%dx^%d",s.temp[i],i);
}
}
}
}
}
}
printf("\n");
}
}
void Daoshu(){
printf("C(x)=");
Node s;
int flag=0;
for(int i=0;i<maxn;i++){
s.temp[i]=0;
}
for(int i=0;i<a.summary;i++){
int j=i+1;
s.temp[i]=j*a.temp[j];
}
for(int i=0;i<a.summary;i++){
if(s.temp[i]!=0){
flag=1;
break;
}
}
if(!flag)printf("0");
else {
true_or_false = 0;
for (int i = 0; i <a.summary; i++) {
if (s.temp[i] != 0) {
if (true_or_false == 0) {
true_or_false = 1;
if (s.temp[i] < 0) {
if (i == 0)printf("%d", s.temp[i]);
else if (i == 1) {
printf("%dx", s.temp[i]);
} else {
if (s.temp[i] == -1) {
printf("-");
printf("x^%d", i);
} else printf("%dx^%d", s.temp[i], i);
}
} else {
if (i == 0)printf("%d", s.temp[i]);
else if (i == 1)printf("%dx", s.temp[i]);
else {
if (s.temp[i] == 1)printf("x^%d", i);
else {
printf("%dx^%d", s.temp[i], i);
}
}
}
} else {
if (s.temp[i] < 0) {
if (i == 1)printf("%dx", s.temp[i]);
else {
if (s.temp[i] == -1) {
printf("-");
printf("x^%d", i);
} else {
printf("%dx^%d", s.temp[i], i);
}
}
} else {
if (i == 1) {
printf("+");
printf("%dx", s.temp[i]);
} else {
if (s.temp[i] == 1) {
printf("+");
printf("x^%d", i);
} else {
printf("+");
printf("%dx^%d", s.temp[i], i);
}
}
}
}
}
}
printf("\n");
}
}
void Qiu(){
float x;
float ans=0;
float tem=1;
cin>>x;
for(int i=0;i<=a.summary;i++){
if(i!=0)tem=tem*x;
ans+=a.temp[i]*tem;
}
printf("%.2f\n",ans);
}
int main()
{
char ch;
int lef,rig;
a.summary=b.summary=0;
for(int i=0;i<maxn;i++){
a.temp[i]=b.temp[i]=0;
}
cin>>ch;
while(cin>>lef){
if(lef==0)break;
else {
cin>>rig;
a.temp[rig]=lef;
a.summary=max(rig,a.summary);
}
}
while(cin>>lef){
if(lef==0)break;
else {
cin>>rig;
b.temp[rig]=lef;
b.summary=max(rig,a.summary);
}
}
while(cin>>ch){
if(ch=='X')break;
else if(ch=='C'){
}
else if(ch=='P'){
Add();
}
else if(ch=='S'){
Sub();
}
else if(ch=='M'){
Muti();
}
else if(ch=='D'){
Daoshu();
}
else if(ch=='V'){
Qiu();
}
}
return 0;
}
中国石油大学华东数据结构实验二
最新推荐文章于 2022-10-31 23:37:40 发布