import time
import os
def cost(fname):
'用于记录花费'
cost_time = time.strftime('%Y-%m-%d')
try:#异常处理机制
cost_deposit = int(input('花销金额:'))
cost_mark = input('花销说明:')
except ValueError:
print('无效的金额')
return # 函数的return类似于循环的break,return提前结束函数。
except (KeyboardInterrupt, EOFError):
print('\nbye-bye')
exit(1)
# 在文件中取出所有的收支记录
with open(fname,'rb') as fobj:
records = pickle.load(fobj)
# 计算最新余额
balance = records[-1][-2] - cost_deposit
# 构建最新一笔收入
record = [cost_time,0,cost_deposit,balance,cost_mark]
# 将收入追加到收支列表中
records.append(record)
# 将最新收支情况写入文件
with open(fname,'wb') as fobj:
pickle.dump(records,fobj)
def save(fname):
save_time = time.strftime('%Y-%m-%d')
try:
save_deposit = int(input('收入金额:'))
save_mark = input('收入说明:')
except ValueError:
print('无效的金额')
return
except (KeyboardInterrupt,EOFError):
print('bye-bye')
exit(1)
with open(fname, 'rb') as fobj:
records = pickle.load(fobj)
balance = records[-1][-2] + save_deposit
record = [save_time,save_deposit,0,balance, save_mark]
records.append(record)
with open(fname, 'wb') as fobj:
pickle.dump(records, fobj)
def query(fname):
'用于查账'
# 打印表头
print(f'{"date":<15}{"save":<8}{"cost":<8}{"balance":<12}{"mark":<50}')
with open(fname,'rb') as fobj:
records = pickle.load(fobj)
for date,cost,save,balance,mark in records:
print(f'{date:<15}{cost:<8}{save:<8}{balance:<12}{mark:<50}')
def menu():
funcs = {'0':cost,'1':save,'2':query}
prompt = '''(0)开销
(1)收入
(2)查询
(3)退出
请选择(0/1/2/3):'''
fname = 'account.data'
if not os.path.exists(fname):
init_data = [[time.strftime('%Y-%m-%d'),0,0,10000,'init_data']]
with open(fname,'wb') as fobj:
pickle.dump(init_data,fobj)
while 1:
try:
choice = input(prompt).strip()
except(KeyboardInterrupt,EOFError):
choice = '3'
if choice not in ['0','1','2','3']:
print('无效输入,重试')
continue
if choice == '3':
print('\nbye_bye')
break
funcs[choice](fname)
if __name__ == '__main__':
menu()
源代码如下:
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<stdio.h>
using namespace std
const int maxb=10000 //最多的图书
class book//图书类
{
int tag //删除标记1:已删0:未删
int number //isbn书号
char name[20] //书名
char author[10]//主编
char number2[10]//版次
char position[20]//出版社
char time[20]//出版年
void addbook(int n,char *na,char *au,char *n2,char *da,char *ti,int pr) //增加图书
{
tag=0
number=n
price=pr
strcpy(name,na)
strcpy(author,au)
strcpy(number2,n2)
strcpy(position,da)
strcpy(time,ti)
onshelf=1
}
扩展资料
1、源程序中,很多符号都是成对匹配出现的,为避免遗漏必须配对使用的符号。
2、用花括号括起来的部分,但从程序结构清晰,便于阅读、理解、维护的角度出发,建议在书写程序时应遵循以下规则,以养成良好的编程习惯。
3、一个说明或一条语句占一行,与该结构开始处的左花括号对齐。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)