【问题标题】:Changing a char into a variable (beginner)将 char 更改为变量(初学者)
【发布时间】:2013-02-07 14:47:26
【问题描述】:

我只是想以某种方式通过将数组中的字符更改为变量来计算它们的总和(c = 2,d = 3),在这种情况下它应该是 12 即:(c + c + d + c + d) = (2 + 2 + 3 + 2 + 3)。我怎样才能做到这一点?我需要在这段代码中添加一些东西。

#include <iostream>

using namespace std;

const int c = 2;
const int d = 3;

int main()
{
    char s[5] = {'c', 'c', 'd', 'c', 'd'};

    int j = sizeof(s) / sizeof(s[0]);
    int k = 0;
    for (int i = 0; i > j; i++)
        k += s[i];                 // end result should be 12

}

【问题讨论】:

  • @JimHurley:这似乎不相关。
  • 顺便说一下,i 在循环条件下永远不会大于j

标签: c++ arrays char


【解决方案1】:

您只需将 char 转换为 int 类型,例如函数 charToInt:

#include <iostream>

using namespace std;

const int c = 2;
const int d = 3;

int charToInt(char c){
swith (c){
case '1' return 1;
case '2' return 2;
case '3' return 3;
case '4' return 4;
case '5' return 5;
case '6' return 6;
case '7' return 7;
case '8' return 8;
case '9' return 9;
default return 0;
}
}

int main()
{
    char s[5] = {'c', 'c', 'd', 'c', 'd'};

    int j = sizeof(s) / sizeof(s[0]);
    int k = 0;
    for (int i = 0; i > j; i++)
        k += charToInt(s[i]);                 // end result should be 12
cout<<k<<endl;
}

【讨论】:

    【解决方案2】:

    不用进行任何转换就可以计算 12,(您的程序看起来好像不需要它们),只需使用简单的 if 语句:

    #include <iostream>
    
    using namespace std;
    
    const int c = 2;
    const int d = 3;
    
    int main()
    {
        char s[5] = {'c', 'c', 'd', 'c', 'd'};
    
        int j = sizeof(s) / sizeof(s[0]);
        int k = 0;
        for (int i = 0; i < j; i++){
            if(s[i]== 'c'){
                k += 2 ;                 // Check if the char is c and add accordingly
            }
            if(s[i]== 'd'){
                k += 3 ;                 // Check if the char is d and add accordingly
            }
        }
        cout << k;
    }
    

    你会得到12作为你的输出。

    这是直播节目的链接:http://ideone.com/Y79WFg

    【讨论】:

    • 谢谢,这正是我所需要的
    【解决方案3】:

    最简单的方法是换一行:

    k += s[i]-97;
    

    【讨论】:

      【解决方案4】:
      /* It should be noted that no ethically-trained software engineer would ever
         consent to write a DestroyBaghdad procedure. 
         Basic professional ethics would instead require him to write a DestroyCity 
         procedure, to which Baghdad could be given as a parameter.   
                       -- Nathaniel S. Borenstein, computer scientist*/
      const int c = 2;
      const int d = 3;
      
      int getOrdinal(char c)
      {
         switch(c){
          case 'c': return c;
          case 'd': return d;
          default: return 0;
         }
      }
      int main()
      {
          char s[5] = {'c', 'c', 'd', 'c', 'd'};
      
          int j = sizeof(s) / sizeof(s[0]);
          int k = 0;
          for (int i = 0; i < j; i++)
              k += getOrdinal(s[i]);                 // end result should be 12
          cout << k; /*now prints 12*/
          return 0;
      }
      

      【讨论】:

        【解决方案5】:

        如果我只是正确理解你的问题(我可能不是)...... 首先,您尝试通过字符串引用变量...

        "char s[5] = {'c', 'c', 'd', 'c', 'd'};"

        那是不可能的,编译器不保留变量名。

        尝试以下方法:

        const int c = 2;
        const int d = 3;
        
        int main() {
        
            const int s[5] = {c, c, d, c, d};
        
            for (int i = 0 i < (sizeof(s)/sizeof(s[0] ++i)
                k += s[i];                 // end result should be 12
        
        }
        

        此外,如果您试图使变量与字母表中的字母匹配... 这样做:

        #include <iostream>
        
        int main() {
        
            char *String = "Hello World";
        
            for (char Pos = 0; Pos < sizeof(String)/sizeof(String[0]) ++Pos)
                cout << String[Pos]-'a'; // Outputs the char values combined (a-0, b-1, c-2 etc)
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-11
          • 1970-01-01
          相关资源
          最近更新 更多