ファイル入力を受け取り、配列に保存するプログラムを作成しています。問題は、二次元アレイを行うべきかどうか、私が完全にはわからないことです。具体的には、私が聞いたループwhile !feof
はおそらく行く方法ではありません。私はまた、city_mpgとhighway_mpgの平均を見つけて、それを別の列として配列に追加する必要があります。列が追加された後、昇順にソートする必要があります。その平均値を1D配列にして、それを別の列に追加するにはどうすればよいですか?もしそれが2Dだったら、[1][4]
と[1][5]
を指定して、それを[1][6]
というように保存しますか?C入力ファイルを読み込み、あるタイプの昇順ソート
入力ファイル:不完全
Mercury Sable 2009 18 28
Jeep Wrangler 2016 17 21
Honda civic 2015 31 41
Toyota Corolla 2015 30 42
Toyta Prius 2010 51 48
Ford Escape 2013 23 33
Ford Fusion 2013 25 37
Acura MDX 2014 20 28
Lexus RX 2013 32 28
プログラム:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CARS 1000 //no more than 1000 cars
#define MAX_STR 30 //str wont be longer than 30
struct car { // declare my structure
char *make; // pointers for char and declares my vars
char *model;
int manufacture_year;
int city_mpg;
int highway_mpg;
int average_mpg;
};
//sorts array based on average mpg here
int main(void) { //main function
int cars = 0;
int c;
struct car *data;
char make[MAX_STR+1]; //char will be 30 + 1 for null char
char model[MAX_STR+1];
int year, city, highway; //declares ints
FILE *file; //declares input file
FILE *file2; //declares output file
file = fopen("cars.txt", "r"); //opens car.txt as read
if(file == NULL) { //if file is null
printf("File error\n"); //throws error
return 1;
}
data = malloc(MAX_CARS * sizeof(struct car)); //allocates memory for array by max cars for pointers
if(data == NULL) {
printf("Memory error\n"); //error if memory is a issue just incase mainly used for testing
return 1;
}
while(fscanf(file, "%30s %30s %d %d %d", make, model, &year, &city, &highway) == 5) { //reads the data with a while loop
if(cars >= MAX_CARS) { //just a check if file is more than 1k
printf("Too many cars\n"); //outputs result if too many cars
break;
}
data[cars].make = strdup(make); // makes a copy of the strings
data[cars].model = strdup(model);
data[cars].manufacture_year = year; // copies the data so that it is headed properly
data[cars].city_mpg = city; // copies the data so that it is headed properly
data[cars].highway_mpg = highway; // copies the data so that it is headed properly
data[cars].average_mpg = (city + highway)/2; //gets the average mpg
cars++; // loop counter
}
fclose(file); //closes file
file2 = fopen("sorted_cars.txt", "a+"); //opens new file or creates one if there isnt one
fprintf(file2,"Make Model Year City mpg Highway mpg Average mpg\n"); //prints to new txt file
for(c=0; c<cars; c++) {
sprintf(model, "%s %s", data[c].make, data[c].model); //sprintf sends formatted output to a string
fprintf(file2,"%-20s %4d %4d %4d %4d\n", model, data[c].manufacture_year,data[c].city_mpg, data[c].highway_mpg, data[c].average_mpg); //prints to oufile
}
// free the memory, It tries to allocate enough memory to hold the old string (plus a null character to mark the end of the string)
while(--cars >= 0) {
free(data[cars].model);
free(data[cars].make);
}
free(data); //frees the array memory
return 0;
}
期待される成果:
Make Model year city mpg highway mpg average mpg
Jeep Wrangler 2016 17 21 19
Mercury Sable 2009 18 28 23
and so on...
[、最小完全、かつ検証例]を作成する場合(http://stackoverflow.com/help/mcve)それは通常*それは実際に*であることができます完全で検証可能なつまり、コンパイルして実行します。あなたがビルドエラーや実行時エラーを要求している場合を除きます。また、期待される出力のほかに、実際の出力も含まれます。 –
また、[while(!feof(file))が常に間違っているのはなぜですか?](http:// stackoverflow。com/questions/5431941/why-is-feof-file-always-wrong)を参照してください。 –
独自のコードでは、各タイプの車は 'struct car'型のオブジェクトで表されます。このようなオブジェクトをいくつか用意する必要があります。あなたがそうすることを決めたのは、 'struct car'(これは問題ありません)の配列です。 2Dアレイが必要なのはなぜですか? –