i have a little problem on my code...
HI open a txt that have this:
LEI;7671;Maria Albertina da silva;xxxx@gmail.com;
9;8;12;9;12;11;6;15;7;11; LTCGM;6567;Artur Pereira Ribeiro;fdsdfsd@gmail.com; 6;13;14;12;11;16;14; LEI;7701;Ana Maria Carvalho;asasssas@gmail.com; 8;13;11;7;14;12;11;16;14;LEI, LTCGM are the college;
7671, 6567, 7701 is student number; Maria, Artur e Ana are the students name; xxxx@gmail.com, ...@gmail are emails from students; the first number of every line is the total of classes that students have; after that is students school notes;example:
College: LEI
Number: 7671 Name: Maria Albertina da Silva email: xxxx@gmail.com total of classes: 9 Classe Notes: 8 12 9 12 11 6 15 7 11.My code:
typedef struct aluno {
char sigla[5];//college
char numero[80];//number
char nome[80];//student name
char email[20];//email
int total_notas;// total of classes
char tot_not[40]; // total classes
char notas[20];// classe notes
int nota; //class notes
char situacao[80]; //situation (aproved or disaproved)
} ALUNO;
void ordena(ALUNO*alunos, int tam)//bubble sort
{
int i=0;
int j=0;
char temp[100];
for (i=0;i<tam;i++)
for (j=0;j<tam-1;j++)
if (strcmp( alunos[i].sigla[j], alunos[i].sigla[j+1])>0)
{
strcpy(temp, alunos[i].sigla[j]);
strcpy(alunos[i].sigla[j],alunos[i].sigla[j+1]);
strcpy(alunos[i].sigla[j+1], temp);
}
}
void xml(ALUNO*alunos, int tam)
{
FILE *fp;
char linha[60];//line
int soma, max, min, count;//biggest note and lowest note and students per course count
float media; //media of notes
fp=fopen("example.txt","r"开发者_运维知识库);
if (fp==NULL)
{
exit(1);
}
else
{
while (!(feof(fp)))
{
soma=0;
media=0;
max=0;
min=0;
count=0;
fgets(linha,60,fp);
if (linha[0]=='L')
{
if (ap_dados=strtok(linha,";"))
{
strcpy(alunos[i].sigla,ap_dados);//copy to struct
// i need to call bubble sort here, but i don't know how
printf("College: %s\n",alunos[i].sigla);
if (ap_dados=strtok(NULL,";"))
{
strcpy(alunos[i].numero,ap_dados);//copy to struct
printf("number: %s\n",alunos[i].numero);
if (ap_dados=strtok(NULL,";"))
{
strcpy(alunos[i].nome, ap_dados);//copy to struct
printf("name: %s\n",alunos[i].nome);
if (ap_dados=strtok(NULL,";"))
{
strcpy(alunos[i].email, ap_dados);//copy to struct
printf("email: %s\n",alunos[i].email);
}
}
}
}
i++;
}
if (isdigit(linha[0]))
{
if (info_notas=strtok(linha,";"))
{
strcpy(alunos[i].tot_not,info_notas);
alunos[i].total_notas=atoi(alunos[i].tot_not);//total classes
for (z=0;z<=alunos[i].total_notas;z++)
{
if (info_notas=strtok(NULL,";"))
{
strcpy(alunos[i].notas,info_notas);
alunos[i].nota=atoi(alunos[i].notas); // student class notes
}
soma=soma + alunos[i].nota;
media=soma/alunos[i].total_notas;//doesn't work
if (alunos[i].nota>max)
{
max=alunos[i].nota;;//doesn't work
}
else
{
if (min<alunos[i].nota)
{
min=alunos[i].nota;;//doesn't work
}
}
//now i need to count the numbers of students in the same college, but doesn't work
/*If(strcmp(alunos[i].sigla, alunos[i+1].sigla)=0){
count ++;
printf("%d\n", count); here for LEI should appear 2 students and for LTCGM appear 1, don't work
}*/
//Now i need to see if student is aproved or disaproved
// Student is disaproved if he gets 3 notes under 10, how can i do that?
}
printf("media %d\n",media); //media
printf("Nota maxima %d\n",max);// biggest note
printf("Nota minima %d\n",min); //lowest note
}
i++;
}
}
}
fclose(fp);
}
int main(int argc, char *argv[])
{
ALUNO alunos;
FILE *fp;
int tam==3;
fp=fopen("example.txt","r");
alunos = (ALUNO*) calloc (tam, sizeof(ALUNO));
xml(alunos,nomeFicheiro, tam);
system("PAUSE");
return 0;
}
If you need to call bubble sort, why won't
ordena(alunos, tam);
work? (Hint: where is the contiguous memory where you store an entire sequence of records?)
(Hint #2: does it make sense to try to sort before you have read in all the records?)
By the way, I urge you most strongly to indent your code to make it readable. Not only will you get a better grade, but it will make it easier to help you. I'm pretty confident that the call I suggest won't work, but because almost all your code is in the left margin, I'm not sure.
You also might want to ask what are the different tasks here (read one record, read all records, sort all records, print records from a single colleage) and see if you can't find a way to organize your code such that you are using one of the available language constructs to separate one task from another. (I'm being deliberately cryptic because I don't know what you've yet to study. You should definitely ask your instructor or teaching assistant about this question.)
I think you also might wish to consider whether your bubble sort is doing the right thing. I suggest you "play computer" by executing your code by hand on an example where tam == 3
.
精彩评论