|
C程序移植到VC开发环境下 下载源代码 The Migration of Old C Code to Visual C++ IDE Abstract: According to the character of C programs, this paper presents some techniques to migrate them to Visual C++ IDE, as a implemention, it reprograms a set of numerical arithmetic programs for further engineering use.
关键词:移植;数值计算;封装;模板类Key words: Migration; Numerical Arithmetic, Encapsulation, Template Class 一、引言由于C语言长期广泛应用,现存有大量经过严格检验的实用C程序,它们可以用来很好地解决工程应用中的实际问题。但是旧的C程序往往有很多与现代编译器不兼容的地方,因此我们要根据具体的代码情况进行相应的移植处理。 本文以改写清华大学出版社出版的C常用算法程序集(以下简称"程序集")为例,说明如何将旧的C程序移植到目前普遍使用的C/C++开发环境Visual C++下。除了列举一些移植程序的方法和技巧,本文还给出两个C++类:数组类和矩阵模板类,以例示如何对C程序进行面向对象的包装处理。 二、基于C语言分析和改换我们知道,Visual C++支持ANSI C,下面列举源代码影响编译、不兼容的情况和相应解决方案,并给出基于ANSI C标准的函数的基本调用例子。 1、函数定义参数声明没有采用现代风格,例如全选主元高斯消去法: int agaus(a,b,n)
int n;
double a[],b[];
{……;}
参数声明应改为数组形式:
int agaus(double a[],double b[],int n)或者改为指针形式: int agaus(double* a,double* b,int n);调用方法: agaus(&a[0][0],&b[0],n); /* a二维双精度型数组、b一维双精度型数组,n整型变量 */C/C++中用下标法和指针法都可以访问一个数组,设有数组a,则a[i]和*(a+i)无条件等价。如果指针变量p指向数组中的一个元素,则p+1指向同一数组的下一个元素。若p的初值为&a[0],则p+i和a+I都是a[i]的地址;*(p+i)和*(a+i)就是p+i或a+i所指向的数组元素,即a[i];指向数组的指针变量也可以带下标,如p[i]与*(p+i)等价。所以,在实际使用该函数,如果遇到数组作形参,可以将数组第一个元素地址作为实参传值调用函数。 2、动态存储分配函数返回void*型指针变量,它指向一个抽象类型的数据,ANSI C标准规定在将它赋值给另一个指针变量时需要进行强制类型转换,所以下面代码Line1要用Line2替换: double* v; v=malloc(n*m*sizeof(double));/* Line1 */ v=(double*)malloc(n*m*sizeof(double)); /* Line2 */3、某些算法函数可能要调用一些用户自定义函数,如最佳一致逼近的里米兹方法: void hremz(a,b,p,n,eps)
int n;
double a,b,eps,p[];
{
extern double hremzf();
…
}
原方法使程序集与应用程序的耦合程度增加,缺乏灵活性,可以改为:
void hremz(double a,double b,double p[],int n,double eps,double (*hremzf)(double x))
{…}
用函数指针作参数,调用时直接将函数名作实参即可:
hremz(a,b,p,4,eps,hremzf);
/* 假设各参数在主程序文件已定义 */ 4、有的时候需要将一些函数的控制台输出作为字符串值返回,比如: printf("%c",xy[i][j]);
我们可以用形似
sprintf( buffer,"%c",xy[i][j]),
strcat( str, buffer );
的合并语句(其中str是一个足够大的字符串数组参数)代替 printf("%c",xy[i][j]);
例如:
char* buffer;
buffer =(char*)malloc(n*sizeof(char)); /*n作为参数传递,例如100 */
sprintf( buffer,"%c",xy[i][j]),
strcat( str, buffer );
/*把终端输出字符添加到str 串尾*/
......
free(buffer)
如果用到了它们,调用方法以随机样本分析为例:
char str[1024];
str[0]=''\0'';/*初始化为空串*/
irhis(x,100,x0,h,10,1,&dt[0],&g[0],&q[0],str);
现在str数组保存了终端输出文本,可以随意使用它,比如在控制台程序里输出:
puts(str);在使用MFC类库时,str可以直接赋值给一个CString对象的实例。 经过以上的工作,我们得到基于ANSI C标准的程序版本,可以在C和C++开发环境下使用。 三. 基于C++语言分析和改换由于C语言本身的弱点,程序集还存在的缺陷主要有
template <class T=double>
class TArray
{
protected:
T* pdata;
unsigned int length;
public:
TArray();
TArray(unsigned int);
TArray(TArray const&);
virtual ~TArray();
void operator = (TArray&);
TArray<T>& operator + (TArray&);
TArray<T>& operator - (TArray&);
T const& operator [] (unsigned int)const;
T& operator [](unsigned int);
T const* GetData() const;
unsigned int GetLenght();
void SetLength(unsigned int,bool=true);
};
template <class T=double>
class TMatrix
{
protected:
unsigned int numberOfRows;
unsigned int numberOfColumns;
TArray<T> array;
public:
class Row
{
TMatrix<T>& matrix;
unsigned int const row;
public:
Row (TMatrix<T>& _matrix,unsigned int _row):matrix(_matrix),row(_row){}
T& operator [](unsigned int column)const
{return matrix.Select(row,column);}
};
TMatrix();
TMatrix(unsigned int, unsigned int);
TMatrix(TMatrix<T>& mat);
virtual ~TMatrix();
T& Select(unsigned int, unsigned int);
Row & operator[](unsigned int);
TMatrix<T>& operator + (TMatrix<T>& mat);
TMatrix<T>& operator - (TMatrix<T>& mat);
TMatrix<T>& operator * (TMatrix<T>& mat);
bool operator == (TMatrix<T>& mat);
TArray<T>& GetData();
unsigned int GetNumberOfRows();
unsigned int GetNumberOfColumns();
bool LoadFromArray(T [],unsigned int,unsigned int);
bool LoadFromString(char*,char,char);
bool ResetMatrix(unsigned int, unsigned int);
bool ReverseMatrix();
void ZeroMatrix();
void RandomMatrix(int max);
};
举例说明我们关于异常机制和数组越界的检测方法的思路。TMatrix类的operator[]返回一个嵌套类Row的引用,它用来描述某一给定二维数组的一个特定行。Row类的operator[]则返回该行一个特定位置的T类型值。最终实现还是通过Matrix<T>::Select()函数,该函数体代码如下:
template <class T>
T& TMatrix<T>::Select(unsigned int i, unsigned int j)
{
char ch[50];
if(i>=numberOfRows)
sprintf(ch," Error -- Invalid row: %d",i), throw (ch);
if(j>=numberOfColumns)
sprintf(ch," Error -- Invalid colum: %d",j),
throw (ch);
return array[i*numberOfColumns+j];
}
应用程序实例:
unsigned int i,j;
unsigned int m=2,n=3;
TMatrix< > mat(m,n);//双精度型矩阵
try
{
for(i=0; i<m; i++)
{
for(j=0; j<n+1; j++)// Line3
cout<<mat[i][j]<<"\t";
cout<<endl;
}
}
catch(char* str) //捕获异常
{cout<<str<<endl;}
终端输出如下(注:类实例mat没有初始化):
-6.27744e+066 -6.27744e+066 -6.27744e+066 Error -- Invalid colum: 3只输出一行,根据出错提示,把Line3改为:"for(j=0; j<n; j++)",重新编译运行,输出2行3列的正确结果: -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066由于我们对operator[]进行了重载,所以数组模板类(矩阵模板类)完全兼容C/C++一维数组(二维数组)的存取操作,因此旧程序中数组变量直接可以用类实例变量替代。 图一是我们用Visual C++ 6开发的演示程序界面,左边是所有算法的目录树,右边是文本计算结果输出,下部悬浮窗口是算法程序源代码,可以拷贝粘贴,稍作修改即可重用。 ![]() 图一
四、结论新的程序与原程序相比较的优点:
五、参考文献
|
背景:
阅读新闻
VC知识库文章 - C程序移植到VC开发环境下
| [日期:2005-12-29] | 作者: | [字体:大 中 小] |
阅读: 次
【 打印 】
【 打印 】
全站导航
说明
gmail.com