我正在关注winforms的这个教程,到目前为止,教程正在编写表单而不使用工具箱.我相信它很快就会更深入地介绍工具箱.
在本教程之后,我在以下两段代码中进行了部分类:
第一档:
using System;
using System.Windows.Forms;
public class Numeric : System.Windows.Forms.TextBox
{
public Numeric()
{
}
}
public partial class Exercise
{
private Numeric txtbox;
System.ComponentModel.Container components;
}
Run Code Online (Sandbox Code Playgroud)
第二档:
using System;
using System.Windows.Forms;
public partial class Exercise : Form
{
private void InitializeComponent()
{
txtbox = new Numeric();
Controls.Add(txtbox);
}
public Exercise()
{
InitializeComponent();
}
}
public class program
{
public static int Main()
{
Application.Run(new Exercise());
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
当我用F5运行代码时,一切看起来都很好:表单弹出文本框.
但出于某种原因,当我右键单击第二个文件并选择"视图设计器"时,我收到一条错误,上面写着"变量'txtbox'未声明或未被分配".我可以选择"忽略并继续",这会将我引导到一个没有文本框的表单.
为什么会这样?我知道有些人认为我应该使用工具箱,这可能是最明智的做法,但我仍然想知道为什么会这样.
我使用BigInteger在C#与阶乘函数连接.该程序闪电般快速计算5000!,但在10000时出现溢出错误!根据wolfram alpha,10000!是约
10000!= 2.8 x 10 ^ 35659
从我在这篇文章中可以看出,BigInteger存储在一个int[]数组中.如果我int正确解释类型,它使用4个字节,意味着10000!使用大约4 x log10(2.8 x 10^35659) = 142636字节,我使用log10(n)(日志到基数10)作为n的位数的近似值.这只有143 MB,但我仍然得到堆栈溢出异常.为什么会这样?
using System;
using System.Numerics;
class Program
{
static void Main()
{
BigInteger hugeFactorial = Calculations.Factorial(5000);
}
}
class Calculations
{
public static BigInteger Factorial(int n)
{
if (n == 1) return n;
else return n*Factorial(n - 1);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在解决这个问题,他们要求第一个斐波纳契数为1000位的索引,我的第一个想法是类似于:
BigInteger x = 1;
BigInteger y = 1;
BigInteger tmp = 0;
int currentIndex = 2;
while (x.NoOfDigits < 1000)
{
tmp = x + y;
y = x;
x = tmp;
currentIndex++;
}
return currentIndex;
Run Code Online (Sandbox Code Playgroud)
但是,据我所知,没有方法可以计算BigInteger的位数.这是真的?绕过它的一种方法是使用BigInteger的.ToString().Length方法,但我被告知字符串处理很慢.
BigInteger也有一个.ToByteArray(),我想把BigInteger转换成一个字节数组并检查该数组的长度 - 但我不认为这唯一地决定了BigInteger中的位数.
为了它的价值,我实现了另一种解决方法,即手动将Fibonacci数存储在数组中,并在数组填满后立即停止,并将其与基于.ToString的方法进行比较,大约为2.5时间慢,但第一种方法需要0.1秒,这也似乎很长一段时间.
编辑:我已经在下面的答案中测试了这两个建议(一个是BigInteger.Log,一个是MaxLimitMethod).我得到以下运行时间:
程序
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Stopwatch clock = new Stopwatch();
clock.Start();
int index1 = Algorithms.IndexOfNDigits(1000);
clock.Stop();
var elapsedTime1 = …Run Code Online (Sandbox Code Playgroud) 我自己学习C#,正在解决项目Euler问题.我编写了下面的代码来解决问题8,它要求在一个长数字序列中找到13个连续数字的最大乘积.我从文件中读取了这个数字序列到程序中.
首先,我首先将数值变量"maxProduct"和"productTmp"定义为类型为"int",但我没有得到正确答案,因为它大于此类型的最大范围.令我困惑的是,我没有收到错误或警告,说我在处理我的注册时试图操纵太大的数字.相反,它似乎"包围"它的范围,这看起来毫无用处.这是怎么回事?
其次,它在哪里读取文件是我在网上找到的.但是,出于某种原因,我不能只编写"IO.StreamReader",但我必须包含"System".在它面前,即使我已经包含了"使用系统"; 线.我认为using语句使得"System"中包含的所有内容都可以在没有System.-prefix的情况下进行参考.
第三,在它读取文件的while循环中,发生了什么?我的猜测是(line = file.ReadLine())是一个布尔语句,但在这种情况下,它可以等于null是没有意义的.另外,我没有给"line"一个值,所以该语句看起来像是将line的null值与其他值进行比较.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace problem8
{
class Program
{
static void Main(string[] args)
{
int noAdjacentDigits = 13;
long maxProduct = 0;
string numberSequence = "", line;
System.IO.StreamReader file = new System.IO.StreamReader("C:/problem8.txt");
while ((line = file.ReadLine()) != null)
{
numberSequence = numberSequence + line;
}
for (int i = 0; i <= numberSequence.Length-noAdjacentDigits;i++)
{
string substrTmp = numberSequence.Substring(i, noAdjacentDigits);
long productTmp = 1;
for …Run Code Online (Sandbox Code Playgroud) 我是F#的新手,想通过递归方式实现列表中最不常见的多重函数,例如lcm(a,b,c)= lcm(a,lcm(b,c)),其中lcm为两个元素是从gcd计算的.
我有以下代码.我尝试将lcm函数的输入与两个元素的列表进行匹配,否则将是一个通用列表,我将其拆分为第一个元素和剩余部分."lcm(tail)"部分给出了编译器错误.它说它应该有类型"int"但是类型为"int list - > int".它看起来像是说"lcm tail"本身就是一个函数,我不明白.为什么不是int?
let rec gcd a b =
if b = 0
then abs a
else gcd b (a % b)
let lcmSimple a b = a*b/(gcd a b)
let rec lcm list = function
| [a;b] -> lcmSimple a b
| head::tail -> lcmSimple (head) (lcm (tail))
Run Code Online (Sandbox Code Playgroud)
最好的祝福.
我在C#中使用字典,我花了几个小时搞清楚为什么我的程序不起作用,原因是当我操作我制作的字典副本时,这些操作也会影响原始字典字典由于某种原因.
我把问题归结为以下示例:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary<int, List<int>> D = new Dictionary<int, List<int>>();
List<int> L1 = new List<int>(){ 1, 2, 3 };
List<int> L2 = new List<int>() { 4, 5, 6 };
D.Add(1,L1);
D.Add(2,L2);
Dictionary<int, List<int>> Dcopy = new Dictionary<int, List<int>>(D);
Dcopy[1].Add(4);
}
}
Run Code Online (Sandbox Code Playgroud)
在此代码中,当我向副本中的键1对应的列表中添加元素时,此元素也会出现在原始字典中.
当我在线搜索时,它似乎与"引用类型"有关,而推荐的修复似乎总是涉及类似于
Dictionary<int, List<int>> Dcopy = new Dictionary<int, List<int>>(D);
Run Code Online (Sandbox Code Playgroud)
我所做的包括在我的程序中,但由于某种原因,这不起作用.
有关为什么它在我的情况下不起作用的任何建议,以及关于该做什么的任何建议?
最好的祝福.