我没有得到所需的输出,如下所示
// My 预期的输出在代码中Equation F(x) = 2 + x^2 * 0.2 Range: Min.: -1 Max.: 1 Interpolation Points: 20 Index x y = 2+x^2*0.2 --------------------------------- *1 -1.0 2.20 2 -0.9 2.16 3 -0.8 2.13 4 -0.7 2.10 5 -0.6 2.07 6 -0.5 2.05 7 -0.4 2.03 8 -0.3 2.02 9 -0.2 2.01 10 -0.1 2.00 11 0 2.00 12 0.1 2.00 13 0.2 2.01 14 0.3 2.02 15 0.4 2.03 16 0.5 2.05 17 0.6 2.07 18 0.7 2.10 19 0.8 2.13 20 0.9 2.16 21 1.0 2.20*
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <Windows.h>
using namespace std;
//
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
// Assure the char is validated as a value or an expression
bool isValue(const char& c, bool countOpeningBracket, bool countClosingBracket)
{
if (countOpeningBracket && countClosingBracket)
{
return c == '(' || c == ')' || c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == 'x';
}
else if (countOpeningBracket)
{
return c == '(' || c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == 'x';
}
else if (countClosingBracket)
{
return c == ')' || c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == 'x';
}
else
{
return c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == 'x';
}
}
// Assure the character on the right side is a value or an expression
bool rightSideValidation(const string& str, const int& position)
{
return isValue(str[position + 1], true, false);
}
// Assure the character on the left side is a value or an expression
bool leftSideValidation(const string & str, const int& position)
{
return isValue(str[position - 1], false, true);
}
// Assure the character is a unary operator
bool isUnaryOperator(const string & str, const int& position)
{
// it is '-' unary operator
if (str[position] == '-')
{
// if the operator is not the first in the array
if (position > 0)
{
// if the previous character is not a number
if (!leftSideValidation(str, position))
{
// this is unary operator e.g. 4*-6
return true;
}
}
else
{
// this is unary operator e.g. -6+2*8
return true;
}
}
// this is not a unary operator e.g. "4-6"
return false;
}
// Assure the character is a valid unary operator
bool UnaryOperatorValidation(string & str, const int& position)
{
// there is a unary operator followed by a character
if (str.length() >= (position + 2))
{
// unary operator follwed by a number or an expression
if (rightSideValidation(str, position))
{
// valid unary operator
return true;
}
}
// invalid unary operator
return false;
}
// Assure the character is a binary operator
bool isBinaryOperator(const string & str, const int& position)
{
// the character is a binary operator
if (str[position] == '-' || str[position] == '^' || str[position] == '/' || str[position] == '*' || str[position] == '+')
{
return true;
}
// the character is NOT a binary operator
return false;
}
// Assure the character is a valid unary operator
bool BinaryOperatorValidation(string & str, const int& position)
{
// binary operator is between two other characters
if (position > 0 && position < (str.length() - 1))
{
// the left side and the right side of the binary operator are numbers, expressions or both
if (rightSideValidation(str, position) && leftSideValidation(str, position))
{
// valid binary operator. these are the four possibilities e.g. "5+6", ")+2", "7+(" or ")+("
return true;
}
}
// invalid binary operator
return false;
}
// Add opening and closing parentheses
void AddParentheses(string & EquationText)
{
EquationText.insert(EquationText.begin(), 1, '(');
EquationText.insert(EquationText.end(), 1, ')');
}
// Remove a specific character from a string
void AbsoluteValueReplacement(string & str, const int& position)
{
str.erase(position, 1);
}
// Assure the parentheses are valid in the equation
bool ParanthesesValidation(string & EquationText)
{
int count = 0;
// Going through the EquationText to find a '(' && ')'
for (int i = 0; i < EquationText.length(); i++)
{
// an open parentheses has been found. an expression have been started
if (EquationText[i] == '(')
{
count++;
}
// a closing parentheses has been found. an expression have been ended
else if (EquationText[i] == ')')
{
if (count == 0) // invalid parentheses. an expression can't start with a closing parentheses
{
return false;
}
else // an expression have been ended
{
count--;
}
}
}
if (count == 0)
{
return true; // valid parantheses if every parentheses is closed properly
}
else
{
return false; // invalid parantheses if every parentheses is closed properly
}
}
// Assure equation is valid
bool EquationValidation(string EquationText)
{
// Assure the validation of paranthesses where it is part of validating the equation
if (!ParanthesesValidation(EquationText))
{
return false;
}
// Go through EquationText to find the unary operators
for (int i = 0; i < EquationText.length(); i++)
{
if (isUnaryOperator(EquationText, i)) // a unary operator was found
{
if (UnaryOperatorValidation(EquationText, i)) // the unary operator is valid
{
AbsoluteValueReplacement(EquationText, i); // remove the unary operator to ease the binary operators validation process
}
else
{
return false; // invalid unary operator was found, so it is an invalid equation
}
}
}
// Go through EquationText to find the binary operators
for (int i = 0; i < EquationText.length(); i++)
{
if (isBinaryOperator(EquationText, i)) // binary operator was found in the equation
{
if (!BinaryOperatorValidation(EquationText, i)) // the binary operator is invalid
{
return false; // invalid binary operator was found, so it is an invalid equation
}
}
}
// valid equation
return true;
}
// Remove spaces from the equation
string SpaceRemoval(string & EquationText)
{
string NewEquationText = "";
// check every character
for (int i = 0; i < EquationText.length(); i++)
{
if (EquationText[i] != ' ') // if the character was not a ' ' then add it to the NewEquationText
{
NewEquationText += EquationText[i];
}
}
// return the NewEquationText
return NewEquationText;
}
// Calculate Incremental Value
float CalculateIncrementalValue(float MinRange, float MaxRange, float InterpoliteanPoints)
{
return abs(MinRange - MaxRange) / InterpoliteanPoints;
}
// count the numbers and dot characters that is connected to each others and in the begining of the string
// e.g. "235*654+65" the length is 3 by counting "235"
int NumberLength(const string & str)
{
// check all characters in the string
for (int i = 0; i < str.length(); i++)
{
// if a character that is not equal to 0-9 or '.' was found, return the counter.
if (!(str[i] >= 48 && str[i] <= 57) && !(str[i] == '.'))
{
return i;
}
}
// return the full length of the given string
return str.length();
}
// Calculate expression by returning value
double CalculateExpression(string expr)
{
// Remove extra spaces in the expression
string xxx = SpaceRemoval(expr);
// calculate the expression that is in-between the parentheses
string tok = "";
for (int i = 0; i < xxx.length(); i++)
{
if (xxx[i] == '(') // Parentheses was found
{
int iter = 1;
string token = "";
i++;
// get the expression that is in-between the parentheses
while (true)
{
if (xxx[i] == '(')
{
iter++;
}
else if (xxx[i] == ')')
{
iter--;
if (iter == 0)
{
i++;
break;
}
}
token += xxx[i];
i++;
}
// calculate the expression that is in-between the parentheses and return the value
tok += to_string(CalculateExpression(token));
// remove '--'. e.g. -(-1) => --1 => 1
for (int i = 0; i < tok.length() - 1; i++)
{
if (tok[i] == '-' && tok[i + 1] == '-')
{
tok.erase(i, 2);
}
}
}
tok += xxx[i];
}
// Calculate the addition and the subtracion
for (int i = 0; i < tok.length(); i++)
{
if (tok[i] == '+')
{
// calculate the expressions on the right and the left by returning their summation
return CalculateExpression(tok.substr(0, i)) + CalculateExpression(tok.substr(i + 1, tok.length() - i - 1));
}
else if (tok[i] == '-')
{
if (i == 0) // it a unary operator '-'
{
int numberLength = NumberLength(tok.substr(1, tok.length() - 1));
// there is only value after the unary operator '-' e.g. "-26"
if ((numberLength == (tok.length() - 1)) || (tok[tok.length() - 1] == '\0' && numberLength == (tok.length() - 2)))
{
return -CalculateExpression(tok.substr(i + 1, numberLength));
}
else // there are value and expressions after the unary operator '-' e.g. "-26*8+2" "-26+8" "-26-2" "-26/2" "-8*2" "-8^4"
{
int startingPosition = i + 2 + numberLength;
if (tok[i + 1 + numberLength] == '+')
{
// calculate the expressions on the right and the left by returning their summation
return -CalculateExpression(tok.substr(i + 1, numberLength)) +
CalculateExpression(tok.substr(startingPosition, tok.length() - startingPosition));
}
else if (tok[i + 1 + numberLength] == '-')
{
// calculate the expressions on the right and the left by returning their subtraction
return -CalculateExpression(tok.substr(i + 1, numberLength)) -
CalculateExpression(tok.substr(startingPosition, tok.length() - startingPosition));
}
else if (tok[i + 1 + numberLength] == '*')
{
// calculate the expressions on the right and the left by returning their multiplication
return -CalculateExpression(tok.substr(i + 1, numberLength)) *
CalculateExpression(tok.substr(startingPosition, tok.length() - startingPosition));
}
else if (tok[i + 1 + numberLength] == '/')
{
// calculate the left expressions by returning its division by the right expression
return -CalculateExpression(tok.substr(i + 1, numberLength)) /
CalculateExpression(tok.substr(startingPosition, tok.length() - startingPosition));
}
else if (tok[i + 1 + numberLength] == '^')
{
// calculate the left expression by returning its power from the right expression
return pow(-CalculateExpression(tok.substr(i + 1, numberLength)),
CalculateExpression(tok.substr(startingPosition, tok.length() - startingPosition)));
}
}
}
// calculate the expressions on the right and the left by returning their subtraction
return CalculateExpression(tok.substr(0, i)) - CalculateExpression(tok.substr(i + 1, tok.length() - i - 1));
}
}
// Calculate the power, multiplication and the division
for (int i = 0; i < tok.length(); i++)
{
if (tok[i] == '^')
{
// calculate the left expression by returning its power from the right expression
return pow(CalculateExpression(tok.substr(0, i)), CalculateExpression(tok.substr(i + 1, tok.length() - i - 1)));
}
else if (tok[i] == '*')
{
// calculate the expressions on the right and the left by returning their multiplication
return CalculateExpression(tok.substr(0, i))* CalculateExpression(tok.substr(i + 1, tok.length() - i - 1));
}
else if (tok[i] == '/')
{
// calculate the left expressions by returning its division by the right expression
return CalculateExpression(tok.substr(0, i)) / CalculateExpression(tok.substr(i + 1, tok.length() - i - 1));
}
}
// Return the value
return stod(tok.c_str());
}
// Calculate equation
double CalculateEquation(string equationText, float substitutionValue)
{
// Check every character
for (int i = 0; i < equationText.length(); i++)
{
if (equationText[i] == 'x') // the variable 'x' was found, substitute the variable 'x'
{
// remove 'x' from the equation
equationText.erase(i, 1);
// insert the value of 'x' in the equation
equationText.insert(i, to_string(substitutionValue));
}
}
// Return the calculated expression
return CalculateExpression(equationText);
}
/// ---------------------- Equation Results Table ----------------------
//
string GetEquationResultsTable(string equationText, float MinRange, float MaxRange, int InterpeolationPoints)
{
string equationTable = "";
float x = MinRange;
float IncrementalValue = CalculateIncrementalValue(MinRange, MaxRange, InterpeolationPoints);
//
equationTable += "INDEX\tx\t\ty = " + equationText + "\n";
equationTable += "--------------------------------------\n";
//
for (int i = 0; i <= InterpeolationPoints; i++)
{
//
x = MinRange + i * IncrementalValue;
//
equationTable += to_string(i + 1) + "\t" + to_string(x) + "\t" + to_string(CalculateEquation(equationText, x)) + "\n";
}
//
return equationTable;
}
//
void PrintEquationResultsTable(string equationText, float MinRange, float MaxRange, int InterpeolationPoints)
{
cout << GetEquationResultsTable(equationText, MinRange, MaxRange, InterpeolationPoints);
}
/// ---------------------- Graph ----------------------
// change the size of the console font
void fontsize(int a, int b)
{
PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx = new CONSOLE_FONT_INFOEX();
lpConsoleCurrentFontEx->cbSize = sizeof(CONSOLE_FONT_INFOEX);
GetCurrentConsoleFontEx(out, 0, lpConsoleCurrentFontEx);
lpConsoleCurrentFontEx->dwFontSize.X = a;
lpConsoleCurrentFontEx->dwFontSize.Y = b;
SetCurrentConsoleFontEx(out, 0, lpConsoleCurrentFontEx);
}
/*
//
void ClearLayer(bool EquationDrawing[])
{
for (int x = 0; x >= sizeX - 1; x++)
{
for (int y = 0; y < sizeY - 1; y++)
{
EquationDrawing[y][x] = false;
}
}
}
*/
//
void DrawGraph(string equationText, float MinRange, float MaxRange, float InterpeolationPoints)
{
//
//
}
// here is my source
#include "Header.h"
using namespace std;
//Global variables
string EquationText = "";
float MinRange;
float MaxRange;
int InterpoliteanPoints = 2;
const int EquationTableDateSizeX = 2;
const int EquationTableDateSizeY = 200;
float EquationTableDate[EquationTableDateSizeX][EquationTableDateSizeY] = { 0 };
string EquationTable = "";
const int sizeX = 201;
const int sizeY = 201;
string EquationGraph = "";
int main()
{
// Get Equation
while (true)
{
if (GetEquation(EquationText))
{
// Get Range
GetRange(MinRange, MaxRange);
cout << endl;
// Get Interpolitean Points
GetInterpolationPoints(InterpoliteanPoints);
// Print Equation Results Table
PrintEquationResultsTable(EquationText, MinRange, MaxRange, InterpoliteanPoints);
//
// DrawGraph(EquationText, MinRange, MaxRange, InterpoliteanPoints);
}
else
{
cout << "invalid equation\n\n";
}
system("pause");
system("cls");
}
return 0;
我修复了代码,我将尽快修复图形,一旦完成,我将对其进行更新
方程F(x)= 4 = 6 /(x) (4 = 6 /(x))
范围:
最小:2
最大:4
交换点:10
INDEX x x y =(4 = 6 /(x))
1 0.000000 inf
2 0.000000 inf
3 0.000000 inf