再说python(接上一篇C语言写的):
用python编程最大的好处就简单、易懂,通过这题就可以看出。界面、增、删和查找不用说了吧。。。。统计用python非常的简单,因为它自带信息截取功能,比如是s[1][1:3],可以截取是s[1]字符串的第1到3之间的字符(从0开始数,含1,不包含3,具体见python手册),现在,你只要依次找到所有学生的邮箱,截取相应的你需要的字段,比如
str[string.index(str,'@'):string.rindex(str,'m')+1](未测试)
# -*- coding: cp936 -*-
N=6#全局变量
def tongji(studinfo):
global N#全局变量使用前要声明一下,不然python会把它当然局部变量
q=0;si=0;ot=0;os=0;so=0
for i in range(0,N):
if(studinfo[i][5][11:13]=='qq'):q+=1
if(studinfo[i][5][11:13]=='si'):si+=1
if(studinfo[i][5][11:13]=='12'):ot+=1
if(studinfo[i][5][11:13]=='16'):os+=1
if(studinfo[i][5][11:13]=='so'):so+=1
print'使用人数---qq:%s人,新浪:%s人,126:%s人,163:%s人,搜狐:%s人'%(q,si,ot,os,so)
return
def add(studinfo):
print '\n======add======\nplease input name,id,sex,age,tel,mail:'
global N#全局变量使用前要声明一下,不然python会把它当然局部变量
N+=1
addinfo=[]
addinfo = raw_input()
addinfo = addinfo.split(',')#拆分函数:把多个元素放在列表里面,主意','是英文标点
if len(addinfo) != 6:
print 'lenth error\n'
return
studinfo.append(addinfo)
f=open('D:\\studinfo.txt','w')
for i in range(0,N):
f.writelines(studinfo[i])
print 'add success\n'
return
def delete(studinfo):
print '\n======delete======\nplease input id:'
global N#全局变量使用前要声明一下,不然python会把它当然局部变量
N-=1
delinfo = raw_input()
for i in range(0,N):
if(int(delinfo)==int(studinfo[i][1])):
del studinfo[i]
for i in range(0,N):
print "姓名:%s 学号:%s 性别:%s 年龄:%s 电话:%s 邮箱:%s"%(studinfo[i][0],studinfo[i][1],studinfo[i][2],studinfo[i][3],studinfo[i][4],studinfo[i][5])
f=open('D:\\studinfo.txt','w')
for i in range(0,N):
f.writelines(studinfo[i])
print 'delete success\n'
return
def search(studinfo):
print '\n======search======\nplease input search id,0 for search all:'
searchinfo = raw_input()
for i in range(0,N):
if(int(searchinfo)==int(studinfo[i][1])):
print "姓名:%s 学号:%s 性别:%s 年龄:%s 电话:%s 邮箱:%s"%(studinfo[i][0],studinfo[i][1],studinfo[i][2],studinfo[i][3],studinfo[i][4],studinfo[i][5])
return
def xianshi(studinfo):
for i in studinfo:
print "姓名:%s 学号:%s 性别:%s 年龄:%s 电话:%s 邮箱:%s"%(i[0],i[1],i[2],i[3],i[4],i[5])
def duqu():
studinfo = []
student=[]
f=open('D:\\studinfo.txt','r')
for i in range(0,N):
student=f.readline()
student=student.split(',')
studinfo.append(student)
f.close()
return studinfo
def selecttype(studinfo):
print '===================\nplease input type:\n1.add\n2.delete\n3.search\n4.xianshi\n5.tongji\n0.exit'
selectkey = raw_input()
if selectkey == '0':
exit(0)
elif selectkey == '1':
add(studinfo)
elif selectkey == '2':
delete(studinfo)
elif selectkey == '3':
search(studinfo)
elif selectkey == '4':
xianshi(studinfo)
elif selectkey == '5':
tongji(studinfo)
else:
pass
selecttype(studinfo)
return
def main():
studinfo = duqu()
selecttype(studinfo)
return
if __name__ == '__main__':
main()