Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Программирование на C / C++ / Лабораторная работа №3 / лаба3_файлы
.docУфимский государственный авиационный технический университет
Кафедра АПрИС
Отчёт по лабораторной работе №3 по программированию.
Операции с файлами
Уфа 2007
Цель работы:
Изучить работу с файлами.
Постановка задачи:
Дан массив записи, содержащии сведения о веществах: указывается название вещества, его удельный вес и проводимость. Найти название и удельные веса веществ заданной проводимости и напечатать их в порядке убывания удельного веса.
Блок-схема:
Листинг программы
#include<stdlib.h>
#include<string.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
struct materials{
char caption[20];
int mass;
int res;
}p[50],result[50],tmp;
int n,res_n,resist;
char file_name[15];
void search(int resistance){
res_n=-1;
for (int i=0;i<n;i++)
if (p[i].res==resistance)
{ res_n++;
result[res_n]=p[i];
}
}
void init_base(char *name){
ifstream inp_file(name);
if (!inp_file)
{ printf("Frror!!! File not found! Press any key to exit.");
getch();
exit(1);
return;
}
n=-1;
while (!inp_file.eof())
{
n++;
inp_file>>p[n].caption>>p[n].res>>p[n].mass;
}
inp_file.close();
}
void sort(){
for (int i = 0; i <= res_n-1; i++)
{
int min = i;
for (int j = i+1; j <= res_n; j++)
if (result[j].mass <= result[min].mass) min = j;
tmp=result[i];
result[i]=result[min];
result[min]=tmp;
}
}
void save(){
printf("Enter output filename:");
scanf("%s",file_name);
ofstream output(file_name,ios::trunc);
for (int j=0;j<=res_n;j++)
output<<result[j].caption<<" "<<result[j].res<<" "<<result[j].mass<<"\n";
output.close();
}
void main(){
clrscr();
printf("Enter filename:");
scanf("%s",file_name);
init_base(file_name);
printf("Enter resistance:");
scanf("%i",&resist);
search(resist);
sort();
save();
}
Тестирование программы
Вывод
В ходе данной лабораторной работы были освоены навыки работы с файлами.