打印结构数组
Printing an Array of a Structure
我用 C 编写了一个程序,可以按年份对汽车记录进行排序并打印它们,建模并打印它们,然后查找并打印重复项。但是,唯一一次打印任何记录是在复制功能中。否则,输出完全是空白的……我试过在 DevC++ 和 Linux 上编译。这些记录是一个全局声明的结构,我正在使用 for 循环打印它们...帮助!
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct Car //struct for cars
{
char make[20];
char model[20];
int year;
char color[20];
} usedCars[10] = //user inputted cars
{
{"toyota","matrix",2006,"silver"},
{"honda","accord",2009,"blue"},
{"chrysler","ptcruiser",2001,"red"},
{"volvo","xc70",2010,"blue"},
{"chevy","blazer",2001,"black"},
{"ford","f150",1998,"blue"},
{"jeep","grandcherokee",2008,"red"},
{"cadillac","deville",2004,"red"},
{"volkswagen","jetta",2010,"silver"},
{"chrysler","ptcruiser",2001,"red"}
};
void print_cars_by_year() //prints by year
{
char temp[20]; //array for swapping records
int i,j,tmp; //variables for swapping
for (i=0; i<10; i++) // nested loop for bubble sort - repeats for 10 records
{
for (j=0 ; j<9; j++) //bubble sort loop
{
if (usedCars[j].year > usedCars[j+1].year) //if the years of the first two records are not equal
{
strcpy(temp, usedCars[j].make); //uses strcpy to swap the records of make
strcpy(usedCars[j].make, usedCars[j+1].make);
strcpy(usedCars[j+1].make, temp);
strcpy(temp, usedCars[j].model); //uses strcpy to swap model
strcpy(usedCars[j].model, usedCars[j+1].model);
strcpy(usedCars[j+1].model, temp);
tmp = usedCars[j].year; //typical int bubble sort
usedCars[j].year = usedCars[j+1].year;
usedCars[j+1].year = tmp;
strcpy(temp, usedCars[j].color); //uses strcpy to swap color
strcpy(usedCars[j].color, usedCars[j+1].color);
strcpy(usedCars[j+1].color, temp);
}
}
}
printf("\nThe records sorted by year are:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
for (i=0; i<10; i++); //prints newly sorted records
{
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
}
void print_cars_by_model()
{
char temp[20]; //array for swapping records
int i,j,tmp; //variables for swapping
for (i=0; i<10; i++) // nested loop for bubble sort - repeats for 10 records
{
for (j=0 ; j<9; j++) //bubble sort loop
{
if (strcmp(usedCars[j].model, usedCars[j+1].model)>0) //if the models of the first two records are not equal
{
strcpy(temp, usedCars[j].make); //uses strcpy to swap the records of make
strcpy(usedCars[j].make, usedCars[j+1].make);
strcpy(usedCars[j+1].make, temp);
strcpy(temp, usedCars[j].model); //uses strcpy to swap model
strcpy(usedCars[j].model, usedCars[j+1].model);
strcpy(usedCars[j+1].model, temp);
tmp = usedCars[j].year; //typical int bubble sort
usedCars[j].year = usedCars[j+1].year;
usedCars[j+1].year = tmp;
strcpy(temp, usedCars[j].color); //uses strcpy to swap color
strcpy(usedCars[j].color, usedCars[j+1].color);
strcpy(usedCars[j+1].color, temp);
}
}
}
printf("\nThe records sorted by model are:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
for (i=0; i<10; i++); //prints unsorted records
{
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
}
void print_duplicate_records()
{
int i,j;
for (i=0; i<10; i++)
{
for (j=i+1; j<10; j++) //will not compare same record
{
if (strcmp(usedCars[i].model, usedCars[j].model) == 0) //only go if models are equal
{
if (strcmp(usedCars[i].make, usedCars[j].make) == 0) //only go if makes are equal
{
if (usedCars[i].year==usedCars[j].year) //only go if years are equal
{
if (strcmp(usedCars[i].color, usedCars[j].color) == 0) //if colors are equal, the record is equal
{
printf("\nThere is a duplicate record:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
}
}
}
}
}
}
int main()
{
int i;
printf("The records (unsorted) are:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
for (i=0; i<10; i++); //prints unsorted records
{
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
print_cars_by_year(); //call to by year func
print_cars_by_model(); //call to by model func
print_duplicate_records();
return 0;
}
输出看起来像
记录(未排序)是:
制作车型年份颜色
0
(然后两个排序函数相同)
存在重复记录:
制作车型年份颜色
克莱斯勒 ptcruiser 2001 红色
你这里的分号错位了
for (i=0; i<10; i++);
/* ^ what? */
这意味着下面的代码只会执行一次。
另外,请格式化你的代码,这样可以防止这种错误,也使用编译器警告,我不想看代码,因为它太乱了,所以我试着用警告编译它 clang
抱怨分号,像这样
error: for loop has empty body [-Werror,-Wempty-body]
如您所见,我没有阅读代码就发现了问题,因为我使用了可用的工具来帮我检查。
您应该修复的更重要的事情是代码中的许多 if
语句,您可以使用 &&
运算符来避免这么多级别的缩进。
我用 C 编写了一个程序,可以按年份对汽车记录进行排序并打印它们,建模并打印它们,然后查找并打印重复项。但是,唯一一次打印任何记录是在复制功能中。否则,输出完全是空白的……我试过在 DevC++ 和 Linux 上编译。这些记录是一个全局声明的结构,我正在使用 for 循环打印它们...帮助!
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct Car //struct for cars
{
char make[20];
char model[20];
int year;
char color[20];
} usedCars[10] = //user inputted cars
{
{"toyota","matrix",2006,"silver"},
{"honda","accord",2009,"blue"},
{"chrysler","ptcruiser",2001,"red"},
{"volvo","xc70",2010,"blue"},
{"chevy","blazer",2001,"black"},
{"ford","f150",1998,"blue"},
{"jeep","grandcherokee",2008,"red"},
{"cadillac","deville",2004,"red"},
{"volkswagen","jetta",2010,"silver"},
{"chrysler","ptcruiser",2001,"red"}
};
void print_cars_by_year() //prints by year
{
char temp[20]; //array for swapping records
int i,j,tmp; //variables for swapping
for (i=0; i<10; i++) // nested loop for bubble sort - repeats for 10 records
{
for (j=0 ; j<9; j++) //bubble sort loop
{
if (usedCars[j].year > usedCars[j+1].year) //if the years of the first two records are not equal
{
strcpy(temp, usedCars[j].make); //uses strcpy to swap the records of make
strcpy(usedCars[j].make, usedCars[j+1].make);
strcpy(usedCars[j+1].make, temp);
strcpy(temp, usedCars[j].model); //uses strcpy to swap model
strcpy(usedCars[j].model, usedCars[j+1].model);
strcpy(usedCars[j+1].model, temp);
tmp = usedCars[j].year; //typical int bubble sort
usedCars[j].year = usedCars[j+1].year;
usedCars[j+1].year = tmp;
strcpy(temp, usedCars[j].color); //uses strcpy to swap color
strcpy(usedCars[j].color, usedCars[j+1].color);
strcpy(usedCars[j+1].color, temp);
}
}
}
printf("\nThe records sorted by year are:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
for (i=0; i<10; i++); //prints newly sorted records
{
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
}
void print_cars_by_model()
{
char temp[20]; //array for swapping records
int i,j,tmp; //variables for swapping
for (i=0; i<10; i++) // nested loop for bubble sort - repeats for 10 records
{
for (j=0 ; j<9; j++) //bubble sort loop
{
if (strcmp(usedCars[j].model, usedCars[j+1].model)>0) //if the models of the first two records are not equal
{
strcpy(temp, usedCars[j].make); //uses strcpy to swap the records of make
strcpy(usedCars[j].make, usedCars[j+1].make);
strcpy(usedCars[j+1].make, temp);
strcpy(temp, usedCars[j].model); //uses strcpy to swap model
strcpy(usedCars[j].model, usedCars[j+1].model);
strcpy(usedCars[j+1].model, temp);
tmp = usedCars[j].year; //typical int bubble sort
usedCars[j].year = usedCars[j+1].year;
usedCars[j+1].year = tmp;
strcpy(temp, usedCars[j].color); //uses strcpy to swap color
strcpy(usedCars[j].color, usedCars[j+1].color);
strcpy(usedCars[j+1].color, temp);
}
}
}
printf("\nThe records sorted by model are:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
for (i=0; i<10; i++); //prints unsorted records
{
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
}
void print_duplicate_records()
{
int i,j;
for (i=0; i<10; i++)
{
for (j=i+1; j<10; j++) //will not compare same record
{
if (strcmp(usedCars[i].model, usedCars[j].model) == 0) //only go if models are equal
{
if (strcmp(usedCars[i].make, usedCars[j].make) == 0) //only go if makes are equal
{
if (usedCars[i].year==usedCars[j].year) //only go if years are equal
{
if (strcmp(usedCars[i].color, usedCars[j].color) == 0) //if colors are equal, the record is equal
{
printf("\nThere is a duplicate record:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
}
}
}
}
}
}
int main()
{
int i;
printf("The records (unsorted) are:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
for (i=0; i<10; i++); //prints unsorted records
{
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
print_cars_by_year(); //call to by year func
print_cars_by_model(); //call to by model func
print_duplicate_records();
return 0;
}
输出看起来像
记录(未排序)是: 制作车型年份颜色 0
(然后两个排序函数相同)
存在重复记录: 制作车型年份颜色 克莱斯勒 ptcruiser 2001 红色
你这里的分号错位了
for (i=0; i<10; i++);
/* ^ what? */
这意味着下面的代码只会执行一次。
另外,请格式化你的代码,这样可以防止这种错误,也使用编译器警告,我不想看代码,因为它太乱了,所以我试着用警告编译它 clang
抱怨分号,像这样
error: for loop has empty body [-Werror,-Wempty-body]
如您所见,我没有阅读代码就发现了问题,因为我使用了可用的工具来帮我检查。
您应该修复的更重要的事情是代码中的许多 if
语句,您可以使用 &&
运算符来避免这么多级别的缩进。