编写一个头文件
不会做啊。。。虽然知道求答案是可耻的。。。但是还是弱弱的求完整代码。。。要符合题目要求的说
-------- wallet.c -----------
#include "wallet.h"
#include <stdio.h>
#include <stdlib.h>
void walletPrintBalance(Wallet w)
{
printf("You have %.2lf euros.\n",w);
}
Wallet walletPay(Wallet w, double amount)
{
if (amount>w)
{
puts("You went bankrupt!");
exit(EXIT_SUCCESS);
}
w-=amount;
if (amount<0.0)
{
printf("You received %.2lf.\n",-amount);
}
else
{
printf("You paid %.2lf.\n",amount);
}
return w;
}
-------- wallet.h -----------
#ifndef _WALLET_H_
#define _WALLET_H_
#include "wallet.h"
typedef double Wallet;
void walletPrintBalance(Wallet w);
Wallet walletPay(Wallet w, double amount);
#endif
-------- bank.c -----------
#include "bank.h"
#include <math.h>
double bankSave(double amount, double years) {
return amount * pow( 1.01, years );
}
-------- bank.h -----------
#ifndef _BANK_H_
#define _BANK_H_
double bankSave(double amount, double years);
#endif
-------- gamble.c -----------
#include "gamble.h"
#include "util.h"
#include "wallet.h"
#include <stdio.h>
Wallet gamble(Wallet w)
{
walletPrintBalance(w);
puts("Place your bet:");
w=walletPay(w, getPositiveValue());
puts("You lose!");
return w;
}
-------- gamble.h -----------
#ifndef _GAMBLE_H_
#define _GAMBLE_H_
#include "wallet.h"
Wallet gamble(Wallet w);
#endif
-------- util.c -----------
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
double getPositiveValue()
{
double value;
if (scanf("%lf",&value)!=1
[解决办法]
value<=0.0)
{
puts("Invalid input!");
exit(EXIT_FAILURE);
}
return value;
}
-------- util.h -----------
#ifndef _UTIL_H_
#define _UTIL_H_
double getPositiveValue();
#endif
-------- main.c -----------
#include "wallet.h"
#include "bank.h"
#include "gamble.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
int main( void ) {
int opt = -1;
double amount, years;
Wallet w = 100.0;
while( opt ) {
walletPrintBalance( w );
printf(
"1. Visit the bank\n"
"2. Visit the casino\n"
"0. Exit program\n" );
scanf( "%d", &opt );
switch( opt ) {
case 1:
printf( "How much would you like to deposit? " );
amount = getPositiveValue();
printf( "Investment period in years: " );
years = getPositiveValue();
w = walletPay( w, amount );
printf( "You feel how you are getting old...\n" );
w = walletPay( w, -bankSave( amount, years ) );
break;
case 2:
w = gamble( w );
break;
case 0:
walletPrintBalance( w );
exit(0);
default:
printf( "You have entered an invalid choice.\n" );
break;
}
}
}