0
  1. 我正在尝试编写一个程序来调用函数来执行我的作业中指定的每个任务。一切似乎都正常工作,除了当我选择使用哪种形式(第一步)它选择选择 2,无论我选择 1 还是 2。请帮助!
// Translates Linear equations from Point-Slope Form to Two-Point Form and Vice Versa
// Also takes given information and displays Slope-Intercept Form

#include <iostream>
#include <cmath>

using namespace std;

int getProblem();
double get2Pt(double& x1, double& y1, double& x2, double& y2);
double getPtSlope(double& x1, double& y1);
double slopeIntcptFrom2Pt(double& x1, double& y1, double& x2, double& y2);
double intcptFromPtSlope(double slope, double& x1, double& y1);
void display2Pt(double x1, double y1, double x2, double y2);
void displayPtSlope(double slope, double x1, double y1);
void displaySlopeIntrcpt(double slope, double  yIntrcpt);

int main()
{
    double x1, x2, y1, y2, slope, yIntcpt;
    int choice;

    choice = getProblem();

    if (choice = 1)
    {   slope = get2Pt(x1, y1, x2, y2);
        yIntcpt = slopeIntcptFrom2Pt(x1, y1, x2, y2);
        display2Pt(x1, y1, x2, y2);
        displaySlopeIntrcpt(slope, yIntcpt);
    }
    else if(choice = 2)
    {
        slope = getPtSlope(x1, y1);
        yIntcpt = intcptFromPtSlope(slope, x1, y1);
        displayPtSlope(slope, x1, y1);
        displaySlopeIntrcpt(slope, yIntcpt);
    }
return 0;
}

int getProblem()
{
    int choice;
    cout << "Select the form you would like to convert to Slope-Intercept Form:" << endl
         << "1) Two-Point From (2 Points on the line are known)" << endl << "2) Point-Slope Form (The slope and one point on the line are known)" << endl;
    cin >> choice;
    return choice;
}
double getPtSlope(double& x1, double& y1)
{
    double slope;
    cout << "Enter the Slope =>" << endl;
    cin >> slope;
    cout << "Enter the X/Y Coordinates of the point, separated by a space =>" << endl;
    cin >> x1 >> y1;
    return slope;
}
double get2Pt(double& x1, double& y1, double& x2, double& y2)
{
    double slope;

    cout << "Enter the X/Y Coordinates of the First point, separated by a space =>" << endl;
    cin >> x1 >> y1;
    cout << "Enter the X/Y Coordinates of the Second point, separated by a space =>" << endl;
    cin >> x2 >> y2;

    slope = ((y2 - y1) / (x2 - x1));
    return slope;
}
double slopeIntcptFrom2Pt(double& x1, double& y1, double& x2, double& y2)
{
    double slope, yIntcpt;
    slope = ((y2 - y1) / (x2 - x1));
    yIntcpt = (slope * x1) + y1;
    return yIntcpt;
}
double intcptFromPtSlope(double slope, double& x1, double& y1)
{
    double yIntcpt;
    yIntcpt = (slope * x1) + y1;
    return yIntcpt;
}
void displayPtSlope(double slope, double x1, double y1)
{
    if (y1 >= 0)
    {
        if (x1 >= 0) cout << "y - " << y1 << " = " << slope << "(x - " << x1 << ")" << endl << endl;
        else cout << "y - " << y1 << " = " << slope << "(x + " << fabs(x1) << ")" << endl << endl;
    }
    else
    {
        if (x1 >= 0) cout << "y + " << fabs(y1) << " = " << slope << "(x - " << x1 << ")" << endl << endl;
        else cout << "y + " << fabs(y1) << " = " << slope << "(x + " << fabs(x1) << ")" << endl << endl;
    }
    return;
}
void display2Pt(double x1, double y1, double x2, double y2)
{
    cout << "   " << y2 << " - " << y1 << endl << "m = ----------------------" << endl
         << "   " << x2 << " - " << x2 << endl << endl;
}
void displaySlopeIntrcpt(double slope, double  yIntrcpt)
{
    if (yIntrcpt >= 0) cout << "y = " << slope << "x + " << yIntrcpt;
    else cout << "y = " << slope << "x " << yIntrcpt;
    return;
}
4

2 回答 2

4

您分配1choice而不是比较相等:

if (choice = 1)

改成这个

if (choice == 1)
...
else if(choice == 2)
于 2013-10-31T17:18:36.553 回答
0

测试相等性时需要使用两个 = 符号。

if (choice == 1)

if (choice == 2)

等等

于 2013-10-31T17:19:28.697 回答