|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 2740881635 于 2022-6-25 11:07 编辑 #include<stdio.h>
int main() {
int run[] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
int feirun[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
char birthday[11], date[11];
int birth_year=0, birth_month=0, birth_day=0, date_year=0, date_month=0, date_day=0;
int D_value=0;
int all_life=0;
double f;
//printf("请输入你的生日(如1988-05-20):");
//scanf_s("%d-%d-%d", &birth_year,&birth_month,&birth_day);
//getchar();
//printf("请输入今年日期(如2016-03-28):");
//scanf_s("%d-%d-%d", &date_year, &date_month, &date_day);
printf("请输入你的生日(如1988-05-20):");
scanf_s("%s", birthday); //问题一
getchar();
printf("请输入今年日期(如2016-03-28):");
scanf_s("%s", date); //问题二
for (int i = 0; i < 10; i++) {
if (i >= 0 && i <= 3) {
birth_year = birth_year * 10 + (int)birthday[i] - 48;
date_year = date_year * 10 + (int)date[i] - 48;
}
else if (i >= 5 && i <= 6) {
birth_month = birth_month * 10 + (int)birthday[i] - 48;
date_month = date_month * 10 + (int)date[i] - 48;
}
else if (i >= 8 && i <= 9) {
birth_day = birth_day * 10 + (int)birthday[i] - 48;
date_day = date_day * 10 + (int)date[i] - 48;
}
}
for (int i=birth_year; i <= date_year-1; i++) {
if (i % 4 == 0) {
D_value += 366;
}
else {
D_value += 365;
}
}
if (birth_year % 4 == 0) {
for (int j = 0; j < birth_month-1; j++) {
D_value -= run[j];
}
}
else {
for (int j = 0; j < birth_month-1; j++) {
D_value -= feirun[j];
}
}
if (date_year % 4 == 0) {
for (int j = 0; j < date_month-1; j++) {
D_value += run[j];
}
}
else {
for (int j = 0; j < birth_month-1; j++) {
D_value += feirun[j];
}
}
D_value = D_value - birth_day + date_day;
for (int k = birth_year; k < birth_year + 80; k++) {
if (k % 4 == 0) {
all_life += 366;
}
else {
all_life += 365;
}
}
printf("你在这个世界上总共生存了%d天\n", D_value);
printf("如果能活到80岁,你还剩下%d天\n", all_life - D_value);
f = D_value / all_life;
printf("你已经使用了%.2f\%的生命,请好好珍惜剩下的时间!",f*100);
system("pause");
return 0;
}
代码为课后作业18动动手2的我的答案。
问题是我使用VS2022时,scanf_s("%s",birthday);
这一行在输入字符串后会出现错误信息:0x00007FF773F3459D 处有未经处理的异常(在 17.exe 中): 堆栈 Cookie 检测代码检测到基于堆栈的缓冲区溢出。
求求各位大佬解答,几个作业都遭遇到这个问题
scanf_s函数少加了一个检查边界的参数
应当将scanf_s("%s", birthday);改成scanf_s("%s", birthday,sizeof(birthday));
将scanf_s("%s", date);改成scanf_s("%s", date,sizeof(date));
还有就是system函数需要引入#include <windows.h> #include <stdio.h>
#include <windows.h>
int main() {
int run[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int feirun[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char birthday[11], date[11];
int birth_year = 0, birth_month = 0, birth_day = 0, date_year = 0, date_month = 0, date_day = 0;
int D_value = 0;
int all_life = 0;
double f;
//printf("请输入你的生日(如1988-05-20):");
//scanf_s("%d-%d-%d", &birth_year,&birth_month,&birth_day);
//getchar();
//printf("请输入今年日期(如2016-03-28):");
//scanf_s("%d-%d-%d", &date_year, &date_month, &date_day);
printf("请输入你的生日(如1988-05-20):");
scanf_s("%s", birthday, sizeof(birthday)); //问题一
getchar();
printf("请输入今年日期(如2016-03-28):");
scanf_s("%s", date, sizeof(date)); //问题二
for (int i = 0; i < 10; i++) {
if (i >= 0 && i <= 3) {
birth_year = birth_year * 10 + (int)birthday[i] - 48;
date_year = date_year * 10 + (int)date[i] - 48;
}
else if (i >= 5 && i <= 6) {
birth_month = birth_month * 10 + (int)birthday[i] - 48;
date_month = date_month * 10 + (int)date[i] - 48;
}
else if (i >= 8 && i <= 9) {
birth_day = birth_day * 10 + (int)birthday[i] - 48;
date_day = date_day * 10 + (int)date[i] - 48;
}
}
for (int i = birth_year; i <= date_year - 1; i++) {
if (i % 4 == 0) {
D_value += 366;
}
else {
D_value += 365;
}
}
if (birth_year % 4 == 0) {
for (int j = 0; j < birth_month - 1; j++) {
D_value -= run[j];
}
}
else {
for (int j = 0; j < birth_month - 1; j++) {
D_value -= feirun[j];
}
}
if (date_year % 4 == 0) {
for (int j = 0; j < date_month - 1; j++) {
D_value += run[j];
}
}
else {
for (int j = 0; j < birth_month - 1; j++) {
D_value += feirun[j];
}
}
D_value = D_value - birth_day + date_day;
for (int k = birth_year; k < birth_year + 80; k++) {
if (k % 4 == 0) {
all_life += 366;
}
else {
all_life += 365;
}
}
printf("你在这个世界上总共生存了%d天\n", D_value);
printf("如果能活到80岁,你还剩下%d天\n", all_life - D_value);
f = D_value / all_life;
printf("你已经使用了%.2f的生命,请好好珍惜剩下的时间!", f * 100);
system("pause");
return 0;
}
|
|