首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++拆分解决方案

2013-02-05 
C++拆分请教大家,一个比较大的C++程序,怎么拆分成一个主程序和几个子程序,运行主程序就能全部运行,主程序

C++拆分
请教大家,一个比较大的C++程序,怎么拆分成一个主程序和几个子程序,运行主程序就能全部运行,主程序中的头文件和调用子程序的语句分别是什么


你说的这些文件就是这样拆分的:
---------------------------------文件Test.h----------------------------------

//Test.h
#ifndef TETST_H
#define TETST_H

#include<iostream>                                   
#include<fstream>                                      //for files
#include<stdio.h>
using namespace std;

void Square(int n, double dx, double a, double b, double x[], double y[]);
void Time_output(ofstream &outdata, double tp, double dt, int N);
double Diffusion(int i, double dx, double y[], double rho[], double D[]);
void Intermediate_output(int n, double x[], double y[], int N);
void Output(int n, double x[], double y[]);

#endif//TETST_H

--------------------------------文件Test.cpp------------------------
//Test.cpp
#include "stdafx.h"
#include "Test.h"

void Square(int n, double dx, double a, double b, double x[], double y[])
{
int i;
for(i=0; i<n; i++)
{
x[i]=(i+0.5)*dx;                          //define initial x value for each cell, mid-point
if((i>=a)&&(i<b))                         //define initial y value for each cell   
y[i]=1.0;                               
else
y[i]=0.0;                             
}
}

void Time_output(ofstream &outdata, double tp, double dt, int N)
{                      
if(outdata.is_open())                                          //check if the file is open
outdata<<N<<"  "<<dt<<"  "<<tp<<endl;                          //write the value of n, dt and tp to file


else                                                           //if _open() return falsch
cout<<N<<"  "<<dt<<"  "<<tp<<endl;                             //write the value of n, dt and tp to screen
}

double Diffusion(int i, double dx, double y[], double rho[], double D[])
{
if(1)                                                                //switch on diffusion?
{
double yW, yC, yE, rhoW, rhoC, rhoE, dW, dC, dE, phiW, phiC, phiE, phiw, phie, rhodw, rhode;
yW=y[i-1]; yC=y[i]; yE=y[i+1];                               //y at W, C and E
rhoW=rho[i-1]; rhoC=rho[i]; rhoE=rho[i+1];                   //density at W, C and E
dW=D[i-1]; dC=D[i]; dE=D[i+1];                               //diffusivity at W, C and E
phiW=yW/rhoW; phiC=yC/rhoC; phiE=yE/rhoE;                    //y divided by density to get phi
phiw=phiC-phiW; phie=phiE-phiC;                              //dleta phi
rhodw=(rhoW*dW+rhoC*dC)/2; rhode=(rhoE*dE+rhoC*dC)/2;        //density*diffusivity at w and e
return((rhode*phie-rhodw*phiw)/(dx*dx));                     //diffusion term
}
else                                                       
return(0);                                                           //no diffusion effect
}

void Intermediate_output(int n, double x[], double y[], int N)
{
int i;
char filename[100];                                          //define for the filename space


sprintf(filename, "%d.dat", N);                              //give the name to each file by Nth time step
ofstream outdata;                                            //libray for output                     
outdata.open(filename, ios::out);                            //open the file
if(outdata.is_open())                                        //check if the file is open
{
for(i=0; i<n; i++)                                   //for each point
{
outdata<<x[i]<<"  "<<y[i]<<endl;            //write the value of x and y to file
}
outdata.close();                                              //close the file
}
else                                                          //if _open() return falsch
{
for(i=0; i<n; i++)
{
cout<<x[i]<<"  "<<y[i]<<endl;               //write the value of x and y to screen
}
}


void Output(int n, double x[], double y[])
{
int i;
ofstream outdata;                                            //libray for output                     
outdata.open("output.dat");                                  //open the file
if(outdata.is_open())                                        //check if the file is open
{
for(i=0; i<n; i++)                                  //for each point


{
outdata<<x[i]<<"  "<<y[i]<<endl;           //write the value of x and y to file
}
outdata.close();                                             //close the file
}
else                                                         //if _open() return falsch
{
for(i=0; i<n; i++)
{
cout<<x[i]<<"  "<<y[i]<<endl;              //write the value of x and y to screen
}
}
}



------------------------------------主程序文件main.cpp-------------------
// Test_cons.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Test.h"

int main()
{
double l=1.0;                                      //total length 
double tpmax=1.0;                                  //total time 
double dt=1.0e-6;                                  //time step interval
const int n=100;                                   //define cell number
const int w=10000;                                 //define interval of intermediate output 
double dx, a, b, tp;
double x[n], y[n], rho[n], D[n], RHS[n], ybuffer[n];
dx=l/n;                                            //calculate the length of each cell
a=0.3*n;                                           //define the critical point
b=0.7*n;                                           //define the critical point
int i, N;
for(i=0; i<n; i++)                                 //define the density and diffusivity values of air


{
rho[i]=1.225;
D[i]=0.00002;
}

Square(n, dx, a, b, x, y);                                    //switch on square to initialize x and y values

ofstream outdata;                                             //libray for output                     
outdata.open("time_output.dat");                              //open the time output file

for(tp=0.0; tp<tpmax; tp=tp+dt)
{
N=int(tp/dt);                                                           //calculate the time step number
Time_output(outdata, tp, dt, N);                                        //switch on time output file
for(i=1; i<(n-1); i++) 
{
RHS[i]=Diffusion(i, dx, y, rho, D);                            //add up RHS terms                                 
ybuffer[i]=y[i]+dt*RHS[i];                                     //explicit time integration to get y
y[i]=ybuffer[i];
}
if(N%w==0)                                                              //condition for intermediate x and y value output
Intermediate_output(n, x, y, N);                                        //intermediate results output in file
}

outdata.close();                                                 //close the time output file


Output(n, x, y);                                                 //final results output in file

return 0;                                                        // finish
}

热点排行