c - 在程序中实现指针

标签 c

我编写了一个程序,要求用户输入美分数并打印出构成该数额的硬币类型。

我正在尝试学习指针,我想在我的程序中包含以下内容:

void coins(int cents, int *quarters, int *dimes, int *nickels, int *pennies);

如果有人能教我在我的程序中实现它,我将不胜感激

#include 
#define QUARTER 25
#define DIME 10
#define NICKEL 5
#define PENNY 1

int main()
{
        //initialize variables and read input
        int cents, pennies, quarters, dimes, nickels;
        pennies = quarters = dimes = nickels =0;
        printf("Enter the number of cents:\n");
        scanf("%d", ¢s);

        //check the range of the input amount
        if(cents< 0 || cents > 10000)
           printf("Invalid amount %d,\nAmount must be between 0 and 10000, inclusive\n", cents);
        else {
                quarters = cents/QUARTER;
                dimes = cents%QUARTER/DIME;
                nickels = cents%QUARTER%DIME/NICKEL;
                pennies = cents%QUARTER%DIME%NICKEL;

                printf("Quarters: %d\n", quarters);
                printf("Dimes: %d\n", dimes);
                printf("Nickels: %d\n", nickels);
                printf("Pennies: %d\n", pennies);
        }
        return 0;
}

最佳答案

只需包含一些修正

#include <stdio.h>
#define QUARTER 25
#define DIME 10
#define NICKEL 5
#define PENNY 1

void coins(int cents, int *quarters, int *dimes, int *nickels, int *pennies);

int main(void)
{
        //initialize variables and read input
        int cents, pennies, quarters, dimes, nickels;
        pennies = quarters = dimes = nickels =0;
        printf("Enter the number of cents:\n");
        scanf("%d", &cents);

        //check the range of the input amount
        if(cents< 0 || cents > 10000)
           printf("Invalid amount %d,\nAmount must be between 0 and 10000, inclusive\n", cents);
        else {
                quarters = cents/QUARTER;
                dimes = cents%QUARTER/DIME;
                nickels = cents%QUARTER%DIME/NICKEL;
                pennies = cents%QUARTER%DIME%NICKEL;

                printf("Quarters: %d\n", quarters);
                printf("Dimes: %d\n", dimes);
                printf("Nickels: %d\n", nickels);
                printf("Pennies: %d\n", pennies);
        }
        return 0;
}

或者利用它

#include <stdio.h>
#define QUARTER 25
#define DIME 10
#define NICKEL 5
#define PENNY 1

void coins(int cents, int *quarters, int *dimes, int *nickels, int *pennies);

int main(void)
{
        //initialize variables and read input
        int cents, pennies, quarters, dimes, nickels;
        pennies = quarters = dimes = nickels =0;
        printf("Enter the number of cents:\n");
        scanf("%d", &cents);

        //check the range of the input amount
        if(cents< 0 || cents > 10000)
           printf("Invalid amount %d,\nAmount must be between 0 and 10000, inclusive\n", cents);
        else {
                coins(cents, &quarters, &dimes, &nickels, &pennies);

                printf("Quarters: %d\n", quarters);
                printf("Dimes: %d\n", dimes);
                printf("Nickels: %d\n", nickels);
                printf("Pennies: %d\n", pennies);
        }
        return 0;
}

void coins(int cents, int *quarters, int *dimes, int *nickels, int *pennies)
{
        *quarters = cents/QUARTER;
        *dimes = cents%QUARTER/DIME;
        *nickels = cents%QUARTER%DIME/NICKEL;
        *pennies = cents%QUARTER%DIME%NICKEL;
}

关于c - 在程序中实现指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35374298/

相关文章:

C 函数参数,内存对齐注意事项

c - 数据包的协议(protocol)

c - 相等运算检查数组中的四个值是否相等,这些值必须依次排列

C:如何将一系列变量应用于函数?

c++ - 为什么不打开 char 值达到 case 0xC2?

c++ - 替换版本信息资源

c - pthread_cond_wait() 如何工作

c - 将服务器套接字绑定(bind)到端口失败 (C)

c - Use System() with some dos commands issue::QUERY.EXE 未被识别为内部或外部命令、可运行程序或批处理文件

c - 如何保存 C 函数中读取的变量?