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

j7dteeu8  于 2022-11-27  发布在  其他
关注(0)|答案(4)|浏览(224)

我试着在C++中把字母变成数字。当我在控制台中写的时候,它应该计算模,如果船来了就输出(我都做了,但是不能把字母变成数字:/)
这该怎么办:ABC的a = 1; b=2; c=3 123=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;
cotxawn7

cotxawn71#

你的问题不够精确,尽管我觉得已经足够了。提供的信息要精确,没有必要显示其余的代码。
假设我们有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 caseif语句来检查单个字符并进行适当解码。

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

ar5n3qh5

ar5n3qh52#

将字母转换成数字(A=1; B=2......)”

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);
....
wj8zmpe1

wj8zmpe13#

这个问题还不清楚,但我认为基本上是将char转换为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
ny6fqffe

ny6fqffe4#

(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

相关问题