运行结果:
C语言代码:
/* c how to program 习题 3.17 */
#include <stdio.h>
#include <stdlib.h>
int main()
{
//定义变量
int accountNumber;
double beginningBalance, totalCharges, totalCredits, creditLimit, balance;
//请输入第一个帐号信息
printf("Enter account number (-1 to end): ");
scanf("%d", &accountNumber);
while(accountNumber != -1){
//请输入月初余额
printf("Enter beginning balance: ");
scanf("%lf", &beginningBalance);
//请输入本月已支付的金额
printf("Enter total charges: ");
scanf("%lf", &totalCharges);
//请输入本月已申请的贷款金额
printf("Enter total credits: ");
scanf("%lf", &totalCredits);
//请输入信贷限额
printf("Enter credit limit: ");
scanf("%lf", &creditLimit);
//计算新余额
balance = beginningBalance + totalCharges - totalCredits;
//判断是否超额
if(balance > creditLimit){
//如果超额打印信息
printf("Account: %d\n", accountNumber);
printf("Credit Limit: %.2lf\n", creditLimit);
printf("Balance: %.2lf\n", balance);
printf("Credit Limit Exceeded.\n");
}
//请输入下一个帐号信息
printf("\nEnter account number (-1 to end): ");
scanf("%d", &accountNumber);
}
return 0;
}