Featured image of post 换个姿势学C语言第5章5.4

换个姿势学C语言第5章5.4

换个姿势学C语言 第5章 获取完整的牌价数据 5.4节 获取和显示货币名称

0. 说明

《换个姿势学C语言》由何旭辉 著,清华大学出版社2022年出版。感谢何老师!

Snipaste_2024-03-10_14-51-10.png

这是一本非常不错的书!

5. 获取完整的牌价数据

5.4 获取和显示货币名称

在5.1.2节中,我们已经可以使用一个双精度浮点型数组存储现汇买入价、现钞买入价、现汇卖出价、现钞卖出价和中行折算价。

Snipaste_2024-03-10_00-12-26.png

BOCRates.h头文件中定义了函数GetRatesAndCurrencyNameByCode

1
int GetRatesAndCurrencyNameByCode(const char* code, char* name, char* publishTime, double* rates);

该函数不仅会返回货币名称,还会顺便返回牌价的发布时间。

创建代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// L05_19_GET_RATES_AND_CURRENCY_NAME_BY_CODE.cpp :
#include "D:/BC101/Libraries/BOCRates/BOCRates.h"
#include <stdio.h>
#pragma comment(lib, "D:/BC101/Libraries/BOCRates/BOCRates.lib")

int main()
{
    // 存储货币的五种价格
    double rates[5] = { 0 };
    // 货币名称
    char currencyName[33] = { 0 };
    // 发布时间
    char publishTime[20] = { 0 };
    int result = GetRatesAndCurrencyNameByCode("USD", currencyName, publishTime, rates);
    printf("%d\n", result);
    if (result == 1) {
        printf("货币名称:%s\n", currencyName);
        printf("发布时间:%s\n", publishTime);
        printf("现汇买入价:%.2f\n", rates[0]);
        printf("现钞买入价:%.2f\n", rates[1]);
        printf("现汇卖出价:%.2f\n", rates[2]);
        printf("现钞卖出价:%.2f\n", rates[3]);
        printf("中行折算价:%.2f\n", rates[4]);
    }
    else {
        printf("网络或服务器异常\n");
    }

    return 0;
}

运行工程,显示如下:

Snipaste_2026-03-02_23-43-27.png