码迷,mamicode.com
首页 > 其他好文 > 详细

记账类问题ACing

时间:2017-04-02 21:04:56      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:scanf   while   浮点   输出   float   .com   余额   include   can   

1,用函数实现财务现金记账

#include<stdio.h>
float cash;  //定义全局变量,保存现金余额
int main(void)
{
    int choice;
    float value;
    void income(float number),expend(float number);  //函数声明
    
    cash = 0;
    printf("Enter operte choice(0--end,1--income,2--expend):");
    scanf("%d",&choice);  //输入操作类型
    while(choice != 0){
        if(choice == 1||choice == 2){
            printf("Enter cash value:");  //输入操作现金额 
            scanf("%f",&value);
            if(choice == 1)
                income(value);  //计算现金收入 
            else
                expend(value);  //计算现金输出
            printf("current cash:%.2f\n",cash); 
        }
        printf("Enter operte choice(0--end,1--income,2--expend):");
        scanf("%d",&choice);  //继续输入操作类型
    } 
    return 0;
} 

void income(float number)
{
    cash = cash + number;  //改变全局变量cash 
}

void expend(float number)
{
    cash = cash - number;
}

 

技术分享

 

2,用函数实现餐厅记账

#include<stdio.h>
float total = 0.0;
short count = 0;
short tax_percent = 6;
float add_with_tax(float f)  //返回一小笔金额
{
    float tax_rate = 1 + tax_percent / 100.0;  //有了.0,计算就会以浮点数进行,否则表达式会返回整数
    total = total + (f * tax_rate);
    count = count + 1;
    return total; 
} 

int main()
{
    float val;
    printf("Price of item:");
    while(scanf("%f",&val)==1){
        printf("Total so far:%.2f\n",add_with_tax(val));
        printf("Price of item:");
    }
    printf("\nFinal total:%.2f\n",total);
    printf("Number of items:%hi\n",count);
    return 0;
}

技术分享

 

记账类问题ACing

标签:scanf   while   浮点   输出   float   .com   余额   include   can   

原文地址:http://www.cnblogs.com/OctoptusLian/p/6659763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!