注意:我不能使用任何默认值.
我试图做一个非常简单的异常处理例程,或者至少做一些看起来很重要的事情.我不想做太多,只是抛出异常并打印错误消息.
在.h
class MyException {
protected: string message;
public:
MyException (string mes) {
this->message = mes;
}
MyException (); // is this necessary ? does it do anything ?
string getMessage() const {
return this->message;
}
};
Run Code Online (Sandbox Code Playgroud)
我想要的是拥有"PersonException"和"ActivityException".可能会使用模板但不确定是否可以解决问题.
class PersonException:public MyException {
public:
PersonException (string message):MyException(message) {
}
};
class PersonValidator {
public:
PersonValidator (Person p) throw (PersonException);
};
Run Code Online (Sandbox Code Playgroud)
在.cpp
void PersonValidator::PersonValidator(Person p) throw (PersonException) {
if (p.getPhone < 0) {
throw PersonException ("Person Number is invalid");
}
Run Code Online (Sandbox Code Playgroud)
这有什么不对或麻烦,怎么可能做得更好?我在哪里打印错误信息?
我目前正在制作一个扑克手历史解析器,作为我的单身汉项目的一部分.我过去几天一直在做一些研究,并遇到了一些不错的解析器生成器(其中我选择了JavaCC,因为项目本身将用Java编码).
尽管手历史语法非常基本且直截了当,但由于玩家昵称中允许的一组字符存在歧义问题.
假设我们有一个以下格式的行:
Seat 5: myNickname (1500 in chips)
Run Code Online (Sandbox Code Playgroud)
令牌myNickname可以包含任何字符以及空格.这意味着,这两个(1500 in chip和Seat 5:有效的绰号-这最终导致的模糊问题.除了长度(4-12个字符)之外,对玩家的昵称没有限制.
我需要解析并存储几个数据以及玩家的昵称(例如在这种特殊情况下的座位位置和筹码数量),所以我的问题是,我在这里有什么选择?
我很乐意使用JavaCC来做这件事:
SeatRecord seat() :
{ Token seatPos, nickname, chipStack; }
{
"Seat" seatPos=<INTEGER> ":" nickname=<NICKNAME> "(" chipStack=<INTEGER>
"in chips)"
{
return new SeatRecord(seatPos.image, nickname.image, chipStack.image);
}
}
Run Code Online (Sandbox Code Playgroud)
现在哪个不起作用(由于上述问题)
我还搜索了GLR解析器(显然处理了暧昧的语法) - 但是除了Bison之外,它们似乎大多被放弃或记录不佳,但是它不支持Java的GLR解析器,并且可能太复杂而无法使用anway(除了歧义问题,语法本身是非常基本的,正如我所提到的)
或者我应该坚持自己标记字符串,并使用indexOf(), lastIndexOf()等来解析我需要的数据?只有当它是剩下的唯一选择时我才会这样做,因为它太丑了恕我直言,我可能会错过一些情况(这会导致错误的解析)
使用C#作为MicroSoft Visual Studio 2010中的控制台程序,我已经对您进行了一些更改,并得到了一些帮助,并且此控制台程序运行正常; 但是我需要实现一个恒定的静态字段,它将在主方法的输出部分显示"遵守女童军法"的座右铭.我知道它一定很简单所以请耐心等待.当我在低音类中包含公共静态const字符串Motto ="To Obey the Girl Scout Law"时,我收到一条错误消息 - 常量'DemoScouts.GirlScout.Motto'不能是静态的.以下是该项目的完整代码:
公共课GirlScout {
public static const string Motto = "To Obey the Girl Scout Law";
public static string scoutName;
public static string enterName()
{
return scoutName;
}
public static int duesOwed;
public static int enterAmount()
{
return duesOwed;
}
public static int troopNumber;
public static int enterNumber()
{
return troopNumber;
}
}
class MainClass : GirlScout
{
static void Main()
{
Console.WriteLine();
Console.Write("Enter the Girl Scout's name: "); …Run Code Online (Sandbox Code Playgroud) 我得到了我的手腕,因为在一项任务中,当输入错误发生时,我有一个方法调用.我不知道如何使用或使用什么而不是我写的代码.我需要帮助才能找到正确的方法.
我喜欢编码所以我只需要以正确的方式轻推!:)
我写的代码看起来像这样.
private void SumTheNumbers()
{
Console.Write("Please give the value no "+ index + " :");
if (false == int.TryParse(Console.ReadLine(), out num))
{
//Errormessage if the user did not input an integer.
Console.WriteLine("Your input is not valid, please try again.");
Console.WriteLine();
sum = 0;
SumTheNumbers();
}
else
{
//Calculate the numbers given by user
sum += num;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个程序,其中我有称为"层次结构"的对象,它只是带有ArrayList<ArrayList<String>>带适当getter的strings()的列表列表.
用户应该能够选择这些层次结构的表示/格式 - 例如,层次结构[1,2,3,4]是否应该表示为{1,2,3,4}或(1-4)或其他,在将其写入文件之前.
是否有一种聪明/标准的方法来进行这种数据和格式分离?我正在考虑创建一个"FormattedHierarchy"对象,它只包含一个Hierarchy-object和一个Formatting-object,但我不知道这是不是一个好的设计选择.
感谢您的任何指示/提示/答案.
gridPane.setStyle("-fx-background-color: #C0C0C0;");
Run Code Online (Sandbox Code Playgroud)
工作并设置整个网格窗格的背景颜色,但如何为某一行设置它,例如.第5行?
我需要使用样式表吗?我更喜欢用Java做.
编辑:每行包含一个标签,但标签不会覆盖窗格中的整行.如果我可以将标签设置为覆盖整行,然后设置标签的背景颜色,这将是一种变通方法.
我正在使用debian,默认的python安装为2.6我想迁移到python 2.7,包括安装easy_install.我按照别人的指示删除我的/ usr/bin/python,然后链接
ln -s /usr/bin/python2.7 /usr/bin/python
Run Code Online (Sandbox Code Playgroud)
我下载了最新版本的setuptools
并cd到文件.安装帮助让我把它作为shell程序运行,我做了,出现以下错误:
sh setuptools-0.6c11-py2.7.egg
Traceback (most recent call last):
File "<string>", line 1 in <module>
ImportError: No Module named setuptools.command.easy_install
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,我的easy_install安装与我正在运行的python版本有关,但我无法弄明白.我还尝试下载.tar.gz文件,cd-ing到目录中,然后运行
python setup.py build; setup.py install
Run Code Online (Sandbox Code Playgroud)
运行之后,我可以使用easy_install,出现以下错误:
Traceback (most recent call last):
File "/usr/local/bin/easy_install", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No Module named pkg_resources
Run Code Online (Sandbox Code Playgroud)
谁有人建议给我一个解决方案?谢谢您的帮助.
Checkers 是一个可重用的QuickCheck属性库,特别是对于标准类型类
如何编写检查器实例来测试我的应用验证实例是否有效?
import Test.QuickCheck
import Test.QuickCheck.Checkers
import Test.QuickCheck.Classes
import Control.Applicative
import Data.Monoid
data Validation e a =
Error e
| Scss a
deriving (Eq, Show)
instance Functor (Validation e) where
fmap _ (Error e) = Error e
fmap f (Scss a) = Scss $ f a
instance Monoid e => Applicative (Validation e) where
pure = Scss
(<*>) (Scss f) (Scss a) = Scss $ f a
(<*>) (Error g) (Scss a) = Error g
(<*>) (Scss a) (Error …Run Code Online (Sandbox Code Playgroud) 作为Rust的第一个项目,我正在尝试创建一个行程编码库.
这是我的src/lib.rs(最初由货物创建):
#[cfg(test)]
mod rle;
mod tests {
#[test]
fn it_works() {
let rle1 = rle::Rle {
lengths: vec![1, 4, 2],
values: vec![1, 2, 1],
};
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的src/rle.rs:
pub struct Rle {
lengths: Vec<i32>,
values: Vec<i32>,
}
Run Code Online (Sandbox Code Playgroud)
使用这种布局,我得到了一个 error[E0433]: failed to resolve. Use of undeclared type or module 'rle'
我试图遵循Rust Docs中称为Crates and Modules的指南.
我究竟做错了什么?
如果布局不明显:
$ ls src
lib.rs rle.rs
Run Code Online (Sandbox Code Playgroud) 我有以下使用基因组数据创建数据框的策略:
from hypothesis.extra.pandas import columns, data_frames, column
import hypothesis.strategies as st
def mysort(tp):
key = [-1, tp[1], tp[2], int(1e10)]
return [x for _, x in sorted(zip(key, tp))]
positions = st.integers(min_value=0, max_value=int(1e7))
strands = st.sampled_from("+ -".split())
chromosomes = st.sampled_from(elements=["chr{}".format(str(e)) for e in list(range(1, 23)) + "X Y M".split()])
genomics_data = data_frames(columns=columns(["Chromosome", "Start", "End", "Strand"], dtype=int),
rows=st.tuples(chromosomes, positions, positions, strands).map(mysort))
Run Code Online (Sandbox Code Playgroud)
我对空数据帧并不真正感兴趣,因为它们是无效的,而且我还想生成一些非常长的 dfs。如何更改为测试用例创建的数据帧的大小?即最小尺寸为 1,平均尺寸为大?