OJ 编译错误,本地可以运行
[local]1[/local]a.c: In function 'main': a.c:28:3: warning: 'gets' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] a.c: In function 'InsertT': a.c:49:3: warning: implicit declaration of function 'stricmp' [-Wimplicit-function-declaration] /tmp/ccDx9q9J.o: In function `main': a.c:(.text.startup+0x39): warning: the `gets' function is dangerous and should not be used. /tmp/ccDx9q9J.o: In function `InsertT': a.c:(.text+0xec): undefined reference to `stricmp' collect2: error: ld returned 1 exit status
我只知道涉及到了关键字有'gets()'(一次读一行)‘stricmp()’(字符串比较,不区分大小写)
这两个都是非常适用于这道题的关键函数,还是说OJ上面就是限制了不让我用着两个函数,非要我自己写??
如果有必要的话,原题在 https://pta.
我的实现代码如下:
程序代码:#include<stdlib.h>
#include<stdio.h>
#include<string.h>
//C语言stricmp()函数:比较字符串(不区分大小写
typedef struct node *BinTree;
struct node {
char Data[33];
int cnt;
BinTree Left;
BinTree Right;
};
BinTree InsertT(BinTree BT,char*);
BinTree BuildT(char*);
void PrintT(BinTree,int);
int main() {
int n;
scanf("%d",&n);
getchar();
char a[33];
BinTree BT=NULL;
for(int i=0; i<n; i++) {
gets(a);
BT=InsertT(BT,a);
// printf("\n------%d----------\n",i+1);
}
PrintT(BT,n);
return 0;
}
void PrintT(BinTree BT,int n) {
if(BT) {
PrintT(BT->Left ,n);
double percent=(double)BT->cnt*100.0/n;
printf("%s %.4lf%%\n",BT->Data ,percent);
// printf("{%d}",BT->cnt );
PrintT(BT->Right,n );
}
}
BinTree InsertT(BinTree BT,char*name) {
BinTree head=BT;
while(BT) {
int flag=stricmp(BT->Data ,name);
if(flag<0) {
if(BT->Right)BT=BT->Right ;
else {
BT->Right =BuildT(name);
return head;
}
}else
if(flag>0) {
if(BT->Left )BT=BT->Left ;
else {
BT->Left =BuildT(name);
return head;
}
}else{
BT->cnt+=1 ;
// printf("[]");
return head;
}
}
return BuildT(name);
}
BinTree BuildT(char*Data) {
BinTree New=(BinTree)malloc(sizeof(struct node));
strcpy(New->Data ,Data);
New->cnt =1;
New->Left =NULL;
New->Right =NULL;
return New;
}重申一下,关键还是想问最前面的“编译错误”提示里都表达了什么意思,给翻译一下,百度上面找不到类似的案例。(或者是我不会找吧)








