/****************************/
/* 例外処理 */
/* coded by Y.Suganuma */
/****************************/
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
class Negative {
public:
char *str;
double x, y;
Negative(char *str, double x, double y) {
this->str = new char [100];
strcpy(this->str, str);
this->x = x;
this->y = y;
}
void message(int sw) {
if (sw == 0)
cout << " 1 番目の値を正にして再実行しました\n";
else
cout << " データを修正してください\n";
}
};
void sq(double x, double y)
{
if (x < 0.0 && y < 0.0)
throw Negative("両方とも負\n", x, y);
else if (x < 0.0 || y < 0.0)
throw Negative("片方が負\n", x, y);
double z = sqrt(x+y);
cout << z << endl;
}
int main()
{
try {
double x, y;
cout << "1 つ目のデータは? ";
cin >> x;
cout << "2 つ目のデータは? ";
cin >> y;
sq(x, y);
}
catch (Negative &ng)
{
cout << ng.str;
if (ng.y > 0.0) {
ng.message(0);
sq(-ng.x, ng.y);
}
else
ng.message(1);
}
return 0;
}