在一个微控制器程序中,我有一些指令,除了默认情况,我想为交换机中的每个案例执行一些指令.但是,我不想为每个案例编写函数调用或使用宏.
因为这适用于以3-7Mhz运行的微控制器,速度和代码空间非常重要.例如:
switch(letter)
{
case a:
ShowApple();
printf("You entered an english letter.");
break;
case b:
ShowBananna();
printf("You entered an english letter.");
break;
...
case z:
ShowZebra();
printf("You entered an english letter.");
break;
default:
printf("You did not enter an english letter. Silly human!");
break;
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个脚本来为我网站的各个部分自动生成RSS源.我已经被数据库查询并且有一个数据数组,代表每个被调用的部分$showData.我正在使用switch语句来设置各种变量,具体取决于站点的部分.我不是每次添加一个节目都要改变这个剧本,我希望case在我的switch声明中是动态的.
<?php
switch($section){
case 'show1':
$title = $showData['show1'] . ' Title';
$description = $showData['show1'] . ' Description';
break;
case 'show2':
$title = $showData['show2'] . ' Title';
$description = $showData['show2'] . ' Description';
break;
}
?>
Run Code Online (Sandbox Code Playgroud)
我试图使用foreach循环来创建每个案例,但你不能把它放在一个switch语句中.我读过的其他帖子中的其他人建议使用eval().
这是我尝试过的代码,但是没有用.
<?php
switch($showFilter)}
foreach($showFilters as $key => $value){
case $key:
$title = $value;
$description = $value;
break;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
这是一个print_r数组
Array
(
[show1] => The Name of …Run Code Online (Sandbox Code Playgroud) 在我们公司,我们使用这个代码(在最后给出)大约10年,它工作正常.
几天前我们遇到了一些问题,我们不得不重新编写完整的包,我们决定用Damian的Switch模块替换这个代码(为了提高代码的可读性).
一切都对我们很好.
后来我在Perlmonks上发现Damian把这个模块放在了下面
您不应该在生产中使用Damian模块,因为它们的目的是探索和构建未来的核心语言功能.
但它对我们来说工作正常,因为我们没有达到这个模块的限制(我猜).
现在我请大家看一下这两个实现(嵌套if else vs switch),让我知道在新的实现中使用Switch是否正常或是否为我们创造了一些未来的问题?在下面给出的代码中使用Switch是否正常或是否有任何隐藏的错误/问题?
我已经在CPAN和Perlmonks上阅读了这个模块的错误和评论,我想我们的代码远远没有碰到那些错误(我想是这样).
我们正在使用Perl 5.8.5.
PS:我知道Switch的替代品,我们given/when在Perl 5.10中,我们可以使用这里dispatch table指定的其他解决方案,但是现在我们只想比较使用Switch的新实现.
使用嵌套if else
if ($command =~ /^enter$/) {
$self->show_main_frames();
}
elsif ($command =~ /^XYZ_MENU/i) {
$self->show_main_menu($manual, $dbot);
}
elsif ($command =~ /^DBOT/i) {
$dbot->process();
}
# XML is used for the reminders-history: Request 2666
elsif ($command =~ /^XML_DBOT/i) {
$dbot->process();
}
elsif ($command =~ /^UGS/i) {
$ugsui->process();
}
elsif ($command eq "kill") {
my $login …Run Code Online (Sandbox Code Playgroud) string liquidClass = string.Empty;
switch (cmbLiquidClass.Text)
{
case "LiquidClass1":
liquidClass = Settings.Default.LiquidClass1;
break;
case "LiquidClass2":
liquidClass = Settings.Default.LiquidClass2;
break;
case "LiquidClass3":
liquidClass = Settings.Default.LiquidClass3;
break;
case "LiquidClass4":
liquidClass = Settings.Default.LiquidClass4;
break;
case "LiquidClass5":
liquidClass = Settings.Default.LiquidClass5;
break;
case "LiquidClass6":
liquidClass = Settings.Default.LiquidClass6;
break;
case "LiquidClass7":
liquidClass = Settings.Default.LiquidClass7;
break;
case "LiquidClass8":
liquidClass = Settings.Default.LiquidClass8;
break;
case "LiquidClass9":
liquidClass = Settings.Default.LiquidClass9;
break;
}
Run Code Online (Sandbox Code Playgroud)
试图获取当前类的内容的局部变量.由于字符串的工作方式(行为类似于值类型),我无法使用字典.有没有办法重构这个,以便它不需要这么多行来找到所选择的液体类?
我正在尝试从文本文件中运行'Recipe'读取并逐行解析以动态调用一系列方法.我想在做了一些谷歌搜索后我需要实现一个工厂,但我缺少一些关键细节.这是我最接近的例子:
http://simpleprogrammer.com/2010/08/17/pulling-out-the-switch-its-time-for-a-whooping/
以下代码是现在的代码片段.
internal static void Run(int Thread_ID, List<StringBuilder> InstructionSet, List<double>[] Waveforms)
{
//Init
List<double>[] Register = new List<double>[10];
for (int i = 0; i < Waveforms.Length; i++) { Register[i] = new List<double>(Waveforms[i]); }
for (int i = 0; i < Register.Length; i++) { if (Register[i] == null) { Register[i] = new List<double>(); } }
//Run Recipe Steps
foreach (var item in InstructionSet)
{
Step Op = Step.Parse(item.ToString());
switch (Op.TaskName)
{
case "SimpleMovingAverage":
Register[Convert.ToInt32(Op.Args[0])] = Signal_Filters.SimpleMovingAverage(Register[Convert.ToInt32(Op.Args[1])], Convert.ToInt32(Op.Args[2]));
break;
case "RollingSteppedStdDeviation": …Run Code Online (Sandbox Code Playgroud) 我正在查看某人的C代码,并发现了一些我甚至不知道可能的东西.在某些情况下,修改了switch变量,意图在中断后执行另一个 case.
switch (variable)
{
case 1:
some_code();
variable = 3;
break;
case 2:
more_code();
variable = 5;
break;
case 3:
more_code();
variable = 5;
break;
case 4:
my_code();
break;
case 5:
final_code();
break;
}
Run Code Online (Sandbox Code Playgroud)
该代码似乎与作者的意图一致.但他是幸运的,还是在C中保证这种行为?我总是假设break语句会导致执行在switch语句之后直接跳转到.我不希望它继续测试其他所有情况.
如何&&在开关盒中使用操作器?
这就是我想要做的:
private int retValue()
{
string x, y;
switch (x && y)
{
case "abc" && "1":
return 10;
break;
case "xyz" && "2":
return 20;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,"abc"并且"1"都是类型string,编译器给我的消息:
"operator &&不能应用于字符串"
我想切换两个整数的可能值,或者在另一种情况下切换两个布尔值。为了讨论的方便,假设我已经完成了
auto mypair = std::make_pair(foo, bar);
Run Code Online (Sandbox Code Playgroud)
我怎样才能达到相当于
switch(mypair) {
case make_pair(true, false): cout << "true and false"; break;
case make_pair(false, true) cout << "false and true"; break;
case default: cout << "something else";
}
Run Code Online (Sandbox Code Playgroud)
使用 C++11?(如果有帮助的话,C++14/17 也相关)?
在 groovy 中是否可以有一个包含多个变量的 switch-case 语句?我尝试使用元组,但案例部分不接受多个参数。
我试图避免几个嵌套的 if 语句,而不是
if (a==1) {
if (b==2) {
if (c==3) {
// do something
}
}
}
else {
if (a==4) {
if (b==5) {
if (c==6) {
//do something else
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可不可以做:
switch(a,b,c) {
case : (1,2,3) // if a==1, b==2 and c==3
// do something
...
case : (4,5,6)
// do something else
...
}
}
Run Code Online (Sandbox Code Playgroud) 我刚刚将当前项目升级到新发布的 .NET Standard 2.1 和 C# 8.0,并决定将一些大型switch语句转换为新的、更紧凑的表达式语法。
由于返回的值在某些计算中进一步使用,我想知道switch当输入变量位于运算符旁边时新表达式的行为如何。
以下面的字符串连接为例:
string input = Console.ReadLine();
string output = "Letter: " + input switch
{
"1" => "a",
"2" => "b",
_ => "else"
};
Console.WriteLine(output);
Run Code Online (Sandbox Code Playgroud)
我猜想它switch与变量的绑定非常强烈input,因此首先被评估。事实上,Letter: a当我输入时,会打印出这样的内容1。
但我现在的问题是:这种行为是否适用于任何运营商?
从我的实验中,我无法确定上述假设不成立的情况,但这显然并不意味着我没有错过任何情况。该文档似乎也没有提到switch表达式上下文中的运算符优先级。还是我这里有更深层次的误解?
c# operator-precedence switch-statement c#-8.0 switch-expression
switch-statement ×10
c# ×4
c ×2
refactoring ×2
c#-8.0 ×1
c++11 ×1
dictionary ×1
factory ×1
foreach ×1
groovy ×1
if-statement ×1
microchip ×1
mysql ×1
perl ×1
php ×1
pic ×1
rss ×1