usingSystem;usingSystem.CodeDom;usingSystem.Collections.Generic;usingCSC=System.CodeDom.CodeStatementCollection;usingCVD=System.CodeDom.CodeVariableDeclarationStatement;usingCC=System.CodeDom.CodeConditionStatement;usingCTR=System.CodeDom.CodeTypeReferenceExpression;usingCMR=System.CodeDom.CodeMethodReferenceExpression;usingCMI=System.CodeDom.CodeMethodInvokeExpression;usingCVR=System.CodeDom.CodeVariableReferenceExpression;usingCP=System.CodeDom.CodePrimitiveExpression;usingCBO=System.CodeDom.CodeBinaryOperatorExpression;classProgram{staticvoidMain(string[]args){GenSort(5,Console.Out);Console.ReadKey();}staticvoidGenSort(intn,System.IO.TextWriterwriter){string[]vName="a b c d e f g h i j k l m n o p q r s t u v w x y z".Split(' ');CodeCompileUnitcompileUnit=newCodeCompileUnit();CodeNamespacename=newCodeNamespace();compileUnit.Namespaces.Add(name);name.Imports.Add(newCodeNamespaceImport("System"));CodeTypeDeclarationtype=newCodeTypeDeclaration("Sort");name.Types.Add(type);CodeMemberMethodmain=newCodeEntryPointMethod();type.Members.Add(main);CMIreadLine=newCMI(newCMR(newCTR("Console"),"ReadLine"));CMIintParse=newCMI(newCMR(newCTR("System.Int32"),"Parse"),readLine);for(inti=0;i<n;i++)main.Statements.Add(newCVD(typeof(int),vName[i],intParse));CMRwriteLine=newCMR(newCTR("Console"),"WriteLine");string[]p=newstring[n];for(inti=0;i<n;i++)p[i]=string.Format("{{{0}}}",i);CPformat=newCP(string.Join(" ",p));CVR[]vars=newCVR[n];for(inti=0;i<n;i++)vars[i]=newCVR(vName[i]);IList<int>orderd=newList<int>(n);IList<int>notyet=newList<int>(n);for(inti=0;i<n;i++)notyet.Add(i);GenIf(main.Statements,orderd,notyet,writeLine,format,vars);(newMicrosoft.CSharp.CSharpCodeProvider()).GenerateCodeFromCompileUnit(compileUnit,writer,(newSystem.CodeDom.Compiler.CodeGeneratorOptions()));}staticvoidGenIf(CSCstatements,IList<int>compared,IList<int>notyet,CMRwriteLine,CPformat,CVR[]vars){if(notyet.Count==0){CMImethod=newCMI(writeLine);method.Parameters.Add(format);foreach(intindexincompared)method.Parameters.Add(vars[index]);statements.Add(method);return;}intcurrent=notyet[0];notyet.RemoveAt(0);if(compared.Count==0){compared.Add(current);GenIf(statements,compared,notyet,writeLine,format,vars);return;}for(inti=0;i<compared.Count;i++){CCifStatement=newCC();statements.Add(ifStatement);ifStatement.Condition=newCodeBinaryOperatorExpression(vars[current],CodeBinaryOperatorType.LessThan,vars[compared[i]]);compared.Insert(i,current);GenIf(ifStatement.TrueStatements,compared,notyet,writeLine,format,vars);compared.RemoveAt(i);statements=ifStatement.FalseStatements;}compared.Add(current);GenIf(statements,compared,notyet,writeLine,format,vars);}}
matarillo
#4853()
[
C#
]
Rating0/0=0.00
stringでだらだら書くのは.NETらしくないので、CodeDOMで書いてみた。
Rating0/0=0.00-0+
[ reply ]