任务是;全局变量与现金收支账 这个任务中我们将实现一个现金收入、支出登记及现金余额查询的程序。与以往所有程序不同的地方在于,这个程序中用到了所谓的“全局变量”来保存现金,这个变量的名称是cash。 程序由main、income、expend三个函数构成。income函数用于登记现金收入(cash增加),expend用于登记现金支出(cash减少),main函数显示一个菜单让用户选择要执行的功能:当选择现金收入、现金支出时,要求用户输入现金额,并分别调用income、expend函数增加、减少全局变量cash中的值。 为了将注意力集中在全局函数的运用上,我们提供了这个程序中main函数的代码,请根据程序的功能要求在程序中添加income、expend两个函数的代码,以最终实现现金收入、支出的登记。请根据这个将下列函数修正#include<stdio.h> void income(float); void expend(float); float cash=0; void main() { int choice; float amount; while(1) { makechoice; printf("1-Income,2-Expenf,3-Balance,0-Exit)\n") printf("Make a choice,please:"); scanf("%d",&choice); if(choice<0 ||choice>3) { printf("Select error,please re-enter!\n\n"); goto makechoice; } if(choice == 0) { printf("Thinks!\n"); break; } if(choice == 3) { printf("Current balance:%.2f\n\n",cash) goto makechoice; } } }
时间: 2024-01-11 19:03:22 浏览: 124
经过分析,我们需要在程序中添加income、expend两个函数的代码,以最终实现现金收入、支出的登记。根据题目要求,我们可以编写income和expend函数如下:
```
void income(float amount)
{
cash += amount;
printf("Income successfully recorded!\n\n");
}
void expend(float amount)
{
if(amount > cash)
{
printf("Not enough cash to expend!\n\n");
}
else
{
cash -= amount;
printf("Expend successfully recorded!\n\n");
}
}
```
在income函数中,我们将传入的amount参数加到全局变量cash中,并输出收入成功的提示信息;在expend函数中,我们首先判断全局变量cash是否足够支出,如果不足则输出提示信息,否则将传入的amount参数减去全局变量cash并输出支出成功的提示信息。
接下来,我们需要在main函数中添加代码来调用income、expend函数,以实现现金收入、支出的登记。修改后的main函数如下:
```
void main()
{
int choice;
float amount;
while(1)
{
makechoice:
printf("1-Income,2-Expend,3-Balance,0-Exit\n");
printf("Make a choice,please:");
scanf("%d",&choice);
if(choice<0 ||choice>3)
{
printf("Select error,please re-enter!\n\n");
goto makechoice;
}
if(choice == 0)
{
printf("Thanks!\n");
break;
}
if(choice == 3)
{
printf("Current balance:%.2f\n\n",cash);
goto makechoice;
}
if(choice == 1)
{
printf("Please input the income amount:");
scanf("%f", &amount);
income(amount);
}
if(choice == 2)
{
printf("Please input the expend amount:");
scanf("%f", &amount);
expend(amount);
}
}
}
```
在修改后的main函数中,我们添加了两个if语句分别处理现金收入、支出的情况。当用户选择1时,提示用户输入收入金额,然后调用income函数;当用户选择2时,提示用户输入支出金额,然后调用expend函数。
修改后的程序完整代码如下:
```
#include<stdio.h>
void income(float);
void expend(float);
float cash = 0;
void main()
{
int choice;
float amount;
while(1)
{
makechoice:
printf("1-Income,2-Expend,3-Balance,0-Exit\n");
printf("Make a choice,please:");
scanf("%d",&choice);
if(choice<0 ||choice>3)
{
printf("Select error,please re-enter!\n\n");
goto makechoice;
}
if(choice == 0)
{
printf("Thanks!\n");
break;
}
if(choice == 3)
{
printf("Current balance:%.2f\n\n",cash);
goto makechoice;
}
if(choice == 1)
{
printf("Please input the income amount:");
scanf("%f", &amount);
income(amount);
}
if(choice == 2)
{
printf("Please input the expend amount:");
scanf("%f", &amount);
expend(amount);
}
}
}
void income(float amount)
{
cash += amount;
printf("Income successfully recorded!\n\n");
}
void expend(float amount)
{
if(amount > cash)
{
printf("Not enough cash to expend!\n\n");
}
else
{
cash -= amount;
printf("Expend successfully recorded!\n\n");
}
}
```
阅读全文