作业帮 > 综合 > 作业

用C#语言编程绘图输出y=a*sin(c*x + b)

来源:学生作业帮 编辑:搜搜做题作业网作业帮 分类:综合作业 时间:2024/06/23 13:30:53
用C#语言编程绘图输出y=a*sin(c*x + b)
要求:
系数a,b和c是可以变化的,可考虑是从键盘输入的,或通过Main方法参数传递的;
x的取值范围在0~4π之间,x,y值采用四舍五入;结果如下图:
#
# #
# # #
# #
#
用C#语言编程绘图输出y=a*sin(c*x + b)
啥?控制台输出正弦函数曲线?头一次遇到这种题,试着做了一下. 参考代码:using System;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "绘制 y = a * sin(c * x + b) 正弦曲线图像";
            while (true)
            {
                try
                {
                    double a, b, c;
                    Console.Write("请输入 a:");
                    a = Convert.ToDouble(Console.ReadLine());
                    if (a == 0) throw new Exception("a 不能为0.");
                    Console.Write("请输入 b:");
                    b = Convert.ToDouble(Console.ReadLine());
                    Console.Write("请输入 c:");
                    c = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine();
                    
                    // 纵坐标 y 取值从 -a 到 a
                    // 0.2 越小画的范围越大,曲线越精细
                    for (double y = -a ; y <= a; y += 0.2)
                    {
                        // 横坐标 x 取值从 0 到 4π
                        for (double x = 0; x <= Math.PI * 4; x += 0.2)
                        {
                            if (Math.Abs(y + Math.Sin(c * x + b)) < 0.092) // 一个字符高度大约0.46,0.092 = 0.2 * 0.46
                                Console.Write("#");
                            else
                                Console.Write(" ");
                        }
                        Console.WriteLine();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\r\n错误!\r\n" + ex.ToString());
                }
                Console.WriteLine("\r\n按任意键继续 ...");
                Console.ReadKey();
                Console.Clear();
            }
        }
    }
} 运行结果: