1

我对 c# 很陌生。命令行参数有问题。我想要做的是利用第三个 cmd 行参数并写入它。我已经指定了要写入的文件的路径和其他内容。但这里的问题是我可以从用户定义的函数访问命令行参数(例如;args [3])吗?我们怎么做?下面是我的代码。

public class Nodes
{
public bool isVisited;
public string parent;
public string[] neighbour;
public int nodeValue;

public Nodes(string[] arr, int nodeValue)
{
    this.neighbour = new string[arr.Length];
    for (int x = 0; x < arr.Length; x++)
        this.neighbour[x] = arr[x];//hi...works??
    this.isVisited = false;
    this.nodeValue = nodeValue;
}


}

public class DFS
{
static List<string> traversedList = new List<string>();

static List<string> parentList = new List<string>();
static BufferBlock<Object> buffer = new BufferBlock<object>();
static BufferBlock<Object> buffer1 = new BufferBlock<object>();
static BufferBlock<Object> buffer3 = new BufferBlock<object>();
static BufferBlock<Object> buffer2 = new BufferBlock<object>();

public static void Main(string[] args)
{

    int N = 100;
    int M = N * 4;
    int P = N * 16;

    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();

    List<string> global_list = new List<string>();


    StreamReader file = new StreamReader(args[2]);

    string text = file.ReadToEnd();

    string[] lines = text.Split('\n');


    string[][] array1 = new string[lines.Length][];
    Nodes[] dfsNodes = new Nodes[lines.Length];

    for (int i = 0; i < lines.Length; i++)
    {
        lines[i] = lines[i].Trim();
        string[] words = lines[i].Split(' ');

        array1[i] = new string[words.Length];
        dfsNodes[i] = new Nodes(words, i);
        for (int j = 0; j < words.Length; j++)
        {
            array1[i][j] = words[j];
        }
    }
    StreamWriter sr = new StreamWriter(args[4]);

    int startNode = int.Parse(args[3]);

    if (args[1].Equals("a1"))
    {
        Console.WriteLine("algo 0");
        buffer.Post(1);
        dfs(dfsNodes, startNode, "root");
    }
    else if (args[1].Equals("a2"))
    {
        Console.WriteLine("algo 1");
        buffer1.Post(1);
        dfs1(dfsNodes, startNode, "root",sr);
    }
    else if (args[1].Equals("a3"))
    {
        buffer3.Post(1);
        List<string> visitedtList = new List<string>();
        Console.WriteLine("algo 2");
        dfs2(dfsNodes, startNode, "root", visitedtList,sr);
    }

    stopwatch.Stop();

    Console.WriteLine(stopwatch.Elapsed);
    Console.ReadLine();
}

public static void dfs(Nodes[] node, int value, string parent,StreamWriter sr1)
{
    int id = (int)buffer.Receive();
    sr1=new StreamWriter(arg
    Console.WriteLine("Node:" + value + " Parent:" + parent + " Id:" + id);
    sr1.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
    id++;
    traversedList.Add(value.ToString());
    buffer.Post(id);
    for (int z = 1; z < node[value].neighbour.Length; z++)
    {
        if (!traversedList.Contains(node[value].neighbour[z]))
        {
            dfs(node, int.Parse(node[value].neighbour[z]), value.ToString(),sr1);
        }

    }
    return;



}

public static void dfs1(Nodes[] node, int value, string parent, StreamWriter sr)
{
    int id = (int)buffer1.Receive();
    sr.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
    node[value].isVisited = true;
    node[value].parent = parent;
    id++;
    buffer1.Post(id);
    for (int z = 1; z < node[value].neighbour.Length; z++)
    {
        buffer2.Post(node[int.Parse(node[value].neighbour[z])]);
        if (!isVisited())
        {
            dfs1(node, int.Parse(node[value].neighbour[z]), value.ToString(),sr);
        }

    }
    return;



}

public static void dfs2(Nodes[] node, int value, string parent, List<string> visitedtList, StreamWriter sr)
{
    int id = (int)buffer3.Receive();
    sr.Write("Node:" + value + " Parent:" + parent + " Id:" + id);
    id++;
    visitedtList.Add(value.ToString());
    buffer3.Post(id);
    for (int z = 1; z < node[value].neighbour.Length; z++)
    {
        buffer2.Post(node[int.Parse(node[value].neighbour[z])]);
        if (!visitedtList.Contains(node[value].neighbour[z]))
            dfs2(node, int.Parse(node[value].neighbour[z]), value.ToString(), visitedtList,sr);

    }
    return;



}

public static bool isVisited()
{
    Nodes node = (Nodes)buffer2.Receive();
    return node.isVisited;
}

}

所以问题是我想将每个 dfs 的输出写入指定为命令行参数的文件。那么我可以访问 dfs、dfs1 方法中的 args 吗???谢谢你。

4

3 回答 3

4

您可以保留一个静态字段来保存它,或者只使用Environment.GetCommandLineArgs().

于 2012-06-04T23:07:00.540 回答
3

好吧,以最简单的形式,只需将其保存以备后用

class Program
{
    static string _fpath;
    static void Main(string[] args)
    {
        // ...stuff
        _fpath = args[3];
    }

    static void WriteFile()
    {
        using(var stream = File.Open(_fpath, ...))
        {
            // write to file
        }
    }
}

不一定完全是我会怎么做,但你明白了。

另外,关于这段代码......

this.neighbour = new string[arr.Length];
for (int x = 0; x < arr.Length; x++)
    this.neighbour[x] = arr[x];//hi...works??

你可以简单地写

this.neighbour = arr;

啊,托管代码的奇迹:D。无需将元素复制到第二个数组。当然,您需要考虑这样一个事实,即参数数组 ( arr) 中元素的更改现在将反映在您的内部数组中。

于 2012-06-04T23:09:38.713 回答
2

最好将参数传递给函数,而不是依靠某种“隐藏”的方式来传递它们。

静态变量和 GetCommandLineArgs 都有助于以隐藏的方式传递它们(如其他答案中所指出的)。缺点更难测试(因为需要设置静态共享依赖项),并且对于未来的读者来说,存在这种隐藏的依赖项不太清楚。

于 2012-06-04T23:17:49.257 回答