| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 447 人关注过本帖
标题:指针的问题
只看楼主 加入收藏
Timber
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2010-1-31
结帖率:71.43%
收藏
已结贴  问题点数:1 回复次数:1 
指针的问题
Write a function which receives a pointer to the amount of money in
your pocket and the price of a bus ticket. Function decrements
the amount of money by the price of a bus ticket only if you have
enough money. If you had enough money function returns non-zero value
and zero if you did not have enough money. The amount of money shall not
decremented if you don't have enough money.

Write a main program that uses this function.


#include <stdio.h>
#include <stdlib.h>

int buy_a_ticket( float *cash, float price )
{
  *cash -= price;
  if (cash > price)
  return 1;
  else
  return 0;
}

int main()
{
  float pocket=20.0, price=0.25;
  printf( "my cash = $%.2f\n", pocket );
  printf( "buy a tickect at $%.2f\n", price );
  buy_a_ticket( &pocket, price );
  printf( "cash remaining = $%.2f\n", pocket );
  return 0;
}
不太对,请教应该怎么改,题目也不是很明白,难道要分别提问口袋里和车票有多少钱?
搜索更多相关主题的帖子: 指针 
2010-02-27 23:30
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:1 
这样改才符合题意吧!
程序代码:
#include <stdio.h>
#include <stdlib.h>
int buy_a_ticket( float *cash, float price )
{
    if (*cash >= price)
    {
        *cash -= price;
        return 1;
    }
    else
        return 0;
}
int main()
{
    float pocket=20.0, price=0.25;
    printf( "my cash = $%.2f\n", pocket );
    printf( "buy a tickect at $%.2f\n", price );
    buy_a_ticket( &pocket, price );
    printf( "cash remaining = $%.2f\n", pocket );
    return 0;
}


[ 本帖最后由 cnfarer 于 2010-2-28 08:46 编辑 ]

★★★★★为人民服务★★★★★
2010-02-28 08:45
快速回复:指针的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018166 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved