开发者

Conversion of Fortran to C++ [closed]

开发者 https://www.devze.com 2023-02-23 00:43 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I want to write an object oriented program. I do not know much about it. It looks for me that it is somewhat similar to subroutine in Fortran. I have created a sample program below. May you help me out translating it into C++ codes please? c Program in Fortran to calculate area and perimeter of a rectangle

implicit double precision(a-h,o-z), integer(i-n)
dimension a(10),p(10)
xl = 0.0
xb = 0.0
do 10 ix = 1,10
   call area(xl,xb,a)
   call perimeter(xl,xb,p)
   write(*,*) ix,a(ix),p(ix)
   xl = xl + 1.0
   xb = xb + 1.0
  10    continue
end

subroutine area(xx,yy,ara)
implicit double precision (a-h,o-z),integer(i-n)
dimension ara(10)
do 40 j = 1,10
     ara(j) = xl*xb
    40     continue
return
end

subroutine perimeter(xl,xb,per)
implicit double precision (a-h,o-z),integer(i-n)
dimension per(10)
do 开发者_JAVA技巧50 i=1,10
   per(i) = 2*(xl+xb)
    50  continue
return
end

Thank you.


I'm not into Fortran, but this looks like what you are getting at (If it's not what you wanted, please clarify):

#include<vector> // allows you to use the C++ STL std::vector (think of it as a better array)

// calculates the area of a rectangle
double rectangle_area( const double height, const double width )
{
    return height*width;
}
// calculates the perimeter of a rectangle
double rectangle_perimeter( const double height, const double width )
{
    return 2*height+2*width;

int main()
{
    const int N = 10; // defined a constant integer
    std::vector<double> area( N ); // creates a vector of double precision floats of size N
    std::vector<double> perimeter( N ); // idem

    double width = 0.;
    double height = 0.;

    for( int i = 0; i < 10; ++i ) // loop over i from 1 to 10, incrementing (++i) after each iteration
    {
        area[i] = rectangle_area( width, height );
        perimeter[i] = rectangle_perimeter( with, height );
        width += 1.; // same thing as 'width = width + 1' or width++;
        height += 1.; // idem
    }        

    return 0;
}

Note that this does not output anything to the screen. To output a number or string in C++, you'll need to #include <iostream> and use the following syntax:

std::cout << variable_you_want_to_output << std::endl;

The std::endl inserts a newline and flushes the output stream.

0

精彩评论

暂无评论...
验证码 换一张
取 消