0

我需要使用数字数组(强制)和 int 时间(可选)从 cmd 调用程序。我从来没有这样做过,所以我对细节有点动摇。

路径是D:\Robert\FactorialConsoleApplication\FactorialConsoleApplication\bin\Debug\FactorialConsoleApplication.exe

如您所知,程序计算数组中数字的阶乘。int 时间用作延迟以显示堆栈的进度。

如何使用参数调用程序?提前致谢!

PS这是一些代码

class Program
{
    public static void Progress(ProgressEventArgs e)
    {
        int result = e.getPartialResult;
        int stack_value = e.getValue ;
        double max = System.Convert.ToDouble(numbers[j]);
        System.Convert.ToDouble(stack_value);
        double percent = (stack_value / max) * 100;

        Console.CursorLeft = 18;
        Console.Write(result + " ");
        Console.CursorLeft = 46;
        Console.Write(System.Convert.ToInt32(percent) + "%      ");

    }
    public static void Calculate(int number, int time=0)
    {

        Factorial Fact = new Factorial();
        Fact.Progression += new Factorial.ProgressEventHandler(Progress);
        Console.Write("\n" + "Partial results : ");
        Console.CursorLeft = 35;
        Console.Write("Progress : ");         
        int Result = Fact.CalculateFactorial(number, time);
        Console.WriteLine(" ");
        Console.WriteLine("The factorial of " + number + " is : " + Result);


        Console.ReadLine();
    }

    static int j;
    static int[] numbers;


    public static void Main(string[] args)
    {
        int i=0;
        bool ok = false;
        string line = string.Empty;
        numbers = new int[10];
        Console.Write("Please insert wait time (0,1 or 2) : ");
        int time = int.Parse(Console.ReadLine()) * 1000;
        Console.Write("Please insert a number : ");
        do
        {
            line = Console.ReadLine();
            if (line != "")
            {
                i++;
                numbers[i] = int.Parse(line);


            }
            else
            {
                ok = true;
            }
        }
        while (ok == false);
        for (j = 1; j <= i; j++)
        {

            Calculate(numbers[j],time);
        }

    }
}
4

2 回答 2

0

在 .net 中,您可以使用System.Diagnostics 中的Process.Start来启动应用程序,您也可以传递参数

For example Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm"); will open Internet Explorer and pass the value "C:\\myPath\\myFile.htm" as parameter to it.For more examplles check the MSDN article on Process.Start method

Update If in case you are looking to take parameters to your application, when launching itself you don't have to do anything, you are already doing that, the parameter args in your Main method will hold the arguments passed to your application, just try and parse those values in the args array to int array and you are good to go.

于 2012-08-01T12:43:28.903 回答
0

Ok, so here is the solution.

I used an args parser like this :

static int extra;
    public static void Main(string[] args)
    {

        foreach (string s in args)
        {
            extra = int.Parse(s);


            Calculate(extra);
        }
     }

And I changed :double max = System.Convert.ToDouble(numbers[j]); to :double max = System.Convert.ToDouble(extra);

To call it I open cmd in the directory where the exe is and I type :

Program.exe 3 4 5

It will calculate the factorials of 3, 4 and 5 respectively

于 2012-08-02T06:18:51.960 回答