Edited -- See below for new info........ I'm new to programming - C++ - and was on Codingame where I ran in to a problem I can't find the answer for...
We were given two strings -- not char array which I expected and seems many of the answers I found on the web expected too -- and were expected to combine them in to one.
Example:
string1 = 01101
string2 = 01001
Answer = 01001
I have learned and understand how the answer is derived but I can't figure out the C++ code for it.
Any help would be great!
Thank you!
Thanks to nicomp I now know the correct terms are bitwise AND, not binary merge.
I was able to get a little further but still having issues getting the answer. I took the following code and got the output of 73.
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
int main()
{
string str1 = "001101";
string str2 = "011001";
int a = stoi(str1);
cout << a << endl;
int b = stoi(str2);
cout << b << endl;
int c = a & b;
cout << c << endl;
return 0;
}
The result I should get is:
string str1 = "001101";
string str2 = "011001";
Answer = "001001";
or I would have settled for 9 since that is the non-binary value, not 73.
Can someone help me progress with this?
Thanks again!
int a = 0b01101
int b = 0b01001
int c = a & b;
You can convert a binary string to int like this:
std::string str = "1101";
int num = std::stoi("01010", 0, 2);
It's not 100% what I was looking for, but it does do what I wanted. The one part I would say isn't to my expectation is that I convert the numbers to decimal rather than leaving everything in binary. Regardless... this does take in two binary numbers and outputs one binary number after ANDing.
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
int main()
{
string input1 = " ";
string input2 = " ";
cout << "Enter first binary number: " << endl;
cin >> input1;
cout << "Enter second binary number: " << endl;
cin >> input2;
int a = (int)bitset<64>(input1).to_ulong();
cout << a << endl;
int b = (int)bitset<64>(input2).to_ulong();
cout << b << endl;
int c = a & b;
cout << c << endl;
string output = bitset<8>(c).to_string();
cout << output << endl;
return 0;
}
Related
How can I get specified number?
For example, the number that a user enters is 4568962358.
I want to save the "96" in a variable; how do I get just the 96 or 23?
This is the real problem:
An important shipping company is making the annual inventory of the goods they have in their warehouse, for which they have implemented a system of codes that are assigned to each good that is in said warehouse. This code, which consists of 16 digits, contains the following information: unique number, indicator of whether it is fragile or not, place of origin of the property and expiration date of the property.
The assigned code structure is UUUFPPPPDDMMAAAA where:
UUU: unique number of the good.
F: digit that indicates if the good is fragile or not. If it is 0 it means that it is fragile.
PPPP: ASCII codes of the two letters that identify the country of origin of the good.
DD: Good's due date.
MM: Month of the expiration of the good.
AAAA: Year of expiration of the good.
You are asked to create a C ++ program that receives the assigned code as data and then prints the following data as shown in the example.
Enter the code: 1120677212042015
Then the program must print: Unique number: 112.
Fragile(N: No; S: Yes): S .
Country of origin: CH .
Day, month and year of expiration: 04-12-2015 .
Well it is outdated to date (N : No; S: Yes): S.
In the solution of the problem you will not be able to make use of selective structures.
Version 1
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace System;
using namespace std;
int main()
{
int code;
int UUU;
int F;
int PP1,PP2;
int DD;
int MM;
int AAAA;
cout << "Enter the code: "; cin >> code; // 1120677212042015
// ...code needed here
UUU = int(code / 10000000000000);
cout << "\nRESULTS" << endl;
cout << "\nUnique number: " << UUU << endl; // prints 112
_getch();
return 0;
}
Version 2
I'm not able to do the next parts; please help!
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace System;
using namespace std;
int main()
{
double code;
double UUU;
double F;
double PP1,PP2;
double DD;
double MM;
double AAAA;
cout << "Enter the code: "; cin >> code; // 1120677212042015
UUU = int(code / 10000000000000);
PP1 = int(code / 10000000000) % 100;
PP2 = int(code / 100000000) % 100;
DD = int(code / 1000000) % 100;
cout << "\nRESULTS" << endl;
cout << "\nUnique number: " << UUU << endl; // 112
cout << "Country of origin: " << (char)PP1 << (char)PP2<<endl; //CH
cout << "Day, Month and Year of expiration: " << DD; // 12/../....
_getch();
return 0;
}
One way is using substr concept.
For example, if user enters input as 1120677212042015, then read it into a string and divide the string into sub-strings according to the format UUUFPPPPDDMMAAAA .
std::string sUserCode = "1120677212042015";
std::string sUniqueNumber = str.substr (0,3);
int iUniqueNumber = stoi(sUniqueNumber);
This is one of those occasions where the C library comes in handy :
int UUU, F, PP1, PP2, DD, MM, AAAA;
cout << "Enter the code: "; // 1120677212042015
scanf("%3d %1d %2d %2d %2d %2d %4d", &UUU, &F, &PP1, &PP2, &DD, &MM, &AAAA);
This captures all the values. The spaces in the format string are not necessary, but makes it easier to read.
u can use % and / to do this
4568962358 / 10000 = 456896 %100 = 96
or you can put it in a array and show the part of integer that u want.
int n = 4568962358;
int a[10];
for (int i = 0; i<10 ; i++)
{
a[i] = n%10;
n /= 10;
}
search your question in stack over flow.
these questions have good answers.
So I'm trying to make a program that asks a user for a planet, this is stored as a string. I want to use a switch statement to make decisions (like if it's earth do this, if it's mars do this). I am aware that switch statements only take ints, so I took the string and translated it to its hex value. The issue I'm stuck on is how do I have the hex value saved as an int.
I am aware that it would be way easier to do a bunch of nested if statements, but I find that really ugly and bulky.
Here is what I have so far:
/*
* Have user input their weight and a planet
* then output how heavy they would be on that planet
* if planet is wrong
* output it
*/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string returnanswer(string x) {
string dat = x;
int test;
ostringstream os;
for (int i = 0; i < dat.length(); i++)
os << hex << uppercase << (int) dat[i];
string hexdat = os.str();
cout << "Text: " << dat << endl;;
cout << "Hex: " << hexdat << endl;
return hexdat;
}
int main() {
int weight;
string planet;
cout << "Please enter your weight: " << endl;
cin >> weight;
cout << "Please enter Planet" << endl;
cin >> planet;
returnanswer(planet);
return 0;
}
One way to convert a string hex to an int (using the suggestion from C++ convert hex string to signed integer) would be this:
unsigned int num;
stringstream ss;
ss << hex << hexdat;
ss >> num;
num would now contain the int value of hexdat.
On a side note, instead of converting the planet name to a hex and then to a string, you should consider using an unordered_map (as others have suggested), for example unordered_map<string, Enum> that maps user input planet names to Enum values. The planet Enum would look something like this:
enum Planet {mercury, venus, earth, mars, jupiter, saturn, uranus, neptune};
I have a problem because i have string input and i want to convert it to decimal.
Here's my code :
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
string inputChecker;
int penghitung =0;
int main(){
string source = "10010101001011110101010001";
cout <<"Program Brute Force \n";
cout << "Masukkan inputan : ";
cin >> inputChecker;
int pos =inputChecker.size();
for (int i=0;i<source.size();i++){
if (source.substr(i,pos)==inputChecker){
penghitung +=1;
}
}
if (source.find(inputChecker) != string::npos)
cout <<"\nData " << inputChecker << " ada pada source\n";
else
cout <<"\nData "<< inputChecker <<" tidak ada pada source\n";
cout <<"\nTotal kombinasi yang ada pada source data adalah " <<penghitung <<"\n";
cout <<"\nDetected karakter adalah " <<inputChecker;
cout <<"\nThe Decimal is :" <<inputChecker;
}
I want to make that last one which is "Decimal" to show converted inputChecker from binary to decimal. Is there any function to easily convert from binary to decimal in c++?
Thanks in advance :))
Use std::strtol with 2 as the base. For example,
auto result = std::strtol(source.c_str(), nullptr, 2);
For brute force:
static const std::string text_value("10010101001011110101010001");
const unsigned int length = text_value.length();
unsigned long numeric_value = 0;
for (unsigned int i = 0; i < length; ++i)
{
value <<= 1;
value |= text_value[i] - '0';
}
The value is shifted or multiplied by 2, then the digit is added to the accumulated sum.
Similar in principle to converting decimal text digits to internal representation.
well I'm trying to get the user to input an integers as much as they want, until they input a negative number. 1st step was to use the ATOF function to convert string to number(which I did), and then allow the user to input integers(I only manage to do once just to see if I can use the atof function correctly.
Any help/tips is appreciated on giving me the right direction.
Here is my code thus far:
#include <iostream>
#include <string>
int main() {
using namespace std;
char buffer[256];
char tempBuff[256] = {'\n'};
double result;
int count = 0;
cout << "Testing " << endl;
cout << "Enter Any integers: ";
cin.getline(buffer,256);
for(int i = 0; i < strlen(buffer); i++)
{
if(isdigit(buffer[i]))
{
tempBuff[count] = buffer[i];
count++;
}
}
if (atof(tempBuff) > 0) {
result = atof(tempBuff) / 2;
}
cout << endl << "The integer you put was: " << tempBuff
<< " And dividing the integers "<< result << endl;
cin.ignore();
return 0;
}
How is atof supposed to know how many valid digits tempBuff contains? The atof function only accepts a C-style string as its input. Otherwise, it has no way to know how many characters are valid.
You can use tempBuff[count] = 0; before the call of atof. A C-style string is terminated by a zero byte.
This here is my code, I have tried googling it but i can find why my code doesnt convert the number properly. however it complies fine. could you please tell me why it is not working and what i should do to fix it.
#include <iostream>
using namespace std;
int main()
{
char a = 0;
char b = 0;
char c = 0;
char d = 0;
cout << "Enter 4 digit octal number ";
cin >> a >> b >> c >> d;
cout << "Decimal form of that number: " << (a * 512) + (b * 64) + (c * 8) + d << endl;
return 0;
}
Because you're getting the values as characters, they're probably ascii values that you're multiplying with. Try using (a-'0'), (b-'0'), etc.
You also should validate your input to be sure it's a number from 0 to 7.
Why not let the standard library do the work?
#include <ios>
#include <ostream>
#include <iostream>
int main()
{
unsigned n;
std::cin >> std::oct >> n;
std::cout << n << std::endl;
}
convert your values (a, b, c, d) to integers, then multiply.