小编Kei*_*son的帖子

Perl正则表达式示例不起作用

我只是想测试一下我在杂志上看过的东西(Linux Shell手册).我从未尝试过这样的事情,但我知道这可能有用

这个例子是

perl -n -e '/^The \s+(.*)$/ print "$1\n"' heroes.txt
Run Code Online (Sandbox Code Playgroud)

在heroes.txt中它有

Catwoman
Batman
The Tick
Spider-Man
Black Cat
Batgirl
Danger Girl
Wonder Woman
Luke Cage
Ant-Man
Spider-Woman
Run Code Online (Sandbox Code Playgroud)

这应该显示Tick,但是我得到了

perl -n -e '/^The \s+(.*)$/ print "$1\n"' heroes.txt
syntax error at -e line 1, near "/^The \s+(.*)$/ print"
Execution of -e aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

perl

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

为什么我在C中得到2个不同的值

当我使用这段代码时:

#include <stdio.h>

int main(void){
  int hi, hello;

  hi = 1;
  hello = 100;

  printf("%d and %d", &hi, &hello);

  printf("\nPress any key to exit...");
  getch();
}
Run Code Online (Sandbox Code Playgroud)

它打印:

2358876 and 2358872

Press any key to exit
Run Code Online (Sandbox Code Playgroud)

但是当我定义变量hi并将它们hello分别定义为整数时,它会做它应该做的事情.为什么要打印这些奇怪的数字?

c

-2
推荐指数
1
解决办法
104
查看次数

为什么函数定义foo(int i + = 1)不可能?

在功能

int foo(int i, int j)
{
    return i + j;
}
Run Code Online (Sandbox Code Playgroud)

ij未初始化,所以这个函数可以用,只用两个参数来调用:

foo(5, 6); // 11
Run Code Online (Sandbox Code Playgroud)

好的,但是C++有默认参数,并且可能的定义如下:

int foo(int i = 1, int j = 2)
{
    return i + j;
}
Run Code Online (Sandbox Code Playgroud)

这里既有ij具有默认参数,然后调用:

foo(5, 6); // 11
foo(5); // 7
foo(); // 3
Run Code Online (Sandbox Code Playgroud)

是可能的.那么,为什么这个定义:

int foo(int i += 1, int j += 2)
{
    return i + j;
}
Run Code Online (Sandbox Code Playgroud)

这些电话:

foo(5, 6); // 14
foo(5); // 8
foo(); // …
Run Code Online (Sandbox Code Playgroud)

c++ arguments function

-2
推荐指数
1
解决办法
148
查看次数

"Sams自己C"中的示例使用"fgets"但返回错误

包含的代码完全不在书中,但它会返回错误.这本书有不正确的做法吗?我以前从未使用#include <string.h>过,#include <stdio.h>但我仍然不知道三个参数应该是什么.

#include <stdio.h>
#include <string.h>
int main(void)
{
    char buffer[256];

    printf("Enter your name and press <Enter>:\n");
    fgets(buffer);

     printf("\nYour name has %d characters and spaces!",
         strlen(buffer));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译说

Semantic issue with (fgets( buffer ); - Too few arguments to function call, expected 3, have 1

Format string issue (strlen(buffer)); - Format specifies type 'int' but the argument has type 'unsigned long'
Run Code Online (Sandbox Code Playgroud)

c c++ compilation fgets

-2
推荐指数
2
解决办法
188
查看次数

字符串中的隐藏或特殊字符或不同的语句代码

如何使我的fgets(或类似语句)的输入字符串S1等于我声明的静态字符串S2?

#include <stdio.h>
#include <string.h>

main() {
    char s1[80];
    char s2[] = "This is a test.";
    int l1, l2;

    system("clear");
    printf("%s\n\n", s2);
    printf("Please type exactly the above sentence: ");
    fgets(s1,80,stdin);

    l1=strlen(s1);
    l2=strlen(s2);
    printf("\n String entered for S1, have a length of %d:\n %s", l1, s1);
    printf("\n String static  for S2, have a legnth of %d:\n %s", l2, s2);
    if(s1==s2)
        printf("\n\nOk! they're with the same length!\n");
    else
        printf("\n\nNop! They are NOT the same.\n");
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

c string fgets

-2
推荐指数
1
解决办法
388
查看次数

为什么在同一行上有2个函数调用时会跳过函数调用?

我很困惑为什么这条线:

cout << "Your average in this course is " << average() << " and your letter grade is " << convert_to_letterGrade() << "\n";
Run Code Online (Sandbox Code Playgroud)

不起作用,而以下工作(一个cout分为2):

cout << "Your average in this course is " << average();
cout << << " and your letter grade is " << convert_to_letterGrade() << "\n";
Run Code Online (Sandbox Code Playgroud)

此代码位于void函数中,该函数是类的公共成员Grades.average()计算并将结果存储在私有成员变量中averageScore,并返回averageScore.如果平均分数不合理,则convert_to_letterGrade()返回char基于值averageScore和错误消息的值,并且这两个函数都是类的私有成员.

在第一种情况下,我从convert_to_letterGrade()第一个开始收到错误消息,然后是cout语句应该打印的内容.该cout语句打印正确的averageScore值,但当我进入时convert_to_letterGrade(),该函数仍然使用垃圾值averageScore,而第二个工作完全正常,convert_to_letterGrade()也使用正确的 …

c++

-2
推荐指数
2
解决办法
92
查看次数

在C中:将数组与字符串和整数元素进行比较

在C:我写了一个函数,它检查arr2类型的子数组是否在数组中int"按原样"存在arr1,具有相同的类型.如果以完全相同的顺序arr2找到所有元素,则返回1 arr1,否则返回0.功能似乎工作正常,但我有两个问题:

Q1:如果我们想对两个数组中的混合类型的元素做同样的事情,特别是如果我们事先不知道将使用哪些类型的元素以及以什么顺序?我的意思是每个元素的类型和它们的外观顺序都必须由用户自行决定.我已经阅读了很多类似的问答,但我仍然不清楚这个解决方案.

Q2:考虑以下示例:

int arr1[] = {34, 231, "not the declared type..", "..recognized nevertheless", 3, 7};

int arr2[] = {231, "not the declared type..", "..recognized nevertheless"};
Run Code Online (Sandbox Code Playgroud)

为什么我的功能实际工作(例如在上述情况下返回1)?我认为它应该失败 - 因为使用的元素不是声明的类型(int) - 或者更糟糕的是我会得到编译器错误.相反,它似乎成功,因为它甚至可以恰当地识别和比较最后一个句号或大写字母.好吧,它确实失败了浮点数(我猜它会将它们转换为整数并只比较整数部分?)但为什么它在其他情况下没问题呢?它是否也会转换字符串,以及会发生什么样的转换?

我对C和论坛都很陌生,所以请原谅无知.

c arrays

-2
推荐指数
1
解决办法
67
查看次数

使用“ calloc”的内存分配失败

我正在尝试使用calloc函数分配内存,但是当数组大小(类型double)为48,000 x 48,000时,它将返回NULL指针。但是,它对于23,000 x 23,000的另一种情况非常适用。从技术上讲应该没有问题,因为这台计算机上的RAM足够多。我也在使用64位库。

操作系统:Linux 64bit

安装的内存:376GB

编译器:Intel MKL 2019

编译器链接行:

icc Main.c -qopenmp -DMKL_ILP64 -m64 -I$MKLROOT/include \
    -L$MKLROOT/lib/intel64 -lmkl_intel_ilp64 -lmkl_intel_thread \
    -lmkl_core -liomp5 -lpthread -lm -ldl -O2 -o main.out
Run Code Online (Sandbox Code Playgroud)

这是我使用的一段代码:

int Jsize = 48000;
double *J = NULL;
J = (double *)calloc(Jsize*Jsize, sizeof(double));
if (J == NULL) printf("Null\n");
Run Code Online (Sandbox Code Playgroud)

c

-2
推荐指数
1
解决办法
55
查看次数

复制矢量在Java中的矢量

我想将一个矢量复制到另一个.问题是如果我改变矢量v1,第二个矢量v2也会改变.我的目标是即使我更改源向量也保持副本完整.

import java.util.Collections;
import java.util.Vector;

public class CopyElementsOfVectorToVectorExample {

  public static void main(String[] args) {

    //create first Vector object
    Vector v1 = new Vector();

    //Add elements to Vector
    v1.add("1");
    v1.add("2");
    v1.add("3");

    //create another Vector object
    Vector v2 = new Vector(v1.size());  

    v2.setSize(v1.size());
    Collections.copy(v2,v1);


    System.out.println("After copy, Second Vector Contains : " + v2);  
  }}
Run Code Online (Sandbox Code Playgroud)

如何保持第二份完整?

java collections

-3
推荐指数
2
解决办法
2万
查看次数

是否存在等于0x80的ascii?

有人知道相当于80(十六进制)的ASCII吗?它甚至存在吗?我只是想知道,桌子只能达到7F.

ascii

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

Caesar Cipher使用char*?C++

所以我想尝试使用Ceasar Cipher char *,我写了一个简单的函数:

char * Encrypt(char * s, int k)
{
    char * c = s;

    for(int i = 0; i < strlen(s); i++)
       c[i] += k;

    return c;
} 
Run Code Online (Sandbox Code Playgroud)

这似乎看起来应该有效,但事实并非如此.它在运行程序时抛出错误.

以下是我如何调用此函数的示例:

int main()
{
    cout << Encrypt("hello", 2) << endl;
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在你说"为什么不只是使用string?"之前,答案是我在某个SDK上编写C++,在使用时会导致编译器错误string.好的但是,任何形式的帮助都将非常感谢,谢谢!

c++ encryption char-pointer caesar-cipher

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

sleep()with float作为参数不起作用

我创建了简单的代码来测试sleep()函数.在C.

我尝试程序在终止前等待5秒后执行以下代码:

#include <stdlib.h>
#include <stdio.h>

int main(){
    int n=0;
    for (n=1;n<=10;n++){
        sleep(.5);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而它等待说100毫秒.好吧,绝对不到一秒钟.

现在,如果我改为执行以下代码:

#include <stdlib.h>
#include <stdio.h>

int main(){
    int n=0;
    for (n=1;n<=10;n++){
        sleep(1);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

程序按预期运行(退出前等待10秒).

当参数是float时,为什么sleep()无法正确执行?

什么是绝对最好的功能,与我可以用来代替睡眠的所有系统兼容?

c floating-point sleep

-4
推荐指数
1
解决办法
262
查看次数