求帮助纠错 ntdll出错

creepyOne 2013-06-19 05:33:35
头文件
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;

class Polynomial {

friend istream& operator>>(istream& in, Polynomial& v);
friend ostream& operator<<(ostream& out, const Polynomial& v);
friend void show(Polynomial* p);

public:
Polynomial();
Polynomial( const Polynomial& cp );
Polynomial( const char *up);
Polynomial operator+( const Polynomial& p ) const;
Polynomial operator-( const Polynomial& p) const;
Polynomial operator*( const Polynomial& p);
Polynomial& operator+=( const Polynomial& e);
Polynomial& operator-=( const Polynomial& e);
Polynomial& operator*=( const Polynomial& e);
int operator[]( int i) const;
int& operator[]( int j);
int operator()( int x );

private:
int coefficients_[10];
};

#endif

方法

#include <cmath>
#include <cstring>

using namespace std;

//insert method
istream& operator>>(istream& in, Polynomial& v){
Polynomial();
int i = 0;
for(;i < 10;i++){
in.get()>>v.coefficients_[i];
}//end for
return in;
}//end istream

//output method
ostream& operator<<(ostream& out,const Polynomial& v){
int i = 9;
for(;-1 < i;i--){
out<<v.coefficients_[i]<<"+";
}//end for
return out;
}//end ostream


//default constructor
Polynomial::Polynomial(){
int i = 0;
for(;i<10;i++){
coefficients_[i] = 0;
}//end for

}//end Polynomial

//copy constructor
Polynomial::Polynomial( const Polynomial& cp){
char* arr = new char [10];
for(int i=0;i<10;i++){
arr[i] = cp.coefficients_[i];
}//end for
}//end copy

//user constructor
Polynomial::Polynomial( const char*up){
char *po = new char [100];
char *temp = NULL;
int i,j,coef,power;

for(i = 0; i < 100; i++){
po[i] = up[i];
if(up[i] == '\0')
break;
}//copy the array.

for(j = 0; j < 100; j++){
*temp = up[j];

if( *temp == '-'){//negative item
if( up[j+1] == ' '){//negative item with space before number
if( up[j+3]== 'x' || up[j+4] == 'x'||up[j+5] == 'x'){
if(up[j+3] == 'x'){
coef = (-1)* (up[j+2]-'0');
power = up[j+4] - '0';
*temp = up[j+5];//move temp after readed item
}
if (up[j+4] == 'x'){
coef = (-1)*( (up[j+2]-'0')* 10 + (up[j+3]- '0'));
power = up[j+5] - '0';
*temp = up[j+6];
}
if(up[i+5] == 'x'){
coef = (-1)*( (up[j+2]-'0')* 100 + (up[j+3]- '0')* 10 + (up[j+4] - '0'));
power = up[i+6] - '0';
*temp = up[j+7];
}
coefficients_[power] = coef;

}

else if(up[j+3] ==' '||up[j+4] == ' '||up[j+5] == ' ')//negative item without x signal
{
power = 0;
if(up[j+3] == ' '){
coef = (-1) * (up[j+2]- '0');
*temp = up[j+4];//move temp after readed item
}
if(up[j+4] == ' '){
coef = (-1)*( (up[j+2]-'0')* 10 + (up[j+3]- '0'));
*temp = up[j+5];
}
if(up[j+5] == ' '){
coef = (-1)*( (up[j+2]-'0')* 100 + (up[j+3]- '0')* 10 + (up[j+4] - '0'));
*temp = up[j+6];
}
coefficients_[power] = coef;
}//end if else

}
if(up[j+2] == 'x' || up[j+3] == 'x'||up[j+4] == 'x'){//negative item with x signal

if(up[j+2] == 'x'){
coef = (-1)* (up[j+1]-'0');
power = up[j+3] - '0';
*temp = up[j+4];//move temp after readed item
}
if (up[j+3] == 'x'){
coef = (-1)*( (up[j+1]-'0')* 10 + (up[j+2]- '0'));
power = up[j+4] - '0';
*temp = up[j+5];
}
if(up[i+4] == 'x'){
coef = (-1)*( (up[j+1]-'0')* 100 + (up[j+2]- '0')* 10 + (up[j+3] - '0'));
power = up[i+5] - '0';
*temp = up[j+6];
}
coefficients_[power] = coef;

}//end if if
else if(up[j+2] ==' '||up[j+3] == ' '||up[j+4] == ' ')//negative item without x signal
{
power = 0;
if(up[j+2] == ' '){
coef = (-1) * (up[j+1]- '0');
*temp = up[j+3];//move temp after readed item
}
if(up[j+3] == ' '){
coef = (-1)*( (up[j+1]-'0')* 10 + (up[j+2]- '0'));
*temp = up[j+4];
}
if(up[j+4] == ' '){
coef = (-1)*( (up[j+1]-'0')* 100 + (up[j+2]- '0')* 10 + (up[j+3] - '0'));
*temp = up[j+5];
}
coefficients_[power] = coef;
}//end if else
}//end if
else{//positive item
if(up[j+2] == 'x' || up[j+3] == 'x'||up[j+4] == 'x'){//positive item with x signal

if(up[j+2] == 'x'){
coef = up[j+1]-'0';
power = up[j+3] - '0';
*temp = up[j+4];//move temp after readed item
}
if (up[j+3] == 'x'){
coef = (up[j+1]-'0')* 10 + (up[j+2]- '0');
power = up[j+4] - '0';
*temp = up[j+5];
}
if(up[i+4] == 'x'){
coef = (up[j+1]-'0')* 100 + (up[j+2]- '0')* 10 + (up[j+3] - '0');
power = up[i+5] - '0';
*temp = up[j+6];
}
coefficients_[power] = coef;

}//end else if
else if(up[j+2] ==' '||up[j+3] == ' '||up[j+4] == ' ')//item without x signal
{
power = 0;
if(up[j+2] == ' '){
coef = up[j+1]- '0';
*temp = up[j+3];//move temp after readed item
}
if(up[j+3] == ' '){
coef = (up[j+1]-'0')* 10 + (up[j+2]- '0');
*temp = up[j+4];
}
if(up[j+4] == ' '){
coef = (up[j+1]-'0')* 100 + (up[j+2]- '0')* 10 + (up[j+3] - '0');
*temp = up[j+5];
}
coefficients_[power] = coef;
}//end else if else if

if(*temp == '\0') {
break;
}//end if

}//end else if
}//end for loop
delete [] po;
delete temp;
}//end user constructor

//Polynomials add
Polynomial Polynomial::operator+(const Polynomial &p) const{
Polynomial temp;
int i;

for( i = 0; i < 10; i++){
temp.coefficients_[i] = coefficients_[i]+ p.coefficients_[i];
}//end for
return temp;
}//end operator+

//Polynomials substract
Polynomial Polynomial::operator-(const Polynomial &p) const{
Polynomial temp;
int i;

for( i = 0; i < 10; i++){
temp.coefficients_[i] =coefficients_[i] - p.coefficients_[i];
}//end for
return temp;
}//end operator-

//Polynomials multipaly
Polynomial Polynomial::operator*( const Polynomial& p){
Polynomial temp;
int i,j;

for(i = 0; i < 10; i++){
for(j = 0; j < 10; i++){
temp.coefficients_[i] = coefficients_[i] * p.coefficients_[j];
}//end j for
}//end i for
return temp;
}//end operator*

//modify polynomial add
Polynomial &Polynomial::operator+=( const Polynomial& e){
Polynomial temp;
int i;

for( i = 0; i < 10; i++){
temp.coefficients_[i] = coefficients_[i]+ e.coefficients_[i];
}//end for
return *this;
}//end +=

//modify polynomial substract
Polynomial &Polynomial::operator-=( const Polynomial& e){
Polynomial temp;
int i;

for( i = 0; i < 10; i++){
temp.coefficients_[i] =coefficients_[i] - e.coefficients_[i];
}//end for
return *this;
}//end -=

//modify polynomial multiply
Polynomial &Polynomial::operator*=( const Polynomial& e){
Polynomial temp;
int i,j;

for(i = 0; i < 10; i++){
for(j = 0; j < 10; i++){
temp.coefficients_[i] = coefficients_[i] * e.coefficients_[j];
}//end j for
}//end i for
return *this;
}//end *=

//set coefficient
int Polynomial::operator[]( int i ) const{
return coefficients_[i];
}//end set

//get coefficient
int& Polynomial::operator[]( int j){
return coefficients_[j];
}//end get

//evaluate the polynomial
int Polynomial::operator()( int x ){
int res=0;
for(int i=0;i<10; i++){
res += (int)(coefficients_[i] * pow((double)x,(double)i));
}
return res;
}//end evaluate

...全文
95 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
istream 已自行解决 求问 ostream以及user constructor写法
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
请指教友元函数 istream和ostream以及user constructor的写法 不胜感激
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
测试用main函数

#include "polynomial.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

void show(Polynomial* p) {
	cout << "Coefficients are: " << p->coefficients_[0];
	for (int i=1; i<10; i++) {
		cout << "," << p->coefficients_[i];
	}
	cout << endl;
}

int main()
{
	int i;

	cout << "Start of default constructor, Polynomial(), tests..." << endl;
	Polynomial *a;
	Polynomial b, c;
	a = new Polynomial();
	show(a);
	cout << "End of Polynomial() tests." << endl << endl;

	cout << "Start of user-defined constructor, Polynomial(const char*), tests..." << endl;
    	a = new Polynomial("-12 + 7x2 + 7x9 - 21x5 - 11x7");
	show(a);
	delete a;

	a = new Polynomial("-2x9 + 7x7 - 18x3 + 6x1 - 14");
	show(a);
	delete a;
	cout << "End of Polynomial(const char*) tests." << endl << endl;

	cout << "Start of >> and << tests..." << endl;
	cout << "Please enter: 3x9 - 6x7 - 18x3 + 6x1 + 14" << endl;
	cin >> b;
	cout << b << endl;
	cout << "Please enter: -4x7 + 12x4 + 9x1 - 9" << endl;
	cin >> c;
	cout << c << endl;
	cout << "End of >> and << tests." << endl << endl;

	cout << "Start of copy constructor tests..." << endl;
	Polynomial d(b);
	cout << d << endl; 
	Polynomial e(c); 
	cout << e << endl;
	cout << "End of copy tests...." << endl << endl;

	Polynomial f("3x9 - 6x7 - 18x3 + 6x1 + 14");
	Polynomial g("-2x9 + 7x7 - 18x3 + 6x2 - 11");
	Polynomial h("-4x7 + 12x4 + 9x1 - 9");

	cout << "Start of + tests..." << endl;
	e = f + g;
	cout << e << endl;
	e = f + h;
	cout << e << endl;
	cout << "End of + tests...." << endl << endl; 

	cout << "Start of - tests..." << endl;
  	e = f - g;
	cout << e << endl;
	e = f - h;
	cout << e << endl;
	cout << "End of - tests...." << endl << endl; 

	cout << "Start of * tests..." << endl;
	e = f * g;
	cout << e << endl;
	e = f * h;
	cout << e << endl;
	cout << "End of * tests...." << endl << endl;   
	 
	cout << "Start of += tests..." << endl;
	e = f;	
	e += g;	
	cout << e << endl;
	e = f;	
	e += h;	
	cout << e << endl;
    cout << "End of += tests...." << endl << endl; 

	cout << "Start of -= tests..." << endl;
	e = f;	
	e -= g;	
	cout << e << endl;
	e = f;	
	e -= h;	
	cout << e << endl;
	cout << "End of -= tests...." << endl << endl; 

	cout << "Start of *= tests..." << endl;
	e = f;	
	e *= g;	
	cout << e << endl;
	e = f;	
	e *= h;	
	cout << e << endl;
	cout << "End of *= tests...." << endl << endl;
      
	cout << "Start of [] tests..." << endl;
	for (i = 9; i >= 0; i--)
		cout << "e[" << setw(1) << i << "] = " << setw(1) << e[i] << "\t";
	cout << endl;
	for (i = 9; i >= 0; i--)
		e[i] = i;
	for (i = 9; i >= 0; i--)
		cout << "e[" << setw(1) << i << "] = " << setw(1) << e[i] << "\t";
	cout << endl;
	cout << "End of [] tests...." << endl << endl;
  
	cout << "Start of () tests..." << endl;
	cout << "Evaluation = " << e(1) << endl;
	cout << "Evaluation = " << e(4) << endl;
	cout << "End of () tests...." << endl;

	return 0;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧