#include <iostream> #include <iomanip> #include <string.h> using namespace std; void toBinary(int nr, char* c) {int i; char a[100]=""; for(i=0;nr!=0;i++) {a[i]='0'+ (nr%2); nr/=2; } strrev(a); strcpy(c,a); } tuple<bool,bool> sumLogic(bool n1, bool n2, bool d) {if(int(n1+n2+d)%2==1 && (n1 || n2 || d)) if(int(n1+n2+d)>1) return make_tuple(true,true); else return make_tuple(true,false); else if(int(n1+n2+d)>1) return make_tuple(false,true); else return make_tuple(false,false); } void sumBinary(char n[100], char m[100], char* f, int l) {int i,j,x,y=0; strrev(n); cout<<"DEBUG (sumBinary): Done reverting "<<n<<endl; strrev(m); cout<<"DEBUG (sumBinary): Done reverting "<<m<<endl; cout<<"DEBUG (sumBinary): Lengths: n="<<strlen(n)<<" m="<<m<<endl; //cout<<l-strlen(n)<<" "<<l-strlen(m); j=strlen(n); for(i=0;i<l-j;i++) strcat(n,"0"); j=strlen(m); for(i=0;i<l-j;i++) strcat(m,"0"); cout<<"DEBUG (sumBinary): Done adding '0' bits. n="<<n<<" m="<<m<<endl; j=strlen(n); for(i=0;i<j;i++) {cout<<"DEBUG (sumBinary): for "<<i<<" until "<<strlen(n)<<endl; tie(x,y)=sumLogic((n[i]-'0'),(m[i]-'0'),y); cout<<"DEBUG (sumBinary): Calculated logic"<<endl; f[i]='0'+x; f[i+1]='0'+y; cout<<"DEBUG (sumBinary): Added values to f: "<<x<<" "<<y<<endl; } cout<<"DEBUG (sumBinary): Done adding up."<<endl; if(f[strlen(f)-1]=='0') f[strlen(f)-1]='\0'; strrev(f); } void convertComplement(char inp[100], bool neg, int comp, char* out, int l) {int i; if(neg) {for(i=0;i<strlen(inp);i++) if(inp[i]=='1') inp[i]='0'; else inp[i]='1'; if(comp==1) strcpy(out,inp); else if(comp==2) cout<<"DEBUG (convertComplement): "<<inp<<" "<<neg<<" "<<comp<<endl; sumBinary(inp,"1",out, l); } else strcpy(out,inp); cout<<"DEBUG (convertComplement): "<<out<<endl; } int main2() {int x,y; tie(x,y)=sumLogic(0,0,0); cout<<x<<" "<<y<<endl; tie(x,y)=sumLogic(1,0,0); cout<<x<<" "<<y<<endl; tie(x,y)=sumLogic(0,1,0); cout<<x<<" "<<y<<endl; tie(x,y)=sumLogic(0,0,1); cout<<x<<" "<<y<<endl; tie(x,y)=sumLogic(1,1,0); cout<<x<<" "<<y<<endl; tie(x,y)=sumLogic(1,0,1); cout<<x<<" "<<y<<endl; tie(x,y)=sumLogic(0,1,1); cout<<x<<" "<<y<<endl; tie(x,y)=sumLogic(1,1,1); cout<<x<<" "<<y<<endl; } int main() {int i,x,y,biti; bool xNeg=false,yNeg=false; char n[100],m[100],a[100],b[100],f[100]; cout<<"x=";cin>>x; cout<<"y=";cin>>y; cout<<"Biti: ";cin>>biti; if(x<0) {xNeg=true; x*=-1; // cout<<"DEBUG: x is negative"<<endl; } if(y<0) {yNeg=true; y*=-1; // cout<<"DEBUG: y is negative"<<endl; } cout<<"DEBUG (main): Done checking negative."<<endl; toBinary(x,n); toBinary(y,m); cout<<"DEBUG (main): Done converting to binary. n="<<n<<"; m="<<m<<endl; // cout<<"DEBUG: "<<n<<endl; // cout<<"DEBUG: "<<m<<endl; // sumBinary(n,m,f); /*if(strlen(f)>biti) {cout<<"EROARE: Limita de biti depasita!"; return 0; } else if(strlen(f)<biti) {strrev(f); for(i=0;i<=biti-strlen(f);i++) strcat(f,"0"); strrev(f); } */ sumBinary(n,m,n,biti); cout<<"DEBUG (main): Done summing."<<endl; cout<<n; return 0; /* if((xNeg && !yNeg && x>y) || (!xNeg && yNeg && x<y) || (xNeg && yNeg)) {cout<<"MS: 1 "<<f<<endl; convertComplement(n, true, 1, a); convertComplement(m, true, 1, b); sumBinary(a,b,f); cout<<"C1: 1 "<<f<<endl; convertComplement(n, true, 2, a); convertComplement(m, true, 2, b); sumBinary(a,b,f); cout<<"C2: 1 "<<f<<endl; } else {cout<<"MS: 0 "<<f<<endl; convertComplement(n, false, 1, a); convertComplement(m, false, 1, b); sumBinary(a,b,f); cout<<"C1: 0 "<<f<<endl; convertComplement(n, false, 2, a); convertComplement(m, false, 2, b); sumBinary(a,b,f); cout<<"C2: 0 "<<f<<endl; } */ }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}