scanf() + printf()

//
// Created by hengxin on 9/30/22.
//

#include <stdio.h>
#include <math.h>
#include <ctype.h>

int main() {
  char first_name[10];
  char last_name[10];
  char gender;

  int birth_year;
  int birth_month;
  int birth_day;
  char weekday[10];

  int c_score;
  int music_score;
  int medicine_score;
  int rank;

  scanf("%9s%9s %c %d-%d-%d %9s %d%d%d %*f%*f %d%%",
        first_name, last_name, &gender,
        &birth_year, &birth_month, &birth_day, weekday,
        &c_score, &music_score, &medicine_score,
        &rank);
  double mean = (c_score + music_score + medicine_score) / 3.0;

  double sd = sqrt(((pow(c_score - mean, 2)
      + pow(music_score - mean, 2)
      + pow(medicine_score - mean, 2))) / 3.0);


  printf("%s %s \t %c\n"
         "%.2d-%d-%d \t %.3s.\n"
         "%d \t %d \t %d\n"
         "%.1f \t %.2f %d%%\n",
         first_name, last_name, toupper(gender),
         birth_month, birth_day, birth_year, weekday,
         c_score, music_score, medicine_score,
         mean, sd, rank);

  return 0;
}

这段代码是一个程序,用于读取用户输入的信息并进行一些计算和打印。现在我们逐步解读这段代码:

1.代码包括三个头文件:&lt;stdio.h&gt;、&lt;math.h&gt;和&lt;ctype.h&gt;。这些头文件提供了输入/输出、数学计算和字符处理的函数。
2.在main()函数中,声明了各种变量用于存储用户的信息。这些变量包括first_name(名字)、last_name(姓氏)、gender(性别)、birth_year(出生年份)、birth_month(出生月份)、birth_day(出生日期)、weekday(星期几)、c_score(C语言分数)、music_score(音乐分数)、medicine_score(医学分数)和rank(排名)。
3.使用scanf()函数从用户输入中读取信息并存储到对应的变量中。scanf函数使用格式化字符串指定输入的格式,例如"%9s%9s %c %d-%d-%d %9s %d%d%d %*f%*f %d%%"表示按照一定格式读取相应的数据,并将其存储到指定的变量中。
4.计算平均值mean,将三个科目的分数相加后除以3.0。
5.计算标准差sd,使用标准差的数学公式计算每个分数与平均值之间差的平方的平均值的平方根。
6.使用printf()函数以格式化的方式打印出用户的信息。该函数使用格式化字符串来指定输出的格式,并使用相应的变量填充格式化字符串的占位符。其中使用了toupper()函数将性别转换为大写字母。
7.最后,程序返回0,表示执行成功。

总体而言,该代码用于读取用户的信息,进行计算,并以格式化的方式将结果打印输出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cytingle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值