C ++,如果是项目,如何计算通过它们的线的斜率。 [英] C++,if project, how to calculate the slope of the line passing through them.

查看:105
本文介绍了C ++,如果是项目,如何计算通过它们的线的斜率。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线的斜率上升(y2-y1)/(x2-x1)。按 x1 y1 x2 y2 的顺序输入值。不要计算未定义的斜率,(只打印单词undefined



示例:



1 1 2 2 = 1

1 1 3 2 = 0.5

1 2 3 2 = 0

1 1 1 4 =未定义



这个问题来自代数1,我可以用纸上的计算器和图解决这个问题,但我试图用C ++编写这个程序。顺便说一句,我正在使用我刚刚从他们的网站上下载的Microsoft Visual C ++。



i迄今为止在visual studio c ++上做到了这一点:

如果斜率无效,我怎么能打印undefined。



The slope of the line is rise over run or (y2 - y1) / (x2 - x1). Enter value in the order of x1 y1 x2 y2 . do not calculate an undefined slope, ( just print the word "undefined" )

Example:

1 1 2 2 = 1
1 1 3 2 = 0.5
1 2 3 2 = 0
1 1 1 4 = undefined

This question is from Algebra 1, i can solve this using calculator and graph on paper but i am trying to write this program in C++. by the way i am using Microsoft visual C++ i have just downloaded from their website.

i have done this so far on visual studio c++:
how can i print "undefined" if the slope is not valid.

#include <iostream>
using namespace std;

int main()
{
        int x1, y1, x2, y2;
	cout << "enter x1: ";
	   cin >> x1;
	cout << "enter y1: ";
	   cin >> y1;
	cout << "enter x2: ";
	   cin >> x2;
	cout << "enter y2: ";
	   cin >> y2;
	
	cout << y2 - y1 / x2 - x1 << endl;
	

	system("pause");
	return 0;
}

推荐答案

严格来说,它应该是浮点坐标,但整数会使问题变得非常简单。浮点值是近似值,不幸的是,现在很多人都不了解如何正确使用它们。现在,记住你的代数1,几何学,或者你学到的任何东西,并思考:什么会导致在两点之间画一条线的问题不确定?是的,如果两个点具有相同的位置,那么您可以从一个点绘制无限数量的直线。所以,你有一个条件。还有另一种情况:无限衍生物。这是两个x坐标相同的情况。在这种情况下,如果y坐标不同,你可以通过两个点绘制一条直线,但这条线是垂直的。



我不担心这条线是垂直的,因为,斜率可以是无限的(尽管这种情况并不是学校代数甚至基本微积分),但这样的东西可以形式化;浮点类型甚至有特殊的Inf值你可以分配给结果)。问题在于,只有两个点,这个无限斜率可以解释为+ Inf或-Inf,也就是说,这是一个无限的情况。请注意,这与公式中的整数除法会产生异常的情况相同。所以,你可以对这种情况做另一次检查,或者看起来很酷:-),捕获这个异常并在捕获时输出你的欠定。



由于这看起来像一个家庭作业,没有代码样本给你。如果您没有找到如何分析这个最简单的数学问题,至少要亲自编写代码。我100%解释了你所需要的东西。如果你不亲自动手做这些事,你就不会真正学习;这个简单的想法已在许多例子中得到证实。



祝你好运,

-SA
Strictly speaking, it should be floating-point coordinates, but integers make the problem extremely simple. Floating-point values are approximate, and, these days, unfortunately, not so many people understand how to correctly work with them. Now, remember your Algebra 1, geometry, or whatever you learned, and think: what would make the problem of of drawing a line between two points indeterminate? Right, if two points have the same location, then you can draw infinite number of straight lines, from one point. So, you got one condition. There is another case: "infinite" derivative. This is when two x-coordinates are the same. In this case, if y-coordinates are different, you can draw a single straight line through two points, but this line would be vertical.

I would not worry that this line is vertical, because, the slope can be infinite (even though this case is not really covered by school algebra or even "elementary" calculus, but such thing can be formalized; the floating-point type even have special Inf values which you could assign to the result). The problem is that, with just two points, this infinite slope can be interpreted as either +Inf or −Inf, that is, this is an infinite case again. Note that this is the same very case where the integer division in your formula would give an exception. So, you can do another check for this case, or, too look cool :-), catch this exception and output your "underfined" when it is caught.

As this looks like a home assignment, no code samples for you. If you did not find out how to analyze this simplest mathematical problem, at least write the code with your own hands. I explained you 100% of what you need. And if you don't do such things with your own hands, you don't really learn; this simple idea has been proven on many examples.

Good luck,
—SA


这篇关于C ++,如果是项目,如何计算通过它们的线的斜率。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆