| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 291 人关注过本帖
标题:有关main函数中提取结构体中的元素
只看楼主 加入收藏
huaijuliu
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2009-9-17
收藏
 问题点数:0 回复次数:1 
有关main函数中提取结构体中的元素
大家好,新手向各位前辈问好。有一个简单程序,我不知道为什么运行出错,但我基本可以断定是main函数中有关U和L的问题,希望得到广大前辈的帮助。谢谢大家。
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


/* FMG Solver of the EHL circular contact */
#define pi 3.1415926535897931

typedef struct
{
double hx;
int ii;
double *p, *f;
double *hfi, *hrhs;
double *w;
double *K;
double *pjac;  /*old pressure for use in Jacobi relaxation*/
double rg;
double Hm;
} Level;


typedef struct
{
int nx0;
int maxlevel;
double xa,xb;
double h0;
Level *Lk;
} Stack;

void initialize(Stack *U, int nx0, int maxl, double xa, double xb, double h0)
{
/* initialize values in datastructure */
double hx;
Level *L;
int l,ii;
U->xa=xa;
U->xb=xb;
U->maxlevel=maxl;
U->h0=h0;
U->Lk=(Level *)calloc(maxl+1,sizeof(Level));
hx=(xb-xa)/nx0;
ii=nx0;
for (l=1; l<=maxl; l++)
{
L=U->Lk+l;
L->hx=hx;
L->ii=ii;
L->p =(double *)calloc(U->maxlevel+1,sizeof(double));
L->w =(double *)calloc(U->maxlevel+1,sizeof(double));
L->f =(double *)calloc(U->maxlevel+1,sizeof(double));
L->pjac =(double *)calloc(U->maxlevel+1,sizeof(double));
L->hfi =(double *)calloc(U->maxlevel+1,sizeof(double));
L->hrhs=(double *)calloc(U->maxlevel+1,sizeof(double));
L->K =(double *)calloc(U->maxlevel+1,sizeof(double));
printf("\n level: %2d ii=%4d, hx=%f",l,ii,hx);
hx*=0.5; ii*=2;
}
}

void init_p(Stack *U, int lev)
{
int i;
Level *L;
double x;
double *p;
L =U->Lk+lev;
p=L->p;
for (i=0; i<=L->ii; i++)
{
x=U->xa+i*L->hx; p[i]=0.0;
if (x*x<1.0) p[i]=sqrt(1.0-x*x);
}
FILE *fp;
fp=fopen("P1.dat","w");
for (i=0; i<=L->ii; i++)
{
x=U->xa+i*L->hx;

 fprintf(fp,"%f %f\n",x,L->p[i]); 
}
fprintf (fp, "\n");
fclose(fp);
}

void relax(Stack *U, int lev)
{
int i;
Level *L;
double *p;
L =U->Lk+lev;
p =L->p;
for (i=0; i<=L->ii; i++)  p[i]=p[i]+1;
}

void main()
{
Stack U;
int i;
double x;
Level *L;
initialize(&U,128,1,-4.5,1.5,1);
L=U->Lk+1;
init_p(&U, 1);
relax(&U, 1);
FILE *fp2;
fp2=fopen("P2.dat","w");
for (i=0; i<=L->ii; i++)
{
x=U->xa+i*L->hx;

 fprintf(fp2,"%f %f\n",x,L->p[i]); 
}
fprintf (fp2, "\n");
fclose(fp2);
}

搜索更多相关主题的帖子: 结构体 元素 
2011-06-19 05:06
huaijuliu
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2009-9-17
收藏
得分:0 
不劳各位了 问题我已经找到 在main函数中应写为U.而不是U->
那既然如此,为什么在其它函数中,如void init_p(Stack *U, int lev)这时候U之前要加星号呢?真的刚开始接触,查了几天还没有太明白。感谢大家指点。
2011-06-19 05:37
快速回复:有关main函数中提取结构体中的元素
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012677 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved