Skip to content
Snippets Groups Projects
Commit 9aa3ce85 authored by ys554's avatar ys554
Browse files

lab9

parent 308690a0
Branches
No related tags found
No related merge requests found
Written in Python 3
Thank you!
#!/usr/bin/env python3
# gInt.py - Gaussian integer class (numbers of the form a+bi, where a & b are integers)
#
# Python 3.5.2 , on
# 4.13.0-38-generic GNU/Linux
#
class gInt :
'''Gaussian integer. Numbers of the form a+bi, where a & b are integers.'''
def __init__( self, a, b=0 ) :
'''Creates a gInt of the form a+bi'''
self.real = a
self.imag = b
def __eq__( self, other ) :
if not isinstance( self, other.__class__ ) :
return False
return self.real == other.real and self.imag == other.imag
def __str__( self ) :
'''Return a string representation'''
op = '+'
i = self.imag
if self.imag < 0 :
op = '-'
i = -i
return '(%d%s%di)' % (self.real, op, i)
def __add__( self, rhs ) :
'''Return a new gInt, self + rsh'''
r = self.real + rhs.real
i = self.imag + rhs.imag
return gInt( r, i )
def __mul__( self, rhs ) :
'''Return a new gInt, self * rhs'''
r = (self.real * rhs.real) - (self.imag * rhs.imag)
i = (self.imag * rhs.real) + (self.real * rhs.imag)
return gInt( r, i)
def norm( self ) :
'''Return real^2 + imag^2 as an int'''
return self.real * self.real + self.imag * self.imag
def test() :
'''A quick example/test function'''
x = gInt( 3, -2 )
y = gInt( 2, 5 )
z = gInt( 13 )
xcopy = gInt( x.real, x.imag )
if not x == xcopy :
print( "gInt not equal to new copy" )
else :
print( "Equal" )
print( "x:", str(x) )
print( "y:", str(y) )
print( "z:", str(z) )
print( "" )
print( "norm(x):", x.norm() )
print( "norm(y):", y.norm() )
print( "norm(z):", z.norm() )
print( "" )
print( "x + y:", x+y )
print( "x * y:", x*y )
print( "" )
print( "x:", str(x) )
print( "y:", str(y) )
print( "z:", str(z) )
print( "" )
print( "x + z:", x+z )
print( "x * z:", x*z )
print( "" )
print( "y + z:", y+z )
print( "y * z:", y*z )
print( "" )
test()
Yegeon Seo
CS265
Q1. Running bug3 produced the following error: segmentation fault (core dumped)
There is a new executable file called bug3 also recently modified a file called core.
Q2.
#1 0x00000000004009ad in inTable ()
#2 0x0000000000400920 in main ()
Q3.
bug3.c error:
Segmentation fault (core dumped)
Fixed the bug by incrementing numlines after the pointer is returned by strdup( buff );, this was causing the error by trying read and write to memory allocation that didnt exist.
st[ numLines ] = strdup( buff );
++numLines;
increment after allocation.
-----------
quicksortBug.c error:
undefined reference to `main'
Fixed. Did not include brackets for for statement, which are needed in c
Q4. Compiler cannot catch this error because it is not a syntax error. The user forgot to put brackets on for loop, which will only run the first line below the for loop. The compiler does not know that you wanter several lines. You can see this in the stack because the same value is repeated over and over again.
Q5. Using qsort to sort integers and accidentally pass the string comparison function will cause error. The string comparison cannot tell the difference between integer values.
#!/usr/bin/env python3
#Yegeon seo
import sys
import unittest
from gInt import gInt
class gIntTest( unittest.TestCase ) :
def setUp( self ) :
self.a1 = gInt(1, 2)
self.a2 = gInt(7, 2)
self.a3 = gInt(-4, 8)
self.a4 = gInt(-2.0, -9.0)
self.a5 = gInt(0, 0)
def test_add(self) :
r = self.a1 + self.a2
self.assertEqual(r, gInt(8, 4), "Addition Failed")
r = self.a1 + self.a3
self.assertEqual(r, gInt(-3, 10), "Addition Failed")
r = self.a3 + self.a4
self.assertEqual(r, gInt(-6.0, -1.0), "Addition Failed")
r = self.a2 + self.a5
self.assertEqual(r, gInt(7, 2), "Addition Failed")
r = self.a5 + self.a5
self.assertEqual(r, gInt(0, 0), "Addition Failed")
def test_multiply(self) :
r = self.a1 * self.a2
self.assertEqual(r, gInt(3, 16), "Multiplication Failed")
r = self.a1 * self.a5
self.assertEqual(r, gInt(0, 0), "Multiplication Failed")
r = self.a3 * self.a4
self.assertEqual(r, gInt(80.0, 20.0), "Multiplication Failed")
r = self.a2 * self.a4
self.assertEqual(r, gInt(4.0, -67.0), "Multiplication Failed")
r = self.a1 * self.a4
self.assertEqual(r, gInt(16, -13), "Multiplication Failed")
def test_norm(self) :
r = self.a1.norm()
self.assertEqual(r, 5, "Norm Failed")
r = self.a2.norm()
self.assertEqual(r, 53, "Norm Failed")
r = self.a3.norm()
self.assertEqual(r, 80, "Norm Failed")
r = self.a4.norm()
self.assertEqual(r, 85, "Norm Failed")
r = self.a5.norm()
self.assertEqual(r, 0, "Norm Failed")
if __name__ == "__main__" :
sys.argv.append( "-v" )
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment