Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // METODA PROSTOKĄTÓW
- #include <iostream>
- #include <math.h>
- using namespace std;
- float F(float x) {
- return sin(x);
- }
- int main () {
- long double p, q, dl, s, pole;
- int n, i;
- cout<<"Podaj liczbe rzeczywista p: "<<endl;
- cin>>p;
- cout<<"Podaj liczbe rzeczywista q: "<<endl;
- cin>>q;
- cout<<"Podaj liczbe prostokatow (liczba naturalna) n: "<<endl;
- cin>>n;
- dl = (q-p) / n;
- s = 0;
- while(i<n) {
- s = s + fabs(F(p+dl*i + (dl/2)));
- i++;
- }
- pole = dl*s;
- cout<<"Pole wynosi: "<<pole<<endl;
- }
- -------------------------------------------------------------------------------------
- // METODA TRAPEZÓw
- #include <iostream>
- #include <math.h>
- using namespace std;
- float F(float x) {
- return sin(x);
- }
- int main () {
- long double p, q, dl, s, pole;
- int n, i;
- cout<<"Podaj liczbe rzeczywista p: "<<endl;
- cin>>p;
- cout<<"Podaj liczbe rzeczywista q: "<<endl;
- cin>>q;
- cout<<"Podaj liczbe trapezow (liczba naturalna) n: "<<endl;
- cin>>n;
- dl = (q-p) / n;
- s = 0;
- i = 1;
- while(i<n) {
- s = s + fabs(F(p+i*dl));
- i++;
- }
- pole = (dl/2) * (fabs(F(p)) + fabs(F(q)) + 2*s);
- cout<<"Pole wynosi: "<<pole<<endl;
- }
- -------------------------------------------------------------------------------------
- // OBIE METODY
- #include <iostream>
- #include <math.h>
- using namespace std;
- float F(float x) {
- return sin(x);
- }
- float prostokat(long double p, long double q, int n) {
- long double dl, s, pole1;
- int i;
- dl = (q-p) / n;
- s = 0;
- while(i<n) {
- s = s + fabs(F(p+dl*i + (dl/2)));
- i++;
- }
- pole1 = dl*s;
- return pole1;
- }
- float trapez(long double p, long double q, int n){
- long double dl, s, pole2;
- int i;
- dl = (q-p) / n;
- s = 0;
- i = 1;
- while(i<n) {
- s = s + fabs(F(p+(i*dl)));
- i++;
- }
- pole2 = (dl/2) * (fabs(F(p)) + fabs(F(q)) + 2*s);
- return pole2;
- }
- int main () {
- int wybor;
- long double p, q, n, dl, i, pole1, pole2;
- cout<<"Podaj liczbe rzeczywista p: "<<endl;
- cin>>p;
- cout<<"Podaj liczbe rzeczywista q: "<<endl;
- cin>>q;
- cout<<"Podaj liczbe liczbe naturalna n: "<<endl;
- cin>>n;
- cout<<"-------------------------------------";
- cout<<endl<<"WYBIERZ OPERACJE (1 - 4):"<<endl;
- cout<<"1. Obliczanie metoda prostokatow."<<endl;
- cout<<"2. Obliczanie metoda trapezow."<<endl;
- cout<<"3. Obliczanie obiema metodami."<<endl;
- cout<<"4. Wyjscie z programu."<<endl;
- cout<<"-------------------------------------"<<endl;
- cout<<"OPERACJA: ";
- cin>>wybor;
- switch(wybor) {
- case 1:
- cout<<endl<<"Pole wynosi: "<<prostokat(p, q, n);
- break;
- case 2:
- cout<<endl<<"Pole wynosi: "<<trapez(p, q, n);
- break;
- case 3:
- cout<<endl<<"Pole obliczone metoda prostokatow wynosi: "<<prostokat(p, q, n)<<endl;
- cout<<"Pole obliczone metoda trapezow wynosi: "<<trapez(p, q, n);
- break;
- case 4:
- cout<<endl<<"Wychodzenie z programu...";
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment