今天,我主要想分享编写多项式运算代码中的一些经验。
第一次分享,如有做得不好的地方,请大家多多包涵和多多指教。
程序的主要功能是读取txt文档中的数据(数据包括多项式的项数、系数你、指数),然后对这些数据进行多项式运算。运算规则有加、减、乘。
首先是两个多项式的截图:
接着是加法的运算结果:
接着就是代码:
以下为主程序代码:
// project7.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include <fstream>
#include <iostream>
#include "polynob.h"
#include <crtdbg.h>
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
void EnableMemLeakCheck()
{
int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(tmpFlag);
}
void main()
{
EnableMemLeakCheck();
{
Poly x;
Poly y;
char *p_path = "E:\\p3.txt";
char *q_path = "E:\\p4.txt";
x.readPoly(p_path);
printf("p=");
x.printPoly();
y.readPoly(q_path);
printf("q=");
y.printPoly();
char op = '+';
//char op = '-';
//char op = '*';
Poly *res = x.algorithm(y, op);
printf("The result is=");
res->printPoly();
delete res;
}
system("PAUSE");
}
接着是类的声明:
#include "stdafx.h"
#include <fstream>
#ifndef PLOYMENT
#define PLOYMENT
//类的声明
using namespace std;
class Poly
{
public:
Poly(); // 构造函数
~Poly(); // 析构函数
void initParams(int n,int *indices,double *coef);//初始化
void readPoly(char * p); //读取多项式
void printPoly();//输出多项式
void Simplification();//化简多项式
Poly * togetherPoly(const Poly &q);//合并多项式
Poly * addition(const Poly &q);//加法
Poly * Subtraction(const Poly &q);//减法
Poly * multiplication(const Poly &q);//乘法
Poly * algorithm( const Poly &q, char op);//运算规则
void clear();
private:
int m_num;
double *m_coefficient;
int *m_index;
int pri;
};
#endif
接着是类的定义:
#include "stdafx.h"
#include <fstream>
#include "polynob.h"
#include <iostream>
// CStudent 类的定义
Poly::Poly()
{
m_num = 0;
m_coefficient = NULL;
m_index = NULL;
pri=0;
}
Poly::~Poly()
{
//printf("hello\n");
clear();
}
void Poly::initParams(int n, int *indices, double *coef)
{
m_num = n;
m_index = indices;
m_coefficient = coef;
}
void Poly::readPoly(char * p)
{
std::ifstream f(p);
if (!f.is_open())
{
printf("Error to Read \n");
exit(0);
}
f >> m_num;
double *pa1 = new double[m_num];
int *pa2 = new int[m_num];
for (int i = 0; i < m_num; i++)
{
f >> pa1[i];
f >> pa2[i];
}
f.close();
m_coefficient = pa1;
m_index = pa2;
}
void Poly::printPoly()
{
printf("\n");
for (int j = 0; j < m_num; j++)
{
if ((m_coefficient[j]) != 0)
{
printf("(");
printf("%.4lf", m_coefficient[j]);
printf(")");
printf("*x^");
if (j < ((m_num)-1))
{
printf("%d+", m_index[j]);
}
else
{
printf("%d", m_index[j]);
}
}
}
pri++;
printf("\n");
}
void Poly::Simplification()
{
double chang1;
int chang2;
//排序
for (int i = 0; i < m_num; i++)
{
for (int j = i; j<m_num; j++)
{
if (m_index[i]>m_index[j])
{
chang2 = m_index[j]; m_index[j] = m_index[i]; m_index[i] = chang2;
chang1 = m_coefficient[j]; m_coefficient[j] = m_coefficient[i]; m_coefficient[i] = chang1;
}
}
}
int m = 0; //合并指数相同的项
for (int i = 0; i < m_num; i++)
{
m_coefficient[m] = m_coefficient[i];
m_index[m] = m_index[i];
for (int j = i + 1; j < m_num; j++)
{
if (m_index[j] == m_index[i])
{
m_coefficient[m] = m_coefficient[m] + m_coefficient[j];
i = j;
}
}
m++;
}
m_num = m;
}
Poly * Poly::togetherPoly(const Poly &q)
{
double *pa1 = new double[m_num + q.m_num];
int *pa2 = new int[m_num + q.m_num];
for (int j = 0; j < (m_num); j++)
{
pa1[j] = m_coefficient[j];
pa2[j] = m_index[j];
}
for (int j = 0; j < (q.m_num); j++)
{
pa1[j+m_num] = q.m_coefficient[j];
pa2[j + m_num] = q.m_index[j];
}
Poly *z = new Poly;
z->initParams(m_num + q.m_num, pa2, pa1);
return z;
}
Poly * Poly::addition(const Poly &q)
{
//Step1: Put p q together
Poly *res1 = Poly::togetherPoly(q);
//Step2: Simplification
res1->Simplification();
return res1;
}
Poly * Poly::Subtraction(const Poly &q)
{
//Step1: Put p q together
for (int i = 0; i < q.m_num; i++)
{
q.m_coefficient[i] = q.m_coefficient[i] * (-1);
}
Poly * res1 = Poly::togetherPoly(q);
//Step2: Simplification
res1->Simplification();
return res1;
}
Poly * Poly::multiplication(const Poly &q)
{
double *pa1 = new double[(m_num)*(q.m_num)];
int *pa2 = new int[(m_num)*(q.m_num)];
int m = 0;
for (int i = 0; i < m_num; i++)
{
for (int j = 0; j < q.m_num; j++)
{
pa2[m] = (m_index[i]) + (q.m_index[j]);
pa1[m] = (m_coefficient[i])*(q.m_coefficient[j]);
m++;
}
}
Poly *z = new Poly;
z->initParams(m_num *q.m_num, pa2, pa1);
z->Simplification();
return z;
}
Poly * Poly::algorithm(const Poly &q, char op)
{
if ('+' == op)
{
Poly *res = Poly::addition(q);
printf("\n");
return res;
}
else if ('-' == op)
{
Poly *res = Poly::Subtraction(q);
printf("\n");
return res;
}
else if ('*' == op)
{
Poly *res = Poly::multiplication(q);
printf("\n");
return res;
}
return NULL;
}
void Poly::clear()
{
if (m_coefficient != NULL)
{
delete[] m_coefficient;
}
if (m_index != NULL)
{
delete[] m_index;
}
m_num = 0;
m_coefficient = NULL;
m_index = NULL;
}