1. Trang chủ
  2. » Công Nghệ Thông Tin

Core C++ A Software Engineering Approach phần 2 pptx

120 300 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 120
Dung lượng 2,13 MB

Nội dung

file://///Administrator/General%20English%20Learning/it2002-7-6/core.htm x = 4*a = b*c; // syntax error: there is no lvalue for 4*a Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com In addition to the traditional assignment operator, C++ has a number of variants¡Xarithmetic assignment operators Their goal is to shorten arithmetic expressions For example, instead of saying x = x + y; we can say x + = y; The result is the same These assignment operators are available for all binary operators ('+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', ' a2.number) // swap accounts to order numbers Simpo PDF Merge and Split { a3=a1; a1=a2; a2=a3; } Unregistered Version -a3 holds data temporarily // http://www.simpopdf.com The next example shows some graphical computations Since the drawing functions are not portable, I not want to draw on the screen in my examples Instead, I ask the user to enter coordinates of endpoints of two lines, AB and CD Then I compute the length of each line and the angle between each line and the x-axis Listing 5.12 shows the source code for this example that uses two data types, Point and Line The program requests input data in pixels (integers), uses the data to initialize points, uses these points to initialize the lines, and then computes the line lengths and angles Functions sqrt() and atan2() come from the math.h library header file They compute the square root and arctangent of their double parameters Because their actual arguments are defined as int, they are converted implicitly to double Since line length is also expressed in pixels as integers, the result of computing the square root is cast implicitly To avoid truncation, 0.5 is added to the length to achieve proper rounding Variable coeff converts angles from radians to degrees Figure 5-12 shows the results of the execution of the program Figure 5-12 Execution example for program in Listing 5.12 Example 5.12 Using #include directive for programmer-defined types #include #include atan2() #include "point.h" nt known to compiler #include "line.h" known to compiler using namespace std; int main () { const double coeff = 180/3.1415926536; Point p1, p2; Line line1, line2; rammer-defined types int diffX, diffY, length1, length2; double angle1, angle2; cout > p1.x >> p1.y; cout > p2.x >> p2.y; line1.start = p1; line1.end = p2; file://///Administrator/General%20English%20Learning/it2002-7-6/core.htm (235 of 1187) [8/17/2002 2:57:49 PM] file://///Administrator/General%20English%20Learning/it2002-7-6/core.htm cout > p1.y; Split Unregistered Version - http://www.simpopdf.com >> p1.x Merge and cout > p2.x >> p2.y; line2.start = p1; line2.end = p2; diffX = line1.end.x - line1.start.x; // ugly notation diffY = line1.end.y - line1.start.y; length1 = sqrt(diffX*diffX + diffY*diffY) + 0.5; angle1 = atan2(diffY,diffX) * coeff; cout

Ngày đăng: 12/08/2014, 11:20

TỪ KHÓA LIÊN QUAN