求帮助纠错 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

...全文
146 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;
}
内容概要:本文系统研究了基于雅可比伪逆迭代的双臂协同轨迹规划与跟踪误差收敛性问题,结合机械臂正向与逆向运动学建模、蒙特卡洛法分析工作空间、笛卡尔空间内的圆弧轨迹生成等关键技术,利用Matlab实现了双臂系统的协同控制与轨迹跟踪仿真。重点探讨了雅可比矩阵伪逆法在解决冗余自由度机械臂逆运动学问题中的应用,深入分析了轨迹跟踪过程中误差的收敛特性,并通过仿真实验验证了该方法在复杂任务场景下的有效性、稳定性与鲁棒性,为多臂机器人在工业自动化、精密操作等领域的协同作业提供了理论依据与技术支持。; 适合人群:具备机器人学、自动控制理论基础及Matlab编程能力的科研人员与工程技术人员,特别适合从事机械臂控制、轨迹规划、多机器人协同等方向的硕士、博士研究生及相关领域研究人员。; 使用场景及目标:①掌握基于雅可比伪逆法的机械臂逆运动学求解原理与实现方法;②理解双臂系统在笛卡尔空间中协同轨迹规划的机制;③分析并优化轨迹跟踪过程中的误差收敛性能;④为多机器人系统在复杂动态环境下的高精度协同控制提供算法设计与仿真验证支持。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,按照文档目录循序渐进地学习,重点关注算法实现细节、仿真参数设置与结果分析部分,同时可进一步探索将智能优化算法或深度学习方法引入轨迹规划与误差补偿的研究方向。

65,214

社区成员

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

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