# include #include #include #include #include using namespace std; #define PI 3.1416 //double log5(double ); // lets define log base 5 double log5(double x){ double y = log2(x)/log2(5); return y; } int solve_a_problem(void); int main(){ // arithmetics int a=3, b=7,c=10+3; c=17+5; a=23-c; int d=c*2; cout <<"\n\n ARITHMETIC OPERATORS\n"; cout<<"first orund a="<>sz; // // cout<<" first round name sz="<>sz2; // cout<<" first round sz="<3; bool v2 = (5>=5); bool v3 = !23; cout<<"\n\nRELATIONAL OPERATORS\n v1="<b) { cout <<"a>b"; cout <<" branch 1 executed\n"; } if(b>a) { cout <<"b>a"; cout <<" branch 2 executed\n"; } if(b==a) { cout <<"b==a"; cout <<" branch 3 executed\n"; } if(10+5-2*7.5){ cout<<" \ngot true 1";} else cout<<"\n got false 1"; if("virgil"){ cout<<" \ngot true 2";} else cout<<"\n got false 2"; if(""){ cout<<" \ngot true 3";} else cout<<"\n got false 3"; int x=0; if(x==1){ cout<<" \ngot true 4";} else cout<<"\n got false 4"; cout<>a; // cin>>b; cout << "\n\n NESTED IF\n a="<0 and b>0";} else if (b<0){cout << "a>0 and b<0";} else {cout << "a>0 and b=0";} } else if (a<0){ if(b>0){cout << "a<0 and b>0";} else if (b<0){cout << "a<0 and b<0";} else {cout << "a<0 and b=0";} } else { if(b>0){cout << "a=0 and b>0";} else { if (b<0){cout << "a=0 and b<0";} else {cout << "a=0 and b=0";} } } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // return 0; //compare floats VS compare doubles double f1=10.01, f2=10.010000000000001; cout<<"\n\nROUNDING ERROR?\n" <a && !7 >= -1&& !2*5; v2= (!a||b) && (!b||7>a) && (!7 >= (-1&& !2*5)); cout<<"\n\nLOGICAL OPERATORS, PRECEDENCE, SHORT CIRCUIT \n v1="<dx) { cout << "\n cx bigger than dx";} else {cout<<"\n cx smaller than dx";} if ((cx>=65) && (cx<=90)) { cout << "\n c is a capital letter";} // return 0; string sx = "1234"; string sy = "1234"; cout << "\n\nsx="<sy){ cout << "sx>sy";} if(sx==sy){ cout << "sx==sy";} // t1 = strcmp(sx,sy); t1 = strcmp(sx.c_str(),sy.c_str()); cout << "\nstrcmp gives t1="<>selection; switch(selection) { case 1: //do some things with Movies; cout<<"You selected Movies... "; cout<<"Favoite actor: Robert De Niro\n"; break; case 2: //do some things with Music; cout<<"You selected Music."; cout<<" Favorite song: Chan Chan by Compay Segundo\n"; // break; case 3: //do some things with Pictures cout<<"You selected Pictures. "; cout<<"Fovorite: Einstein portrait\n"; // break; default: cout<< "Your selection is not valid\n"; //repeat=0; } //*/ return 0; // solve the train intersection problem solve_a_problem(); return 0; } int solve_a_problem(){ //input, validate double d,t1,s1,t2,s2; cout<<"\n\n SOLVING THE TRAINS INTERSECTION\n"; cout<<"distance in ft between A and B="; cin>>d; if (d<=0) {cout<<"Invalid distance. EXIT"; return 0;} cout <<"time in seconds departure for train ab (A to B)="; cin>>t1; if (t1<=0) {cout<<"Invalid time. EXIT"; return 0;} cout <<"speed in ft/s for train ab (A to B)="; cin>>s1; if (s1<=0) {cout<<"Invalid speed. EXIT"; return 0;} cout <<"time in seconds departure for train ba (B to A)="; cin>>t2; if (t2<=0) {cout<<"Invalid time. EXIT"; return 0;} cout <<"speed in ft/s for train ba (B to A)="; cin>>s2; if (s2<=0) {cout<<"Invalid speed. EXIT"; return 0;} //compute double t = (d+t1*s1 +t2*s2) / (s1+s2); double d1=(t-t1)*s1; double d2=(t-t2)*s2; //verify, output if (d1<0 || d1>d) {cout <<"\ndo not intersect"; return 0;} if (d2<0 || d2>d) {cout <<"\ndo not intersect"; return 0;} cout <<"\n trains intersect at time="<