我需要一个函数将"显式"转义序列转换为相对不可打印的字符.ES:
char str[] = "\\n";
cout << "Line1" << convert_esc(str) << "Line2" << endl:
Run Code Online (Sandbox Code Playgroud)
会给出这个输出:
Line1
Line2
Run Code Online (Sandbox Code Playgroud)
有没有这样做的功能?
我有以下Java主类,我试图使用IntelliJ IDEA中的Gradle插件编译和运行:
package com.mikidep.bookshop;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.print("Inserisci testo qui: ");
System.out.println(in.nextLine());
System.out.println("Yee!");
}
}
Run Code Online (Sandbox Code Playgroud)
我的build.gradle如下:
group 'com.mikidep.bookshop'
version '1.0-SNAPSHOT'
apply plugin: 'application'
mainClassName = "com.mikidep.bookshop.Main"
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
run {
standardInput = System.in
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我gradle run -q在终端中运行,一切都按预期工作.但是,我想使用这个IDEA运行配置进行测试运行:
一切都很好,直到我做一些控制台输入.in.nextLine()抛出以下异常:
线程"main"java.util.NoSuchElementException中的异常:在com.mikidep.bookshop.Main.main(Main.java:10)的java.util.Scanner.nextLine(Scanner.java:1585)中找不到任何行
我也尝试调试它,并注意到所有字段System.in都显示为零,就像整个流尚未初始化一样.谁知道发生了什么?
编辑: …
我正在尝试编写一个带有一些纯虚拟二元运算符的抽象类,它应该由派生类实现,以实现运算符多态性.这是一个简化的例子:
class Base {
public:
virtual const Base& operator+ (const Base&) const = 0;
};
class Derived : public Base {
public:
const Derived& operator+ (const Derived&) const;
};
const Derived& Derived::operator+ (const Derived& rvalue) const {
return Derived();
}
Run Code Online (Sandbox Code Playgroud)
现在操作符的作用并不重要,重要的部分是它返回的内容:它返回一个临时的Derived对象或对它的引用.现在,如果我尝试编译,我得到这个:
test.cpp: In member function ‘virtual const Derived& Derived::operator+(const Derived&) const’:
test.cpp:12:17: error: cannot allocate an object of abstract type ‘Derived’
test.cpp:6:7: note: because the following virtual functions are pure within ‘Derived’:
test.cpp:3:22: note: virtual const Base& Base::operator+(const Base&) const …Run Code Online (Sandbox Code Playgroud) 在问题中,如果我在我的类中定义一个字符串运算符:
class Literal {
operator string const () {
return toStr ();
};
string toStr () const;
};
Run Code Online (Sandbox Code Playgroud)
然后我用它:
Literal l1 ("fa-2bd2bc3e0");
cout << (string)l1 << " Declared" << endl;
Run Code Online (Sandbox Code Playgroud)
使用显式转换一切正常,但如果我删除(字符串)编译器说它需要在std :: string中声明的强制转换运算符.它不应该自动投射我的类型?解决:我正在重载运算符<<(ostream&os,const Literal&l).
我正在解决一个类似这样的编程练习:
文件包含正整数和之间的等式列表,每行一个,每个以分号结尾,没有空格.这些平等可能是对的,也可能是错的.例如,请考虑以下文件:
Run Code Online (Sandbox Code Playgroud)2+3+12=9+8; 2+3+4=9; 22=3+4+5+10; 3+5+1=4+44;编写一个程序,将文件名作为其第一个参数,并输出正确行的比例.例如,给定上面的文件,程序应输出0.75.
我在Scala中的解决方案如下,它可以工作,但我正在寻找一种在没有var的情况下重写它的方法.
import scala.io.Source
object Hello {
def main(args: Array[String]): Unit = {
var count: Int = 0
var correct: Int = 0
Source.fromFile(args(0)).getLines().zipWithIndex.foreach { case (line, i) =>
val regex = """^[1-9][0-9]*(\+[1-9][0-9]*)*=[1-9][0-9]*(\+[1-9][0-9]*)*;$""".r
regex findFirstIn line match {
case Some(_) =>
case None => throw new Exception("Error reading file, line " + i)
}
val sums = line.substring(0, line.length - 1).split('=').map {
_.split('+').map(Integer.parseInt).sum
}
count += 1
correct += (if (sums(0) == sums(1)) 1 …Run Code Online (Sandbox Code Playgroud)