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

运行异常

2012-02-27 
运行错误#include stdio.h#include string.hstruct student{char name[10]float score}stu[10]int

运行错误
#include "stdio.h"
#include "string.h"
struct student
{
  char name[10];
  float score;
}stu[10];
int main(void)
{
  int i,j;
  char xm[10];
  float cj;
  for(i=0;i<10;i++)
  {
scanf("%s,%f",stu[i].name,&stu[i].score);
  }
  for(i=0;i<9;i++)
  {
for(j=i+1;j<10;j++)
{
if(stu[i].score<stu[j].score)
{
cj=stu[i].score;
stu[i].score=stu[j].score;
stu[j].score=cj;
strcpy(xm,stu[i].name);
strcpy(stu[i].name,stu[j].name);
strcpy(stu[j].name,xm);
}
}
  }
  for(i=0;i<10;i++)
  {
printf("%s,%f\n",stu[i].name,stu[i].score);
  }
  return 0;
}
这段代码可以通过编译,但运行有问题,都在附件里。

[解决办法]
你的代码是要按照score由高到低排序吗?
输入部分有问题,

C/C++ code
scanf("%s,%f",stu[i].name,&stu[i].score);
[解决办法]
那可能是没有加载浮点运算指令。
你可以在开始随便加上一条浮点运算语句试试看?
C/C++ code
int main( void )  {      int i,j;      char xm[M];      float cj ;      float tmp;    tmp = 1.0; //随便加上一条浮点运算语句,有些编译器需要这样才能在后面的浮点运算才能用    for( i = 0 ; i  <M ; i++)          scanf( "%s %f",stu[i].name,&stu[i].score ) ;      for( i=0 ; i  < M ;i++ )          for( j = i+1 ;j  < 10 ; j++ )              if(stu[i].score  < stu[j].score)              {                  cj = stu[i].score;                  stu[i].score = stu[j].score;                  stu[j].score = cj;                  strcpy(xm,stu[i].name);                  strcpy(stu[i].name,stu[j].name);                  strcpy(stu[j].name,xm);              }      for( i = 0 ; i  < 10 ; i++ )          printf("%s,%f\n",stu[i].name,stu[i].score);      system( "pause" ) ;  } 

热点排行