Листинг программы. Открытие списка, считывание файла. Внесение данных в файл

Страницы работы

2 страницы (Word-файл)

Содержание работы

Листинг программы:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <math.h>

#include <fstream.h>

#include <iostream.h>

#include<string.h>

#pragma hdrstop

#pragma argsused

void show(struct Stud *spisok);        // показываем список

void open(struct Stud *spisok);        // считываем из файла    

void writefile(struct Stud *spisok);// записываем в файл

void srball(struct Stud *spisok);

int M=0;

float Srb;

struct Stud

{  char family[20];

char name[20];

char surname[20];

char sex;

char education;

char adress;

char donation;

struct Bdate {

int day, month, year;

} birth;

int Marks[5];

int ball;

} ;

void open(struct Stud *spisok)

{  FILE *fs;

char buff[501],*sn;

unsigned int i;

if((fs=fopen("input.txt","r"))==NULL)

printf("+°шсър ўЄхэш  Їрщыр");

else

do{  fgets(buff,500,fs);  sn=strtok(buff," ");

for(i=0;i<=strlen(sn);i++)

((spisok+M)->family[i])=*(sn+i);

sn=strtok(0," ");

for(i=0;i<=strlen(sn);i++)

((spisok+M)->name[i])=*(sn+i);

sn=strtok(0," ");

for(i=0;i<=strlen(sn);i++)

((spisok+M)->surname[i])=*(sn+i);

sn=strtok(0," ");

((spisok+M)->sex)=*sn;  sn=strtok(0," ");

((spisok+M)->education)=*sn;  sn=strtok(0," ");

((spisok+M)->adress)=*sn;  sn=strtok(0," ");

((spisok+M)->donation)=*sn; sn=strtok(0,".");

((spisok+M)->birth.day)=atoi(sn); sn=strtok(0,".");

((spisok+M)->birth.month)=atoi(sn); sn=strtok(0," ");

((spisok+M)->birth.year)=atoi(sn); sn=strtok(0," ");

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

{  ((spisok+M)->Marks[i])=atoi(sn); sn=strtok(0," ");

}

((spisok+M)->ball)=atoi(sn);

M++;

}

while(!feof(fs));

fclose(fs);

}

void show(struct Stud *spisok)

{  printf("\n\tInput Table:\n");

printf("--------------------------------------------------------------\n");

printf("¦%2s¦%23s¦%1s¦%1s¦%1s¦%1s¦%10s¦%9s¦%2s¦\n","№","Студент","П","О","А","С","Дата рожд","Оценки","Балл");

printf("--------------------------------------------------------------\n");

for(int i=0;i<M;i++)

{ printf("¦%2i¦%15s %3s %3s¦%c¦%c¦%c¦%c¦%2i.%2i.%4i¦%i %1i %1i %1i %1i¦%4i¦",

i+1,(spisok+i)->family,(spisok+i)->name,(spisok+i)->surname,(spisok+i)->sex,

(spisok+i)->education,(spisok+i)->adress,(spisok+i)->donation,(spisok+i)->birth.day,

(spisok+i)->birth.month,(spisok+i)->birth.year,(spisok+i)->Marks[0],(spisok+i)->Marks[1],

(spisok+i)->Marks[2],(spisok+i)->Marks[3],(spisok+i)->Marks[4],(spisok+i)->ball);

printf("\n");

}

printf("L-------------------------------------------------------------\n");

}

void srball(struct Stud *spisok)

{  float temp;

for(int i=0;i<M;i++)

{  for(int j=0;j<5;j++)

{  temp += (spisok+i)->Marks[j];

}

}

Srb = temp/(M*5.0);

}

void writefile(struct Stud *spisok)

{  float tem, spbstud;

FILE *fp;

fp = fopen("output.txt", "w");

fprintf(fp,"\n\tOutput Table:\n");

fprintf(fp,"----------------------------------------------------------------------\n");

fprintf(fp,"|%2s|%32s|%1s|%1s|%1s|%1s|%10s|%9s|%2s|\n","№","Студент","П","О","А","С","Дата рожд","Оценки","Балл");

fprintf(fp,"----------------------------------------------------------------------\n");

printf("\n\tOutput Table:\n");

printf("--------------------------------------------------------------\n");

printf("¦%2s¦%23s¦%1s¦%1s¦%1s¦%1s¦%10s¦%9s¦%2s¦\n","№","Студент","П","О","А","С","Дата рожд","Оценки","Балл");

printf("--------------------------------------------------------------\n");

for(int i=0;i<M;i++)

{ tem=0;

for(int j=0;j<5;j++) {tem += (spisok+i)->Marks[j];}

spbstud = tem/5;

if(spbstud < Srb) {

fprintf(fp,"|%2i|%10s %10s %10s|%c|%c|%c|%c|%2i.%2i.%4i|%i %1i %1i %1i %1i|%4i|\n",

i+1,(spisok+i)->family,(spisok+i)->name,(spisok+i)->surname,(spisok+i)->sex,

(spisok+i)->education,(spisok+i)->adress,(spisok+i)->donation,(spisok+i)->birth.day,

(spisok+i)->birth.month,(spisok+i)->birth.year,(spisok+i)->Marks[0],(spisok+i)->Marks[1],

(spisok+i)->Marks[2],(spisok+i)->Marks[3],(spisok+i)->Marks[4],(spisok+i)->ball);

fprintf(fp,"----------------------------------------------------------------------\n");

printf("¦%2i¦%15s %3s %3s¦%c¦%c¦%c¦%c¦%2i.%2i.%4i¦%i %1i %1i %1i %1i¦%4i¦",

i+1,(spisok+i)->family,(spisok+i)->name,(spisok+i)->surname,(spisok+i)->sex,

(spisok+i)->education,(spisok+i)->adress,(spisok+i)->donation,(spisok+i)->birth.day,

(spisok+i)->birth.month,(spisok+i)->birth.year,(spisok+i)->Marks[0],(spisok+i)->Marks[1],

(spisok+i)->Marks[2],(spisok+i)->Marks[3],(spisok+i)->Marks[4],(spisok+i)->ball);

printf("\n");

}

}

printf("--------------------------------------------------------------\n");

fclose(fp);

getch();

}

int main()

{  struct Stud spisok[20]={"1","1","1",'1','1','1','1',{1,1,1},{1,1,1,1,1},1};

open(spisok);

show(spisok);

srball(spisok);

printf("\n\tAverage Ball of group: %3.4f\n", Srb);

writefile(spisok);

}

Похожие материалы

Информация о работе