📄 girdercomment.c
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define E 1e-9//定义0
struct FandM;//定义结构体存放集中力和力矩,成员为集中力和力矩的位置和大小
struct qlen;//定义结构体存放均匀载荷,成员为载荷的起点、终点和大小以及作用位置
struct Data;//定义结构体存放某截面的位置剪力和弯矩
typedef struct FandM* fm;
typedef struct qlen* ql;
typedef struct Data* data;
int num[3];//定义全局数组存放集中力、力矩、均布力的数目,其初始化由函数initial完成
double F[2];//定义全局变量存放两个支点的支反力
double FM[2];//定义全局变量存放固定端的力和力矩
double length=0;//定义全局变量存放梁的长度
double pos1;//定义全局变量支点一和支点二的位置
double pos2;
int flag;//定义全局变量存放用户选择的梁类型
struct FandM
{
double pos;//集中力和力矩的位置
double val;//集中力和力矩的大小
};
struct qlen
{
double start;//均布力的起始点
double end;//均布力的终点
double val;//均布力的大小
double pos;//作用点位置
};
struct Data
{
double pos;//截面位置
double Fsy;//该截面剪力
double Mz;//该截面的弯矩
};
//说明:坐标原点为梁左端,数据必须自带其正负号输入,并且各数据的输入必须由左向右,如果输入负数个集中力、力矩或者均布力,按0个处理
void readme(void)
{
printf("********** README **********\n\n");
printf("The orignal point is the left edge of the girder.\n");
printf("The sign of F,M,and q are depends on the direction of them.\n");
printf("You must input the data with the SIGN of them.\n");
printf("You must input the F,M and q from LEFT to RIGHT.\n");
printf("If the number of F,M and q you input isn't >=0, it will be treated as 0.\n");
printf("\n");
}
void getint(int *p)//从缓冲区里读入需要的整数
{
int ret;
while(ret=scanf("%d",p)==0||ret==EOF)
{
printf("Illegal inputting,input again!\n");
fflush(stdin);
}
fflush(stdin);
}
void getdouble(double *p)//从缓冲区里读入需要的双精度数
{
int ret;
while(ret=scanf("%lf",p)==0||ret==EOF)
{
printf("Illegal inputting,input again!\n");
fflush(stdin);
}
fflush(stdin);
}
void getanswer(int *p)//读入用户的选择并筛选
{
while((*p=getchar())!='Y'&&*p!='y'&&*p!='N'&&*p!='n')
{
printf("Illegal inputting!Please choose Y(y)/N(n):");
fflush(stdin);
}
fflush(stdin);
}
void menu()//菜单:选择梁的类型(简支梁、左端固定梁、右端固定梁)结果由flag存放
{
printf("Please choose the type of the girder:\n");
printf("1.______________________\n");
printf(" _|_ _|_\n");
printf("2.|\n");
printf(" |_____________________\n");
printf(" |\n");
printf("3. |\n");
printf(" _____________________|\n");
printf(" |\n");
choice:
printf("Please input your choice:(1 2 3)\n");
getint(&flag);
if(flag!=1&&flag!=2&&flag!=3)
{
printf("Illegal data!(error:your choice isn't 1,2,3)\n");
goto choice;
}
}
//初始化,由用户输入集中力、力矩、均布力的数目,结果由全局变量num[3]存放
//输入梁的长度,由全局变量length存放
//如果用户选择的是双支点梁,需输入两个支点的位置
void initial()
{
printf("How many F?");
getint(num);
printf("How many M?");
getint(num+1);
printf("How many q?");
getint(num+2);
length:
printf("Please input the length of the girder:");
getdouble(&length);
if(length<=E)
{
printf("Illegal data!(error:length<=0)\n");
goto length;
}
if(flag==1)
{
points:
printf("Please input the position of the two points:\n");
printf("Point1:");
getdouble(&pos1);
printf("Point2:");
getdouble(&pos2);
if(pos1==pos2)
{
printf("Illegal data!(only one point)\n");
goto points;
}
else if(pos1<=-E||pos2>length)
{
printf("Illegal data!(out of boundary)\n");
goto points;
}
}
}
void inputall(fm a,fm b,ql c) //根据全局变量num[3]、length输入各个集中力、力矩、均布力的数据
{
int i;
if(num[0]!=0)
{
printf("Please input the data about F:\n");
for(i=0;i<num[0];i++)
{
printf("F%d-position:",i+1);
getdouble(&(a+i)->pos);
if((a+i)->pos<=-E||(a+i)->pos>length)
{
printf("Illegal data!(out of boundary)\n");
i--;
continue;
}
printf("F%d-value:",i+1);
getdouble(&(a+i)->val);
}
}
if(num[1]!=0)
{
printf("Please input the data about M:\n");
for(i=0;i<num[1];i++)
{
printf("M%d-position:",i+1);
getdouble(&(b+i)->pos);
if((b+i)->pos<=-E||(b+i)->pos>length)
{
printf("Illegal data!(out of boundary)\n");
i--;
continue;
}
printf("M%d-value:",i+1);
getdouble(&(b+i)->val);
}
}
if(num[2]!=0)
{
printf("Please input the data about q:\n");
for(i=0;i<num[2];i++)
{
printf("q%d-start:",i+1);
getdouble(&(c+i)->start);
printf("q%d-end:",i+1);
getdouble(&(c+i)->end);
if((c+i)->end>length||(c+i)->start<=-E)
{
printf("Illegal data!(out of boundary)\n");
i--;
continue;
}
printf("q%d-value:",i+1);
getdouble(&(c+i)->val);
}
}
}
double Sumfq(fm a,ql b) //求作用在梁上的力的总和,并返回
{
double sumf=0;
double sumq=0;
double sumfq=0;
double temp;
int i;
for(i=0;i<num[0];i++)
sumf+=(a+i)->val;//计算集中力的总和
for(i=0;i<num[2];i++)//计算均布力的总和
{
temp=((b+i)->end-(b+i)->start)*((b+i)->val);
sumq+=temp;
}
sumfq=sumf+sumq;//二者相加
return sumfq;//返回
}
double SumM(fm a,fm b,ql c) //求作用在梁上的在坐标原点(即最左端)力矩的总和,并返回
{
double sum1=0;//存放集中力的力矩的总和
double sum2=0;//存放所有施加的力矩的总和
double sum3=0;//存放均布力的力矩的总和
double temp;
double summ;
int i;
for(i=0;i<num[0];i++)//计算集中力的力矩
{
temp=((a+i)->pos)*((a+i)->val);
sum1+=temp;
}
for(i=0;i<num[1];i++)//计算施加的力矩
sum2+=(b+i)->val;
for(i=0;i<num[2];i++)//计算均布力的力矩
{
(c+i)->pos=((c+i)->start+(c+i)->end)/2;
temp=(c+i)->val*((c+i)->end-(c+i)->start);
temp*=(c+i)->pos;
sum3+=temp;
}
summ=sum1+sum2+sum3;//相加
return summ;//返回
}
//解关于两个支反力的二元一次方程组,数组f1和f2存放两个方程的三个系数,其形式为Ax+By=C,将结果存入数组out[2]中
//注意:out[0]存放F1,out[1]存放F2
void func(double f1[3],double f2[3],double out[2])
{
double k;
int i;
if(f2[0]>E)
{
k=f1[0]/f2[0];
for(i=0;i<3;i++)
f2[i]*=k;
out[1]=(f2[2]-f1[2])/(f2[1]-f1[1]);
out[0]=(f1[2]-out[1]*f1[1])/f1[0];
}
else if(f2[1]>E)
{
k=f1[1]/f2[1];
for(i=0;i<3;i++)
f2[i]*=k;
out[0]=(f2[2]-f1[2])/(f2[0]-f1[0]);
out[1]=(f1[2]-out[0]*f1[0])/f1[1];
}
else
printf("Illegal data!\n");
}
//主计算过程
//入口参数是截面的位置,力、力矩和均布力的结构体数组,返回该截面的数据指针
data Compute(double position,fm f,fm m,ql q)
{
data sum=(data)malloc(sizeof(struct Data));//定义Data结构体存放截面数据
double sumF=0;
double sumM=0;
double temp;
int i=0;
if(flag==1)//如果是简支梁
{
if(position>pos1)//如果截面位于第一个支点右,则计算第一个支反力和其力矩
{
sumF-=F[0];
sumM+=(F[0]*(position-pos1));
}
if(position>pos2)//如果截面位于第二个支点右,则再计算第二个支反力和其力矩
{
sumF-=F[1];
sumM+=(F[1]*(position-pos2));
}
}
else if(flag==2)//如果是左端固定端梁,则计算固定端的反力和反力矩的大小
{
sumF-=FM[0];
sumM-=FM[1];
sumM+=FM[0]*position;
}
//如果是右端固定梁,对sumF和sumM不做处理,仍等于零
for(i=0;i<num[0]&&(f+i)->pos<position;i++)//计算截面以左所有集中力和其力矩的总和
{
sumF-=(f+i)->val;
sumM+=(((f+i)->val)*(position-(f+i)->pos));
}
for(i=0;i<num[1]&&(m+i)->pos<position;i++)//计算截面以左外加力矩的总和
sumM-=(m+i)->val;
for(i=0;i<num[2]&&(q+i)->start<position;i++)//计算截面以左所有均布力和其力矩的和
{
if((q+i)->end<position)
{
temp=((q+i)->end-(q+i)->start)*((q+i)->val);
sumF-=temp;
(q+i)->pos=((q+i)->start+(q+i)->end)/2;
temp*=(position-(q+i)->pos);
sumM+=temp;
}
else
{
temp=(position-(q+i)->start)*((q+i)->val);
sumF-=temp;
temp*=(position-(q+i)->start)/2;
sumM+=temp;
}
}
sum->pos=position;//为该点的结构体赋值
sum->Fsy=sumF;
sum->Mz=sumM;
return sum;//返回结构体指针
}
data Auto(fm f,fm m,ql q,int n) //自动计算模式
{
double step;
int i;
data p,pt;
p=(data)malloc(n*sizeof(struct Data));
step=length/(n-1);
for(i=0;i<n;i++)
{
pt=Compute((i*step),f,m,q);
(p+i)->pos=i*step;
(p+i)->Fsy=pt->Fsy;
(p+i)->Mz=pt->Mz;
}
return p;
}
void Display(data p,int n) //输出自动计算的结果以供研究
{
int i,j;
printf("Format of the datas:position|Fsy|Mz\n");//输出格式
for(i=0,j=1;i<n;i++,j++)
{
printf("%.4lf|%.4lf|%.4lf ",(p+i)->pos,(p+i)->Fsy,(p+i)->Mz);
if(j==3)
{
j=0;
printf("\n");
}
}
printf("\n");
}
void Free(fm a,fm b,ql c) //释放存储空间
{
free(a);
free(b);
free(c);
}
void save(data p,int n,char filename[30])
{
FILE *ptr;
int i,j;
ptr=fopen(filename,"w");
if(ptr==NULL)
{
printf("Fail to open the file.\n");
return;
}
fprintf(ptr,"Format of the datas:position|Fsy|Mz\n");
for(i=0,j=1;i<n;i++,j++)
{
fprintf(ptr,"%.4lf|%.4lf|%.4lf ",(p+i)->pos,(p+i)->Fsy,(p+i)->Mz);
if(j==3)
{
j=0;
fprintf(ptr,"\n");
}
}
fprintf(ptr,"\n");
fclose(ptr);
printf("The data has been saved in the file %s.\n",filename);
}
int main()//主函数
{
fm f,m;
ql q;
data comp;
data autocomp;
int n,ans,ans1,ans2,choice,ans3,name,i;
char head[4]="C:/";
char filename[30];
char tail[3]=".C";
double position;
double func1[3]={1,1,0};
double func2[3]={0,0,0};
readme();//说明
printf("Creat a new girder?(Y(y)/N(n))\n");//建立一个梁
getanswer(&ans);
while(ans=='Y'||ans=='y')
{
menu();//选择梁的类型
initial();//根据类型初始化
f=(fm)malloc(num[0]*sizeof(struct FandM));//建立动态结构体数组以存放所有集中力的数据
m=(fm)malloc(num[1]*sizeof(struct FandM));//建立动态结构体数组以存放所有力矩的数据
q=(ql)malloc(num[2]*sizeof(struct qlen));//建立动态结构体数组以存放所有的均布力的数据
inputall(f,m,q);//按提示输入所有力、力矩、载荷的数据
if(flag==1)//如果是双支点梁则为func1[3] func2[3]赋值,由力的平衡和力矩的平衡列两个方程
{
func1[2]=-Sumfq(f,q);
func2[0]=pos1;
func2[1]=pos2;
func2[2]=-SumM(f,m,q);
func(func1,func2,F);//求解
printf("The two opposite forces are:F1=%.4lf\tF2=%.4lf\n",F[0],F[1]);//输出两个支反力
}
else if(flag==2)//如果是一端固定的梁则只需求固定端的反力和反力矩的大小,将其求出并存入数组FM[2]
{
FM[0]=-Sumfq(f,q);//反力存入FM[0]
FM[1]=-SumM(f,m,q);//反力矩存入FM[1]
printf("Fo=%.4lf\tMo=%.4lf\n",FM[0],FM[1]);
}
else
printf("Fo=%.4lf\tMo=%.4lf\n",-Sumfq(f,q),(-SumM(f,m,q)+Sumfq(f,q)*length));
printf("Start compute?(Y(y)/N(n))\n");//开始计算提问
getanswer(&ans1);
if(ans1=='Y'||ans1=='y')
{
printf("Choose a section?(Y(y)/N(n))\n");//提示选择一个截面
getanswer(&choice);
}
if(choice=='Y'||choice=='y')
{
while(ans1=='Y'||ans1=='y')
{
section:
printf("Please input the position of the section:");
getdouble(&position);//输入截面位置
if(position<=-E||position>length)//错误处理
{
printf("Illegal data!(out of boundary)\n");
goto section;
}
comp=Compute(position,f,m,q);
printf("The outcome is:Fsy=%.4lf\tMz=%.4lf\n",comp->Fsy,comp->Mz);//输出该截面的剪切力和弯矩的数值
free(comp);
printf("Would you want once more?(Y(y)/N(n))\n");//循环提示
getanswer(&ans1);
}
}
printf("Auto compute?(Y(y)/N(n))\n");
getanswer(&ans2);
if(ans2=='Y'||ans2=='y')
{
printf("How many datas do you want?\n");//输入所要的数据的数目
getint(&n);
autocomp=Auto(f,m,q,n);
Display(autocomp,n);//输出自动计算结果
printf("Save the data??(Y(y)/N(n))\n");
getanswer(&ans3);
if(ans3=='Y'||ans3=='y')
{
printf("Please input the filename(without extern name, the path is C:/):");
i=0;
while((name=getchar())!=EOF&&name!='\n')
filename[i++]=(char)name;
filename[i]='\0';
strcat(head,filename);
strcat(head,tail);
save(autocomp,n,head);
}
free(autocomp);//释放空间
}
Free(f,m,q);//释放空间
printf("Creat a new girder?(Y(y)/N(n))\n");//循环提示
getanswer(&ans);
}
printf("Thank you for your using!\nPlease press the ENTER key to terminate this program...");
getchar();//程序结束
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -