{"id":24772492,"url":"https://github.com/mateuszcalderon/cpp-quadratic-equation-solver","last_synced_at":"2025-03-23T20:44:53.440Z","repository":{"id":274472207,"uuid":"923008467","full_name":"mateuszcalderon/cpp-quadratic-equation-solver","owner":"mateuszcalderon","description":"A C++ program that solves quadratic equations, calculating and displaying real roots based on the discriminant.","archived":false,"fork":false,"pushed_at":"2025-02-11T19:20:21.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-11T20:27:29.189Z","etag":null,"topics":["cpp"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mateuszcalderon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-27T13:42:19.000Z","updated_at":"2025-02-11T19:30:46.000Z","dependencies_parsed_at":"2025-01-27T15:25:28.269Z","dependency_job_id":"552fe292-f0d1-4c43-8bb7-1a4384d4a52f","html_url":"https://github.com/mateuszcalderon/cpp-quadratic-equation-solver","commit_stats":null,"previous_names":["mateuszcalderon/cpp-quadratic-equation-solver"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mateuszcalderon%2Fcpp-quadratic-equation-solver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mateuszcalderon%2Fcpp-quadratic-equation-solver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mateuszcalderon%2Fcpp-quadratic-equation-solver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mateuszcalderon%2Fcpp-quadratic-equation-solver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mateuszcalderon","download_url":"https://codeload.github.com/mateuszcalderon/cpp-quadratic-equation-solver/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245168816,"owners_count":20571799,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp"],"created_at":"2025-01-29T04:23:06.953Z","updated_at":"2025-03-23T20:44:53.418Z","avatar_url":"https://github.com/mateuszcalderon.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/devicons/devicon/blob/v2.16.0/icons/cplusplus/cplusplus-original.svg\" height=\"60\" width=\"60\" style=\"margin-right: 20px;\"\u003e\n\u003c/p\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003eC++ Quadratic Equation Solver\u003c/h1\u003e\n\u003c/div\u003e\n\nThis C++ program solves quadratic equations in the form of _ax² + bx + c = 0_. It calculates the roots using the discriminant, which can result in two distinct real roots, one repeated root, or a notification if there are no real solutions. The program includes input validation to ensure correct user input and displays results with clear formatting.\n\n#### Key Features:\n  - Handles invalid input with error checking.\n  - Implements efficient computation and clear output formatting.\n  - Provides informative messages for equations with no real solutions.\n  - Supports both distinct real roots and repeated roots.\n\n## Code Walkthrough:\n#### Libraries:\n```cpp\n  #include \u003ciostream\u003e\n  #include \u003ccmath\u003e\n  #include \u003ciomanip\u003e\n  #include \u003climits\u003e\n```\n\n  - ` \u003ciostream\u003e `: Used for input/output operations.\n  - ` \u003ccmath\u003e `: Provides mathematical functions like ` pow ` and ` sqrt `.\n  - ` \u003ciomanip\u003e `: Used for controlling the output format, such as setting decimal precision.\n  - ` \u003climits\u003e `: Allows checking for input errors and handling input buffer.\n\n#### GetInput Function:\n```cpp\n  bool GetInput(double \u0026variable) {\n    std::cin \u003e\u003e variable;\n    if (std::cin.fail()) {\n        std::cin.clear();\n        std::cin.ignore(std::numeric_limits\u003cstd::streamsize\u003e::max(), '\\n');\n        return false;\n    }\n  return true;\n  }\n```\n\n  - **Purpose:** Ensures that the user inputs valid numerical values. If the input fails, it clears the input buffer and prompts the user to try again.\n  - **How it works:** The function reads input from the user and checks if the input was successful using ` std::cin.fail() `. If the input is invalid (e.g., non-numeric input), it clears the error flag and ignores the invalid input.\n\n#### Main Function and 'a' Input Check:\n```cpp\n  int main() {\n      double a, b, c;\n...\n  std::cout \u003c\u003c \"a = \";\n  while (true) {\n    if (!GetInput(a)) {\n        std::cout \u003c\u003c \"Please enter a valid variable for 'a' = \";\n    } else if (a == 0) {\n        std::cout \u003c\u003c \"This is not a quadratic equation. 'a' cannot be zero. Please enter a non-zero value for 'a' = \";\n    } else {\n        break;\n    }\n  }\n```\n\n  - The program asks the user to input values for the quadratic equation's coefficients (` a `, ` b `, and ` c `).\n  - It uses the ` GetInput ` function to ensure that all inputs are valid.\n  - The program ensures that ` a ` is a valid, non-zero value (since a = 0 would not represent a quadratic equation).\n\n#### 'b' and 'c' Input:\n```cpp\n  std::cout \u003c\u003c \"b = \";\n  while (!GetInput(b)) {\n      std::cout \u003c\u003c \"Please enter a valid variable for 'b' = \";\n  }\n  std::cout \u003c\u003c \"c = \";\n  while (!GetInput(c)) {\n      std::cout \u003c\u003c \"Please enter a valid variable for 'c' = \";\n  }\n```\n\n  - The program prompts the user for values of ` b ` and ` c `, ensuring valid inputs for both.\n\n#### Discriminant and Roots Calculation:\n```cpp\n  const double delta = pow(b, 2) - 4 * a * c;\n\n  std::cout \u003c\u003c \"\\nDelta = \" \u003c\u003c delta \u003c\u003c \"\\n\";\n  if (delta \u003c 0) {\n      std::cout \u003c\u003c \"The equation has no real roots.\\n\";\n  } else {\n      const double x1 = (-b + std::sqrt(delta)) / (2 * a);\n      const double x2 = (-b - std::sqrt(delta)) / (2 * a);\n```\n\n  - The discriminant (` delta `) is calculated using the formula _b² - 4ac_. The discriminant helps determine the number and type of roots of the equation.\n  - If ` delta ` is negative, the equation has no real roots, and the program outputs this information.\n  - If ` delta ` is non-negative, the program calculates two real roots (` x1 ` and ` x2 `) using the quadratic formula.\n\n#### Output the Roots:\n```cpp\n  std::cout \u003c\u003c std::fixed \u003c\u003c std::setprecision(1);\n  if (delta == 0) {\n      std::cout \u003c\u003c \"Both roots are equal: x1 = x2 = \" \u003c\u003c x1 \u003c\u003c \"\\n\";\n  } else {\n      std::cout \u003c\u003c \"x1 = \" \u003c\u003c x1 \u003c\u003c \"\\n\";\n      std::cout \u003c\u003c \"x2 = \" \u003c\u003c x2 \u003c\u003c \"\\n\";\n  }\n```\n\n- The roots are printed with one decimal place precision.\n- If the discriminant is zero, both roots are equal and printed accordingly.\n- If the discriminant is positive, both distinct roots are displayed.\n\n## Development Environment:\nTo develop this project, I used the online IDE [OnlineGDB.com](https://www.onlinegdb.com/online_c++_compiler), which offers an easy-to-use compiler and debugger for C++.\n\n## Coding Standards:\nThis project follows the [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html). You're welcome to suggest any improvements or share tips on coding style.\n\n## Contact:\nFeel free to reach out to me with any questions, suggestions, or feedback!\u003cbr/\u003e\n[![GitHub](https://github.com/CLorant/readme-social-icons/blob/main/large/filled/github.svg)](https://github.com/mateuszcalderon)\n[![Instagram](https://github.com/CLorant/readme-social-icons/blob/main/large/filled/instagram.svg)](https://www.instagram.com/mateuszcalderon/)\n[![LinkedIn](https://github.com/CLorant/readme-social-icons/blob/main/large/filled/linkedin.svg)](https://www.linkedin.com/in/mateuszcalderonreis/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmateuszcalderon%2Fcpp-quadratic-equation-solver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmateuszcalderon%2Fcpp-quadratic-equation-solver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmateuszcalderon%2Fcpp-quadratic-equation-solver/lists"}