有人可以帮我一些评论 [英] Can someone help me with some commentary

查看:42
本文介绍了有人可以帮我一些评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解释一下这个程序的解释吗?



Could someone help me with some commentary as in explain whats going on this program?

        static string[] groups = { "AEIOUHWY", "BFPV", "CGJKQSXZ", "DT", "L", "MN", "R" }; 

       static string getCode(string str)
        {
            string tempstr = "";
            int i;

            for (i = 0; i < 7; i++)
            {
                if (groups[i].Contains(str))
                {
                    tempstr = i.ToString();
                    break;
                }
            }
            return tempstr;
        }

        static string encodeString(string str)
        {
            string tempstr = "";
            int i;

            for (i = 0; i < str.Length; i++)
            {
                tempstr = tempstr + getCode(str.Substring(i, 1));
            }
            return tempstr;
        }


        static string removeZeroes(string str)
        {
            string tempstr = "";
            int i;


            for (i = 0; i < str.Length; i++)
            {
                if (str.Substring(i, 1) != "0")
                {
                    tempstr = tempstr + str.Substring(i, 1);
                }
            }
            return tempstr;
        }

        static string RemoveAdjacent(string str)
        {
            string tempstr = "";
            int i;

            string stringToCheckAgainst = "#";

            for (i = 0; i < str.Length; i++)
            {
                if (str.Substring(i, 1) != stringToCheckAgainst)
                {
                    tempstr = tempstr + str.Substring(i, 1);
                    stringToCheckAgainst = str.Substring(i, 1);
                }
            }
            return tempstr;
        }
    }
}





谢谢



Thank you

推荐答案

为了培养你的编程思维,你需要学习如何研究代码执行时的代码,而且,imho,没有替代动手实验。



你是否通过学习语法并在字典中查找来学习一种对你来说不熟悉的口语:或者,你是通过口语和练习来学习的吗? 生活语言与母语人士互动,犯错误,分析错误,正式学习?



我建议您在Visual Studio中创建一个新项目(Windows窗体),向项目添加一个类,您可以像这样生成静态和公共:
To develop your mind for programming, you need to learn how to study what code does as it is executed, and, imho, there's no substitute for hands-on experimentation.

Do you learn a spoken language that is new to you by just studying its grammar, and looking it up in a dictionary: or, do you learn by speaking and practicing the living language in interaction with native speakers, making mistakes, analyzing the mistakes, and formal study ?

I suggest you create a new Project (Windows Forms) in Visual Studio, add a Class to the Project which you make static and public like this:
namespace TestNameSpace
{
    public static class TestCodeClass
    {
       // insert all the code you show in your example

       // add the access modifier 'public for all the methods
    }
}

完成后,转到表单,并创建一些这样的测试用例:

Once you've done that, go to the Form, and create some test cases like this:

// in the Form Load EventHandler, or somewhere in a method, or Button Click EventHandler:

// set a break-point here and single-step using F11 through the code
// observing the value of the variables in the 'getCode method
string testStr1 = TestCodeClass.getCode("L");
Console.WriteLine(testStr1);

// set a break-point here and single-step using F11 through the code
// observing the value of the variables in the 'encodeString and 'getCode methods
string testStr2 = TestCodeClass.encodeString("LRwhateverBFPVetc.");
Console.WriteLine(testStr2);

应用相同的技术来研究'removeZeroes和'RemoveAdjacent方法。



我有看到许多只有一两个月的学生经常练习分析代码在运行的科学和艺术,使用断点和单步,伴随着正式的指导和勤奋的学习,在编程语言中变得非常流利,开始达到他们可以在字面上阅读代码并设想内部行为的美妙点。



当前工具,IDE,用于代码开发,如Visual Studio,提供了一个惊人的调试工具集,与普通程序员甚至十年前常用的工具集相比。



CodeProject有一篇Abhisit Jana撰写的精彩文章在Visual Studio 2010中掌握调试 - 初学者指南[ ^ ]涵盖Visual Studio用于调试和代码分析的工具的基础知识和高级用法。

Apply the same technique to study the 'removeZeroes and 'RemoveAdjacent methods.

I have seen many students with only a month or two of regularly practicing the science and art of analyzing code on-the-run, using break-points, and single-stepping, accompanied by formal instruction, and diligent study, become very "fluent" in a programming language, start to reach that wonderful point where they can, literally, read code and envision how it behaves internally.

The current tools, IDE's, for code development, like Visual Studio, offer an amazing toolset for debugging compared to what was commonly available to the mere-mortal programmer even ten years ago.

CodeProject has an excellent article by Abhisit Jana "Mastering Debugging in Visual Studio 2010 - A Beginner's Guide" [^] covering fundamentals and advanced use of Visual Studio's tools for debugging and code analysis.


Hello,



首先,

Hello,

Firstly,
Quote:

static string [] groups = {AEIOUHWY,BFPV,CGJKQSXZ,DT,L,MN,R};



static string[] groups = { "AEIOUHWY", "BFPV", "CGJKQSXZ", "DT", "L", "MN", "R" };





- 这是一个包含7个字符串的数组。









其次:



- This is an array of 7 strings.




Secondly:

引用:

静态字符串getCode(字符串str)

{

string tempstr =;

int i;



for(i = 0; i< 7; i ++)

{

if(groups [i] .Contains(str))

{

tempstr = i.ToString();

break ;

}

}

返回tempstr;

}

static string getCode(string str)
{
string tempstr = "";
int i;

for (i = 0; i < 7; i++)
{
if (groups[i].Contains(str))
{
tempstr = i.ToString();
break;
}
}
return tempstr;
}





- 这个函数有一个输入(参数),它是一个字符串和一个输出(一个返回值)也是一个字符串。

- 有2个场景:

1)你从数组中传递一个字符串:groups []到我这里thod。

- 在这种情况下,for循环运行7次(直到数组的长度:到达group [])。因此,而不是明确地指定7最好是说:groups.Length;使它更通用。

- 条件满足是因为:



- This function has an input(argument)which is a "String" and an output(a return value) which is also a string.
- There are 2 scenarios:
1) You pass a string from the array: groups[] into this method.
- In this case the for loop runs for 7 times(till length of the array: group[] is reached). So instead of specifying 7 explicitly best is to say : groups.Length; to make it more generic.
- the condition is satisfied because:

引用:

if(groups [i] .Contains(str))

if (groups[i].Contains(str))



评估为真。



- 现在变量tempstr在条件为真时指定for循环的索引,即i。你不能为字符串分配一个int,因为这些类型之间没有关系。



- 所以你需要将它转换为字符串。所以我说i.ToString()。所以我变成了一个字符串。

现在你摆脱了循环。因为没有必要进一步检查。如果我添加一个休息时间,那么将进行进一步的检查,这是不需要的。



- 现在这个i以字符串的形式作为代码返回(因为该方法的名称建议使用getcode())。



示例:我们说我将BFPV传递给方法getcode,如下所示:




evaluates to be true.

- Now to the variable "tempstr" assign the index of the for loop at that instant when the condition is true which is i. You cant assign an int to a string because there is no relation between these types.

- So you need to convert it to a string. So i say i.ToString(). So i becomes a string.
Now you break out of the loop. Because there is no need to check further. If i dint add a break then further checking would be done which is not required.

- Now this "i" is returned in the form of string as a code(as the name of the method suggests getcode()).

Example: Let us say i pass "BFPV" to the method getcode like this:

引用:

getCode(BFVP)

getCode("BFVP")





它的返回类型是一个字符串,所以如果我说:







Its return type is a string so if i say:


string code = getCode("BFVP");





代码将评估为:1。因为它是数组中的第二个位置和基于零的索引,因此它的评估结果为1.



正如先生所说,





code will evaluate to be : 1. Because it is the second location in the array and a zero based index so it evaluates to be 1.

As Sir said,

引用:

使用break-积分和单步,伴随着正式的指导和勤奋的学习,在编程语言中变得非常流利, tart到达那个奇妙的地方,他们可以从字面上阅读代码并设想它在内部的行为。

using break-points, and single-stepping, accompanied by formal instruction, and diligent study, become very "fluent" in a programming language, start to reach that wonderful point where they can, literally, read code and envision how it behaves internally.





你可以评估任何代码,但请记住,当代码在该行中断时,指令未执行,因此您将找不到与该语句相关的任何结果。



在下一行添加断点,以便你会得到你想要的调试结果。



家庭作业:尝试从阵列中的其他值:群组[]。 :)。



场景2:



- 你没有传递group []数组中的元素:循环执行
,但条件评估为false。因此返回一个空字符串。

这意味着我可以说tempstr = string.empty;因此返回一个空字符串。所以代码是空的。







现在继续前进,下一个功能:





You can evaluate any thing any piece of code, but remember when the code breaks at that line that instruction is not executed so you wont find any result associated with that statement.

Add a breakpoint at the next line so you will get the debugging results you want.

Homework : try for other values from the array : groups[]. :).

Scenario 2:

- You dont pass an element from the group[] array:
the loop executes, but condition evaluates to be false. So an empty string is returned.
Which means i can say tempstr = string.empty; So an empty string is returned. So the code is empty.



Now moving ahead, the next function:

static string encodeString(string str)
    {
        string tempstr = "";
        int i;

        for (i = 0; i < str.Length; i++)
        {
            tempstr = tempstr + getCode(str.Substring(i, 1));
        }
        return tempstr;
    }







为此你需要了解Substring()的重要性。如果我有一个字符串说Rahul。

我说Rahul.Substring(i,1);



- 这个语句返回R,因为substring返回一个charecter字符串,第一个参数i表示从哪里开始提取,下一个参数意味着:多少提取?

- 如果i = 0;结果将是R。



查看代码片段:






For this you need to understand the significance of Substring(). If i have a string say "Rahul".
I say "Rahul".Substring(i,1);

- this statement returns "R", because substring returns a single charecter string, the first parameter i means from where to start extracting and the next parameter means: how many to extract?
- If i = 0; the result will be "R".

Look at the code snippet:

for (i = 0; i &lt; str.Length; i++)
 {
     tempstr = tempstr + getCode(str.Substring(i, 1));
 }





你传递给这个函数的字符串str由charecter评估。



- 每个字符都传递给getCode以获取字符串代码并附加到tempstr。

- 所以最终你会得到一个编码的字符串,用于你传递的字符串。







继续前进:





The string "str" you passed into this function is evaluated charecter by charecter.

- Every charecter is passed to getCode to get the string code and is appended to tempstr.
- So eventually you will get an encoded string for the string you pass.



Moving ahead:

static string removeZeroes(string str)
       {
           string tempstr = "";
           int i;


           for (i = 0; i < str.Length; i++)
           {
               if (str.Substring(i, 1) != "0")
               {
                   tempstr = tempstr + str.Substring(i, 1);
               }
           }
           return tempstr;
       }







- 您传递的字符串将逐个字符地进行零点评估。

- 如果它不包含零,则附加对应于索引i的特定字符。将它分配给tempstr。

- 如果它包含零返回string.Empty。这意味着一个空字符串。

- 这意味着零删除。





下一个代码片段:< br $>





- The string you passed is evaluated character by character for zeros.
- If it does not contain a zero then append that particular charecter corresponding to the index i. assign it to tempstr.
- If it contains a zero return string.Empty. Which means an empty string.
- Which means zero removed.


The next code snippet:

static string RemoveAdjacent(string str)
      {
          string tempstr = "";
          int i;

          string stringToCheckAgainst = "#";

          for (i = 0; i < str.Length; i++)
          {
              if (str.Substring(i, 1) != stringToCheckAgainst)
              {
                  tempstr = tempstr + str.Substring(i, 1);
                  stringToCheckAgainst = str.Substring(i, 1);
              }
          }
          return tempstr;
      }







- 在这里你传递一个字符串让我们说Rahul。这将返回Rahul。为什么?

- 条件




- In this you pass a string let us say "Rahul". This returns "Rahul". Why?
- The condition

引用:

if(str.Substring(i) ,1)!= stringToCheckAgainst)

if (str.Substring(i, 1) != stringToCheckAgainst)





评估为真,tempstr建立为Rahul。因此Rahul被归还。



尝试:Rahu#,#和其他组合你发现了什么以及为什么?试试看,请告诉我。



按f11单步。





谢谢,

- Rahul



evaluates true and "tempstr" builds up to "Rahul". And hence "Rahul" is returned.

Try out: "Rahu#","#" and other combinations what do you find and why? Just try out and please let me know.

Press f11 to single step.


Thanks,
- Rahul


这篇关于有人可以帮我一些评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆