Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
0 of .
Results for:
P. 1
Exercitii laborator - C++

# Exercitii laborator - C++

Ratings: (0)|Views: 1,412|Likes:

### Availability:

See more
See less

07/31/2013

pdf

text

original

Exercitii laborator cursul 2
D. INSTRUCTIUNILE LIMBAJULUI C++
1. INSTRUCTIUNEA EXPRESIE.
Exemplul1. Interschimbarea continutului a 2 variabile care au fost initial citite.
#include<iostream.h>#include<conio.h>void main(){int a,b,m;clrscr();cout<<"a=" ; cin>>a;cout<<"b="; cin>>b;m=a,a=b,b=m;cout<<"a="<<" "<<a<<endl;cout<<"b="<<" "<<b;getch();}
Exemplul2. Se citesc 3 valori intregi
a,b,c
si se afiseaza media lor aritmetica
#include<iostream.h>#include<conio.h>void main(){int a,b,c;float m;clrscr();cout<<"a=" ; cin>>a;cout<<"b="; cin>>b;cout<<"c=" ; cin>>c;m=float(a+b+c)/3;cout<<"media aritmetica ="<<" "<<m;getch();}

2. INSTRUCTIUNEA IF.
Forma generala:
if (
expresie
)
instructiune1
else
instructiune2Se evalueaza expresia, daca este
se executa
instructiune1
, daca este
falsa
seexecuta
instructiune2
Exemplul 1. Calculeaza maximul dintre 2 numere citite
#include<iostream.h>#include<conio.h>void main(){int a,b,max;clrscr();cout<<"a=" ; cin>>a;cout<<"b="; cin>>b;if(a>b) max=a;else max=b;cout<<"numarul mai mare este "<<" "<<max;getch();}
Exemplul 2. Se citesc coeficientii
a, b, c
ale unei ecuatii de gradul doi si se precizeaza naturaradacinilor si semnul lor.
#include<iostream.h>#include<math.h>#include<conio.h>void main(){float a,b,c,d,s,p;clrscr();cout<<"a=";cin>>a;cout<<"b=";cin>>b;cout<<"c=";cin>>c;d=b*b-4*a*c; s=float(-b/a); p=float(c/a);cout<<"Discriminantul ecuatiei D="<<d<<endl;cout<<"Produsul radacinilor P="<<p<<endl;cout<<"Suma radacinilor S="<<s<<endl;if(d<0) cout<<"Ecuatia nu are solutii reale";else{ if(d==0) {if(s>0) cout<<"Ecuatia are 2 solutii reale egale si pozitive";else cout<<"Ecuatia are 2 solutii reale egale si negative";}elseif(p>0){if(s>0) cout<<"Ecuatia are 2 solutii reale pozitive";

else cout<<"Ecuatia are 2 solutii reale negative";}else cout<<"Ecuatia are 2 solutii reale de semne opuse";}getch();}
Exemplul 3. Rezolvarea unei ecuatii de gradul 1.
#include<iostream.h>#include<conio.h>void main(){float a,b,x;clrscr();cout<<"a=" ; cin>>a;cout<<"b="; cin>>b;if (a!=0){x= -b/a ;cout<<"x="<<" "<<x; }elseif(b==0) cout<<"ecuatia are o infinitate de solutii";else cout<<"ecuatia nu are solutie";getch();}
Exemplul 4. Rezolvarea unei ecuatii de gradul 2.
#include<iostream.h>#include<conio.h>#include<math.h>void main(){float a,b,c,d,x1,x2,x;clrscr();cout<<"a=" ; cin>>a;cout<<"b="; cin>>b;cout<<"c=" ; cin>>c;d=float( b*b-4*a*c);cout<<"discriminantul ecuatiei este"<<" "<<sqrt(d)<<endl;if(d<0) {cout<<"ecuatia nu are solutii reale";}elseif (d>0){ x1=(-b+sqrt(d)) / (2*a) ; x2=(-b-sqrt(d)) / (2*a);cout<<"x1="<<x1<<endl;cout<<"x2="<<x2<<endl;}else{x=float(-b/2*a);cout<<"ecuatia are solutie unica x=x1=x2="<<" "<<x;}getch();}