#include<stdio.h> #include<string.h> struct date { int year; int month[12]; int day; }da; int main() { printf("请输入某年某月某天:\n"); scanf("%d,%d,%d",&da.year,&da.month,&da.day); da.month[12]={31,28,31,30,31,30,31,31,31,30,30,31};
时间: 2024-02-11 11:08:17 浏览: 35
这段代码存在一些问题,编译时会出现错误。具体来说,问题出在这一行代码:
```
da.month[12]={31,28,31,30,31,30,31,31,31,30,30,31};
```
在这里,你试图定义一个包含12个元素的整数数组,并将其初始化为固定的12个值。但是,`da.month`实际上是一个结构体`date`中的一个数组成员,而不是一个独立的数组。因此,你不能在这里定义并初始化它。
另外,即使你将其定义为独立的数组,也应该使用`[]`而不是`{}`来访问其元素。
如果你的意图是将每个月的天数存储在`da.month`中,可以将其初始化为常量数组,如下所示:
```
const int days_in_month[] = {31,28,31,30,31,30,31,31,31,30,30,31};
```
然后在输入日期后,你可以通过`da.month`数组访问相应的月份的天数:
```
int days_in_current_month = days_in_month[da.month-1];
```
相关问题
#include<stdio.h> #include<string.h> typedef struct date { int year; int month; int day; }DATE; typedef struct student { int num; char name[20]; char sex; DATE birthday; float score; }STUDENT; void input(STUDENT *s); void output(STUDENT s); /********** Begin **********/ /********** End **********/
完整代码如下所示:
```c
#include <stdio.h>
#include <string.h>
typedef struct date {
int year;
int month;
int day;
} DATE;
typedef struct student {
int num;
char name[20];
char sex;
DATE birthday;
float score;
} STUDENT;
void input(STUDENT *s);
void output(STUDENT s);
void sort(STUDENT s[], int n);
int main() {
STUDENT s[5];
int i;
for (i = 0; i < 5; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
input(&s[i]);
}
sort(s, 5);
printf("\n排序后的学生信息为:\n");
for (i = 0; i < 5; i++) {
output(s[i]);
}
return 0;
}
void input(STUDENT *s) {
printf("学号:");
scanf("%d", &s -> num);
printf("姓名:");
scanf("%s", s -> name);
printf("性别:");
scanf(" %c", &s -> sex);
printf("生日:");
scanf("%d/%d/%d", &s -> birthday.year, &s -> birthday.month, &s -> birthday.day);
printf("成绩:");
scanf("%f", &s -> score);
}
void output(STUDENT s) {
printf("学号:%d,姓名:%s,性别:%c,生日:%d/%d/%d,成绩:%.2f\n",
s.num, s.name, s.sex, s.birthday.year, s.birthday.month, s.birthday.day, s.score);
}
void sort(STUDENT s[], int n) {
int i, j;
STUDENT temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (s[i].score < s[j].score) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
```
这段代码中,定义了一个main函数,用于程序的入口,创建了一个学生信息数组s。在main函数中,使用for循环对每个学生的信息进行输入。输入完成后,调用sort函数对学生信息数组s进行排序。最后,使用for循环对排序后的学生信息进行输出。
从键盘输入一个学生的信息,在屏幕上输出这个学生的信息,要求用函数实现学生信息的输入输出。 #include<stdio.h> #include<string.h> typedef struct date { int year; int month; int day; }DATE; typedef struct student { int num; char name[20]; char sex; DATE birthday; float score; }STUDENT; void input(STUDENT *s); void output(STUDENT s); /********** Begin **********/ /********** End **********/
下面是实现学生信息输入输出的代码:
```
#include<stdio.h>
#include<string.h>
typedef struct date {
int year;
int month;
int day;
} DATE;
typedef struct student {
int num;
char name[20];
char sex;
DATE birthday;
float score;
} STUDENT;
void input(STUDENT *s);
void output(STUDENT s);
int main() {
STUDENT s;
input(&s);
output(s);
return 0;
}
void input(STUDENT *s) {
printf("请输入学生的信息:\n");
printf("学号:");
scanf("%d", &(s->num));
printf("姓名:");
scanf("%s", s->name);
printf("性别:");
scanf(" %c", &(s->sex));
printf("生日:");
scanf("%d%d%d", &(s->birthday.year), &(s->birthday.month), &(s->birthday.day));
printf("成绩:");
scanf("%f", &(s->score));
}
void output(STUDENT s) {
printf("学生的信息如下:\n");
printf("学号:%d\n", s.num);
printf("姓名:%s\n", s.name);
printf("性别:%c\n", s.sex);
printf("生日:%d年%d月%d日\n", s.birthday.year, s.birthday.month, s.birthday.day);
printf("成绩:%.2f\n", s.score);
}
```
函数 `input` 用于输入学生信息,函数 `output` 用于输出学生信息。在 `main` 函数中,先定义一个 `STUDENT` 类型的变量 `s`,然后分别调用 `input` 和 `output` 函数进行输入和输出。注意,在输入字符串时,应该使用 `%s`,而在输入字符时,应该使用 `%c`。另外,在输入生日时,可以使用 `scanf("%d%d%d", &(s->birthday.year), &(s->birthday.month), &(s->birthday.day));` 一次性输入三个整数。输出时,使用 `%d` 输出整数,使用 `%f` 输出浮点数,并且可以使用 `%.2f` 输出两位小数。