我的程序似乎只读取输入数据文件的一部分
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "BST_ADT.h"
// Structure
typedef struct
{
char* name;
char* market;
char* initial;
float stock;
}COMPANY;
// Prototype Delarations
void addComp (BST_TREE* list);
void deleteComp (BST_TREE* list);
void findStu (BST_TREE* list);
void printList (BST_TREE* list);
int compareStu (void* stu1, void* stu2);
void processStu (void* dataPtr);
int main (void)
{
// Local Definitions
BST_TREE* list;
// Statements
list = BST_Create(compareStu);
addComp(list);
deleteComp(list);
findStu (list);
printList(list);
return 0;
}
/*===================== addComp =========================*/
void addComp (BST_TREE* list)
{
// Local Declarations
COMPANY* stuPtr;
FILE* fp;
char fileName[25];
char buffer [100];
// Statements
stuPtr = (COMPANY*)malloc (sizeof (COMPANY));
stuPtr->name = (char*) malloc(128 * sizeof(char));
stuPtr->market = (char*) malloc(128 * sizeof(char));
stuPtr->initial = (char*) malloc(128 * sizeof(char));
printf("Enter the file name: ");
gets(fileName);
fp = fopen(fileName, "r");
if(fp == NULL)
{
printf("Error cannot open the file!\n");
exit(101);
}
while(fgets(buffer, 100, fp) != NULL)
{
if (!stuPtr)
printf("MEmory overflow!\n"), exit(101);
sscanf(buffer, "%s %s %s %f ", stuPtr->name, stuPtr->market, stuPtr->initial, &(stuPtr->stock));
BST_Insert(list, stuPtr);
} // end while
} //addStu
/*===================== deleteComp =========================*/
void deleteComp (BST_TREE* list)
{
// local definitions
char name[100];
char* namePtr = (char*) malloc(128 * sizeof(char));
namePtr = name;
// statements
printf("Enter Company name: ");
scanf ("%39s", namePtr);
if (!BST_Delete (list, namePtr))
printf("ERROR: No Company: %0\n", *namePtr);
} // deleteStu
这是我从文本文件中输入的数据:
Target Corporation; NYSE TGT 44.14B
PriceSmart, Inc.; NASDAQ PSMT 721.96M
Eastman Kodak Company; NYSE EK 5.14B
Concord Camera Corp.; NASDAQ LENS 25.43M
Siemens AG (ADR); NYSE SI 123.13B
3M Company; NYSE MMM 56.47B
Toshiba Corporation; OTC TOSBF 24.43B
Tyco International Ltd.; NYSE TYC 19.91B
Textron Inc.; NYSE TXT 13.90B
PHH Corporation; NYSE PHH 1.11B
Activision, Inc.; NASDAQ ATVI 8.07B
The Walt Disney Company; NYSE DIS 61.27B
在 addComp 函数的末尾
name contain 'The'
market contain 'Walt
initial contain 'Disney
and stock contain 6.09
这不是该功能的目的
相反,如果此功能正确完成:
name should contain 'The Walt Disney Company'
market contain 'NYSE'
initial contain 'DIS'
and stock contain '61.27B
'
addComp 函数我做错了什么?是否需要使用令牌才能将这些数据读入二叉树?
提前感谢您的帮助