I coded a calculator that could solve normal expressions, (3+5, 6/7), and then I added a feature that could factor quadratic trinomials (ax^2+bx+c, this is unfinished as you can see in my code). The primary thing that I am wondering though is actually not related to my code, (unless you prove it to be).
Problem: So when I tested my program before initially, it worked fine. After running it again, I seem to be getting a form of assertion failure, which you can see in the error I get. Is there any way to fix this?:
1 2 3 4 5 6 7 8
|
Program: ...o 2015\Projects\CalculatorRevised\Debug\CalculatorRevised.exe
File: minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp
Line: 42
Expression: c >= -1 && c <= 255
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
|
I am wondering, what is going on, because the program worked at first, but now its not. I can't even ignore the failure, so my program just fails...
Here is my Program Code. (Please do not post anything related to sloppy algorithms or anything, I just want an answer to the Problem)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
void run();
int main()
{
run();
std::cout << "\n\n";
std::cin.get();
return EXIT_SUCCESS;
}
void run()
{
std::cout << "\nWelcome to Calc v1.0.\n\n";
std::cout << "Menu:\n\n1. Basic Math (Eg: 6 + 5 or 65 / 77 or 6^3)\n";
std::cout << "2. Trinomial Factoring (Eg: 6x^2 + 5x + 6 or 1x^2 - 6x + 3)\n";
std::cout << "> ";
int choice;
std::cin >> choice;
if (choice == 1)
{
while (true)
{
double a, b;
char oper;
std::cout << ">";
std::cin >> a >> oper >> b;
if (oper == '+')
{
std::cout << (a + b) << "\n";
continue;
}
else if (oper == '-')
{
std::cout << (a - b) << "\n";
continue;
}
else if (oper == '*')
{
std::cout << (a * b) << "\n";
continue;
}
else if (oper == '/')
{
if (b == 0) {
std::cout << "Error: No division by 0 permitted!\n";
continue;
}
else
{
std::cout << (a / b) << "\n";
continue;
}
}
else if (oper == '^')
{
if (b == 0)
{
std::cout << "1\n";
}
else if (a == 0)
{
std::cout << "0\n";
}
else if (b < 0)
{
double result = 1;
double abs = -1 * b;
for (int i = 0; i < abs; i++)
{
result *= a;
}
std::cout << "1/" << result << "\n";
}
else
{
double result = 1;
for (int i = 0; i < b; i++)
{
result *= a;
}
std::cout << result << "\n";
}
continue;
}
else
{
std::cout << "Invalid...\n";
exit(1);
}
}
} //Basic Math Operations
else if (choice == 2)
{
while (true) {
int a = 1, b, c, t = 2;
char x1 = 'x', x2 = 'x', power = '^', oper1, oper2;
std::cout << "> ";
std::cin >> a >> x1 >> power >> t >> oper1 >> b >> x2 >> oper2 >> c;
if (a == 1)
{
if ((oper1 == '+') && (oper2 == '+'))
{
double discriminant = (b*b) - (4 * a*c); //Discriminant is important, negative = non-factorable
if (discriminant < 0)
{
std::cout << "Non Factorable\n";
continue;
}
else
{
double sol1 = ((-1 * b) + sqrt((b*b) - (4 * a*c))) / 2 * a;
double sol2 = ((-1 * b) - sqrt((b*b) - (4 * a*c))) / 2 * a;
if ((sol1 < 0) && (sol2 < 0))
{
if (isdigit(sol1) || (sol2 - (int)sol2 < 0))
{
std::cout << "Non Factorable\n";
continue;
}
else
{
std::cout << "(x + " << (-1 * sol1) << ")(x + " << (-1 * sol2) << ")\n";
continue;
}
}
}
}
else if ((oper1 == '-') && (oper2 == '+'))
{
double discriminant = (b*b) - (4 * a*c);
if (discriminant < 0)
{
std::cout << "Non Factorable\n";
continue;
}
else
{
double sol1 = (b + sqrt((b*b) - (4 * a*c))) / 2 * a;
double sol2 = (b - sqrt((b*b) - (4 * a*c))) / 2 * a;
if ((sol1 > 0) && (sol2 > 0))
{
if ((sol1 + (int)sol1 < 0) || (sol2 + (int)sol2 < 0))
{
std::cout << "Non Factorable\n";
continue;
}
else
{
std::cout << "(x - " << (sol1) << ")(x - " << (sol2) << ")\n";
continue;
}
}
}
}
}
else
{
std::cout << "Invalid...\n";
exit(1);
}
}
}
}
|