Diskuze: Existence trojuhelniku
Zobrazeno 5 zpráv z 5.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
Proc pouzivas globalni promenne ? Snaz se je nepouzivat.
Jsi v C++, tak pouzivej novejsi knihovny math.h -> cmath.
Otevri ucebnici matematiky. Mas tam logickou chybu v podmince.
Program je napsan strasne. Zlepsi psani kodu.
Zbytecna zavorka v sqrt();
Pridal jsem cyklus, ktery si vyzada spravny zapis stran.
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
double a,b,c,s,S;
cout << "Zadejte delky stran trojuhelniku" << endl;
cin >> a >> b >> c;
while((cin.fail()|| a <= 0 || b <= 0 || c <= 0)){
cout << "Nespravny vstup" << endl;
cout << "Zadejte delky stran trojuhelniku" << endl;
cin >> a >> b >> c;
}
if((a+b)>c && (a+c)>b && (b+c)>a){
s = (a+b+c)/2; // obvod trujuhelniku
S = sqrt(s*(s-a)*(s-b)*(s-c)); // Obsah trojuhelniku
cout << "Trojuhelnik existuje, obsah je: " << S << endl;
}
else
cout << "Strany netvori trojuhelnik" << endl;
system("PAUSE");
return 0;
}
Ještě jsem si s tím včera pohrál a funguje.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
cout << "Zadejte delky stran trojuhelniku" << endl;
double a,b,c,s,S,obsah;
cin >> a >> b >> c;
while(cin.fail()|| a <= 0 || b <= 0 || c <= 0)
{
cout << "Nespravny vstup." << endl;
return 0;
}
if((a+b<c) || (a+c<b) || (b+c<a) )
{
cout << "Strany netvori trojuhelnik." << endl;
return 0;
}
else
{
s = (a+b+c)/2;
S = (s * (s - a) * (s - b) * (s - c));
obsah = sqrt(S);
cout << fixed << setprecision(6) << "Trojuhelnik existuje, obsah je: " << obsah << endl;
}
return 0;
}
Ten kód co jsi mi poslal - Když zadáš nečíselné hodnoty tak se spustí nekonečný cyklus.
Aha. Tak takhle je to vyreseno.
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
double a,b,c,s,S;
cout << "Zadejte delky stran trojuhelniku" << endl;
cin >> a >> b >> c;
while((cin.fail()|| a <= 0 || b <= 0 || c <= 0)){
system("CLS");
cout << "Zadejte delky stran trojuhelniku" << endl;
cout << "Nespravny vstup" << endl;
cin.clear();
cin.ignore();
cin >> a >> b >> c;
}
if((a+b)>c && (a+c)>b && (b+c)>a){
s = (a+b+c)/2; // obvod trujuhelniku
S = sqrt(s*(s-a)*(s-b)*(s-c)); // Obsah trojuhelniku
cout << "Trojuhelnik existuje, obsah je: " << S << endl;
}
else
cout << "Strany netvori trojuhelnik" << endl;
system("PAUSE");
return 0;
}
Zobrazeno 5 zpráv z 5.