[問題] 程式碼疑問??

看板C_and_CPP (C/C++)作者 (育)時間13年前 (2012/07/18 16:28), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/1
// stl.cpp : Defines the entry point for the console application. // // #include "stdafx.h" #include <stdlib.h> #include <string.h> #include <stdio.h> #include <algorithm> #include <vector> #include <math.h> int main(int argc,int *argv[]) { FILE *input,*output,*tempfile; char filename[260],instr[10000]; unsigned long int N,totalN,gsize,skipN; double a,b, c, x,total,avg, avg2, skip, total2, time; printf("infile : "); scanf("%s",filename); input=fopen(filename,"r"); printf("outfile : "); scanf("%s",filename); output=fopen(filename,"w"); printf("group size : "); scanf("%lu",&gsize); printf("skip valut : "); scanf("%lf",&skip); * printf("time : "); * scanf("%lf",&time); fgets(instr,9999,input); fgets(instr,9999,input); do { * totalN=0; total=0.0; total2=0.0; skipN=0; if (fgets(instr,9999,input)==NULL) break; if (instr[0]!='Z') break; tempfile=fopen("tempfile.tmp","w"); for(unsigned long int i=0;i<gsize;i++) { if (i) fgets(instr,9999,input); instr[strlen(instr)-1]=0; fprintf(output,"%s ",instr+7); fgets(instr,9999,input); sscanf(instr+3,"%lu",&N); fprintf(output,"%6lu\n",N); fgets(instr,9999,input); fgets(instr,9999,input); totalN+=N; for(unsigned long int j=0;j<N;j++) { fgets(instr,9999,input); sscanf(instr,"%lf",&x); if (x>=skip) skipN++; else total+=x; fprintf(tempfile,"%20.15lf\n",x); } } fclose(tempfile); total = total*(double)time; total2 = total2 * (double)time*(double)time; avg=total/(double)(totalN-skipN); tempfile=fopen("tempfile.tmp","r"); total=0.0; total2=0.0; while(fscanf(tempfile,"%lf",&a)==1) total+=((a*time-avg)*(a*time-avg)); total2+=(a*a); b=sqrt(total/(double)(totalN-skipN)); c=(sqrt(total2/(double)(totalN-skipN)))*time; fclose(tempfile); fprintf(output,"%10d %10d %20.15lf %20.15lf %20.15lf\n",totalN,skipN,avg,b, c); } while(1); fclose(output); ------------------------------------------------------------------------------ 這個程式碼是用來分析商用軟體跑出來的data,data裡面存放的速度值 在這個程式碼裡 我希望將讀入檔案的數值作坊均跟值的計算,變數time都設為1 變數c得到的是速度的方均根值,skip value 是一門檻值(我希望將data檔裏面大於理想值 的部分去除掉),但是在計算C值時我發現在我邏輯裡面應該被去除掉的值也被計算進去了 想請問各為C++前輩,是否在以下這一段有錯誤呢?? for(unsigned long int j=0;j<N;j++) { fgets(instr,9999,input); sscanf(instr,"%lf",&x); if (x>=skip) skipN++; else total+=x; fprintf(tempfile,"%20.15lf\n",x); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.38.128

07/18 17:43, , 1F
用Debug確認一下 if (x>=skip) 這行有沒有成立
07/18 17:43, 1F

07/18 20:11, , 2F
skipN確定有執行....但我想問fprintf這行讀入的x是會全部
07/18 20:11, 2F

07/18 20:12, , 3F
全部將DATA裡的值都讀入嗎? 還是只讀入沒被去掉的值?
07/18 20:12, 3F

07/18 20:15, , 4F
因為我試著讀檔看不出這一行站存檔裡面的值是什麼??
07/18 20:15, 4F
文章代碼(AID): #1G1dESX2 (C_and_CPP)
文章代碼(AID): #1G1dESX2 (C_and_CPP)