昨天一个同学叫我帮编写一个符号三角形的c代码,今天就把它改写成用CodeDom生成的c#代码。
符号三角形:在一组字符串里(只有用空格分割的+或者-组成的字符串),在每次的相邻的两个符号比较,如果相同就在中间空格插入+,否则就插入-。一直运行到字符串里只有一个+或者-时停止,输出的字符串为符号三角形。
由于是CodeDom些列,所以先介绍几个CodeDom表达式:
1:CodeConditionStatement:判断语句即是if(condition){} else{},看最全的那个构造函数:
public CodeConditionStatement( CodeExpression condition,//条件 CodeStatement[] trueStatements,//为true的语句体 CodeStatement[] falseStatements//为false语句体 )2:CodeIterationStatement():表示 for 语句或语句块内的循环(使用测试表达式作为继续循环的条件):在codedom中没有提高while和dowhile但是For已经够用的public CodeIterationStatement( CodeStatement initStatement,//for初始化 CodeExpression testExpression,//条件表达式 CodeStatement incrementStatement,//for变化体,增或减 CodeStatement[] statements//循环体 )3:CodeBinaryOperatorExpression:表示一个表达式,该表达式包含在两个表达式间进行的二进制运算,public CodeBinaryOperatorExpression( CodeExpression left,//表达式左边 CodeBinaryOperatorType op,//操作符 CodeExpression right//表达式右边 )4:CodeArrayIndexerExpression:表示对数组的索引的引用:public CodeArrayIndexerExpression( CodeExpression targetObject,//数组对象 CodeExpression[] indices//下标 )其他参见CodeDOM 快速参考:msdn.microsoft.com/zh-cn/library/f1dfsbhc(VS.80).aspxcode:public CodeNamespace CreateNameSpace() { public CodeNamespace CreateNameSpace() { //Test CodeMemberMethod test = new CodeMemberMethod(); test.Name = "Test"; test.Attributes = MemberAttributes.Public | MemberAttributes.Final; test.Statements.Add(new CodeVariableDeclarationStatement(typeof(char[]), "ch",new CodeMethodInvokeExpression(new CodePrimitiveExpression("+ - + - + + - -"), "ToCharArray",new CodeExpression[] { }))); test.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "FuHaoSanJiao"), new CodeExpression[] { new CodeVariableReferenceExpression("ch"),new CodePrimitiveExpression(0) })); test.Statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"), "Read")); //FuHaoSanJiao CodeMemberMethod fuHaoSanJiao = new CodeMemberMethod(); fuHaoSanJiao.Name = "FuHaoSanJiao"; fuHaoSanJiao.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(char[])),"ch")); fuHaoSanJiao.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(int)), "start")); fuHaoSanJiao.Statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"),"WriteLine", new CodeExpression[] { new CodeObjectCreateExpression(typeof(string), new CodeArgumentReferenceExpression("ch")) })); fuHaoSanJiao.Statements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeArgumentReferenceExpression("start"), CodeBinaryOperatorType.GreaterThanOrEqual, (new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("ch"),"Length"), CodeBinaryOperatorType.Divide ,new CodePrimitiveExpression(2)))), new CodeMethodReturnStatement())); CodeBinaryOperatorExpression condition=new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("ch"),"Length"), CodeBinaryOperatorType.Subtract ,new CodeArgumentReferenceExpression("start")), CodeBinaryOperatorType.Subtract, new CodePrimitiveExpression(1)));//for 条件 CodeConditionStatement iterationBody=new CodeConditionStatement(new CodeBinaryOperatorExpression( new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"),new CodeVariableReferenceExpression("i")), CodeBinaryOperatorType.IdentityEquality, new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"), new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"),CodeBinaryOperatorType.Add,new CodePrimitiveExpression(2)))), new CodeAssignStatement( new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"),new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add,new CodePrimitiveExpression(1))), new CodePrimitiveExpression('+'))); iterationBody.FalseStatements.Add(new CodeAssignStatement( new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"),new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add,new CodePrimitiveExpression(1))), new CodePrimitiveExpression('-'))); CodeAssignStatement iteerationbody2 = new CodeAssignStatement(new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"), new CodeVariableReferenceExpression("i")), new CodePrimitiveExpression(' ')); fuHaoSanJiao.Statements.Add(new CodeIterationStatement(new CodeVariableDeclarationStatement(typeof(int),"i", new CodeArgumentReferenceExpression("start")), condition, new CodeAssignStatement(new CodeVariableReferenceExpression("i"),new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add, new CodePrimitiveExpression(2))), new CodeStatement[] { iterationBody, iteerationbody2 })); // ch[ch.Length - start - 1] = ' '; fuHaoSanJiao.Statements.Add(new CodeAssignStatement( new CodeArrayIndexerExpression(new CodeArgumentReferenceExpression("ch"),new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("ch"), "Length"), CodeBinaryOperatorType.Subtract ,new CodeArgumentReferenceExpression("start")),CodeBinaryOperatorType.Subtract, new CodePrimitiveExpression(1))),new CodePrimitiveExpression(' '))); // FuHaoSanJiao(ch, start + 1); fuHaoSanJiao.Statements.Add(new CodeMethodInvokeExpression(new CodeThisReferenceExpression(),"FuHaoSanJiao",new CodeArgumentReferenceExpression("ch") , new CodeBinaryOperatorExpression(new CodeArgumentReferenceExpression("start"),CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1)))); CodeTypeDeclaration codeDomDemo2 = new CodeTypeDeclaration("CodeDomDemo2"); codeDomDemo2.Members.Add(test); codeDomDemo2.Members.Add(fuHaoSanJiao); codeDomDemo2.Attributes = MemberAttributes.Public; codeDomDemo2.Comments.Add(new CodeCommentStatement("this code is from CodeDom!")); //codeDomDemo2.Members.AddRange(); CodeNamespace nspace = new CodeNamespace("CodeDomDemo2"); nspace.Imports.Add(new CodeNamespaceImport("System")); nspace.Types.Add(codeDomDemo2); return nspace; } } }输出代码为 :
namespace CodeDomDemo2 { using System; // this code is from CodeDom! public class CodeDomDemo2 { public void Test() { char[] ch = "+ - + - + + - -".ToCharArray(); this.FuHaoSanJiao(ch, 0); System.Console.Read(); } private void FuHaoSanJiao(char[] ch, int start) { System.Console.WriteLine(new string(ch)); if ((start >= (ch.Length / 2))) { return; } for (int i = start; (i < ((ch.Length - start) - 1)); i = (i + 2)) { if ((ch[i] == ch[(i + 2)])) { ch[(i + 1)] = '+'; } else { ch[(i + 1)] = '-'; } ch[i] = ' '; } ch[((ch.Length - start) - 1)] = ' '; this.FuHaoSanJiao(ch, (start + 1)); } } }代码下载Demo1,Demo2