当前位置:网站首页>c现代方法16章基础
c现代方法16章基础
2022-08-03 06:22:00 【发狂的蜗牛】
练习题
6.
#include <stdio.h>
#include <stdlib.h>
struct time {
int hours;
int minutes;
int seconds;
};
struct time split_time(long total_seconds)
{
struct time ret;
ret.hours = total_seconds / 3600;
ret.minutes = (total_seconds - ret.hours * 3600) / 60;
ret.seconds = total_seconds - ret.hours * 3600 - ret.minutes * 60;
return ret;
}
int main()
{
struct time t1 = split_time(10000);
printf("hours: %ld minutes : %ld seconds : %ld\n",t1.hours,t1.minutes,t1.seconds);
system("pause");
return 0;
}
运行结果如下:
7.
#include <stdio.h>
struct fraction {
int numerator; //分子
int denominator;//分母
};
//为啥这个函数自己写出来,自己也貌有点不明白
int get_gcd(int m, int n)
{
if (n == 0)
{
return m;
}
return get_gcd(n,m % n);
}
struct fraction
simplify_fraction(struct fraction * f)
{
struct fraction ret;
int gcd = get_gcd(f->numerator,f->denominator);
ret.numerator = f->numerator / gcd;
ret.denominator = f->denominator / gcd;
return ret;
}
//(b)分数相加
struct fraction add(struct fraction * l,struct fraction * r)
{
struct fraction ret;
ret.numerator = l->numerator * r->denominator + l->denominator * r->numerator;
ret.denominator = l->denominator * r->denominator;
ret = simplify_fraction(&ret);
return ret;
}
//(c)分数相减
struct fraction subtraction(struct fraction * l, struct fraction * r)
{
struct fraction ret;
ret.numerator = l->numerator * r->denominator - l->denominator * r->numerator;
ret.denominator = l->denominator * r->denominator;
ret = simplify_fraction(&ret);
return ret;
}
//(d)把两个分数相乘
struct fraction multiply(struct fraction * l, struct fraction * r)
{
struct fraction ret;
ret.numerator = l->numerator * r->numerator;
ret.denominator = l->denominator * r->denominator;
ret = simplify_fraction(&ret);
return ret;
}
//(e)除法 略(和乘法类似)
int main()
{
//(a)
struct fraction f1;
f1.numerator = 1001;
f1.denominator = -7;
struct fraction sim_of_f1 = simplify_fraction(&f1);
printf("numerator:%d ,denominator: %d\n",sim_of_f1.numerator,sim_of_f1.denominator);
//(b)
struct fraction f2,f3;
f2.denominator = 3;
f2.numerator = 4;
f3.denominator = 5;
f3.numerator = 6;
struct fraction r2 = add(&f2,&f3);
printf("nume:%d,deno:%d\n",r2.numerator,r2.denominator);
//(c)
struct fraction r3 = subtraction(&f2,&f3);
printf("nume:%d,deno:%d\n", r3.numerator, r3.denominator);
//(d)
struct fraction r4 = multiply(&f2,&f3);
printf("nume:%d,deno:%d\n", r4.numerator, r4.denominator);
return 0;
}
边栏推荐
- 帆软11版本参数联动为null查询全部
- 924. 尽量减少恶意软件的传播 前缀和
- 连续型特征做embedding代码示例
- 第一章:ARM公司Cortex-M 系列处理器介绍,第二章:嵌入式软件开发介绍和第三章:Cortex-M3和Cortex-M4处理器的一般介绍
- MySQL日期和时间戳的转换
- MySQL性能优化(硬件,系统配置,表结构,SQL语句)
- pyspark --- count the mode of multiple columns and return it at once
- 阿里云-武林头条-建站小能手争霸赛
- nacos-2.0.3启动报错出现no datasource set的坑
- 关于任命韩文弢博士代理NOI科学委员会主席的公告
猜你喜欢
分布式数据库数据一致性的原理、与技术实现方案
华为设备配置BFD状态与接口状态联动
el-table gets the data attribute of a row in the read data table
亿流量大考(1):日增上亿数据,把MySQL直接搞宕机了...
人脸检测和识别--face recognition包
Scala 基础 (三):运算符和流程控制
Charles capture shows
solution 关于NOI 2022的报到通知
【RT_Thread学习笔记】---以太网LAN8720A Lwip ping 通网络
Flink的Exactly-Once、状态机制、watermark机制
随机推荐
【着色器实现Glow可控局部发光效果_Shader效果第十三篇】
El - table column filter functions, control columns show and hide (effect and easy to implement full marks)
el-tree设置利用setCheckedNodessetCheckedKeys默认勾选节点,以及通过setChecked新增勾选指定节点
volatile
信息学奥赛一本通T1446:素数方阵
AutoInt网络详解及pytorch复现
模型训练前后显卡占用对比、多卡训练GPU占用分析【一文读懂】
AlexNet网络详解及复现
Chrome configuration samesite=none method
Multi-Head-Attention原理及代码实现
JS 原型原型链
信息学奥赛一本通T1453:移动玩具
信息学奥赛一本通T1452:Keyboarding
关于Attention的超详细讲解
mysql or语句的优化
关于利用canvas画带箭头的直线旋转
Basic syntax of MySQL DDL and DML and DQL
JS 预编译
MySQL - 视图操作
10 分钟彻底理解 Redis 的持久化和主从复制