有没有办法让 Objective-C 支持多变量 switch 结构? [英] Is there a way to make Objective-C support a multi-variable switch construct?

查看:60
本文介绍了有没有办法让 Objective-C 支持多变量 switch 结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道:有没有办法让 Objective-C 支持多变量 switch 构造?

I was wondering: is there a way to make Objective-C support a multi-variable switch construct?

我的意思是,很多时候我必须处理解决方案依赖于一对变量而不是单个变量的问题.对于一长串单个变量值,可以使用简单的 switch/case 构造:

I mean, very often I have to deal with problems in which the solution depends on a pair of variables instead of a single one. For a long list of single variable values one can use a simple switch/case construct:

switch (var) {
    case 0: ...
    case 1: ...
    default: ...
}

但是当你必须处理两个值的组合时,你经常会做这样的事情:

But when you have to deal with the combination of two values you often happen to do something like this:

switch (var1) {
    case 0: 
        switch (var2) {
            case 0: ...
            case 1: ...
        }
    case 1: 
        switch (var2) {
            case 0: ...
            case 1: ...
        }        
    ...
}

它变得越来越复杂......我真正想做的是这样的事情:

And it gets more and more complicated ... What I'd really love to do is something like this:

switch (var1, var2) {
    case (0,0) : ...
    case (1,0) : ...
    case (*,1) : ...
    default: ...
}

这将导致更简单和可读的构造.有没有办法支持这样的结构?或者这个的一个轻微的变化?

that will result in a more simple and readable construct. Is there any solution to support such a structure? Or a slight variation of this one?

推荐答案

我喜欢 saphrosit 的答案,但我会尽量使其易于理解.

I like the answer from saphrosit but I'll make try to make it easy to understand.

将问题的可能结果想象成网格中的正方形,其中网格边缘的一条边代表 var1 的值,另一条边代表 var2 的可能值,那么如果您通过网格你会得到这样的东西

Imagine the possible outcomes of your problem as squares in a grid where one edge of the edge of the grid represents values of var1 and the other edge represents the possible values of var2, then if you incrementialy counted through the squares of the of the grid you would it get something like this

      ||                              var1                                   |    
      ||    0    |     1     |     2     | ... |     j     | ... |   n-1     |
======++=====================================================================|
    0 ||    0    |     1     |     2     | ... |     j     | ... |   n-1     |
   ---||---------+-----------+-----------+-----+-----------+-----+-----------|
    1 ||    n    |    n+1    |    n+2    | ... |    n+j    | ... |  n+(n-1)  |
   ---||---------+-----------+-----------+-----+-----------+-----+-----------| 
    2 ||   2*n   |   2*n+1   |  2*n+2    | ... |   2*n+j   | ... | 2*n+(n-1) |
 v ---||---------+-----------+-----------+-----+-----------+-----+-----------|
 a    ||    .    |     .     |     .     |     |     .     |     |  .        |
 r ---||---------+-----------+-----------+-----+-----------+-----+-----------|
 2  i ||   i*n   |   i*n+1   |   i*n+2   | ... |   i*n+j   | ... | i*n+(n-1) |
   ---||---------+-----------+-----------+-----+-----------+-----+-----------|
      ||    .    |      .    |      .    |     |     .     |     |  .        |
  ----||---------+-----------+-----------+-----+-----------+-----+-----------|
  m-1 || (m-1)*n | (m-1)*n+1 | (m-1)*n+2 | ... | (m-1)*n+j | ... |   mn-1    | <-- (m-1)*n+(n-1) = m*n-n + (n-1) = mn-1
------||---------+-----------+-----------+-----+-----------+-----+-----------|

这被称为行主矩阵,因为您从跨行计数开始.还有一个列主要矩阵,您可以首先开始倒计时而不是跨过.这就是矩阵在 C BLAS 库中的存储方式,因此对很多人来说应该非常熟悉.

This would is called a row major matrix since you start by counting accross the rows. There is also a column major matrix where you start counting down first instead of across. This is how matrixes are stored in the C BLAS library so it should be very fimilar to many people.

在你的情况下,你正在寻找的结果可以被处理为 var3 = var2*n + var1 你可以在代码中将其布局为

In your case the outcome you're looking for can be addressed as var3 = var2*n + var1 you could lay this out in code as

#define N 10 // Maximum size of var1

int main() {

   int var1 = 1;
   int var2 = 1;

   switch(var1 + var2 * N){
      case 1 + 1 * N: printf("One One"); break;
      case 2 + 2 * N: printf("Two Two"); break;
      default:
      printf("Bada Bing");
   }

   return 0;
}

注意:之前的代码不会起作用,现在起作用了.

NOTE: the code that was here earlier wouldn't have worked, this works.

这篇关于有没有办法让 Objective-C 支持多变量 switch 结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆