qq_39240112 2017-06-20 04:12 采纳率: 0%
浏览 929

为什么这个程序经过for循环指针指向内容会清空呢

#include
#include
#include
#include
#define MAX_SIZE 100
#define OK 1
#define ERROR -1
struct Student
{
int num;
char name[MAX_SIZE];
int score;

} Student [MAX_SIZE];
struct Student_ReadFromFile
{
int num;
char name[MAX_SIZE];
int score;

} Student_ReadFromFile [MAX_SIZE];
int Write(int i);
void Input();
//void sort(int Size);
int main()
{
Input();
Read();
}
int Write(int i)
{
FILE * fp;
int j;

if ((fp=fopen("Data.txt"," w+ "))==NULL)
{
    printf("ERROR !Can not open the file !\n");
    exit(0);
}


for (j=0; j<i; j++)
{
    fprintf(fp,"%10d ",Student[j].num);
    fprintf(fp,"%s ",Student[j].name);
    fprintf(fp,"%d\n",Student[j].score);

}
fclose (fp);
return OK;

}
void Input()
{
int i,flag=1,count=1,valid=1,check;
char *pstr;

// printf("请输入学生总数,n = ");

//scanf("%d",&n);
// printf("\n");
printf("请输入学生学号,姓名,成绩。\n");
do
{


    do
    {
        printf("第 %d 位学生的学号(10位)是:\n",count);
        scanf ("%10d",&Student [count-1] . num);
        check=(Student [count-1] . num<=9999999999&&Student [count-1] . num>=1000000000);
    }
    while(!check);

    do
    {

        printf("第 %d 位学生的成绩是(成绩在0-100内取值):\n",count);
        scanf ("%d",&Student [count-1] . score);

    }
    while (Student [count-1] . score<0||Student [count-1] . score>100);
    do
    {
        valid=1;
        printf("第 %d 位学生的姓名是(仅含汉字不应包含数字字母等符号):\n",count);
        scanf ("%s",Student [count-1] . name);
        getchar();
        pstr=&(Student [count-1] . name);
        for (; *pstr!='\0'; pstr++)
        {
            // printf("%c\n",*pstr);
            //if (!(   *pstr       >= 0x81 && *pstr       <= 0xFE && *(pstr + 1) >= 0x40 && *(pstr + 1) <= 0xFE))
            //if ((*pstr<='z'&&*pstr>='0'))||(*pstr<='Z'&&*pstr>='A')||(*pstr>='0'&&*pstr<='9'))
            if (isalnum(*pstr))
                valid=0;
        }
    }
    while(!valid);


    count ++;
    printf("继续请输入1,结束输入请输入0:");
    scanf("%d",&flag);

}
while (flag);
Write (count-1);
//Output(i);
printf("您录入的数据是:\n");
for (i=0; i<count-1; i++)

    printf("%10d %s %d \n",Student [i] . num,Student [i] . name,Student [i] . score );

}
void Read()
{
FILE *fp;
struct Student_ReadFromFile *pt=NULL;
int i,count;
printf("自文件中读入数据为:\n");
if ((fp=fopen("Data.txt","r"))==NULL)
{
printf("打开失败\n");
exit(0);
}
for (i=0; !feof(fp); i++)
{
fscanf(fp,"%10d ",&Student_ReadFromFile [i] . num);
fscanf(fp,"%s ",&Student_ReadFromFile [i] . name);
fscanf(fp,"%d",&Student_ReadFromFile [i] . score);
}
fclose(fp);
count=i-1;

for (i=0; i<count; i++)

    printf("%10d %s %d \n",Student_ReadFromFile [i] . num,Student_ReadFromFile [i] . name,Student_ReadFromFile [i] . score );

pt=Student_ReadFromFile;

// printf("%10d %s %d \n",pt->num,pt->name,pt->score );

sort(count,pt);

// printf("经过按照成绩降序排列后输出如下:\n");
//for (i=0; i<count; pt++)

// printf("%10d %s %d \n",pt->num,pt->name,pt->score );

}

void sort(int Size,struct Student_ReadFromFile *pt)
{
int i,j,k;

struct Student_ReadFromFile *Move=pt;
struct Student_ReadFromFile temp;


printf("经过按照成绩降序排列前输出如下:\n");
for (i=0; i<Size; i++, pt++)

    printf("%10d %s %d \n",pt->num,pt->name,pt->score );


for(k=0; k<Size-1; k++)
{

    printf("%d\n",pt->score);
    printf("%d\n",pt->num);
    printf("%s\n",pt->name);
    for (j=i+1,Move=pt+1; j<Size; j++,Move++)
    {
        if ((pt->score)<(Move->score))
        {

            temp=*pt;
            *pt=*Move;
            *Move=temp;
        }


    }
    pt++;
}


printf("经过按照成绩降序排列后输出如下:\n");
for (i=0; i<Size; i++)

    printf("%10d %s %d\n",(pt+i)->num,(pt+i)->name,(pt+i)->score );

}

  • 写回答

2条回答 默认 最新

  • qq_39240112 2017-06-20 04:14
    关注

    void sort(int Size,struct Student_ReadFromFile *pt)
    {
    int i,j,k;
    struct Student_ReadFromFile *Move=pt;
    struct Student_ReadFromFile temp;

    printf("经过按照成绩降序排列前输出如下:\n");
    for (i=0; i<Size; i++, pt++)

    printf("%10d %s %d \n",pt->num,pt->name,pt->score );
    

    for(k=0; k<Size-1; k++)
    {

    printf("%d\n",pt->score);
    printf("%d\n",pt->num);
    printf("%s\n",pt->name);
    for (j=i+1,Move=pt+1; j<Size; j++,Move++)
    {
        if ((pt->score)<(Move->score))
        {
    
            temp=*pt;
            *pt=*Move;
            *Move=temp;
        }
    
    
    }
    pt++;
    

    }

    printf("经过按照成绩降序排列后输出如下:\n");
    for (i=0; i<Size; i++)

    printf("%10d %s %d\n",(pt+i)->num,(pt+i)->name,(pt+i)->score );
    

    }

    我觉得问题应当就出现在这段代码中的交换部分 , 前面fp指向的值什么的都是正常的经过for哪怕是在for循环内部输出fp指向也都是0 和空 新手实在不太知道为什么了 请各位大佬指教一二 拜托了

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?