需求是一個同學給的,我幫他做完,記錄下。
需求如下:
業務變更內容及需求確認: |
1. 原交易序號——>演算法——>打亂交易序號 2. 打亂的交易序號和原交易序號一一對應,確保顧客可以根據打亂的交易序號退貨 3. 收銀小票只顯示打亂的交易序號 4. POS螢幕上方依舊顯示正確的交易序號,便於收銀員日常操作 |
系統變更內容說明: |
在以前的pos小票中添加一個打亂後的時自動產生的欄位,在顧客小票顯示打亂後的序號,在退貨時從表中查詢原來的正確的交易序號進行退貨交易。 演算法的實現: 移位演算法。比如:一筆正常的交易序號為:1 2 3 4 5 6 對應順序(a,b,c,d,e,f,g)現在按(b,a,d,c,f,e)再排序, 那麼打亂之後的序號就變為:(2 1 4 3 6 5) |
1、 設定系統參數是否打亂交易序號ConfuseTranNumber =yes;
2、 如果需要打亂,用程式把原交易序號打亂存放到記憶體變數A中,如果不需要打亂則把正確的交易序號存放到記憶體變數A裡;
3、 列印時讀取交易序號A;
4、 退貨時,輸入打亂後的交易序號用演算法計算出正常的交易序號,把正常的交易序號內容取到介面上進行退貨;
5、 演算法解析:
Ø 銷售時正算交易序號:
把傳過來的交易序號打亂,如果交易序號長度不足8位前面用"0"補足,各位上之和與10求餘,餘數則有可能是0、1、2、3、4、5、6、7、8、9,針對不同的餘數各位上有不同的相片順序:56204173、20417365、41736520、73652041、65412073、20736541、65204173、20417365、41736520、73652041
例如:交易序號為12345678,各位之和為36,和10求餘為6,此時選擇第六種打亂方式65204173,打亂後的交易序號為76315284。
Ø 退貨時倒算交易序號:
把輸入打亂後的退貨交易序號,如果交易序號長度不足8位前面用"0"補足,各位上之和與10求餘,餘數則有可能是0、1、2、3、4、5、6、7、8、9,針對不同的餘數各位上有不同的打亂方式:35274016、13052764、71630542、57416320、53472106、17036542、35274106、13052764、71630542、57416320
例如:退貨交易序號為76315284,各位之和為36,和10求餘為6,此時選擇第六種打亂方式35274106,正常的交易序號為12345678。
直接上代碼,注釋一切都有:
代碼
1 # -*- coding: gbk -*-
2
3 '''交易號編碼解碼'''
4 #銷售號相片順序
5 saleOrder = ['56204173','20417365','41736520','73652041','65412073',\
6 '20736541','65204173','20417365','41736520','73652041']
7 #退貨號相片順序
8 sendbackOrder = ['35274016','13052764','71630542','57416320','53472106',\
9 '17036542','35274106','13052764','71630542','57416320']
10 def addAll(tid):
11 '''計算號碼數值總和'''
12 total = 0
13 for n in tid:
14 total += int(n)
15 return total
16
17 def selectOrder(total,flag):
18 '''選擇相片順序
19 total : 數值總和
20 flag : sale 銷售號打亂;sendback 退貨號還原
21 '''
22 if flag == 'sale':
23 return saleOrder[total % 10]
24 else:
25 return sendbackOrder[total % 10]
26
27 def showResult(tid,flag):
28 '''返回解碼or編碼結果
29 tid : 銷售號 or 退貨號
30 flag : sale 銷售號打亂;sendback 退貨號還原
31 '''
32 order = selectOrder(addAll(tid),flag)
33 res = ''
34 for index in order:
35 res += tid[int(index)]
36 return res
37
38 def formatId(tid):
39 '''格式化去除空格換行制定8位長度'''
40 tid = tid.strip()
41 tid = tid[:8]
42 tid = tid.zfill(8)
43 #tid = '%08d' % tid
44 return tid
45
46
47 def main():
48 '''測試函數'''
49 while(True):
50 tid = raw_input('\n輸入號碼:\n')
51 flag = raw_input('輸入號碼類型:sale or sendback:\n')
52 tid = formatId(tid)
53 print showResult(tid,flag)
54 if __name__ == '__main__':
55 main()
另外用到幾個函數:
strip( [chars])
Return a copy of the string with trailing characters removed.
返回一個去除多餘字元的字串的拷貝
The chars argument is a string specifying the set of characters to be removed.
chars參數是一個指定的要刪除的字元集
If omitted or None, the chars argument defaults to removing whitespace.
如果政略或者是None,chars參數預設為刪除空白字元
The chars argument is not a suffix; rather, all combinations of its values are stripped:
char參數不是一個尾碼。實際上所有其值的組合,都會被刪去。
lstrip( [chars])
Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:
rstrip( [chars])
Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:
ljust( width[, fillchar])
Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space).
返回一個字串長度為width的左邊排版的字串,填充使用指定的(具體的,明確的)參數fillchar(填充字串),預設是空格
The original string is returned if width is less than len(s). Changed in version 2.4: Support for the fillchar argument.
如果wdith小於字串長度,則返回原來的字串。在版本2.4的改變:支援fillchar參數。
rjust( width[, fillchar])
Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is a space).
The original string is returned if width is less than len(s). Changed in version 2.4: Support for the fillchar argument.
zfill( width)
Return the numeric string left filled with zeros in a string of length width.
返回一個左邊用0填充的字串長度是width的數字字串
The original string is returned if width is less than len(s). New in version 2.2.2.
如果width小於字串長度,則返回原字串。版本2.2.2新增。