#include<stdio.h> #include<string.h> struct student{ char name[100]; int age; }; int main(void) { int i,j; struct student arr[5]; for(i=0;i<5;i++) { scanf("%s%d",arr[i].name,&arr[i].age); } for(i=0;i<4;i++) {
时间: 2024-01-15 18:03:46 浏览: 22
在代码中,最后一个for循环没有结束,缺少右括号。下面是修改后的代码:
```
#include<stdio.h>
#include<string.h>
struct student{
char name[100];
int age;
};
int main(void) {
int i,j;
struct student arr[5];
for(i=0;i<5;i++) {
scanf("%s%d",arr[i].name,&arr[i].age);
}
for(i=0;i<4;i++) {
for(j=0;j<4-i;j++) {
if(arr[j].age>arr[j+1].age) {
struct student t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
for(i=0;i<5;i++) {
printf("%s %d\n",arr[i].name,arr[i].age);
}
return 0; //需要加上返回值
} //需要加上右括号
相关问题
修改程序:#include <stdio.h> #include <stdlib.h> #include <string.h> #define Element 6 struct Student { char name[8]; char id[12]; int sex; int age; float total_gpa; } student[Element]; int main() { int i; FILE *fp; if ((fp = fopen("stu.dat", "rb")) == NULL) { printf("Can't open file!\n"); exit(0); } for(i=0;i<Element;i++) { fread(&student[i],sizeof(struct student_type),6,fp); printf("%-10s% %4s %4d %4d %-15f\n",student[i].id, student[i].name, student[i].sex, student[i].age, student[i].total_gpa) } fclose(fp); return 0; }
修改程序如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Element 6
struct Student {
char name[8];
char id[12];
int sex;
int age;
float total_gpa;
} student[Element];
int main() {
int i;
FILE *fp;
if ((fp = fopen("stu.dat", "rb")) == NULL) {
printf("Can't open file!\n");
exit(0);
}
for(i = 0; i < Element; i++) {
fread(&student[i], sizeof(struct Student), 1, fp);
printf("%-10s %-8s %4d %4d %-15f\n", student[i].id, student[i].name, student[i].sex, student[i].age, student[i].total_gpa);
}
fclose(fp);
return 0;
}
```
修改点如下:
1. 结构体名称应该与定义的一致,即 `student` 而不是 `student_type`;
2. 在 `fread` 中,`sizeof(struct student_type)` 应该改为 `sizeof(struct Student)`,且第三个参数应该为 1 而不是 6;
3. 在 `printf` 中,`% %4s` 应该改为 `%-8s`,即输出 `name` 时保证对齐;
4. `printf` 格式化字符串中,`%-10s` 后应该有一个空格,即输出 `id` 时保证对齐。
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include <string.h> typedef struct student { char num[50]; char name[20]; char sex[4]; char age[4]; }Student; int main() { Student stu = {"202230000630\n","yxy\n" ,"F\n","19\n"}; FILE* fp; if ((fp = fopen("student.dat", "w+")) == NULL) { printf("can not open the file. "); } else { fputs(stu.num, fp); fputs(stu.name, fp); fputs(stu.sex, fp); fputs(stu.age, fp); } fclose(fp); fp = fopen(fp, "r"); if (fp != NULL) { printf("can not open the file. "); }else{ fgets(fp,500,fp); fclose(fp); printf("%s\n", stu.num); printf("%s\n", stu.name); printf("%s\n", stu.sex); printf("%s\n", stu.age);//屏幕显示串信息 } system("pause"); }
这段代码存在一些问题,可以尝试进行以下改进:
1. 在定义结构体时,将年龄的数据类型改为整型,而不是字符型,更符合实际情况。
2. 在文件读取的时候,应该先判断文件是否成功打开,而不是直接使用 `fp` 作为文件名进行打开。
3. 在文件读取的时候,应该使用一个新的变量来存储读取的字符串,而不是直接使用 `fp`,因为 `fp` 是一个 `FILE*` 类型的指针,无法存储字符串。
4. 在文件读取的时候,应该将读取的字符串打印出来,而不是直接打印结构体中的信息,因为结构体中的信息并没有被修改。
改进后的代码如下:
```
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
char num[50];
char name[20];
char sex[4];
int age;
} Student;
int main() {
Student stu = {"202230000630\n", "yxy\n", "F\n", 19};
FILE* fp;
if ((fp = fopen("student.dat", "w+")) == NULL) {
printf("can not open the file.");
} else {
fputs(stu.num, fp);
fputs(stu.name, fp);
fputs(stu.sex, fp);
fprintf(fp, "%d\n", stu.age);
}
fclose(fp);
if ((fp = fopen("student.dat", "r")) == NULL) {
printf("can not open the file.");
} else {
char buffer[500];
fgets(buffer, 500, fp);
strcpy(stu.num, buffer);
fgets(buffer, 500, fp);
strcpy(stu.name, buffer);
fgets(buffer, 500, fp);
strcpy(stu.sex, buffer);
fscanf(fp, "%d", &stu.age);
fclose(fp);
printf("%s", stu.num);
printf("%s", stu.name);
printf("%s", stu.sex);
printf("%d\n", stu.age);
}
system("pause");
}
```