工行纸黄金不同于股票交易账户, 它直接和银行账户绑定.在进行多次不同重量的买入和卖出后, 统计一段时间的收益和收益率变得比较困难.
下面的这个程序可以对工行的导出纯文本进行自动分析得到收益值和收益率, 并可以对时间有序的多数据文本文件进行统计操作.
[Environment: GCC-4.4.3.]
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h> /* isdigit */
#define GOLDCALC_VERSION "0.0.3 for ICBC paper gold."
#define GOLDCALC_DATE "2011-12-28"
//#define DTEST
#ifdef DTEST
#define dprint(x...) printf(x)
#else
#define dprint(x...)
#endif
#define MAX_FILE_NUMBER 10
#define MAX_PATH_LENGTH 150
enum {
UNKOWN = 0,
BUY,
SELL,
};
struct one_trade_node {
char date[11];
#if 0
char time[9];
char no[10];
#endif
int type;
float price;
float weight;
float subsum;
int line;
struct one_trade_node *next;
};
static double current_price, current_weight, current_sum;
static inline int _validline(char *line)
{
int i = 0x1 & (isdigit(line[0]) && isdigit(line[1]) && isdigit(line[2]) && isdigit(line[3]));
return ((i) && (line[4] == '-') && (line[7] == '-'));
}
static double _katof(const char *str)
{
char p[50];
bzero(p, sizeof(p));
char *q = p;
while ( *str != '\0') {
if (*str != ',')
*q = *str, q++;
str++;
}
return (atof(p));
}
static inline int _date_order_check(const char *new, const char *old)
{
int i, ret=1;
for (i=0; i<10; i++) {
if (new[i]- old[i] != 0) {
ret = ((new[i] - old[i] > 0) ? 1 : 0);
break;
}
}
return (ret);
}
static double _get_cost_sum(struct one_trade_node *head)
{
struct one_trade_node *p = head->next;
if (p->type != BUY) {
dprint("This data file is probable incomplete, normal one should start with a BUY.\n");
}
double cost, cash;
int i = 0;
cost = cash = 0;
while(p) {