#include <iostream> #include <math.h> using namespace std; //measurement input function with error catcher //returns the value of the measur //argument is the name of the measur double getval(string measur) { double c=0; while(c<=0) { cout « measur; cin » c; if (cin.fail()) { cin.clear(); cin.ignore(INT_MAX, '\n'); cout « "You can only enter numbers.\n"; c = 0; continue; } if (c <= 0) { cout « "Enter positive value!\n"; continue; } return c; } } //calculates diagonal of paralelogram double hypcalc(double leg1, double leg2) { return (floor(sqrt(pow(leg1,2)+pow(leg2,2))*10)/10) ? (floor(sqrt(pow(leg1, 2) + pow(leg2, 2)) * 10) / 10) : 0.01; } int main() { //whole code loop do { //enter brick size cout « "The Brick"; double bl = getval("\nlength: "); double bw = getval("\nwidth: "); double bh = getval("\nheight: "); //enter hole size cout « "\nHole"; double ol = getval("\nlength:"); double ow = getval("\nwidth: "); //so sizes of brick and hole is cout « "\nBrick have side: a(" « bl « 'x' « bw « "mm), " « "b(" « bl « 'x' « bh « "mm), " « "c(" « bh « 'x' « bw « "mm)\n" « "Hole is (" « ol « 'x' « ow « "mm)\n"; //compare sides of the brick diagonals with hole diagonal if (hypcalc(ow, ol) >= hypcalc(bw, bl) || hypcalc(ow, ol) >= hypcalc(bw, bh) || hypcalc(ow, ol) >= hypcalc(bh, bl)) { //and if any smaller cout « "The brick fit the hole by side\n"; if (hypcalc(ow, ol) >= hypcalc(bl, bw)) cout « "a(" « bl « 'x' « bw « "mm)\n"; if (hypcalc(ow, ol) >= hypcalc(bl, bh)) cout « "b(" « bl « 'x' « bh « "mm)\n"; if (hypcalc(ow, ol) >= hypcalc(bw, bh)) cout « "c(" « bh « 'x' « bw « "mm)\n"; } //and if not else cout « "The brick does not fit the hole"; //have another brick? cout « "\nTry another brick? (Y/N)\n"; cin.get(); } while (cin.get()=='y'); }

Теги других блогов: программирование C++ калькулятор