from ctypes import Structure
class Student(Structure): #structure
roll_no =int
name =str
branch =str
marks=int
s1=Student() #object of struct student
print "Enter data for student..."
s1.roll_no=int(raw_input("Roll Number ? "))
s1.name=raw_input("Name ? ")
s1.branch=raw_input("Branch ? ")
s1.marks=int(raw_input("Total Marks <max-325> ? "))
print "Student Report"
print "--------------"
print "Roll Number:", s1.roll_no
print "Name:", s1.name
print "Branch:", s1.branch
print "Percentage:%f" %(s1.marks*(100.0/325))
from ctypes import Structure, c_int
class date(Structure):
_fields_=[("day", c_int),("month", c_int),("year",c_int)]
d1=date(14, 4, 1971)
d2=date(3, 7, 1996)
print "Birth date:",
print "%s-%s-%s" %(d1.day, d1.month, d1.year)
print "Today date:",
print "%s-%s-%s" %(d2.day, d2.month, d2.year)
from ctypes import Structure, c_int, c_char
class date(Structure):
_fields_=[("day", c_int),("month", c_int),("year",c_int)]
class Student(Structure):
_fields_=[("roll_no", c_int), ("name", c_char*25), ("birthday", date), ("branch", c_char*15),("marks", c_int)]#nested structure
s1=Student()
print "Enter data for student..."
s1.roll_no=int(raw_input("Roll Number ? "))
s1.name=raw_input("Name ? ")
birthday=date(0, 0, 0)
print "Enter date of birth <day month year>: ",
d, m, y=[int(x) for x in raw_input().split()]
birthday.day=d
birthday.month=m
birthday.year=y
s1.birthday=birthday
s1.branch=raw_input("Branch ? ")
s1.marks=int(raw_input("Total Marks <max-325> ? "))
print "Student Report"
print "--------------"
print "Roll Number:", s1.roll_no
print "Name:", s1.name
print "%s-%s-%s" %(s1.birthday.day, s1.birthday.month, s1.birthday.year)
print "Branch:", s1.branch
print "Percentage:%f" %(s1.marks*(100.0/325))
from ctypes import Structure, c_int, c_char
class Student(Structure):
_fields_=[("roll_no", c_int), ("name", c_char*25), ("branch", c_char*15),("marks", c_int)]
s=[]
n=int(raw_input("How many students to be processed <max-10>: "))
for i in range(n):
print "Enter data for student", i+1, "..."
r=int(raw_input("Roll Number ? "))
name=raw_input("Name ? ")
b=raw_input("Branch ? ")
m=int(raw_input("Total marks <max-325> ? "))
s.append(Student(r, name, b, m)) #array of structure objects
print "Students Report"
print "--------------"
for i in range(n):
print "Roll Number:", s[i].roll_no
print "Name:", s[i].name
print "Branch:", s[i].branch
print "Percentage: %f" %(s[i].marks*(100.0/325))
from ctypes import Structure, c_int, c_char
class Student(Structure):
_fields_=[("roll_no", c_int), ("name", c_char*25), ("branch", c_char*15),("marks", c_int)]
STUDENTS_COUNT=5
s=[]
s.append(Student(2, 'Tejaswi', 'CS', 285))#initialization of array of structures
s.append(Student(3, 'Laxmi', 'IT', 215))
s.append(Student(5, 'Bhavani', 'Electronics', 250))
s.append(Student(7, 'Anil', 'Civil', 215))
s.append(Student(9, 'Savithri', 'Electrical', 290))
print "Students Report"
print "--------------"
for i in range(STUDENTS_COUNT):
print "Roll Number:", s[i].roll_no
print "Name:", s[i].name
print "Branch:", s[i].branch
print "Percentage: %0.4f" %(s[i].marks*(100.0/325))
from ctypes import Structure, c_int, c_char
class Student(Structure):
_fields_=[("roll_no", c_int), ("name", c_char*25), ("branch", c_char*15),("marks", c_int)]
def read():
dull=Student()
dull.roll_no=int(raw_input("Roll Number ? "))
dull.name=raw_input("Name ? ")
dull.branch=raw_input("Branch ? ")
dull.marks=int(raw_input("Total marks <max-325> ? "))
return dull #returning structure object
def show(genius): #passing object of structure
print "Roll Number:", genius.roll_no
print "Name:", genius.name
print "Branch:", genius.branch
print "Percentage: %0.4f" %(genius.marks*(100.0/325))
s=[]
n=int(raw_input("How many students to be processed <max-10>: "))
for i in range(n):
print "Enter data for student", i+1, "..."
s.append(read())
print "Students Report"
print "--------------"
for i in range(n):
show(s[i])
from ctypes import Structure, c_int, c_char
class Student(Structure):
_fields_=[("roll_no", c_int), ("name", c_char*25), ("branch", c_char*15),("marks", c_int)]
def HighestMarks(s, count): #passing array of structures
big=s[0].marks
for i in range(1, count):
if s[i].marks>big:
big=s[i].marks
index=i
return index
def read():
dull=Student()
dull.roll_no=int(raw_input("Roll Number ? "))
dull.name=raw_input("Name ? ")
dull.branch=raw_input("Branch ? ")
dull.marks=int(raw_input("Total marks <max-325> ? "))
return dull
def show(genius):
print "Roll Number:", genius.roll_no
print "Name:", genius.name
print "Branch:", genius.branch
print "Percentage: %0.4f" %(genius.marks*(100.0/325))
s=[]
n=int(raw_input("How many students to be processed <max-10>: "))
for i in range(n):
print "Enter data for student", i+1, "..."
s.append(read())
print "Students Report"
print "--------------"
Id=HighestMarks(s, n)
print "Details of student scoring higest marks..."
show(s[Id])
from ctypes import Structure
import sys
import math
class Complex(Structure):
x=int
y=int
def read(self):
self.x=int(raw_input("Real part? "))
self.y=int(raw_input("Imaginary part? "))
def show(self, msg):
print msg, self.x,
if self.y<0:
sys.stdout.write('-i')
else:
sys.stdout.write('+i')
print math.fabs(self.y)
def add(self, c2):
self.x+=c2.x
self.y+=c2.y
c1=Complex()
c2=Complex()
c3=Complex()
print "Enter complex number c1..."
c1.read()
print "Enter complex number c2..."
c2.read()
c1.show('c1 =')
c2.show('c2 =')
c3=c1
c3.add(c2)
c3.show('c3 = c1 + c2 =')
from ctypes import Structure
class employee(Structure): #structure member functions
name=str
ID=long
dept=str
salary=float
def read(self):
self.name=raw_input("Employee Name: ")
self.ID=long(raw_input("Employee ID: "))
self.dept=raw_input("Department: ")
self.salary=float(raw_input("Salary: "))
def show(self):
print "Employee Name:", self.name
print "Employee ID:", self.ID
print "Department:", self.dept
print "Salary:", self.salary
emp=employee()
print "Enter employee data:"
emp.read()
print "\n****Employee Record****"
emp.show()
from ctypes import Union, c_char, sizeof
#Creates a union
class Strings(Union):
_fields_ = [("filename",c_char*200),
("output", c_char*400)]
s=Strings()
s.filename="/cdacb/usrl/raj/oops/mcrokernel/pserver.cpp"
print "filename:", s.filename
s.output="OOPs is a most complex entity ever created by humans"
print "output:", s.output
print "Size of union Strings =", sizeof(Strings)
from collections import namedtuple
from ctypes import c_char, c_int, c_float, sizeof, Structure, Union
class struct(Structure):
_fields_ = [("name",c_char*25), ("idno", c_int), ("salary", c_float)]
emp=struct()
class union(Union):
_fields_ = [("name",c_char*25), ("idno", c_int), ("salary", c_float)]
desc=union()
print "The size of the structure is", sizeof(emp)
print "The size of the union is", sizeof(desc)
from ctypes import Union, c_int, c_char, c_float
class emp(Union):
_fields_=[("name",c_char*25), ("idno", c_int), ("salary", c_float)]
def show(e):
print "Employee Details..."
print "The name is %s" %e.name
print "The idno is %d" %e.idno
print "The salary is %g" %e.salary
e=emp()
e.name="Rajkumar"
show(e)
e.idno=10
show(e)
e.salary=9000
show(e)
from ctypes import Union, c_int, c_char, c_float
#Creates a union
class union(Union):
_fields_ = [("i", c_int),
("c", c_char),
("f", c_float)]
u=union()
u.i=10
u.c='9'
u.f=4.5
print "The value of i is", u.i
print "The value of c is", u.c
print "The value of f is", u.f
from collections import namedtuple
from ctypes import Structure, c_uint, c_int, Union
import sys
class with_bits(Structure):
_fields_ = [("first", c_uint), ("second", c_uint)]
class union(Union):
_fields_=[("b", with_bits), ("i", c_int)]
i=0
u=union()
print "On i=0: b.first =",u.b.first,"b.second =", u.b.second
u.b.first=9
print "b.first =9:",
print "b.first =", u.b.first, "b.second =", u.b.second