将字母转换为数字(A = 1; B = 2 ...)c ++

时间:2017-07-09 18:24:48

标签: c++ string numbers letters

我试图用c ++将数字写成数字。当我在控制台中写字时,它应该计算模数并输出如果船来了(我做了所有,但不能把字母写成数字:/)

这会发生什么:ABC a = 1; B = 2; c = 3 1 * 2 * 3 = 6 ....

所以我需要写一个单词,它应该被分成字母并转换成这样的数字。

我只是在学习,我不太了解:)

我目前的代码:

int shipnum, groupnum, moduleship, modulegroup;

cout << "type ship number "; cin >> shipnum;
cout << "type group number "; cin >> groupnum;



/*shipnum dabar 5... (5 mod 2)
groupnum dabar 3... (3 mod 2)
*/

moduleship = shipnum % 47;        //skaiciuojam moduli...
modulegroup = groupnum % 47;

if (moduleship == modulegroup) {  
 cout << "YES ship is coming for you :)";
 }

else if (moduleship != modulegroup) {             //  "!=" reiskia "nelygu"
    cout << "SORRY, NO ship for you :(";
}

return 0;

4 个答案:

答案 0 :(得分:0)

你的问题不准确,虽然我觉得它已经足够了。提示:请准确了解您提供的信息,无需显示其余代码。

假设我们有char Ship[20]="ABCDEF";。如果编码简单到A = 1,B = 2等,那么你只需要这样的东西:

char Ship[20]="ABCDEF";
int decoded=1;
for(int i=1; Ship[i]=!'/0'; ++i) {
    decoded = decoded * i
}
cout<<decoded;

此循环将一直运行,直到遇到字符串末尾的'\ 0'(空字符)。所以,你会对你的编码(A = 1,B = 2等)代表一个因子这一事实有一个因素。

否则,您可以使用 switch case if 语句来检查单个字符并进行适当的解码。

char Ship[20]="ABCDEF";
int decoded=1;
for(int i=1; Ship[i]=!'/0'; ++i) {
    switch(Ship[i]){
        case 'A'  :   decoded = decoded * 1;
                      break;
        case 'B'  :   decoded = decoded *2;
                      break;
              //So on
        default   :   break;
    }
}
cout<<decoded;

两种情况下的输出:

  

720

答案 1 :(得分:0)

  

&#34;将字母转换为数字(A = 1; B = 2 ......)&#34;

 string a{"ABC"};

    int a0 = a[0]; // 65
    int a1 = a[1]; // 66
    int a2 = a[2]; // 67
    .....

想要包裹A = 1,B = 2 ......

a0 = a0 - (65 - 1);
a1 = a1 - (65 - 1);
....

答案 2 :(得分:0)

问题不明确但我认为问题基本上是将int转换为A=1, B=2, ......, Z=26,它遵循编码#include <iostream> #include <string> using namespace std; int main(){ string s; //Input string cout << "enter the string(CAPITALS ONLY) :"; cin >> s; //read the input string int result = 1; for (auto &elem : s){ //process all the characters of s result *= elem - 'A' + 1; //corresponding int value is multiplied to the result } cout << "the result is :" << result; } 并执行所需的处理,即将所有编码相乘。

所以你可以这样做:

enter the string(CAPITALS ONLY) :AEF
the result is :30

示例输出:

{{1}}

答案 3 :(得分:0)

(a+b)2=a2+b2+2ab:在 cpp 示例中

#include<iostream>

using namespace std;

//Declaring Function in scope

void firstFormula();

int main(void) //Main function
{
    cout << "Hello World!!" << endl; //sample test text printing

    firstFormula(); //executing function

    return 0;
}


//function implementation

void firstFormula(){

//initializing variables

    int a, b;

    cout << "Enter Value of A" << endl;

    cin >> a;//updating input values of a

    cout << "Enter Value of B" << endl;

    cin >> b;//updating input values of b

    int v1 = a + b; //(a+b)2

    int v2 = v1 * v1; //L.H.S

    cout << "Value of v1=" << v1 << endl << "V2=" << v2 << endl;


    cout << "Value of a=" << a << endl << "Value of B=" << b << endl;

    int v3 = a * a;

    int v4 = b * b;

    int v5 = 2 * a * b;

    int v6 = v3 + v4 + v5; //` R.H.S value after equation` `egfyufe`

    cout << "Output=" << "V3=" << v3 << endl << "V4=" << v4 << endl << "v5="
 << v5 << endl << "V6=" << v6 << endl;  

} //end