2017-01-02 39 views
0
//program to calculate horizontal Distance and Reduced Level by tacheometry 
#include <stdio.h> 
#include <math.h> 

void main() 
{ 
    float m,s,cosine,t,c,d,pi,v,h,hi,bm,rl,r; 
    printf("enter value of stadia intercept,theta,bench mark,central reading on bm,central reading on staff station"); 
    printf("\n"); 
    scanf("%f %f %f %f %f", &s, &t, &bm, &r, &h); 
    m=100; c=0; 
    pi=22.0/7.0; 
    cosine=cos(t*pi/180.0); 
    d=((m*s*pow(cosine,2))+(c*cosine)); 
    v=d*tan(t*pi/180.0); hi=bm+r-v; rl=hi+v-h; 
    printf("distance=%f meters, vertival distance=%f meters,height of instrument=%f meters,reduced level=%f meters",d,v,hi,rl); 
} 

上記では、水平距離と削減レベルを計算するプログラムを提供しました。Cプログラム「プログラムを終了する代わりに取得した後に特定のステップを繰り返す」

画像に12行目の回答を印刷した後に質問があります。 プログラムを再起動する代わりに、手順8〜12を繰り返してもかまいませんか?

私はCプログラミングの初心者です。だから重要なことを何も指定しなければ、すみません。あなたは、ループを学ぶために(コメントを参照)が、あなたのコードは次のようになります助けるために必要

+5

ループを学習する必要があります。 'while'と' for'ループを探します。再帰もうまくいくだろうが、今のところそれを避けるだろう。 – Carcigenicate

+2

コードをインデントする方法も学びます。 – Pointy

+1

void main(){}これは私にとって非常に古く見えます – Michi

答えて

0

:ユーザーは5つのパラメータを入力しない場合に

#include <stdio.h> 
#include <math.h> 

int main(void) 
{ 
    do { 
     float m,s,cosine,t,c,d,v,h,hi,bm,rl,r; 
     printf("enter value of stadia intercept,theta,bench mark,central reading on bm,central reading on staff station"); 
     printf("\n"); 
     if (scanf("%f %f %f %f %f", &s, &t, &bm, &r, &h)!=5) 
      break; 
     m=100; c=0; 
     cosine=cos(t*M_PI/180.0); 
     d=((m*s*pow(cosine,2))+(c*cosine)); 
     v=d*tan(t*M_PI/180.0); hi=bm+r-v; rl=hi+v-h; 
     printf("distance=%f meters, vertival distance=%f meters,height of instrument=%f meters,reduced level=%f meters",d,v,hi,rl); 
    } while (1); 
    return(0); 
} 

do {...} while(1);は、無限ループが、ループ断線であります。ループの後では、main関数はゼロを返します。これはオペレーティングシステムに転送され、ゼロが成功として解釈されます。

+0

あなたは変数宣言を 'do {'ブロックの中に置くことができます。それは少し良いです。 –

+0

おおよその近似値 '22.0/7.0'の代わりに' M_PI'の使用を提案すると役に立つかもしれません。 –

+1

@DavidBowling [適合する標準ライブラリファイルmath.hは必須ではなく、実際にはM_PIを定義してはいけません](http://stackoverflow.com/a/5008376/2410359)。しかし、 'pi = 22.0/7.0;は粗である。 – chux

関連する問題