Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct robot{
- int hour;
- float metrs;
- char mode[15];
- }rob;
- unsigned string_count(FILE *text){
- int count=0;
- while(!feof(text)){
- if(fgetc(text)=='\n') count++;
- }
- fseek(text,0,SEEK_SET);
- return count;
- }
- int read_file(char* name,rob** robot){
- FILE *text=fopen(name,"r");
- if(!text) printf("Error opening file!Try again\n");
- unsigned strings = string_count(text);
- rob* arr= calloc(strings, sizeof(rob));
- for(int i=0;i<strings;i++){
- if(fscanf(text,"%d %f %s",&arr[i].hour,&arr[i].metrs,arr[i].mode)!=3)
- printf("Error reading!Try again\n");
- }
- *robot=arr;
- free(arr);
- fclose(text);
- return (int)strings;
- }
- void sort_metr(rob sorting_robot[], int* strings) {
- for (int i = 0; i < *strings; i++) {
- for (int j = 0; j < *strings - i - 1; j++) {
- if (sorting_robot[j].metrs < sorting_robot[j + 1].metrs) {
- rob temp = sorting_robot[j];
- sorting_robot[j] = sorting_robot[j + 1];
- sorting_robot[j + 1] = temp;
- }
- }
- }
- }
- void sort_hour(rob sorting_robot[], int* strings,) {
- for(int i=0;i<*strings;i++) {
- if(strcmp(sorting_robot[i].mode,"поломка")==0) {
- for (int j = 0; j < *strings - i - 1; j++) {
- if (sorting_robot[j].hour < sorting_robot[j + 1].hour) {
- rob temp = sorting_robot[j];
- sorting_robot[j] = sorting_robot[j + 1];
- sorting_robot[j + 1] = temp;
- }
- }
- }
- }
- }
- int main() {
- char filename[80]={0};
- printf("Put filename:\n");
- scanf("%s",filename);
- rob* robot=NULL;
- int strings = read_file(filename,&robot);
- if(strings<0) exit(1);
- sort_metr(robot,&strings);
- printf("Максимальная глубина погружения: %f\n",robot[0].metrs);
- sort_hour(robot,&strings);
- printf("Максимальная длительность поломки: %d\n",robot[0].hour);
- free(robot);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment