Home php教程 PHP源码 AA制分账

AA制分账

May 25, 2016 pm 05:09 PM

php代码

<?php
header(&#39;content-type: text/html; charset=utf-8&#39;);
/**
 * AA账目
 * @author heping1
 */
class account{
	public $accounts;
	public $income;
	public $pay; 
	public $desc;
	function __construct($accounts)
	{
		foreach($accounts as $k=>$v)
    	{
    		if($v > 0)
    		{
    			$arr_income[$k] = $v;
    		}elseif($v < 0)
    		{
    			$arr_pay[$k] = $v;
    		}
    	}
    	arsort($arr_income);
		asort($arr_pay);
		$this->income = $arr_income;
		$this->pay = $arr_pay;
	}
    function AA()
    {
    	foreach($this->income as $k=>&$v)
    	{
    		foreach($this->pay as $k1=>&$v1)
    		{
    			if($v + $v1 > 0)
    			{
    				$this->desc[] = $k.&#39;给&#39;.$k1.($v1 * -1).&#39;元&#39;;
    				$this->income[$k] = $v + $v1;
    				unset($this->pay[$k1]);
    				$this->AA();
    			}else
    			{
    				$this->desc[] = $k.&#39;给&#39;.$k1.$v.&#39;元&#39;;
    				$this->pay[$k1] = $v + $v1;
    				unset($this->income[$k]);
    				$this->AA();
    			}
    		}
    	}
    }
}
$accounts = array(
	&#39;A&#39;=>50, // A总共需要支付的金额为50
	&#39;B&#39;=>40,
	&#39;C&#39;=>10,
	&#39;D&#39;=>-20, // D需要收取的金券为20
	&#39;E&#39;=>-30,
	&#39;F&#39;=>-20,
	&#39;G&#39;=>-30,
	&#39;H&#39;=>0
);
$account = new account($accounts);
$account->AA();
print_r($account->desc);
// output :
// Array ( [0] => A给G30元 [1] => A给E20元 [2] => B给E10元 [3] => B给D20元 [4] => B给F10元 [5] => C给F10元)
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)