Моя работа состоит в том, чтобы полностью изменить процесс, который я использовал в моей переменной int sum. Тем не менее, я должен использовать оператор модуля вместе с целочисленным делением. Вот мой код до сих пор.
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main(){
string fourLetterWord;
cout << setw(10) << "" << "Please enter a four letter word! ";
while (cin >> fourLetterWord)
{
if (fourLetterWord.length() == 4)
{
cout << setw(10) << "" << "The ASCII value of " << fourLetterWord[3] << "=" << int(fourLetterWord[3]) << endl;
cout << setw(10) << "" << "The ASCII value of " << fourLetterWord[2] << " = " << int(fourLetterWord[2]) << endl;
cout << setw(10) << "" << "The ASCII value of " << fourLetterWord[1] << " = " << int(fourLetterWord[1]) << endl;
cout << setw(10) << "" << "The ASCII value of " << fourLetterWord[0] << " = " << int(fourLetterWord[0]) << endl;
int sum = fourLetterWord[3] * pow(2, 24) + fourLetterWord[2] * pow(2, 16) + fourLetterWord[1] * pow(2, 8) + fourLetterWord[0] * pow(2, 0);
cout << setw(10) << "" << "If the four bytes of " << fourLetterWord << " were read as an int, its value would be " << sum << endl;
cout << setw(10) << "" << "Let's reverse the process. " << endl;
cout << setw(10) << "" << "Enter an unsigned int value: ";
while(cin >> sum)
{
cout << setw(10) << "" << sum % int(fourLetterWord[3]) * pow(2, 24) + sum % (fourLetterWord[2]) * pow(2, 16)
+ sum % int(fourLetterWord[1]) * pow(2, 8) + sum % int(fourLetterWord[0]) * pow(2, 0);
}
}
else
{
cout << setw(10) << "" << "Please re-enter a four letter word.";
cin >> fourLetterWord;
}
}
}
Когда я вывожу это, я получаю:
Please enter a four letter word! FORE
The ASCII value of E = 69
The ASCII value of R = 82
The ASCII value of O = 79
The ASCII value of F = 70
If the four bytes of FORE were read as an integer, its value would be 1163022150
Let's reverse the process.
Enter an unsigned integer value: 1163022150
5.58767e+08
Мне нужна помощь, чтобы получить значение 1163022150, чтобы стать словом FORE.