C++

C++

Ce este C++?

C++ este un limbaj compilat, multi-paradigmă, folosit pentru aplicații performante (sisteme, jocuri, embedded). În continuare găsești o sinteză practică cu accent pe bibliotecile <iostream> și <cmath>.

What is C++?

C++ is a compiled, multi-paradigm language for high-performance apps (systems, games, embedded). Below is a practical tour focused on <iostream> and <cmath>.

Intrare / Ieșire cu <iostream>

Fluxuri standard: std::cin (input), std::cout (output), std::cerr (erori), std::clog (log).
#include <iostream>   // std::cout, std::cin, std::endl
#include <string>

int main() {
  std::string nume;
  int varsta = 0;

  std::cout << "Nume: ";
  std::getline(std::cin, nume);

  std::cout << "Vârsta: ";
  if (!(std::cin >> varsta)) {
    std::cerr << "Input invalid!\n";
    return 1;
  }

  std::cout << "Salut, " << nume << " (" << varsta << " ani)\n";
  std::clog << "[INFO] Date afișate cu succes.\n";
}

Formatare simplă (endl, \n, concatenare)

#include <iostream>
int main() {
  std::cout << "Linia 1" << std::endl;  // goliți bufferul și newline
  std::cout << "Linia 2\n";              // doar newline (mai rapid)
}

Input / Output with <iostream>

Standard streams: std::cin (input), std::cout (output), std::cerr (errors), std::clog (logs).
#include <iostream>
#include <string>

int main() {
  std::string name;
  int age = 0;

  std::cout << "Name: ";
  std::getline(std::cin, name);

  std::cout << "Age: ";
  if (!(std::cin >> age)) {
    std::cerr << "Invalid input!\n";
    return 1;
  }

  std::cout << "Hello, " << name << " (" << age << ")\n";
  std::clog << "[INFO] Output successful.\n";
}

Simple formatting (endl, \n, chaining)

#include <iostream>
int main() {
  std::cout << "Line 1" << std::endl;  // flush + newline
  std::cout << "Line 2\n";              // just newline (faster)
}

Operații matematice cu <cmath>

Funcții uzuale: std::sqrt, std::pow, std::hypot, std::sin, std::cos, std::tan, std::ceil, std::floor, std::round, std::fabs.
#include <iostream>
#include <cmath>      // sqrt, pow, sin, cos, etc.

int main() {
  double a = 3.0, b = 4.0;

  double c = std::hypot(a, b);         // radical(a^2 + b^2)
  double aria_cerc = M_PI * std::pow(5.0, 2); // πr^2 (M_PI e disponibil pe majoritatea platformelor)
  double s = std::sin(M_PI/6.0);       // sin(30°) = 0.5
  double c0 = std::cos(M_PI/3.0);      // cos(60°) = 0.5

  std::cout << "Cateta a=" << a << ", b=" << b
            << ", ipotenuza=" << c << '\n';
  std::cout << "Aria cerc r=5 este " << aria_cerc << '\n';
  std::cout << "sin(30°)=" << s << ", cos(60°)=" << c0 << '\n';

  // Rotunjiri:
  std::cout << "ceil(2.3)=" << std::ceil(2.3) << ", floor(2.7)=" << std::floor(2.7)
            << ", round(2.5)=" << std::round(2.5) << '\n';
}

Exemplu: ecuație de gradul II

#include <iostream>
#include <cmath>

int main() {
  double a, b, c;
  std::cout << "ax^2 + bx + c = 0\nA=";
  std::cin >> a; std::cout << "B="; std::cin >> b; std::cout << "C="; std::cin >> c;

  double delta = b*b - 4*a*c;
  if (delta < 0) {
    std::cout << "Fără soluții reale.\n";
  } else {
    double r1 = (-b - std::sqrt(delta)) / (2*a);
    double r2 = (-b + std::sqrt(delta)) / (2*a);
    std::cout << "Rădăcini: " << r1 << " și " << r2 << '\n';
  }
}

Math operations with <cmath>

Usual functions: std::sqrt, std::pow, std::hypot, std::sin, std::cos, std::tan, std::ceil, std::floor, std::round, std::fabs.
#include <iostream>
#include <cmath>

int main() {
  double a = 3.0, b = 4.0;

  double c = std::hypot(a, b);           // sqrt(a^2 + b^2)
  double circle = M_PI * std::pow(5.0, 2);
  double s = std::sin(M_PI/6.0);         // 0.5
  double c0 = std::cos(M_PI/3.0);        // 0.5

  std::cout << "a=" << a << ", b=" << b << ", hyp=" << c << '\n';
  std::cout << "Area r=5: " << circle << '\n';
  std::cout << "sin(30°)=" << s << ", cos(60°)=" << c0 << '\n';

  std::cout << "ceil(2.3)=" << std::ceil(2.3)
            << ", floor(2.7)=" << std::floor(2.7)
            << ", round(2.5)=" << std::round(2.5) << '\n';
}

Example: quadratic equation

#include <iostream>
#include <cmath>

int main() {
  double a, b, c;
  std::cout << "ax^2 + bx + c = 0\nA=";
  std::cin >> a; std::cout << "B="; std::cin >> b; std::cout << "C="; std::cin >> c;

  double d = b*b - 4*a*c;
  if (d < 0) {
    std::cout << "No real roots.\n";
  } else {
    double r1 = (-b - std::sqrt(d)) / (2*a);
    double r2 = (-b + std::sqrt(d)) / (2*a);
    std::cout << "Roots: " << r1 << " and " << r2 << '\n';
  }
}

Structuri liniare

#include <iostream>
#include <cmath>
int main() {
  double x = 2.0;
  double y = std::sqrt(x) + std::pow(x, 3); // calcule în linie
  std::cout << "Rezultat: " << y << '\n';
}

Linear structures

#include <iostream>
#include <cmath>
int main() {
  double x = 2.0;
  double y = std::sqrt(x) + std::pow(x, 3); // inline math
  std::cout << "Result: " << y << '\n';
}

Structuri alternative

#include <iostream>
#include <cmath>
int main() {
  double r; std::cin >> r;
  double aria = M_PI * r * r;
  if (aria > 100.0) {
    std::cout << "Cerc mare\n";
  } else {
    std::cout << "Cerc mic\n";
  }
}

Alternative structures

#include <iostream>
#include <cmath>
int main() {
  double r; std::cin >> r;
  double area = M_PI * r * r;
  if (area > 100.0) {
    std::cout << "Big circle\n";
  } else {
    std::cout << "Small circle\n";
  }
}

Structuri repetitive

#include <iostream>
#include <cmath>
int main() {
  // tablă trigonometrie simplă
  for (int deg = 0; deg <= 90; deg += 30) {
    double rad = deg * M_PI / 180.0;
    std::cout << deg << "°: sin=" << std::sin(rad)
              << ", cos=" << std::cos(rad) << '\n';
  }
}

Loop structures

#include <iostream>
#include <cmath>
int main() {
  // tiny trig table
  for (int deg = 0; deg <= 90; deg += 30) {
    double rad = deg * M_PI / 180.0;
    std::cout << deg << "°: sin=" << std::sin(rad)
              << ", cos=" << std::cos(rad) << '\n';
  }
}

Test C++ (6 întrebări)

Întrebări despre <iostream> și <cmath>.

1) Ce flux folosești pentru erori?

2) Ce face std::endl?

3) Formula corectă pentru ipotenuză?

4) Care este valoarea lui std::sin(M_PI/2)?

5) Ce funcție rotunjește în sus?

6) Ce tip returnează std::cin >> x?

C++ Quiz (6 questions)

Questions about <iostream> and <cmath>.

1) Which stream is for errors?

2) What does std::endl do?

3) Correct hypotenuse formula?

4) Value of std::sin(M_PI/2)?

5) Which function rounds up?

6) What type does std::cin >> x return?