Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Программирование на C / C++ / Лабораторная работа №41 / #4
.CPP#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],tmp;
int n,res_n;
char file_name[15],author[20];
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();
}
int check(int param,int i, int j)
{
switch (param)
{
case 1:{if (strcmp(p[i].caption,p[j].caption)>0) return(1);else return(0);}
case 2:{if (p[i].res>p[i].res) return(1);else return(0);}
case 3:{if (p[i].mass>p[i].mass) return(1);else return(0);}
}
return(0);
}
void sort()
{
int param;
printf("enter # of column by witc you want to sort: ");
cin>>param;
if (param>3)
{
printf("Error!!! Incorrect value! Press any key to exit");
getch();
exit(1);
}
int min;
for (int i = 0; i <n-1; i++)
{
int min = i;
for (int j = i+1; j <n; j++)
if (check(param,min,j)) min = j;
tmp=p[i];
p[i]=p[min];
p[min]=tmp;
}
}
void save(){
for (int j=0;j<n;j++)
printf("%10s %3i %3i\n",p[j].caption,p[j].res,p[j].mass);
getch();
}
void main(){
clrscr();
printf("Enter filename:");
scanf("%s",file_name);
init_base(file_name);
sort();
save();
}