C Program porting to VC development environment

Source: Internet
Author: User
Tags abstract arithmetic function definition printf sprintf

Description

This paper is a summary of the work done by the author when learning computational methods. We rewrote Mr. Xu Shiliang's "C Common algorithm Assembly" (published by Tsinghua University Press)

The numerical calculation section-the first 15 chapters of all programs, and all under VC6 + Windows2000 debugging pass. There are two versions of the array class and Matrix class: Encapsulated into a template class, General class, I personally think the latter may be more practical, but the paper in the form of template class.

This article has been published on Www.vchelp.net, which allows authors to invest in other sites.

Summary

According to the features of C program, this paper introduces the technology of porting to VC integrated environment, implements large-scale rewriting to a common assembly, and provides C + + array and Matrix template class, and carries on object-oriented encapsulation to C program. 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.
keywords: transplanting; numerical calculation; encapsulation; template class Key words: Migration; Numerical Arithmetic, Encapsulation, Template Class  I. INTRODUCTION

Because C language has been widely used for a long time, there are a lot of practical C programs that have been rigorously tested, which can be used to solve practical problems in engineering applications. But the old C program often has a lot of incompatible with the modern compiler, so we have to according to the specific code conditions for the corresponding transplant processing.

This paper is to rewrite the C-Common algorithm assembly ("The Assembly") published by Tsinghua University Press for example, how to transplant the old C program to the common use of Visual C/C + + development environment. In addition to enumerating some of the methods and techniques of porting programs, this article also gives two C + + classes: Array classes and matrix template classes, to illustrate how to handle the object-oriented packaging of C programs.

Second, based on C language analysis and changing

As we know, Visual C + + supports ANSI C, where the source code affects compilation, incompatibility, and the corresponding solution, and gives a basic invocation example of functions based on ANSI C standards.

1, the function definition parameter declaration does not adopt the modern style, for example the total choice principal element Gauss elimination method: the int agaus(a,b,n)
int n;
double a[],b[];
{……;}
parameter declaration should change to the array form: int agaus(double a[],double b[],int n)  or change to the pointer form: int agaus(double* a,double* b,int n);  Call the method:agaus(&a[0][0],&b[0],n);
/* a二维双精度型数组、b一维双精度型数组,n整型变量 */

Both the subscript method and the pointer method can access an array, with array A, then a[i] and * (a+i) unconditionally equivalent. If the pointer variable p points to an element in the array, p+1 points to the next element of the same array. If the initial value of P is &a[0], then P+i and A+i are a[i] addresses; * (P+i) and * (A+i) are the array elements that p+i or a+i point to, that is, a[i], and pointer variables that point to the array can also be marked with the same value as * (P[i). So, in the actual use of this function, if you encounter an array as a parameter, you can call the first element address of the array as the argument value of the function.

2, the dynamic storage allocation function returns the void* pointer variable, which points to an abstract type of data, the ANSI C standard stipulates that it is assigned to another pointer variable needs to cast, so the following code LINE1 to replace with Line2: double* v;
v=malloc(n*m*sizeof(double));/* Line1 */
v=(double*)malloc(n*m*sizeof(double));
/* Line2 */
3, Some algorithm functions may want to invoke some user-defined functions, such as the best consistent approximation of the Rimiz method: The void hremz(a,b,p,n,eps)
int n;
double a,b,eps,p[];
{
  extern double hremzf();
  …
}
original method to make the assembly and application coupling to increase the degree of lack of flexibility, can be changed to: void hremz(double a,double b,double p[],int n,double eps,double (*hremzf)(double x))
{…}
using function pointers as parameters, the call directly to the function of the real parameters: Hremz (a,b, P,4,EPS,HREMZF); /* Assume that each parameter is defined in the main program file * * *

4, sometimes need to be some function of the console output as a string value returned, such as:

printf("%c",xy[i][j]);We can use sprintf( buffer,"%c",xy[i][j]),
    strcat( str, buffer );
the shape of the merge statement (where STR is a large enough string array parameter) instead of printf("%c",xy[i][j]); , for example: char* buffer;
    buffer =(char*)malloc(n*sizeof(char)); /*n作为参数传递,例如100 */
    sprintf( buffer,"%c",xy[i][j]),
    strcat( str, buffer );
    /*把终端输出字符添加到str 串尾*/
    ......
    free(buffer)
If you use them, call the method with a random sample analysis as an example:

char str[1024];
    str[0]=''\0'';/*初始化为空串*/
    irhis(x,100,x0,h,10,1,&dt[0],&g[0],&q[0],str);

The STR array now holds the terminal output text and can be used at will, such as in the console program:

puts(str);

When using the MFC class Library, STR can be assigned directly to an instance of a CString object. With the above work, we get a version of the program based on ANSI C standard, which can be used in C and C + + development environments.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.