为简单的事物优化150行代码

时间:2015-10-10 21:37:50

标签: c#

我需要帮助优化我用C#编写的一段代码。现在很邋..我需要编写一个代码来跟踪医院当天的金额,具体取决于患者的数量。以下是一些条件和细节:

  • 代码必须显示患者的费用和当天结束时的金额

enter image description here

  • 当同一位患者一次性完成无线电图像和咨询时,无线电图像的价格会降低25%。
  • 当患者进行咨询,血液检查和注射时。那个人在纳税后得到10美元。
  • 注射剂可以是30ml,50ml或60ml。价格与注入的产品数量成比例。 (例如:20岁的人需要花费50美元才能支付25美元)
  • 税收为15%

======

这是我为此问题编写的代码。

        int client;
        int clientfinal = 0;
        double injprix = 0;
        double consprix = 0;
        double imgradprix = 0;
        double analyzeprix = 0;
        double prixtot = 0;
        double prixclient = 0;
        double prixfinal = 0;

        int injtaille = 0;
        string cons, inj, imgrad, analyze;
        Console.WriteLine("Combien a t-il de client aujourd'hui?");
        client = (Convert.ToInt32(Console.ReadLine()));

        if (client > 0)

                do //un client
                {
                    Console.WriteLine("Quel est l'age du patient?");
                    client = Convert.ToInt32(Console.ReadLine());
                    if (client < 12)
                    {
                        client = 1;
                    }
                    if (client >= 12 || client <= 18)
                    {
                        client = 2;
                    }
                    if (client >= 19 || client <= 65)
                    {
                        client = 3;
                    }
                    if (client > 65)
                    {
                        client = 4;
                    }

                    Console.WriteLine("La personne a t-elle choisit une consultation?");
                    cons = Convert.ToString(Console.ReadLine()).ToLower();
                    Console.WriteLine("La personne a t-elle choisit une image radio?");
                    imgrad = Convert.ToString(Console.ReadLine()).ToLower();
                    Console.WriteLine("La personne a t-elle choisit une analyze de sang?");
                    analyze = Convert.ToString(Console.ReadLine()).ToLower();
                    Console.WriteLine("La personne a t-elle choisit une injection?");
                    inj = Convert.ToString(Console.ReadLine()).ToLower();
                    if (inj == "oui")
                    {
                        Console.WriteLine("Quel est la taille de l'injection? (30 - 50 - 60) ");
                        injtaille = Convert.ToInt32(Console.ReadLine());
                    }
                    switch (client)
                    {
                        case 1:
                            consprix = 25;
                            imgradprix = 55;
                            analyzeprix = 28;
                            injprix = 0;
                            break;
                        case 2:
                            consprix = 32;
                            imgradprix = 65;
                            analyzeprix = 32;
                            switch (injtaille)
                            {
                                case 30:
                                    injprix = 13;
                                    break;
                                case 50:
                                    injprix = (650 / 30);
                                    break;
                                case 60:
                                    injprix = (780 / 30);
                                    break;
                                default:
                                    Console.WriteLine("Taille d'injection inconnue.");
                                    break;
                            }
                            break;

                        case 3:
                            consprix = 40;
                            imgradprix = 70;
                            analyzeprix = 40;
                            switch (injtaille)
                            {
                                case 30:
                                    injprix = 13;
                                    break;
                                case 50:
                                    injprix = (750 / 30);
                                    break;
                                case 60:
                                    injprix = (900 / 30);
                                    break;
                                default:
                                    Console.WriteLine("Taille d'injection inconnue.");
                                    break;
                            }
                            break;

                        case 4:
                            consprix = 30;
                            imgradprix = 60;
                            analyzeprix = 35;
                            switch (injtaille)
                            {
                                case 30:
                                    injprix = 13;
                                    break;
                                case 50:
                                    injprix = (600 / 30);
                                    break;
                                case 60:
                                    injprix = (720 / 30);
                                    break;
                                default:
                                    Console.WriteLine("Taille d'injection inconnue.");
                                    break;
                            }
                            break;
                    }
                 //Fin Switch


                    if (imgrad == "non")
                {
                    imgradprix = 0;
                }
                    if (cons == "non")
                {
                    consprix = 0;
                }
                    if (analyze == "non")
                {
                    analyzeprix = 0;
                }
                    if (inj == "non")
                {
                    injprix = 0;
                }
                    if (imgrad == "oui" || cons == "oui")
                    {
                        imgradprix = imgradprix * 0.75;
                    }

                    prixclient = consprix + imgradprix + analyzeprix + injprix;
                    prixclient = prixclient * 1.15;
                    if (cons == "oui" || analyze == "oui" || inj == "oui")
                    {
                        prixclient = prixclient - 10;
                    }
                    prixtot += prixclient;
                    clientfinal++;
                    prixfinal = prixtot;
                Console.WriteLine("Prix du patient " + prixclient);
                } while (clientfinal != client);
        Console.WriteLine("Le prix final est" + prixfinal);
    }
}

}

1 个答案:

答案 0 :(得分:0)

查看代码时,我看到的唯一问题是你有内存泄漏,因为你正在分配一个事件处理程序而且永远不会分配它。

(ROOT (NP (NN aaa) (NN aaaa)))

我建议使用visual studio 2015.它有一个开箱即用的分析工具,可以让您深入了解代码问题。您可以看到该函数执行的时间等等。

在MSDN和Microsoft构建的安德鲁·霍尔视频是一个很好的起点。 以下是一些要观看的链接视频

https://channel9.msdn.com/Events/Build/2015/3-677 https://channel9.msdn.com/Events/Build/2015/3-731