(注:暂时先记录这些问题,后期会持续更新)
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 ",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 ",add_with_tax(val)); printf("Price of item:"); } printf(" Final total:%.2f ",total); printf("Number of items:%hi ",count); return 0; }