记账类问题汇总
(注:暫時先記錄這些問題,后期會持續更新)
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); //計算現金收入 elseexpend(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; }?
總結
- 上一篇: 第二个例子:单链表实现基排序(桶排序)
- 下一篇: Docker 从Docker Hub 拉