我用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红色
答案 0 :(得分:4)
你这里有一个错位的分号
for (i=0; i<10; i++);
/* ^ what? */
这意味着以下代码只执行一次。
另外,请格式化你的代码,这将防止这种错误,也使用编译器警告,我不想阅读代码因为它太乱了然后我试着用警告编译它并且clang
抱怨分号,就像这样
error: for loop has empty body [-Werror,-Wempty-body]
如您所见,我在没有阅读代码的情况下发现了问题,因为我使用了可用的工具来检查这些问题。
您应该解决的更重要的事情是代码中的许多if
语句,您可以使用&&
运算符来避免这么多级别的缩进。