作者wodada (wodada)
看板C_and_CPP
標題[問題] 關於使用SWITCH與檔案讀寫的問題
時間Fri Apr 8 21:12:41 2011
本人目前正在寫一個關於阻尼震盪的問題,
老師要求我們一定要用到switch指令和file I/O跑出來
本人已經將所有程式都寫好了,執行也沒有錯誤,可是在EXCEL上
始終看不到數值所在,我試了很久一直不知道原因為何?
想請各位高手能幫我解答一下
程式碼如下:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//宣告副程式
double dis1(double u0, double v0, double wn, double zeta0, double ti);
double vel1(double u0, double v0, double wn, double zeta0, double ti);
double dis2(double u0, double v0, double wn, double zeta0, double ti);
double vel2(double u0, double v0, double wn, double zeta0, double ti);
double dis3(double u0, double v0, double wn, double zeta0, double ti);
double vel3(double u0, double v0, double wn, double zeta0, double ti);
double dis4(double u0, double v0, double wn, double zeta0, double ti);
double vel4(double u0, double v0, double wn, double zeta0, double ti);
//定義圓周率
//定義四個圖的zeta數值
#define EPS 0.001
#define PI 3.1415926
#define zeta1 0.02
#define zeta2 0.05
#define zeta3 0.1
#define zeta4 0.2
int main(int argc, char* argv[])
{
FILE *f1;
int choice= 0;
double mm, //mass
kk, //stiffness
wn, //natural freqency
cc, //damping
u0, //initial displacement
v0, //initial velocity
T, //natural period
time, //time
dis_ti, //displacement at time ti
vel_ti, //velocity at time ti
dis_ti2,
vel_ti2,
dis_ti3,
vel_ti3,
dis_ti4,
vel_ti4;
mm= 1; //題目給質量,彈簧係數和阻尼
cc= 0.1;
kk= 1;
wn= sqrt(kk/mm);
T= 2*PI/wn;
u0= 10; //初始位置和初速度
v0= 0;
f1= fopen("show_time.csv", "w+");
if(f1==NULL)
{
printf("Cannot open show_time.csv, is your EXCEL open?\n");
exit(0);
}
fprintf(f1, "time, distance, velocity, distance2, velocity2, distance3, velocity3, distance4, velocity4,\n");
switch(choice)
{
for(time= 0.0;time<= 100.0;time+= 1){
case 1:
dis_ti= dis1(u0, v0, wn, zeta1, time);
vel_ti= vel1(u0, v0, wn, zeta1, time);
fprintf(f1, "%f, %f, %f,\n", time, dis_ti, vel_ti);
break;
case 2:
dis_ti2= dis2(u0, v0, wn, zeta2, time);
vel_ti2= vel2(u0, v0, wn, zeta2, time);
fprintf(f1, "%f, %f, %f,\n", time, dis_ti2, vel_ti2);
break;
case 3:
dis_ti3= dis3(u0, v0, wn, zeta3, time);
vel_ti3= vel3(u0, v0, wn, zeta3, time);
fprintf(f1, "%f, %f, %f,\n", time, dis_ti3, vel_ti3);
break;
case 4:
dis_ti4= dis4(u0, v0, wn, zeta4, time);
vel_ti4= vel4(u0, v0, wn, zeta4, time);
fprintf(f1, "%f, %f, %f,\n", time, dis_ti4, vel_ti4);
break;
}
}
fclose(f1);
return 0;
}
//用副程式計算方程式各個係數求出位置
double dis1(double u0, double v0, double wn, double zeta0, double ti)
{
double wd, A, B, C, D, E;
wd= wn*sqrt(1-pow(zeta1,2));
A= exp(-zeta1*wn*ti);
B= u0*cos(wd*ti);
C= (v0+wn*zeta1*u0)/wd;
D= C*sin(wd*ti);
E= A*(B+D);
return E;
}
//用副程式計算速度
double vel1(double u0, double v0, double wn, double zeta0, double ti)
{
double dis_t1, dis_t2, vel_ti;
dis_t1= dis1(u0, v0, wn, zeta1, ti);
dis_t2= dis1(u0, v0, wn, zeta1, ti+EPS);
vel_ti= (dis_t1-dis_t2)/EPS;
return(vel_ti);
}
double dis2(double u0, double v0, double wn, double zeta0, double ti)
{
double wd2, A2, B2, C2, D2, E2;
wd2= wn*sqrt(1-pow(zeta2,2));
A2= exp(-zeta2*wn*ti);
B2= u0*cos(wd2*ti);
C2= (v0+wn*zeta2*u0)/wd2;
D2= C2*sin(wd2*ti);
E2= A2*(B2+D2);
return E2;
}
double vel2(double u0, double v0, double wn, double zeta0, double ti)
{
double dis_t12, dis_t22, vel_ti2;
dis_t12= dis3(u0, v0, wn, zeta2, ti);
dis_t22= dis3(u0, v0, wn, zeta2, ti+EPS);
vel_ti2= (dis_t12-dis_t22)/EPS;
return(vel_ti2);
}
double dis3(double u0, double v0, double wn, double zeta0, double ti)
{
double wd3, A3, B3, C3, D3, E3;
wd3= wn*sqrt(1-pow(zeta3,2));
A3= exp(-zeta3*wn*ti);
B3= u0*cos(wd3*ti);
C3= (v0+wn*zeta3*u0)/wd3;
D3= C3*sin(wd3*ti);
E3= A3*(B3+D3);
return E3;
}
double vel3(double u0, double v0, double wn, double zeta0, double ti)
{
double dis_t13, dis_t23, vel_ti3;
dis_t13= dis3(u0, v0, wn, zeta3, ti);
dis_t23= dis3(u0, v0, wn, zeta3, ti+EPS);
vel_ti3= (dis_t13-dis_t23)/EPS;
return(vel_ti3);
}
double dis4(double u0, double v0, double wn, double zeta0, double ti)
{
double wd4, A4, B4, C4, D4, E4;
wd4= wn*sqrt(1-pow(zeta4,2));
A4= exp(-zeta4*wn*ti);
B4= u0*cos(wd4*ti);
C4= (v0+wn*zeta4*u0)/wd4;
D4= C4*sin(wd4*ti);
E4= A4*(B4+D4);
return E4;
}
double vel4(double u0, double v0, double wn, double zeta0, double ti)
{
double dis_t14, dis_t24, vel_ti4;
dis_t14= dis3(u0, v0, wn, zeta4, ti);
dis_t24= dis3(u0, v0, wn, zeta4, ti+EPS);
vel_ti4= (dis_t14-dis_t24)/EPS;
return(vel_ti4);
}
程式雖然看起來很多,可是卻很瑣碎,我目前遇到的困難是無法用SWITCH
配合fprintf指令把四個選項的數值輸出到EXCEL上,而輸出到EXCEL上的規格如下:
第一排 第二排 第三排 第四排 第五排.......
以此類推.....
time dis_ti vel_ti dis_ti2 vel_ti2.......
想請問我該怎麼做才會有此結果產生?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.39.17.138
→ wodada:最主要是switch和fprintf部分... 04/08 21:13
→ loveme00835:你可以用置底文網址貼程式碼, 比較好看 04/08 21:19
→ wodada:請問一下,是將程式碼丟進該網站後跑出來再複製嗎? 04/08 21:38
推 VictorTom:貼上後它會給你個網址什麼的, 貼出來那個行了:) 04/08 21:40
→ wodada:網址如上 04/08 21:45
→ wodada:忘了說,我用VC++2008 04/08 21:49
→ tropical72:資料型態為double,引數為%lf非%f (%lf 都是英文字母) 04/08 21:54
→ tropical72:把所有 %f 都改成 %lf 先試試.. 04/08 21:55
→ kaomark:這個switch(choice)到底是作什麼的? choice一直都是0啊 04/08 22:22
→ kaomark:然後如果你要做的事情是 選某個case跑101次 04/08 22:31
→ kaomark:那應該要在每一個case中都寫for loop 04/08 22:32
→ kaomark:如果你要的是 跑101次每次都挑一個case來做 04/08 22:32
→ kaomark:那應該要把for放在switch外面 04/08 22:32