java表示价格除以汇率_java - 已知外汇牌价折算汇率

黄舟2017-04-18 10:24:181楼

上面的有些算法写得好复杂,写个简单的:

#include

#include

#include

#include

typedef struct node{

char *str; /*拼接后的字符串*/

float value; /*汇率值*/

}node_t;

int main(int argc, char *argv[])

{

/*用户输入的一序列汇率对应关系*/

static char const *buff[] = {"CNY","GBP","0.116",\

"CNY","RUB","8.406",\

"CNY","AUD","0.184",\

"JPY","RUB","0.5072",\

"USD","EUR", "0.9456"};

int npairs = sizeof(buff)/sizeof(buff[0])/3;

node_t * buf = calloc(1,npairs*sizeof(node_t));

if(NULL == buf){

printf("calloc is null !\n");

return(-1);

}

int i = 0;

int j = 0;

int len = 0;

char tmp[16] = {'\0'};

for(i=0;i

memset(tmp,'\0',sizeof(tmp));

/*把两个字符串进行拼接*/

snprintf(tmp,16,"%s%s",buff[i],buff[i+1]);

len = strlen(tmp);

buf[j].str = calloc(1,sizeof(char)*(len+1));

if(NULL != buf[j].str){

memmove(buf[j].str,tmp,len);

buf[j].value = atof(buff[i+2]);

j += 1;

}

}

printf("please input the two node:\n");

char input0[8] = {'\0'};

char input1[8] = {'\0'};

scanf("%s%s",input0,input1);

char data0[16] = {'\0'};

char data1[16] = {'\0'};

/*考虑正序和反序*/

snprintf(data0,16,"%s%s",input0,input1);

snprintf(data1,16,"%s%s",input1,input0);

for(i=0;i

/*轮训匹配*/

if((0==strcmp(buf[i].str,data0))){

printf("%s->%s %f \n",input0,input1,buf[i].value);

break;

}

if( 0==strcmp(buf[i].str,data1) ){

printf("%s->%s %f \n",input1,input0,buf[i].value);

break;

}

}

if(i==j){

printf("can not find the pair \n");

}

/*

add the free

*/

return 0;

}

测试结果如下:

[field@learn]$./test_hello

please input the two node:

CNY GBP

CNY->GBP 0.116000

[field@learn]$./test_hello

please input the two node:

GBP CNY

CNY->GBP 0.116000

[field@learn]$

这里利用的是币种名字的唯一性,两个币种拼接在一起必然也是唯一的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值