小言_互联网的博客

C#练习题答案: 抛体运动【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

236人阅读  评论(0)

抛体运动【难度:2级】:

答案1:

using System;
using System.Globalization;
using System.Xml.Schema;
    
public class Projectile {
    public double StartingHeightFeet { get; private set; }

    public double StartingVelicityFeetSecond { get; private set; }

    public double AngleDegrees { get; private set; }

    public Projectile( double startingHeightFeet, double startingVelocityFeetSecond, double angleDegrees ) {
        if ( startingHeightFeet < 0 || 200 <= startingHeightFeet ) {
            throw new ArgumentException( "", "startingHeightFeet" );
        }
        if ( startingVelocityFeetSecond < 0 || 200 <= startingVelocityFeetSecond ) {
            throw new ArgumentException( "", "startingVelocityFeetSecond" );
        }
        if ( angleDegrees < 0 || 90 <= angleDegrees ) {
            throw new ArgumentException( "", "angleDegrees" );
        }
        StartingHeightFeet = startingHeightFeet;
        StartingVelicityFeetSecond = startingVelocityFeetSecond;
        AngleDegrees = angleDegrees;
    } 

    public double AngleRadians {
        get { return ToRadians( AngleDegrees ); }
    }

    private double ToRadians( double angleDegrees ) {
        return angleDegrees*( Math.PI/180 );
    }

    public string HeightEq( ) {
        var s = string.Format( @"h(t) = -16.0t^2 + {0}t",
            ToString( Math.Round( StartingVelicityFeetSecond*Math.Sin( AngleRadians ), 3 ) ) );
        if ( StartingHeightFeet > 0 ) {
            s += string.Format( " + {0}", ToString( StartingHeightFeet ) );
        }
        return s;
    }

    private static string ToString( double value ) {
        var s = value.ToString( "F3", CultureInfo.GetCultureInfo( "en" ) );
        if ( s.Contains( ".000" ) ) {
            return s.Substring( 0, s.IndexOf( '.' ) + 2 );
        }
        return s;
    }

    public string HorizEq( ) {
        return string.Format( @"x(t) = {0}t",
            ToString( Math.Round( StartingVelicityFeetSecond*Math.Cos( AngleRadians ), 3 ) ) );
    }

    public double Height( double t ) {
        var r = -16*Math.Pow( t, 2 ) + StartingVelicityFeetSecond*Math.Sin( AngleRadians )*t + StartingHeightFeet;
        return Math.Round( r, 3 );
    }

    public double Horiz( double t ) {
        var r = t*StartingVelicityFeetSecond*Math.Cos( AngleRadians );
        return Math.Round( r, 3 );
    }

    public double[] Landing( ) {
        var d = Math.Pow( StartingVelicityFeetSecond*Math.Sin( AngleRadians ), 2 ) + 4*16*StartingHeightFeet;
        var t = ( StartingVelicityFeetSecond*Math.Sin( AngleRadians ) + Math.Sqrt( d ) )/32;
        var x = Horiz( t );
        return new[] {x, 0, Math.Round( t, 3 )};
    }

}

答案2:

using System;
public class Projectile
{
  private int _h0;
  private int _v0;
  private int _a;
  public Projectile(int h0,int v0,int a)
  {
      _h0 = h0;
      _v0 = v0;
      _a = a;
  }

  private double GetYV() => _v0 * Math.Sin(_a / 180.0 * Math.PI);

  private double GetXV() => _v0 * Math.Cos(_a / 180.0 * Math.PI);

  public string HeightEq() => $"h(t) = -16.0t^2 + {GetYV():F3}t{(_h0 == 0 ? "" : $" + {_h0:F1}")}";

  public string HorizEq() => $"x(t) = {GetXV():F3}t";

  public double Height(double t) => Math.Round(-16 *t* t + GetYV() * t + _h0, 3);

  public double Horiz(double t) => Math.Round(GetXV() * t, 3);

  public double[] Landing()
  {
      var t = (GetYV() + Math.Sqrt(64 * _h0 + GetYV() * GetYV())) / 32.0;
      return new double[] { Horiz(t), 0, Math.Round(t, 3) };
  }
}

答案3:

using System;

public class Projectile {
  private double startVelocity;
  private double startHeight;
  private double angle;
  private double horizVel;
  private double verticalVel;
  public Projectile(double h, double v0, double a) {
    startVelocity = v0;
    startHeight = h;
    angle = a;
    horizVel = v0 * Math.Cos((a * Math.PI) / 180.0);
    verticalVel = v0 * Math.Sin((a * Math.PI) / 180.0);
  }
  public String HeightEq() {
    String s;
    if(startHeight % 1 == 0) s = " + " + startHeight + ".0";
    else s = " + " + Math.Round(startHeight, 3).ToString();
    if(s.Equals(" + 0.0")) s = "";
    return "h(t) = -16.0t^2 + " + verticalVel.ToString("N3") + "t" + s;
  }
  public String HorizEq() {
    return "x(t) = " + horizVel.ToString("N3") + "t";
  }
  public double Height(double time) {
    double d = (-16 * Math.Pow(time, 2) + (verticalVel * time) + startHeight);
    return (double)Math.Round(d * 1000d) / 1000d;
  }
  public double Horiz(double time) {
    return (double)Math.Round((horizVel * time) * 1000d) / 1000d;
  }
  public double[] Landing() {
    double t = ((0-verticalVel - Math.Sqrt(Math.Pow(verticalVel,2)-(4*(-16)*startHeight)))/-32.0);
    double x = Horiz(t);
    t = (double)Math.Round(t * 1000d) / 1000d;
    x = (double)Math.Round(x * 1000d) / 1000d;
    return new double[]{x, 0, t};
  }
}

答案4:

using System;

public class Projectile {
  double h0 { get; set; }
  double v0 { get; set; }
  double a { get; set; }
  
  private string Display(double x){
    string res = x.ToString("0.000");
   
     if(res[res.Length-1]=='0')res=res.Substring(0, res.Length-1);
     if(res[res.Length-1]=='0')res=res.Substring(0, res.Length-1);
     if(res[res.Length-1]=='0')res=res.Substring(0, res.Length-2);
     
     return res;
  }
  
  private string Display2(double x){
    if(Math.Round(x,0) == x){
      return x.ToString("0.0");
    }
    return x.ToString("0.000");
  }
  
  public Projectile(double h, double v, double a){
    this.h0 = h;
    this.v0 = v;
    this.a = a;
  }
  
  public string HeightEq(){
    double v = v0*Math.Sin(a*Math.PI/180);
    if(h0==0.0){
      return $"h(t) = -16.0t^2 + {Display(v)}t";
    }
    else{
      return $"h(t) = -16.0t^2 + {Display(v)}t + {Display2(h0)}";
    }
  }
  
  public string HorizEq(){
    double v = v0*Math.Cos(a*Math.PI/180);
    return $"x(t) = {Display2(v)}t";
  }
  
  public double Height(double t){
    double v = v0*Math.Sin(a*Math.PI/180);
    return Math.Round(-16.0*t*t + v*t + h0,3);
  }
  
  public double Horiz(double t){
    double v = v0*Math.Cos(a*Math.PI/180);
    return Math.Round(v*t,3);
  }
  
  public double[] Landing(){
    double g = 32;

    double t = (v0 * Math.Sin(a*Math.PI/180.0) / g)
                +
                Math.Sqrt(
                  2*h0/g
                  +
                  (v0 * v0 * Math.Sin(a * Math.PI/180.0) * Math.Sin(a * Math.PI/180.0))
                  /
                  (g * g)
                );
    
    return new double [3] {Horiz(t),0.0,Math.Round(t,3)};
    
  }
    
}




转载:https://blog.csdn.net/weixin_45444821/article/details/101192491
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场