我正在做一个关于读取“file.txt”中的股票代码然后使用 BST 查找用户想要查找的每个合适代码的项目。 我们可以利用开盘价和收盘价的差值来安排每个代码的顺序。 用户将输入股票代码以及开盘价和收盘价。如果股票代码不存在会打印“Not Found”如果存在,程序会计算开盘价和收盘价之间的差值以找到所需的股票代码。
编辑:如果你们想到任何其他方法来重新排列树的顺序,您可以向我推荐它们。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
typedef struct node
{
char stock_code[4];
double open_price; // data
double close_price; // data
struct node* left;
struct node* right;
}node;
node* root; //store the address of root
node* getNewNode(char stock_code[4], double open_price, double close_price)
{
node* cursor = (node*) malloc(sizeof(node));
if(cursor == NULL)
{
printf("Allocating failure!\n");
return NULL;
}
else
{
strcpy(cursor->stock_code, stock_code);
cursor->open_price = open_price;
cursor->close_price = close_price;
cursor->left = cursor->right = NULL;
}
return cursor;
}
node* Insert(node* root1, char stock_code[4], double open_price, double close_price )
{
if(root1 == NULL)
{
root1 = getNewNode(stock_code, open_price, close_price);
return root1;
}
else if(fabs(open_price - close_price) <= fabs(root1->open_price - root1->close_price))
{
root1->left = Insert(root1->left, stock_code, open_price, close_price);
}
else
{
root1->right = Insert(root1->right, stock_code, open_price, close_price);
}
return root1;
}
bool Search(node* root2, char stock_code[4], double open_price, double close_price)
{
if(root2 == NULL)
{
return false;
}
if(strncmp(root2->stock_code, stock_code, 4) != 0) return false;
else
{
if(fabs(close_price - open_price) == fabs(root2->close_price - root2->open_price)) return true;
else if(fabs(close_price - open_price) <= fabs(root2->close_price - root2->open_price))
{
return Search(root2->left, stock_code, open_price, close_price);
}
else if(fabs(close_price - open_price) > fabs(root2->close_price - root2->open_price))
{
return Search(root2->right, stock_code, open_price, close_price );
}
}
return true;
}
int main(void)
{
int n;
do
{
printf("Function 1: Read file!\n");
printf("Choose your option: ");
scanf("%d", &n);
}while(n < 1 || n > 5);
switch(n)
{
case 1:
root = NULL;
FILE* infile = fopen("first_assignment.txt", "r");
if(infile == NULL)
{
printf("The file is empty!\n");
return 1;
}
char buffer[1000];
while(fgets(buffer, 1000, infile) != NULL)
{
node* new_node = NULL;
// node* new_node = getNewNode(new_node->stock_code, new_node->open_price, new_node->close_price);
sscanf(buffer, "%c %lf %lf", &(new_node->stock_code), &(new_node->open_price), &(new_node->close_price));
root = Insert(root, new_node->stock_code, new_node->open_price, new_node->close_price);
}
char stock_code[4];
double open_price, close_price;
printf("Enter your stock code you want to find: ");
scanf("%s", stock_code);
printf("Enter open price: ");
scanf("%lf", &open_price);
printf("Enter close price: ");
scanf("%lf", &close_price);
if(Search(root, root->stock_code, root->open_price, root->close_price))
{
printf("Found!\n");
}
else
printf("Not Found!\n");
fclose(infile);
breileak;
}
}
file.txt
VIC 96.4 91.2
HSG 31.9 31.9
FLC 11.45 11.85
FLC 10.5 11.1
VIC 95.1 97.0
HSG 30.6 30.35
VIC 95.0 95.8
FLC 13.3 13.0
HSG 33.5 33.0
VIC 94.6 93.5
FLC 11.75 12.00
HSG 30.78 31.12
VIC 96.4 91.2
HSG 31.9 31.9
FLC 11.45 11.85