0 c
我正在尝试从数组中排序日期我有以下代码(不包括我正在尝试阅读的数组和文件,另一个包含我正在尝试编写的已排序日期.
int aniomayor=tot[0].anio;
int diamayor=tot[0].dia;
int mesmayor=tot[0].mes;
while (i<nf) {
if (tot[i].anio > aniomayor) {
int aniomayor=tot[i].anio;
int diamayor=tot[i].dia;
int mesmayor=tot[i].mes;
}
else if (tot[i].anio == aniomayor && tot[i].mes > mesmayor) {
int aniomayor=tot[i].anio;
int diamayor=tot[i].dia;
int mesmayor=tot[i].mes;
}
else if (tot[i].anio == aniomayor && tot[i].mes == mesmayor && tot[i].dia > diamayor) {
int aniomayor=tot[i].anio;
int diamayor=tot[i].dia;
int mesmayor=tot[i].mes;
}
i++;
}
fprintf(f, "%s ", diamayor);
fprintf(f, "%s ", mesmayor);
fprintf(f, "%s \n", aniomayor);
Run Code Online (Sandbox Code Playgroud)
我认为它会起作用,但在2,3,4 ..行中它将始终打印相同的日期,我不知道该怎么做才能忽略已经排序的日期.提前致谢.
原始int声明建立变量.随后的变量会创建具有相同名称但不是同一变量的"影子"变量.
这是一个演示:
#include <stdio.h>
int main() {
int x = 1;
if (x == 1) {
int x = 2;
printf("x=%d\n", x);
}
printf("x=%d\n", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这打印:
x=2
x=1
Run Code Online (Sandbox Code Playgroud)
顶级x永远不会被修改,因此它似乎恢复到原始值.
您应该int从那些中删除前缀,只需分配给现有变量.
当你int x = y;在C中说你正在声明一个变量并分配一个值.分配给现有变量x = y;就足够了.
该int所以编译器知道使用什么类型,它和所有后续引用前缀是只对变量的第一个实例需要相同的范围内.
现在,如果在同一范围内完成,编译器通常会抱怨创建另一个具有相同名称的变量.在你的情况下,因为你在一个内部if,在技术上,这是一个不同的范围,所以你可以有重复.
| 归档时间: |
|
| 查看次数: |
278 次 |
| 最近记录: |