| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
为什么我的总是说runtime error?这是我的代码,希望哪位大神能够抽空帮我看一看啊!我的是用链表实现的,其中的初始point型的matrix【100】的数组,是用来存放初始的电话号码的,结构体goal中的line【7】是用来存放电话号码,num是存储该电话号码出现的次数的,我之前一直以为是matrix数组太小或是point中的数组太小,不够存放足够的电话号码或是每一个电话号码的长度,可后来改大了之后,还是不对,求大神指教,谢谢啊!
#include "stdio.h"
#include "malloc.h"
typedef struct point
{
char line[16];
}point;
typedef struct goal
{
char line[7];
int num;
goal *next;
}goal;
int tag;
void transform(point matrix[],int i,int j);
void newgoal(point matrix[],int locate,goal *head);
int main()
{
tag = 0;
int n,i,j;
char temp;
scanf("%d",&n);
scanf("%c",&temp);
point matrix[100];
goal *head;
head = (goal *)malloc(sizeof(goal));
head->next = NULL;
for (i = 0; i < n; i ++)
{
gets(matrix[i].line);
}
for (i = 0; i < n; i ++)
{
for (j = 0; matrix[i].line[j] != '\0'; j ++)
{
transform(matrix,i,j);
}
newgoal(matrix,i,head);
}
if (tag == 0)
{
printf("No duplicates. \n");
return 0;
}
else
{
while (head->next != NULL)
{
if (head->next->num > 1)
{
for (i = 0; i < 7; i ++)
{
if (i == 3)
{
printf("-");
}
printf("%c",head->next->line[i]);
}
printf(" %d\n",head->next->num);
}
head = head->next;
}
}
return 0;
}
void newgoal(point matrix[],int locate,goal *head)
{
goal *p;
goal *temp;
temp = head;
int i,j,t1,t2;
int tagtemp = 0;
if (temp->next == NULL)
{
p = (goal *)malloc(sizeof(goal));
p->num = 1;
p->next = NULL;
i = 0;
for (j = 0; matrix[locate].line[j] != '\0'; j ++)
{
if (matrix[locate].line[j] != '-')
{
p->line[i] = matrix[locate].line[j];
i ++;
}
}
temp->next = p;
}
else
{
while((temp->next != NULL)&&(!tagtemp))
{
j = 0;
for (i = 0; matrix[locate].line[i] != '\0'; i ++)
{
if (matrix[locate].line[i] != '-')
{
if (matrix[locate].line[i] == temp->next->line[j])
{
j ++;
}
else
{
if (matrix[locate].line[i] > temp->next->line[j])
{
break;
}
else
{
p = (goal *)malloc(sizeof(goal));
t1 = 0;
for (t2 = 0; matrix[locate].line[t2] != '\0'; t2 ++)
{
if (matrix[locate].line[t2] != '-')
{
p->line[t1] = matrix[locate].line[t2];
t1 ++;
}
}
p->num = 1;
p->next = temp->next;
temp->next = p;
tagtemp = 1;
}
}//end else
}//end if
}//end for
if ((matrix[locate].line[i] == '\0')&&(tagtemp == 0))
{
temp->next->num ++;
tag = 1;
tagtemp = 1;
}
temp = temp->next;
}//end while
if((temp->next == NULL)&&(!tagtemp))
{
p = (goal *)malloc(sizeof(goal));
p->num = 1;
p->next = NULL;
i = 0;
for (j = 0; matrix[locate].line[j] != '\0'; j ++)
{
if (matrix[locate].line[j] != '-')
{
p->line[i] = matrix[locate].line[j];
i ++;
}
}
temp->next = p;
}
}
}
void transform(point matrix[],int i,int j)
{
switch(matrix[i].line[j])
{
case 'A':
case 'B':
case 'C':
matrix[i].line[j] = '2';break;
case 'D':
case 'E':
case 'F':
matrix[i].line[j] = '3';break;
case 'G':
case 'H':
case 'I':
matrix[i].line[j] = '4';break;
case 'J':
case 'K':
case 'L':
matrix[i].line[j] = '5';break;
case 'M':
case 'N':
case 'O':
matrix[i].line[j] = '6';break;
case 'P':
case 'R':
case 'S':
matrix[i].line[j] = '7';break;
case 'T':
case 'U':
case 'V':
matrix[i].line[j] = '8';break;
case 'W':
case 'X':
case 'Y':
matrix[i].line[j] = '9';break;
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator