情景:商店的资金基本就是两个方向,进货花钱,卖货赚钱,能不能编一个程序,反映实时的周转资金?

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    
struct product
    
{
        
public string name;
        
public float price;
        
public int number;
        
public float total()
        
{
            
return price * number;
        }

    
    }

    
class shop
    
{
       
       
        
static void Main(string[] args)
        
{   float myAmount = 1000;
            product p1;
            p1.name 
= "牙膏";
            p1.price 
= 3;
            p1.number 
= 100;

            product p2;
            p2.name 
= "牙刷";
            p2.price 
= 2.5f;
            p2.number 
= 150;

            Console.WriteLine(
"原始资金为{0}元", myAmount);
            Buy(p1, 
ref myAmount);
            Console.WriteLine(
"周转资金还剩{0}元", myAmount);
            Sell(p2, 
ref myAmount);
            Console.WriteLine(
"周转资金还剩{0}元", myAmount);
            Console.ReadKey();
         
        }


        
public static void Buy(product p, ref float amount)
        
{   
            amount 
-= p.total();
            Console.WriteLine(
"进货-->商品名:{0,-6}单价:{1,-6}数量:{2,-6}合计:{3,-6}",p.name,p.price,p.number,p.total());
        
        }


        
public static void Sell(product p, ref float amount)
        
{
            amount 
+= p.total();
            Console.WriteLine(
"出货-->商品名:{0,-6}单价:{1,-6}数量:{2,-6}金额:{3,-6}", p.name, p.price, p.number, p.total());

        }

    }

}

 

进阶:使用委托,定义一个委托,根据需要指向不同的函数。 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    
struct product
    
{
        
public string name;
        
public float price;
        
public int number;
        
public float total()
        
{
            
return price * number;
        }

    
    }

    
class shop
    
{
        
public delegate void business(product p, ref float amount);
       
        
static void Main(string[] args)
        
{   float myAmount = 1000;
            product p1;
            p1.name 
= "牙膏";
            p1.price 
= 3;
            p1.number 
= 100;

            product p2;
            p2.name 
= "牙刷";
            p2.price 
= 2.5f;
            p2.number 
= 150;
            
            Console.WriteLine(
"原始资金为{0}元", myAmount);

            business t;
            t 
= Buy;
            
            t(p1, 
ref myAmount);
            Console.WriteLine(
"周转资金还剩{0}元", myAmount);

            t 
= Sell;
            t(p2, 
ref myAmount);
            Console.WriteLine(
"周转资金还剩{0}元", myAmount);
            Console.ReadKey();
         
        }


        
public static void Buy(product p, ref float amount)
        
{   
            amount 
-= p.total();
            Console.WriteLine(
"进货-->商品名:{0,-6}单价:{1,-6}数量:{2,-6}合计:{3,-6}",p.name,p.price,p.number,p.total());
        
        }


        
public static void Sell(product p, ref float amount)
        
{
            amount 
+= p.total();
            Console.WriteLine(
"出货-->商品名:{0,-6}单价:{1,-6}数量:{2,-6}金额:{3,-6}", p.name, p.price, p.number, p.total());

        }

    }

}

posted on 2009-04-01 18:07  蓝色侵略  阅读(191)  评论(0编辑  收藏  举报