C # implement the calculation of any large number and proof of a simple logical proposition -- Preface,

Source: Internet
Author: User

C # implement the calculation of any large number and proof of a simple logical proposition -- Preface,
Introduction

This is my graduation project. I have always wanted to write it into my documents, but four years have passed without caution (this time I can go to another university ). Now, set a goal for yourself. All the key points of the project will be sorted out in one month. Otherwise, I am afraid that four years have passed, and the code is still on the hard disk.

The project name is MathAssist, and vs2008 is used. Divided into four sub-projects:

The main program can scan available commands from the plug-in, or display available forms in the plug-in. The following is the main program interface, which is loaded with two plug-ins: "superCalculator" and "command proof ". They provide the command cal prove.

Click the sub-menu of the menu item "plug-in" to open the windows implemented in the two plug-ins. For example

The proof of the calculation of large numbers and logical propositions is achieved respectively.

In this preface, we will introduce only the implementation of the plug-in mechanism. The proof of the calculation of large numbers and logical propositions will be left to the subsequent series.

Implementation of the plug-in mechanism MathAssistLibrary Interface Definition

In the MathAssistLibrary project, only two interfaces are defined: ICommand and IForm, which are used to provide command line and window functions respectively.

1 // <summary> command interface </summary> 2 public interface ICommand 3 {4 // <summary> command Name </summary> 5 string Name {get ;} 6 7 /// <summary> Execute Command </summary> 8 /// <param name = "cmd"> return the command parameter </param> 9 /// <returns> result </returns> 10 string Excute (string cmd ); 11 12 // <summary> Describe the command usage </summary> 13 string Describe {get ;} 14} 15 /// <summary> obtain the form of the plug-in </summary> 16 public interface IForm17 {18 /// <summary> form name </summary> 19 string Text {get ;} 20 21 /// <summary> Form object </summary> 22 Form GetForm {get;} 23}MathAssistLibrary

ICommand Interface

  • Name indicates the command Name.
  • Excute is used to execute commands
  • Describe is used to provide a brief help document for this command.

IForm Interface

  • Text indicates the form name.
  • GetForm is used to obtain the Form object. After obtaining the Form object in the main program, call Show () to display it.
Plugin implementation

In the plug-in project, you only need to implement the ICommand and IForm interfaces. Take SuperCalculator as an example:

Public partial class frmSuperCalculator: Form, IForm {string IForm. text {get {return "Calculator" ;}} frmSuperCalculator frm; Form IForm. getForm {get {if (frm = null | frm. isDisposed) {frm = new frmSuperCalculator ();} return frm ;}}...}
Public class Calculator: MathAssistLibrary. ICommand {string MathAssistLibrary. ICommand. Describe {get {return "cal command can be used to perform mathematical operations. For example, cal 1 + max (2, 3) * 2 ";} string MathAssistLibrary. ICommand. name {get {return "cal" ;}} string MathAssistLibrary. ICommand. excute (string cmd) {try {Expression exp = new Expression (); exp. format = cmd; return exp. calculator (). toString ();} catch (ExpressionException e) {return string. format ("expression error. Error Type: {0}, error location {1} ", e. Message, e. Index );}}}

 

The main program scans the plug-in

The FindDllFile () function finds all dll files in the same path as the program. The Code is as follows:

1 List <string> FindDllFile (string foldername) {2 DirectoryInfo dir = new DirectoryInfo (foldername); 3 FileInfo [] files = dir. getFiles (); 4 List <string> result = new List <string> (); 5 6 foreach (FileInfo fi in files) {7 if (fi. name. toUpper (). endsWith (". DLL ") 8 result. add (fi. fullName); 9} 10 return result; 11}FindDllFile

LoadOne () finds a specific type from a file and returns its object. The Code is as follows:

        private List<object> LoadOne(string filename, Type type) {            List<object> result = new List<object>();            try {                Assembly ass = Assembly.LoadFrom(filename);                Module[] mods = ass.GetModules();                foreach (Module mod in mods) {                    Type[] typs = mod.GetTypes();                    foreach (Type typ in typs) {                        if (type.IsAssignableFrom(typ)) {                            result.Add(ass.CreateInstance(typ.FullName));                        }                    }                }            } catch (BadImageFormatException) {            }            return result;        } // end func

First use Assembly. loadFrom () loads the Assembly, obtains all modules, and finally uses type in all modules. isAssignableFrom () finds the type that matches the input parameter type. If yes, an object is created and a result is returned.

In the main program, use the following two lines of code to call LoadOne ()

List<object> cmd = LoadOne(filename, typeof(ICommand));List<object> frm = LoadOne(filename, typeof(IForm));

In this way, cmd. Excute () can be used to execute the Code implemented in the plug-in, and frm. Show () can be used to display the forms implemented in the plug-in.

 

Download path of mathassist.exe. The source code of the entire program will be provided in subsequent articles ~~

 

Parameter reference: http://www.cnblogs.com/conexpress/archive/2009/03/04/MyCalculator_01.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.