プログラミングにまだ慣れていないので、以下に示す問題に手を貸してください。
コードビュー用のテキストボックスと、以下の各問題の結果を表示するためのラベル、および機能を実行するためのボタンで構成されるプログラムがあります。これはプロジェクトの単純なビューです。
解決すべき問題:
- テキストボックス内のコードを読み取り、コードの複雑さを計算するボタンを押したときに関数が必要です。
- コード内の属性とオブジェクトの数を知る必要があります。
- 継承クラスはどこにありますか。
- 遠結合クラスとは何ですか。
実際、私は長い間コードを作成しようとしてきましたが、正しい結果が得られませんでした。私が終えたもの:
- .Cs ファイルからサンプル コードを開きます。
- コードを string Builder に読み取ります。
- 以下のコードで複雑になりますが、結果は完全に間違っています。
これは私のコードです(配列の一致に依存するようにしました):
public int Get_complexity(string SourceCode)
{
int result = 0;
try
{
StringBuilder sb = new StringBuilder();
sb.Append(SourceCode);
char[] delimiterChars = { ' ', '.', '{', '}', '(', ')', ';' };
string SourceCodeText = sb.ToString();
string[] words = SourceCodeText.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
int No_if = FindMatchesInStringArray("if", words);
int No_elseIf = FindMatchesInStringArray("elseIf", words);
int No_while = FindMatchesInStringArray("while", words);
int No_for = FindMatchesInStringArray("for", words);
int No_foreach = FindMatchesInStringArray("foreach", words);
int No_case = FindMatchesInStringArray("case", words);
int No_default = FindMatchesInStringArray("default", words);
int No_finaly = FindMatchesInStringArray("finaly", words);
int No_continue = FindMatchesInStringArray("continue", words);
int No_continues = FindMatchesInStringArray("continues", words);
int No_try = FindMatchesInStringArray("try", words);
int No_catch = FindMatchesInStringArray("catch", words);
int No_and_op = FindMatchesInStringArray("&", words);
int No_or_op = FindMatchesInStringArray("||", words);
int No_words = words.Length;
result = No_if + No_elseIf + No_while + No_for + No_foreach + No_case + No_default + No_finaly + No_continue + No_continues + No_try + No_catch + No_and_op + No_or_op;
}
catch { throw; }
return result;
}
public int FindMatchesInStringArray(string markup, string[] strArray)
{
int result = 0;
try
{
for (int i = 0; i < strArray.Length; i++)
{
if (markup.ToLower() == strArray[i].ToLower())
{
result += 1;
}
}
}
catch
{
throw;
}
return result;
}