将char更改为变量(初学者)

时间:2013-02-22 03:46:37

标签: c++ arrays char

我只想以某种方式计算数组中字符的总和,方法是将它们更改为变量(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

}

5 个答案:

答案 0 :(得分:3)

您只需将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;
}

答案 1 :(得分: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

答案 2 :(得分:1)

最简单的方法是更改​​一行:

k += s[i]-97;

答案 3 :(得分:0)

/* 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;
}

答案 4 :(得分:0)

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

“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)

}