四则运算题目生成程序

一·作业信息

博客班级 https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18
作业要求 https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18/homework/11377
作业目标 能够精准高效完成四则运算并输出结果;迅速理解需求定制功能
学号 3180701203

二·作业要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
1.除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
2.程序要求能处理用户的输入,判断对错,累积分数
3.程序支持可以由用户自行选择加、减、乘、除运算
4.使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目

三·代码提交与运行截图

include <stdio.h>

struct fraction {
int numerator;
int denominator;
};

// 求解最大公约数
int find_gcd(int n1, int n2);

// 将分数化为最简形式
struct fraction reduce_fraction(struct fraction f);

// 分数的四则运算
struct fraction add_fractions(struct fraction f1, struct fraction f2);
struct fraction subtract_fractions(struct fraction f1, struct fraction f2);
struct fraction multiply_fractions(struct fraction f1, struct fraction f2);
struct fraction divide_fractions(struct fraction f1, struct fraction f2);

int main(void)
{

struct fraction f = { 21, 3 };
struct fraction f1 = { 8, 64 };
struct fraction f2 = { 9, 711 };

struct fraction reducedf = reduce_fraction(f);
printf("%d/%d reduced to simplest terms: %d/%d\n", f.numerator,
    f.denominator, reducedf.numerator, reducedf.denominator);

struct fraction addedf = add_fractions(f1, f2);
printf("%d/%d + %d/%d = %d/%d\n", f1.numerator, f1.denominator,
    f2.numerator, f2.denominator, addedf.numerator, addedf.denominator);

struct fraction subtractedf = subtract_fractions(f1, f2);
printf("%d/%d - %d/%d = %d/%d\n", f1.numerator, f1.denominator,
    f2.numerator, f2.denominator, subtractedf.numerator,
    subtractedf.denominator);

struct fraction multipliedf = multiply_fractions(f1, f2);
printf("%d/%d * %d/%d = %d/%d\n", f1.numerator, f1.denominator,
    f2.numerator, f2.denominator, multipliedf.numerator,
    multipliedf.denominator);

struct fraction dividedf = divide_fractions(f1, f2);
printf("%d/%d / %d/%d = %d/%d\n", f1.numerator, f1.denominator,
    f2.numerator, f2.denominator, dividedf.numerator,
    dividedf.denominator);
return 0;

}

int find_gcd(int n1, int n2)
{
int temp;

while (n1 != 0) {
    temp = n2 % n1;
    n2 = n1;
    n1 = temp;
}

return n2;

}

struct fraction reduce_fraction(struct fraction f)
{
int gcd = find_gcd(f.numerator, f.denominator);
f.numerator /= gcd;
f.denominator /= gcd;
return f;
}

struct fraction add_fractions(struct fraction f1, struct fraction f2)
{
f1.numerator *= f2.denominator;
f2.numerator *= f1.denominator;

struct fraction result = {
    f1.numerator + f2.numerator,
    f1.denominator * f2.denominator
};
result = reduce_fraction(result);

return result;

}

struct fraction subtract_fractions(struct fraction f1, struct fraction f2)
{
f1.numerator *= f2.denominator;
f2.numerator *= f1.denominator;

struct fraction result = {
    f1.numerator - f2.numerator,
    f1.denominator * f2.denominator
};
result = reduce_fraction(result);

return result;

}

struct fraction multiply_fractions(struct fraction f1, struct fraction f2)
{
struct fraction result = {
f1.numerator * f2.numerator,
f1.denominator * f2.denominator
};
result = reduce_fraction(result);

return result;

}
struct fraction divide_fractions(struct fraction f1, struct fraction f2)
{
struct fraction result = {
f1.numerator * f2.denominator,
f1.denominator * f2.numerator
};
result = reduce_fraction(result);

return result;

}

四·个人小结
|psp2.1 任务内容 |计划完成需要的时间(min)| 实际完成需要的时间(min)|
|Planning |计划 |300 |250|
|Estimate |估计这个任务需要多少时间,并规划大致工作步骤| 30| 45|
|Development |开发 |40 |40|
|Analysis |需求分析(包括学习新技术)| 30| 20|
|Design Spec |生成设计文档| 30 |20|
|Design Review |设计复审 |20 |15|
|Coding Standard |代码规范 |10 |10|
|Design |具体设计 |30 |60|
|Coding |具体编码| |90 |120|
|Code Review| 代码复审| 30| 60|
|Test |测试(自我测试,修改代码,提交修改)| 40| 60|
|Reporting |报告 |20 |20|
|Test Report |测试报告 |10 |25|
|Size Measurement |计算工作量 |30 |20|
|Postmortem & Process Improvement Plan |事后总结,并提出过程改进计划| 20| 30|

上一篇:Java有理数类的设计


下一篇:AngularJS项目git提交失败"node --max-old-space-size=10240"