首页 > > 详细

辅导留学生C、C辅导、讲解C/C++、C语言程序辅导留学生

#include
#include
#include
int cot=0,count=0;
typedef struct listnode
{
char ch;
char str[11];
struct listnode * next_deep;
struct listnode *next;
int weight;
int valid;
}listnode;
listnode a[500];
char *SubString(char *str, int StartPostion, int SubstringLength)
{
int stringlen = 0;
int i = 0;
int x = 0;
char *tmp;
stringlen = strlen(str);
tmp = (char *)malloc(sizeof(char)*(SubstringLength + 1));
if ((StartPostion = stringlen))
{
strcpy(tmp, "\0");
return tmp;
}
for (i = StartPostion; ((i ch=ch;
p->next_deep=p->next=NULL;
p->weight=-1;
p->valid=0;
return p;
}
listnode* create2(char *ch,int w)
{
listnode* p=a[cot++];
int len=strlen(ch);
for(int i=0;istr[i]=ch[i];
p->str[len]='\0';
p->next_deep=p->next=NULL;
p->weight=w;
p->valid=1;
return p;
}
void insert_deep(listnode* p,char* ch,int w)
{
listnode* q=p->next_deep;
if(!q)
{
p->next_deep=create2(ch,w);
return ;
}
while(p)
{
if(strcmp(p->str,ch)==0)
{
count--;
return ;
}
q=p;
p=p->next_deep;
}
q->next_deep=create2(ch,w);
return;
}
void insert(listnode* p,char c,char* ch,int w)
{
//printf("c=%c ch=%s w=%d\n",c,ch,w);
p=p->next;
while(p)
{
if(p->ch==c)
{
insert_deep(p,ch,w);
//printf(" %s %c\n",p->next_deep->str,p->next->ch);
return ;
}
p=p->next;
}
return ;
}
void delet(listnode* p,char ch,char *str)
{
listnode *q;
while(p)
{
if(p->ch==ch)
{
q=p->next_deep;
while(q)
{
if(strcmp(q->str,str)==0)
{
q->valid=0;
break;
}
q=q->next_deep;
}
return;
}
p=p->next;
}
return ;
}
void query(listnode* p,char ch,char *str,FILE *fp2)
{
listnode *q;
while(p)
{
if(p->ch==ch)
{
q=p->next_deep;
while(q)
{
if(q->valid==0)
{
q=q->next_deep;
continue;
}
fprintf(fp2,"%d ",q->weight);
printf("%d ",q->weight);
if(strcmp(q->str,str)==0)
{
//fprintf(fp2,"\n");
return ;
}
q=q->next_deep;
}
}
p=p->next;
}
//fprintf(fp2,"\n");
return ;
}
void swap(listnode* p,char ch,char *str,char* s)
{
int len,i;
listnode *q,*k=p;
while(p)
{
if(p->ch==ch)
{
q=p->next_deep;
while(q)
{
if(q->valid==0)
{
q=q->next_deep;
continue;
}
if(strcmp(q->str,str)==0)
{
q->valid=0;
insert(k,s[0],s,q->weight);
return ;
}
q=q->next_deep;
}
}
p=p->next;
}
return ;
}
void show(listnode* p)
{
listnode* q;
while(p)
{
q=p->next_deep;
printf("%c ",p->ch);
while(q)
{
printf("%s ",q->str);
q=q->next_deep;
}
p=p->next;
printf("\n");
}
}
int main()
{
FILE *fp1;
fp1=fopen("sample_input.txt","r");
FILE *fp2;
fp2=fopen("sample_output.txt","w+");
int len,i,j,k;
listnode *head=(listnode*)malloc(sizeof(listnode));
head->next=head->next_deep=NULL;
listnode *p,*q;
for(i='Z';i>='A';i--)
{
if(!(head->next))
head->next=create1(i);
else
{
p=head->next;
q=create1(i);
head->next=q;
q->next=p;
}
}
for(i='z';i>='a';i--)
{
p=head->next;
q=create1(i);
head->next=q;
q->next=p;
}
//show(head->next);
char str[1000];
char *ptr,*pt,*tp;
fgets(str,1000,fp1);
str[strlen(str)-1]='\0';
len=strlen(str);
for(i=0;inext);
i=j;
}
//show(head->next);
while(fgets(str,1000,fp1))
{
if(strcmp(str,"\n")==0)
continue;
if(strlen(str)==1str[0]=='0')
break;
str[strlen(str)-1]='\0';
ptr=strtok(str," ");
pt=strtok(NULL," ");
if(str)
tp=strtok(NULL," ");
if(strcmp(ptr,"del")==0)
delet(head->next,pt[0],pt);
if(strcmp(ptr,"sch")==0)
{
query(head->next,pt[0],pt,fp2);
fprintf(fp2,"\n");
printf("\n");
}
if(strcmp(ptr,"sub")==0)
swap(head->next,pt[0],pt,tp);
}
return 0;
}
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!