[译]用于教学的C#命令行实现.也是命令行计算器(.NET 2)
By robot-v1.0
本文链接 https://www.kyfws.com/applications/a-command-line-implementation-of-c-made-for-teachi-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3531 个词 阅读量 0[译]用于教学的C#命令行实现.也是命令行计算器(.NET 2)
原文地址:https://www.codeproject.com/Articles/12030/A-Command-Line-Implementation-of-C-Made-for-Teachi
原文作者:Timothy the lion
译文由本站 robot-v1.0 翻译
前言
A command line calculator I am writing to help me teach a 9 year old to code
我正在编写的命令行计算器可帮助我教9岁的孩子编程
- 下载源Beta 2.3-58.68 KB(Download source beta 2.3 - 58.68 KB)
- 下载带有示例flet beta 2.3的二进制文件-32.89 KB(Download binary w example flet beta 2.3 - 32.89 KB)
- 下载Visual C#2005 Express(Download Visual C# 2005 express)
- 下载.NET 2.0运行时(Download .NET 2.0 runtime)
介绍(Introduction)
该项目的可执行文件位于(The executable for this project is in the)**箱子(bin)**目录.(directory.)
该源代码是在Visual Studio 2005 C#Express beta 2中开发和编译的,可以在发行版中使用.(The source was developed and compiled in Visual Studio 2005 C# Express beta 2 and works in the release version.)
这是我在这里的第一篇文章,其风格与许多其他文章不同,所以我希望我不会太糟.对不起,语法不好.(This is my first article here and is of a different style than many others, so I hope I don’t mess up too badly. Sorry for the bad grammar.)
有一天,当我在一次田径比赛中,我不知道自己是一名程序员.我通常保持安静,因为14岁编程的想法吓坏了我大多数朋友.因此,无论如何,教练的儿子加布里埃尔(Gabriel)碰巧也是一名程序员,他想学习C#,不能怪他一直在Chipmonk Basic工作.因此,我需要一种方法来使这个认为编程与C#线性相关的孩子.他从未见过"(One day when I was at a track meet, I let it slip that I was a programmer. I usually keep this quiet because the idea of a 14 year old programming scares the heck out of most of my friends. So anyway, the coach’s son Gabriel happened to also be a programmer and he wanted to learn C#, couldn’t blame him he had been working in chipmonk basic. So I needed a way to get this kid who thinks programming is linear to think C#. He had never seen a “) for
循环,我需要一种即时满足的方法.因此,我开始设计"功能"功能的想法,该功能的返回值立即显示在屏幕上.我知道我可以通过Python来获得,但是他想要C#.这个项目就是结果.(” loop and I needed a method of instant gratification. So I set out to design the idea of a “FLet” a little function whose return value is instantly displayed on the screen. I knew I could get this through Python, but he wanted C#. This project is the result of that.)
使用此程序(Using this Program)
该程序的目的是您可以键入C#代码,然后按Enter键并获得结果.它启动时设置为命令行计算器,因此您可以键入5 * 5并在答案字段中看到25.(The purpose of the program is that you can type in C# code, press enter and get a result. It launches set up as a command line calculator so you can type 55 and see 25 in the answer field.*)
用户界面快速概述(A Quick Overview of the UI)
标为答案的第一个框包含值(The first box labelled Answer contains the value of) ans.ToString();
.有一个大的绿色或红色区域,当红色通知您处于高级编辑模式时,绿色通知您处于简单编辑模式.编辑模式如下所述.(. There is a big green or red area that is when red notifies you that you are in the advanced edit mode and when green notifies you that you are in the simple edit mode. Edit modes are described below.)
在此顶部区域的下方,您有一个主编辑框,其中包含您的代码(Below this top band, you have the main edit box which has the code for your) functionlet
(正在运行的东西).((the stuff that gets run).)
在主编辑框中,有几个按钮.从顶部到底部,有一个设置模式按钮,单击该按钮会弹出一个列表框.在其下方,项目数学和语言(有关以下说明,请参见"语言模式").然后在主编辑框中有运行按钮,用于运行代码.还有一个显示帮助的帮助按钮.(Inside the Main edit box, there are several buttons. From top to bottom, there is the set mode button that when clicked, brings up a list box. Below it, the items math and language (see “the language modes” for a description below). Then there is the run button which runs the code in the main edit box. There is also a help button which displays help.)
编辑模式(The Edit Modes)
使用该程序非常简单.有两种编辑模式"简单"和"高级".从程序员的角度来看,您正在编写一个C#函数,并且您在文本框中输入的文本将成为该函数的一部分.在高级编辑模式下,用于实现该功能的代码如下:(Using this program is quite simple. There are two edit modes “Simple” and “Advanced”. From a programmer’s point of view, there is a C# function that you are writing and the text you put in the text box will be part of that function. In advanced edit mode, the code for making the function is this:)
ftext += "double ans = (double)Parameters[0];";
ftext += code;
ftext += "return ans;";
因此,无论ans的先前值是第一个参数,因此(So whatever the previous value of ans was is the first parameter, therefore) ans ==
最后这个(the last this) ans
是.在高级编辑模式下,您可以输入"(was. In advanced edit mode, you could type “) ans=5*5;
“和”(” and “) ans=ans*4;
“和(” and) ans
将会(would be) 100
.在简单模式下,您只需要编写”(. In simple mode, you would just have to write “) 5*5
“和”(” and “) ans*4
做出(” to make) ans
等于(equal to) 100
.在简单模式下,程序会添加"(. In simple mode, the program adds “) ans=
“和”(” and “) ;
“周围的代码.(” around the code.)
高级模式的优点是您可以拥有所有有用的C#逻辑.(The advantage of advanced mode is that you can have all the logic of C# which can be useful.)
语言模式(The Language Modes)
对象模式和数学模式之间存在3个差异.(There are 3 differences between object mode and math mode.)
- 在数学模式下,(In math mode, the type of)
ans
是(is)double
.在对象模式下,(. In object mode, the type of)ans
是(is)object
- 在数学模式下,所有数字都强制转换为(In math mode, all numbers are cast to)
double
,即在数学模式下,(, i.e. in math mode,)5*5
实际上是(is actually)(double)5*(double)5
- 在数学模式下,帮助中描述的数学函数将替换为(In math mode, the math functions described in help are replaced with)
System.Math.functionname
该程序背后的代码(The Code Behind this Program)
我首先需要知道如何动态编译C#.我在这里学到了(I first needed to know how to compile C# on the fly. I learned how here) http://www.developerfusion.co.uk/show/4529/1/(http://www.developerfusion.co.uk/show/4529/1/) 并将他的代码敲入子项目fexec中.这应该很容易理解.(and have hammered his code into the subproject fexec. This should be fairly easy to understand.)
由于Gabriel是数学天才,因此我想使该程序成为一种计算器.在预编译器项目中,我具有以下功能:(Since Gabriel is a math genius, I wanted to make the program a sort of calculator. In the precompiler project, I have functions for both precompiling with answer being an) object
并预先编译为答案是(and precompiling as answer being a) double
.计算器证明很困难.我发现为了执行简单的方程式,我需要将所有数字更改为(. The calculator proved difficult. I found that in order to do simple equations, I needed to change all numbers to) double
.在为数组建立索引时证明这很简单(也许我需要使程序上下文敏感,即,不要在括号内强制转换).我发布本文的原因之一是查看该项目是否有足够的兴趣来弄清楚如何解决该问题.(. This proves dicey when indexing arrays (maybe I need to make the program context sensitive, i.e., not cast inside brackets). One of the reasons I am publishing this article is to see if there is enough interest in this project to figure out how to fix that problem.)
我遇到的另一个问题是数学函数的使用.我希望我的计算器完全能够(Another problem I had was the use of math functions. I wanted my calculator fully capable of the) system.math
功能无需编写(functions without writing) system.math
每次.以来(every time. Since) Cos
可能出现在变量名中,我做了两个功能(could appear in a variable name, I made two functions) getwords
和(and) setwords
.(.)
private static string[] Getwords(string page, char[] grammar, char[] letters)
{
System.Collections.ArrayList words = new System.Collections.ArrayList();
bool end = false;
int i = 0;
while (!end)
{
int word = page.IndexOfAny(letters, i);
if (word == -1)
end = true;
else
{
int endword = page.IndexOfAny(grammar, word);
words.Add(page.Substring(word, endword-word));
i = endword;
}
}
return (string[])words.ToArray(typeof(string));
}
private static string Setwords(string page, char[] grammar, char[] letters,
string[] words)
{
bool end = false;
int i = 0;
int wordindex = 0;
while (!end)
{
int word = page.IndexOfAny(letters, i);
if (word == -1)
end = true;
else
{
int endword = page.IndexOfAny(grammar, word);
page = page.Remove(word, endword-word);
if (wordindex >= words.Length)
return null;
page = page.Insert(word, words[wordindex]);
i = word + words[wordindex].Length;
wordindex++;
}
}
return page;
}
}
}
这些函数将单词中的单词数组(These functions make an array of the words in a) functionlet
并允许您编辑该数组并重新插入新单词.然后,我可以将所有数学函数实例替换为(and allow you to edit that array and reinsert the new words. I can then replace all instances of math functions with) system.math
功能.(functions.)
//replace math functions with System.Math functions
string[] cl2csgrammar = new string[] { "Abs", "Acos", "Asin",
"Atan", "Atan", "Cos", "Cosh", "Log", "Log10", "Sign", "Sinh", "Sin",
"Sqrt", "Tan", "Tanh", "Truncate", "E", "PI" };//the math functions of system.math
string Functionpath = "System.Math.";
char[] Chars = new char[] { 'q', 'w', 'e', 'r', 't', 'y', 'u',
'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z',
'c', 'v', 'b', 'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X',
'C', 'V', 'B', 'N', 'M', '.' };
char[] Punctuation = new char[] { ' ', ';', '+', '-', '*', '/',
'(', ')', '=', '&', '{', '}' };
string[] words = Getwords(code, Punctuation, Chars);
int wordindex = 0;
foreach (string word in words)
{
foreach (string function in cl2csgrammar)
{
if (word.ToLower() == function.ToLower())
words[wordindex] = Functionpath + function;
}
wordindex++;
}
code = Setwords(code, Punctuation, Chars, words);
到此为止.我希望在这里看到有关我的代码有多糟糕以及我浪费多少时间执行x的响应.另外,我需要写作方面的帮助!这已经过时了,我不知道从哪里开始. ;)(That’s about it at this point. I am hoping to see a response here on how bad my code is and how much time I wasted doing x. Plus I need help writing! It is way out of date and I don’t know where to start. ;))
版本号(Versions)
快来了(Coming Soon)
- XML档案格式(XML file format)
- 支持嵌入式(Support for embedded)
functionlet
的GUI和库.(s GUI and libraries.) - 代码着色和代码完成(Code coloring and code completion)
到目前为止发行(Releases So Far)
- Beta 2.3更干净的代码格式并修复了一个焦点错误(Beta 2.3 cleaner code formatting and fixed a focus bug)
- Beta 2.2 UI重新设计错误修复性能修复(Beta 2.2 UI redesign bug fixes performance fixes)
- Beta 2.1功能和错误修复.很多…(Beta 2.1 features and bug fixes. Lots…)
- Beta 2.0基本未经测试的功能发布(Beta 2.0 basically untested feature release)
- 还是同一程序,编辑过的文章的语法,拼写和可读性(Still the same program, edited article for grammar, spelling, and readability)
- Beta 1.1修复了更改模式时的错误(Beta 1.1 fixed bug when changing modes)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C++ C# VC8.0 WinXP Windows .NET2.0 Visual-Studio VS2005 QA Dev 新闻 翻译