?? girdercomment.c
字號(hào):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define E 1e-9//定義0
struct FandM;//定義結(jié)構(gòu)體存放集中力和力矩,成員為集中力和力矩的位置和大小
struct qlen;//定義結(jié)構(gòu)體存放均勻載荷,成員為載荷的起點(diǎn)、終點(diǎn)和大小以及作用位置
struct Data;//定義結(jié)構(gòu)體存放某截面的位置剪力和彎矩
typedef struct FandM* fm;
typedef struct qlen* ql;
typedef struct Data* data;
int num[3];//定義全局?jǐn)?shù)組存放集中力、力矩、均布力的數(shù)目,其初始化由函數(shù)initial完成
double F[2];//定義全局變量存放兩個(gè)支點(diǎn)的支反力
double FM[2];//定義全局變量存放固定端的力和力矩
double length=0;//定義全局變量存放梁的長度
double pos1;//定義全局變量支點(diǎn)一和支點(diǎn)二的位置
double pos2;
int flag;//定義全局變量存放用戶選擇的梁類型
struct FandM
{
double pos;//集中力和力矩的位置
double val;//集中力和力矩的大小
};
struct qlen
{
double start;//均布力的起始點(diǎn)
double end;//均布力的終點(diǎn)
double val;//均布力的大小
double pos;//作用點(diǎn)位置
};
struct Data
{
double pos;//截面位置
double Fsy;//該截面剪力
double Mz;//該截面的彎矩
};
//說明:坐標(biāo)原點(diǎn)為梁左端,數(shù)據(jù)必須自帶其正負(fù)號(hào)輸入,并且各數(shù)據(jù)的輸入必須由左向右,如果輸入負(fù)數(shù)個(gè)集中力、力矩或者均布力,按0個(gè)處理
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)//從緩沖區(qū)里讀入需要的整數(shù)
{
int ret;
while(ret=scanf("%d",p)==0||ret==EOF)
{
printf("Illegal inputting,input again!\n");
fflush(stdin);
}
fflush(stdin);
}
void getdouble(double *p)//從緩沖區(qū)里讀入需要的雙精度數(shù)
{
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()//菜單:選擇梁的類型(簡支梁、左端固定梁、右端固定梁)結(jié)果由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;
}
}
//初始化,由用戶輸入集中力、力矩、均布力的數(shù)目,結(jié)果由全局變量num[3]存放
//輸入梁的長度,由全局變量length存放
//如果用戶選擇的是雙支點(diǎn)梁,需輸入兩個(gè)支點(diǎn)的位置
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) //根據(jù)全局變量num[3]、length輸入各個(gè)集中力、力矩、均布力的數(shù)據(jù)
{
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;//計(jì)算集中力的總和
for(i=0;i<num[2];i++)//計(jì)算均布力的總和
{
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) //求作用在梁上的在坐標(biāo)原點(diǎn)(即最左端)力矩的總和,并返回
{
double sum1=0;//存放集中力的力矩的總和
double sum2=0;//存放所有施加的力矩的總和
double sum3=0;//存放均布力的力矩的總和
double temp;
double summ;
int i;
for(i=0;i<num[0];i++)//計(jì)算集中力的力矩
{
temp=((a+i)->pos)*((a+i)->val);
sum1+=temp;
}
for(i=0;i<num[1];i++)//計(jì)算施加的力矩
sum2+=(b+i)->val;
for(i=0;i<num[2];i++)//計(jì)算均布力的力矩
{
(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;//返回
}
//解關(guān)于兩個(gè)支反力的二元一次方程組,數(shù)組f1和f2存放兩個(gè)方程的三個(gè)系數(shù),其形式為Ax+By=C,將結(jié)果存入數(shù)組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");
}
//主計(jì)算過程
//入口參數(shù)是截面的位置,力、力矩和均布力的結(jié)構(gòu)體數(shù)組,返回該截面的數(shù)據(jù)指針
data Compute(double position,fm f,fm m,ql q)
{
data sum=(data)malloc(sizeof(struct Data));//定義Data結(jié)構(gòu)體存放截面數(shù)據(jù)
double sumF=0;
double sumM=0;
double temp;
int i=0;
if(flag==1)//如果是簡支梁
{
if(position>pos1)//如果截面位于第一個(gè)支點(diǎn)右,則計(jì)算第一個(gè)支反力和其力矩
{
sumF-=F[0];
sumM+=(F[0]*(position-pos1));
}
if(position>pos2)//如果截面位于第二個(gè)支點(diǎn)右,則再計(jì)算第二個(gè)支反力和其力矩
{
sumF-=F[1];
sumM+=(F[1]*(position-pos2));
}
}
else if(flag==2)//如果是左端固定端梁,則計(jì)算固定端的反力和反力矩的大小
{
sumF-=FM[0];
sumM-=FM[1];
sumM+=FM[0]*position;
}
//如果是右端固定梁,對(duì)sumF和sumM不做處理,仍等于零
for(i=0;i<num[0]&&(f+i)->pos<position;i++)//計(jì)算截面以左所有集中力和其力矩的總和
{
sumF-=(f+i)->val;
sumM+=(((f+i)->val)*(position-(f+i)->pos));
}
for(i=0;i<num[1]&&(m+i)->pos<position;i++)//計(jì)算截面以左外加力矩的總和
sumM-=(m+i)->val;
for(i=0;i<num[2]&&(q+i)->start<position;i++)//計(jì)算截面以左所有均布力和其力矩的和
{
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;//為該點(diǎn)的結(jié)構(gòu)體賦值
sum->Fsy=sumF;
sum->Mz=sumM;
return sum;//返回結(jié)構(gòu)體指針
}
data Auto(fm f,fm m,ql q,int n) //自動(dòng)計(jì)算模式
{
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) //輸出自動(dòng)計(jì)算的結(jié)果以供研究
{
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) //釋放存儲(chǔ)空間
{
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()//主函數(shù)
{
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");//建立一個(gè)梁
getanswer(&ans);
while(ans=='Y'||ans=='y')
{
menu();//選擇梁的類型
initial();//根據(jù)類型初始化
f=(fm)malloc(num[0]*sizeof(struct FandM));//建立動(dòng)態(tài)結(jié)構(gòu)體數(shù)組以存放所有集中力的數(shù)據(jù)
m=(fm)malloc(num[1]*sizeof(struct FandM));//建立動(dòng)態(tài)結(jié)構(gòu)體數(shù)組以存放所有力矩的數(shù)據(jù)
q=(ql)malloc(num[2]*sizeof(struct qlen));//建立動(dòng)態(tài)結(jié)構(gòu)體數(shù)組以存放所有的均布力的數(shù)據(jù)
inputall(f,m,q);//按提示輸入所有力、力矩、載荷的數(shù)據(jù)
if(flag==1)//如果是雙支點(diǎn)梁則為func1[3] func2[3]賦值,由力的平衡和力矩的平衡列兩個(gè)方程
{
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]);//輸出兩個(gè)支反力
}
else if(flag==2)//如果是一端固定的梁則只需求固定端的反力和反力矩的大小,將其求出并存入數(shù)組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");//開始計(jì)算提問
getanswer(&ans1);
if(ans1=='Y'||ans1=='y')
{
printf("Choose a section?(Y(y)/N(n))\n");//提示選擇一個(gè)截面
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)//錯(cuò)誤處理
{
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);//輸出該截面的剪切力和彎矩的數(shù)值
free(comp);
printf("Would you want once more?(Y(y)/N(n))\n");//循環(huá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");//輸入所要的數(shù)據(jù)的數(shù)目
getint(&n);
autocomp=Auto(f,m,q,n);
Display(autocomp,n);//輸出自動(dòng)計(jì)算結(jié)果
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");//循環(huán)提示
getanswer(&ans);
}
printf("Thank you for your using!\nPlease press the ENTER key to terminate this program...");
getchar();//程序結(jié)束
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -