我只是在研究新的.NET 4.0功能.有了这个,我正在尝试使用Parallel.For和正常for(x;x;x)循环进行简单的计算.
但是,我有50%的时间得到不同的结果.
long sum = 0;
Parallel.For(1, 10000, y =>
{
sum += y;
}
);
Console.WriteLine(sum.ToString());
sum = 0;
for (int y = 1; y < 10000; y++)
{
sum += y;
}
Console.WriteLine(sum.ToString());
Run Code Online (Sandbox Code Playgroud)
我的猜测是线程正在尝试同时更新"sum".
有明显的方法吗?
我是一个糟糕的程序员,因为我正在复制和粘贴.一个例子是,每当我连接到数据库并检索记录集时,我将复制先前的代码并进行编辑,复制设置datagridview和编辑的代码.我知道短语代码重用,但我实际上并没有使用它.我如何利用代码重用,这样我就不必复制和粘贴数据库代码和datagridview代码.
我理解指针,并且很少需要在C#代码中使用它们.我的问题是:在一段代码中明确说明"不安全"的原因是什么.另外,为什么必须更改编译器选项以允许"不安全"代码?
结论: CLR(或语言规范)中的内容使得我们不能随时使用指针(很像C和C++)而不必键入"unsafe"并更改编译器选项?
澄清一下:我知道"不安全"和"安全"的代码是什么.这只是一个问题,为什么我们必须做所有额外的工作(好吧,不是那么多)只是为了能够使用这些功能.
所以,我是一个尝试学习C的C#人.作为第一个(个人)项目,我试图编写一个基本的坐标几何库.
问题:在幕后的堆上分配内存而不是让以图书馆为目标的程序员这样做是不是最好的C编程实践呢?
例如,我的'point'结构和相关方法:
point.h
/* A basic point type. */
typedef struct point
{
float x;
float y;
float z;
char *note;
}point;
/* Initializes a basic point type. [Free with free_point method] */
point *create_point(float pos_x, float pos_y, float pos_z, char *_note);
/* Frees a point type. */
void free_point(point *_point);
/* Finds the midpoint between two points. */
point *midpoint(point *pt1, point *pt2);
Run Code Online (Sandbox Code Playgroud)
point.c
#include "point.h"
/* Initializes a basic point type. [Free with free_point method] */ …Run Code Online (Sandbox Code Playgroud) 这是在3中找到最大值的代码,但我想找到最大值的代码5:
Dim a, b, c As Integer
a = InputBox("enter 1st no.")
b = InputBox("enter 2nd no.")
c = InputBox("enter 3rd no.")
If a > b Then
If a > c Then
MsgBox("A is Greater")
Else
MsgBox("C is greater")
End If
Else
If b > c Then
MsgBox("B is Greater")
Else
MsgBox("C is Greater")
End If
End If
Run Code Online (Sandbox Code Playgroud) 我总是有一个C++/CLI的东西.也许是因为没有多少开发人员使用它......或者仅仅因为它不同.
假设Microsoft完全支持C++/CLI,因为它们执行VB.NET和C#(即LINQ,WPF等).你会用吗?
如果没有,为什么?
我正在尝试在文本文件中编译代码以更改WinForms应用程序主窗体上的TextBox中的值.IE浏览器.使用方法向调用表单添加另一个partial类.表单有一个按钮(button1)和一个TextBox(textBox1).
文本文件中的代码是:
this.textBox1.Text ="Hello World !!";
和代码:
namespace WinFormCodeCompile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Load code from file
StreamReader sReader = new StreamReader(@"Code.txt");
string input = sReader.ReadToEnd();
sReader.Close();
// Code literal
string code =
@"using System;
using System.Windows.Forms;
namespace WinFormCodeCompile
{
public partial class Form1 : Form
{
public void UpdateText()
{" + input + @"
}
}
}";
// Compile code
CSharpCodeProvider cProv …Run Code Online (Sandbox Code Playgroud) 我正在编写一个C程序,它将运行Linux命令,如:
cat /etc/passwd | grep list | cut -c 1-5
我没有任何结果
*这里父母等待第一个孩子(child_pid)完成;
第一个孩子等第二个孩子(pfils)!!
任何的想法?
代码:
main()
{
pid_t child_pid;
int fd[2];
int pfd[2];
pipe(pfd);
child_pid = fork ();
if (child_pid != 0)
{
wait(child_pid);
close(pfd[1]);
dup2(pfd[0],0);
close(pfd[0]);
execlp("cut","cut","-c","1-5",NULL);
}
else
{
pid_t pfils = fork();
pipe(fd);
if(pfils == 0)
{
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp("cat", "cat","/etc/passwd",NULL);
}
else
{
wait(pfils);
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
close(pfd[0]);
dup2(pfd[1],1);
close(pfd[1]);
execlp("grep","grep","list",NULL);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我将列表视图设置为详细信息以尝试创建列,但不会显示列.什么阻止列显示?
Imports System.IO
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.Menu
Public Class Jonathan_Roda_P4
Dim TitlesData(21)
Dim BOData(21)
'Clear Button Function
Private Sub ResetClearToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetClearToolStripMenuItem.Click
'Clear Title LV
DispResView.Clear()
'Clear Box Office List
BOList.Items.Clear()
'Resets User Message to default
UsrMsg.Text = "Data Not Loaded"
End Sub
'Help/Instructions Properties
Private Sub InstructionsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InstructionsToolStripMenuItem.Click
MessageBox.Show("1. Click the 'Load Files' Button" & vbNewLine _
& vbNewLine _
& "2. Select two respective files." & vbNewLine _
& …Run Code Online (Sandbox Code Playgroud) 我需要编写一个程序,打印出1到15之间所有奇数整数的乘积.无论我尝试什么,它都行不通,只打印产品.我搞砸了if声明吗?这是我的代码:
//Sophia Ali
#include <iostream>
using namespace std;
int main()
{
int i;
int product;
for ( i = 1; i <=15; i+=2)
{
if(i%2!=0)
product = product * i;
else
continue;
}
cout << "\n product: " << product;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我需要能够为一个小项目使用fork().问题是示例代码不起作用:
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
pid_t pID = fork();
if (pID == 0) // child
{
printf("CHILD\n");
// Code only executed by child process
}
else if (pID < 0) // failed to fork
{
printf("FAIL\n");
}
else // parent
{
printf("PARENT\n"); // Code only executed by parent process
}
// Code executed by both parent and child.
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器说:"20 D:\ Untitled1.cpp`forb'unclaclared(首先使用这个函数)"
但我已经在互联网上看到它应该位于#include <unistd.h>.
有任何想法吗?谢谢!
此代码计时输出~380Kb字符串的两种方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static string outbuff = "";
static void Main(string[] args)
{
{
Stopwatch exectime = new Stopwatch();
System.IO.StreamWriter file;
exectime.Reset(); exectime.Start();
file = new System.IO.StreamWriter("output.html");
for (int i = 0; i < 18000; i++)
{
outbuff += "444444444, 5555555555\n";
}
string fin = "\nString method took " + exectime.Elapsed.TotalSeconds + "s";
file.WriteLine(outbuff);
Console.WriteLine(fin);
file.WriteLine(fin);
file.Close();
}
{
Stopwatch exectime = new Stopwatch(); …Run Code Online (Sandbox Code Playgroud) 我有一个程序显示ascii值和字母,但现在我修改程序只显示我正在寻找的西班牙字符.该程序被调用,是的,你得到它,"ascii".用户要做的是键入C:>ascii或C:>ascii all显示所有ascii字符.要仅显示用户必须键入的西班牙语字符C:>ascii es.
为此我写了这个简单的程序
int main(int argc, char** argv) {
if(argv[0] == "es"){
abc::ascii_es();
}
if(argv[0] == "all"){
abc::ascii_all();
}
else
abc::ascii_es();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但程序总是跳转到ascii_eselse条件关键字中的方法.我究竟做错了什么?