首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

请问几个三级题目.

2012-02-15 
请教几个三级题目..#defineMAXLINE20#includestdio.hmain(){intiFILE*fpchar*pstr[10],str[10][MAXLIN

请教几个三级题目..
#define   MAXLINE   20
#include   <stdio.h>
main()
{   int   i;
  FILE   *fp;
  char   *pstr[10],str[10][MAXLINE];
  for(i=0;i <10;i++)
    pstr[i]=str[i];
    fp=fopen( "in.dat ", "r ");
    if(!fp)
    {printf( "can 't   open! ");

    }
    for(i=0;i <10;i++)
      fscanf(fp, "%s ",pstr[i]);
    fclose(fp);
    sort(pstr);
    fp=fopen( "out.dat ", "w ");
    if(!fp){
    printf( "can 't   open! ");}
    printf( "sord   strings:\n ");
    fprintf(fp, "sorted   strings\n ");
    for(i=0;i <10;i++)
    {printf( "%s\n ",pstr[i]);
      fprintf(fp, "%s\n ",pstr[i]);

      }
      fclose(fp);
}
  sort(pscr)
{
int   i,j;

    char   *a,*pstr[10];

    clrscr();
    for(i=0;i <10;i++)
    {for(j=i+1;j <10;j++)
      if(strcmp(*(pstr+i),*(pstr+j))> 0)
  {
        a=*(pstr+i);
        *(pstr+i)=*(pstr+j);
        *(pstr+j)=a;
        }

      }
  for(i=0;i <10;i++)
  printf( "%s ",*pstr[i]);
}
//以上的在in.dat   读取几行字符串..把他安大小顺序重新排序.但是out.dat   并没有发生变化...请高手帮忙看...

[解决办法]
void sort(char **pscr)
{
int i,j;

char *a;

//clrscr();
for(i=0;i <10;i++)
{for(j=i+1;j <10;j++)
if(strcmp(*(pscr+i),*(pscr+j))> 0)
{
a=*(pscr+i);
*(pscr+i)=*(pscr+j);
*(pscr+j)=a;
}

}
for(i=0;i <10;i++)
printf( "%s \n ",*(pscr+i));
}


看看
[解决办法]
楼主,字符串交换的时候不能用char*来操作,要用strcpy来操作,我给你改了一下,并且用我的例子实验成功了,你可以参考着改一下,再有问题再讨论好了.

sort(pscr)
{
int i,j;

char *a,*pstr[10],char temp[10];

clrscr();
for(i=0;i <10;i++)
{for(j=i+1;j <10;j++)
if(strcmp(*(pstr+i),*(pstr+j))> 0)
{
// a=*(pstr+i);
// *(pstr+i)=*(pstr+j);
// *(pstr+j)=a;
strcpy(temp,*(pstr+i);
strcpy(*(pstr+i),*(pstr+j));
strcpy(*(pstr+j),temp);
}

}
for(i=0;i <10;i++)
{
printf( "%s ",*pstr[i]);
printf( "\n ");
}
}

热点排行
Bad Request.