Lets say A is equal to 12345678912345. How can I select the third and fourth number in the numbers as input in B. And the eighth and ninth number as input in C. And B and C will be combined to become D. Can someone help me? Thank u.
#include <iostream>
#include <string>
#include <cctype>
using std::string;
using std::cout;
using std::cin;
using std::to_string;
/*
Lets say A is equal to 12345678912345.
How can I select the third and fourth number in the numbers as input in B.
And the eighth and ninth number as input in C.
And B and C will be combined to become D.
*/
int main()
{
longlong A = 12345678912345;
string s = to_string(A);
int B = (s[2] - '0') * 10 + (s[3] - '0'); // * has higher priority than -
int C = (s[7] - '0') * 10 + (s[8] - '0');
int D = B * 100 + C;
cout << "B = " << B << "\nC = " << C << "\nD = " << D << "\n\n";
}
#include <iostream>
#include <vector>
#include <algorithm>
usingnamespace std;
using INT = unsignedlonglong;
vector<int> digitise( INT n )
{
vector<int> digits;
for ( ; n; n /= 10 ) digits.push_back( n % 10 );
reverse( digits.begin(), digits.end() );
return digits;
}
int main()
{
INT A, B, C, D;
// cout << "Input A: "; cin >> A;
A = 12345678912345;
vector<int> digits = digitise( A );
if ( digits.size() < 9 )
{
cout << "Number not large enough";
return 1;
}
else
{
B = 10 * digits[2] + digits[3];
C = 10 * digits[7] + digits[8];
D = 100 * B + C; // Is this what you meant by "combined"?
// D = B + C; // Or just add them up?
}
cout << "B, C, D = " << B << ", " << C << ", " << D << '\n';
}
#include <iostream>
#include <string>
usingnamespace std;
using INT = unsignedlonglong;
int main()
{
INT A, B, C, D;
// cout << "Input A: "; cin >> A;
A = 12345678912345;
string sA = to_string( A );
if ( sA.size() < 9 )
{
cout << "Number not large enough";
return 1;
}
else
{
B = stoi( sA.substr( 2, 2 ) );
C = stoi( sA.substr( 7, 2 ) );
D = 100 * B + C;
}
cout << "B, C, D = " << B << ", " << C << ", " << D << '\n';
}
or with a sledgehammer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <cassert>
usingnamespace std;
using INT = unsignedlonglong;
int main()
{
INT A, B, C, D, power = 1;
// cout << "Input A: "; cin >> A;
A = 12345678912345;
while ( power <= A ) power *= 10; assert( power >= 1000000000 );
B = ( A / ( power / 10000 ) ) % 100;
C = ( A / ( power / 1000000000 ) ) % 100;
D = 100 * B + C;
cout << "B, C, D = " << B << ", " << C << ", " << D << '\n';
}