0

我只是想通过将它们更改为变量(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

}
4

5 回答 5

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;
}
于 2013-02-22T03:51:14.780 回答
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

于 2013-02-22T03:54:08.883 回答
1

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

k += s[i]-97;
于 2013-02-22T03:56:02.417 回答
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;
}
于 2013-02-22T03:59:07.403 回答
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)

}
于 2013-02-22T04:05:05.580 回答