注册 登录
编程论坛 C语言论坛

求助 一道c语言题目

曾几何时丨 发布于 2021-11-11 15:10, 2148 次点击
小明每个月基本工资x元,还有奖金y元,每迟到1次扣奖金的50元。这个月迟到z次,最多将所有奖金扣完。

请问小明这个月领多少钱?

输入:3个正整数

输出:1个整数(没有回车)

如果输入不合法,则输出"error"

比如:

输入:3000 200 2

输出:  3100


输入:1000 -2 5

输出:error

____________________________________________________

#include<stdio.h>
int main()
{
    int x, y, z;
    int month;
    printf("输入:");
    scanf_s("%d %d %d", &x, &y, &z);
    if (x > 0 && y > 0 && z > 0)
    {
        month = x + y - (z * 50);
        printf("输出:%d", month);                                       我作答的

    }
    else
    {
        printf("输出:error");
    }
   
    return 0;
}


_______________________________________________________

在软件中正常运行,提交作业提示如一下内容:

编译错误

a.cpp: In function 'int main()':
a.cpp:7:32: error: 'scanf_s' was not declared in this scope



请指教
4 回复
#2
diycai2021-11-11 15:27
程序代码:
#include<stdio.h>
int main()
{
    int basePay, bonus, lateTimes;
    int salary;

    scanf("%d%d%d", &basePay, &bonus, &lateTimes);
    if (basePay < 0
        || bonus < 0
        || lateTimes < 0 //此处未判断每月迟到次数上限
        )
    {
        printf("error");
    }
    else
    {
        salary = bonus - (50 * lateTimes);
        if (salary < 0)
        {
            salary = 0;
        }
        salary += basePay;
        printf("%d", salary);
    }
   
    return 0;
}
#3
Mrluoyuzhao2021-11-11 15:57
直接使用scanf就好了。scanf_s是个安全函数,很多_s函数都所谓安全。标准C库里没有安全函数,在VS高版本加入了这些安全函数
#4
Hhu_TF2021-11-13 19:17
回复 楼主 曾几何时丨
你的代码没有反映如果奖金全部扣光后的情况。
#5
Hhu_TF2021-11-13 19:28
回复 楼主 曾几何时丨
程序代码:
#include <stdio.h>

int main() {
    int x, y, z;
    int s;
    printf("请输入:");
    scanf("%d%d%d", &x, &y, &z);
    if (x > 0 && y > 0 && z > 0) {
        s = y - 50 * z; //计算剩余奖金是否为零
        if (s < 0) {
            s = 0;
        }
        printf("输出:%d", s + x);

    } else {
        printf("输出:error");
    }
    return 0;
}
1