小编orf*_*uit的帖子

c#join string逗号分隔,但双引号内的所有值

我有一个字符串列表

new List<string> { "One", "Two", "Three", "Four", "Five", "Six" }
Run Code Online (Sandbox Code Playgroud)

我想要一个包含完整内容的字符串(包括双引号)

"One", "Two", "Three", "Four", "Five", "Six"
Run Code Online (Sandbox Code Playgroud)

因为会写一个文本文件,它将是一个数组[] = {my_string}

我试过这个没有成功

var joinedNames = fields.Aggregate((a, b) => "\"" + a + ", " + b + "\"");
Run Code Online (Sandbox Code Playgroud)

小LINQ帮助将非常感谢:)

c#

10
推荐指数
3
解决办法
9047
查看次数

c,获得一个特殊的随机数

我有一个算法问题,我需要加快:)

我需要一个32位随机数,精确的10位设置为1.但同时,101(5 dec)和11(3 dec)等模式被认为是非法的.

现在MCU是8051(8位),我在Keil uVision中测试了所有这些.我的第一次尝试完成,给出解决方案

0x48891249
1001000100010010001001001001001   // correct, 10 bits 1, no 101 or 11
Run Code Online (Sandbox Code Playgroud)

问题是它完成了97秒或1165570706 CPU周期,这是荒谬的!

这是我的代码

// returns 1 if number is not good. ie. contains at leats one 101 bit sequence
bool checkFive(unsigned long num)
{
    unsigned char tmp;

    do {

            tmp = (unsigned char)num;

        if(
            (tmp & 7) == 5 
        || (tmp & 3) == 3
        ) // illegal pattern 11 or 101
                return true; // found 

            num >>= 1;
    }while(num);

    return false;
} …
Run Code Online (Sandbox Code Playgroud)

c random

6
推荐指数
2
解决办法
171
查看次数

c# 预编译大数组或其他解决方案以在可执行文件中嵌入大数据

我必须在我的应用程序中填写一个 excel 文件,打开、写入和关闭。

问题是文件包含很多格式和宏(是公司模板),我意识到从 C# 重新生成将非常困难,最重要的是,要维护。

所以我用十六进制编辑器打开了excel模板,将所有字节作为数组

namespace myapp
{

    public static class ReviewTemplate // exported with HXD
    {
        public static string name = "AS-04522-EN";
        public static byte[] rawData = {
    0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x3E, 0x00, 0x03, 0x00, 0xFE, 0xFF, 0x09, 0x00, 0x06, 0x00, 0x00, 0x00,
...
...
...
}
}
Run Code Online (Sandbox Code Playgroud)

然后,像这样从 C# 生成

File.WriteAllBytes(ReviewTemplate.name + DateTime.Now.ToString("_dd_MM_yyyy_HH_mm_ss") + ".xls", …
Run Code Online (Sandbox Code Playgroud)

c#

3
推荐指数
1
解决办法
190
查看次数

c#通过附加可变数量的空格来使字符串列表的元素唯一

我有一点算法问题,我坚持(快速)实现.实际上我的未完成,但已经减慢了DataGridView的负载.

最初的问题:WinForms的DataGridView有一个来自2005的错误(显然还没有解决到VS2015),这使得具有相同名称的列的不正确绑定不敏感.更精确的是,如果您有2列"Cat"和"cat",它们将绑定到数据库中的相同(第一个找到)对象.

无论如何,我正在使用ITypedList和GetItemProperties()来通知DGV我想要链接的字段.这个想法(在stackoverflow上看到的地方)是在"重复大小写区分"列之后添加空格,如下所示:

"cat"   --> leave as is
"Cat"   --> needs to be come "Cat_"   _ means space
"cAt"   --> needs to become "cAt__"   __ means two spaces and so on
Run Code Online (Sandbox Code Playgroud)

算法问题:使用循环在列表中添加字符串.在添加之前,检查字符串是否存在(修剪和不区分大小写),如果是,则在名称末尾附加一个空格.保持名称不变.换句话说,通过将n个空格附加到名称来使字符串唯一.

希望我描述得很好,任何想法都赞赏.

我已经用我的变体做了一些测试并且速度受到影响,也许还有DGV正在触发GetItemProperties()回调5次或更多次的事实.

这是我的代码:

   public partial class Form1 : Form
    {

        List<string> list = new List<string>
        {
            "cat",      // the cat
            "Cat",      // first duplicate needs to become Cat_ (one space)
            "kitty",
            "kittY",
            "dog",
            "cAt",      // third duplicate needs to become cAt__ (two spaces)
            "Dog",
            "monkey",
            "monKey",
            "Monkey",
        }; …
Run Code Online (Sandbox Code Playgroud)

c# .net-3.5

1
推荐指数
1
解决办法
81
查看次数

以编程方式获取嵌套的静态类名称并访问方法

我有一个像这样的嵌套类结构

public static class Animals{


    private static class Dolphins
    {
        public static int GetAge()
        {

        }
    }

    private static class Cats
    {
        public static int GetAge()
        {

        }
    }

    private static class Dogs
    {
        public static int GetAge()
        {

        }
    }

}
Run Code Online (Sandbox Code Playgroud)

是否有可能在for循环中以编程方式获取子类名称,因此我可以为所有它们调用GetAge()方法?像这样(伪代码):

foreach(subclass in Animals)
{
   subclass.GetAge()
}
Run Code Online (Sandbox Code Playgroud)

我通过反射进行了尝试,但是我能够获得基类的后代类,而不是像上面的静态基类中嵌套的类。

提前致谢,

c#

1
推荐指数
1
解决办法
77
查看次数

gcc,枚举没有按预期打包

枚举的大小没有包装,为什么呢?

typedef enum{
    STATE_IDLE = 0,
    STATE_RUN,
}State_t;

#pragma pack(1)
struct {
    char c;             //1
    State_t State;      // should be 1 but is 4
    uint32_t Time;      //4
    sCB_t CB;           //4

}App;
#pragma pack()

int main()
{
    printf("Size bytes: %u\n", sizeof(App));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以上将输出

Size bytes: 13
Run Code Online (Sandbox Code Playgroud)

我在x86上使用CodeBlocks 16.01和gcc

C:\Program Files (x86)\CodeBlocks\MinGW\bin>mingw32-gcc.exe --version
mingw32-gcc.exe (tdm-1) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for …
Run Code Online (Sandbox Code Playgroud)

c gcc

0
推荐指数
1
解决办法
1459
查看次数

标签 统计

c# ×4

c ×2

.net-3.5 ×1

gcc ×1

random ×1