请修改以下代码,使其能够正常运行。#include<stdio.h> #include<string.h> #include<stdlib.h> struct date { int year; int month; int day; }; struct stu { int num; char name[20]; char sex; struct date birth; float score; }; int main() { struct stu s1 = {10010,"zhangsan",'m',2000,5,4,84.5}, s2, s3; /*****输入学生信息存放在变量s2中*****/ /********** Begin **********/ scanf("%d",&(s2.num)); scanf("%s",&(s2.name)); scanf("%c",&(s2.sex)); scanf("%d %d %d",&(s2.birth.year),&(s2.birth.month),&(s2.birth.day)); scanf("%f",&(s2.score)); /********** End **********/ /*****交换两个结构体变量s1和s2*****/ /********** Begin **********/ strcpy(s1.num,s3.num); strcpy(s1.name,s3.name); strcpy(s1.sex,s3.sex); strcpy(s1.date.birth,s3.date.birth); strcpy(s1.score,s3.score); strcpy(s1.num,s2.num); strcpy(s1.name,s2.name); strcpy(s1.sex,s2.sex); strcpy(s1.date.birth,s2.date.birth); strcpy(s1.score,s2.score); strcpy(s3.num,s2.num); strcpy(s3.name,s2.name); strcpy(s3.sex,s2.sex); strcpy(s3..birth,s2.birth); strcpy(s3.score,s2.score); /********** End **********/ /*****输出结构体变量s1和s2的成员*****/ /********** Begin **********/ printf("学号:%d",s1.num); printf("姓名:%s"s1.name); printf("性别:%c",s1.sex); printf("出生日期:%d年%d月%d日",s1.birth.year); printf("成绩:%d",s1.score); printf("学号:%d",s2.num); printf("姓名:%s",s2.name); printf("性别:%d",s2.sex); printf("出生日期:%d年%d月%d日",s2.birth.year,s2.birth.month,s2.birth.day); printf("成绩:%d",s2.score); /********** End **********/ return 0; }
时间: 2023-08-18 11:09:45 浏览: 172
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct date {
int year;
int month;
int day;
};
struct stu {
int num;
char name[20];
char sex;
struct date birth;
float score;
};
int main() {
struct stu s1 = {10010,"zhangsan",'m',{2000,5,4},84.5}, s2, s3;
/*****输入学生信息存放在变量s2中*****/
/********** Begin **********/
scanf("%d",&(s2.num));
scanf("%s",s2.name);
scanf(" %c",&(s2.sex));
scanf("%d%d%d",&(s2.birth.year),&(s2.birth.month),&(s2.birth.day));
scanf("%f",&(s2.score));
/********** End **********/
/*****交换两个结构体变量s1和s2*****/
/********** Begin **********/
s3 = s1; // 先将 s1 的值赋给 s3,避免丢失 s1 的信息
s1 = s2;
s2 = s3;
/********** End **********/
/*****输出结构体变量s1和s2的成员*****/
/********** Begin **********/
printf("学号:%d\n",s1.num);
printf("姓名:%s\n",s1.name);
printf("性别:%c\n",s1.sex);
printf("出生日期:%d年%d月%d日\n",s1.birth.year,s1.birth.month,s1.birth.day);
printf("成绩:%.1f\n",s1.score);
printf("学号:%d\n",s2.num);
printf("姓名:%s\n",s2.name);
printf("性别:%c\n",s2.sex);
printf("出生日期:%d年%d月%d日\n",s2.birth.year,s2.birth.month,s2.birth.day);
printf("成绩:%.1f\n",s2.score);
/********** End **********/
return 0;
}
阅读全文