首页 > 编程语言 >C++编程练习||创建一个名为Rational的类,进行分数运算。

C++编程练习||创建一个名为Rational的类,进行分数运算。

时间:2024-02-02 19:32:40浏览次数:28  
标签:denominator void 编程 numerator C++ flag num Rational

题目:创建一个名为Rational的类,进行分数运算。

创建一个名为Rational的类,进行分数运算。用整数变量表示类的private数据-numerator(分子)和denominator(分母)。提供一个带默认值的构造函数,并且它应该以简化的形式保存分数。

例如分数2/4应在对象中保存为numerator为1,denominator为2的形式。

对下列任务,提供完成它们的public成员函数:

a)两个Rational值相加,结果应以简化的形式保存。

b)两个Rational值相减,结果应以简化的形式保存。

c)两个Rational值相乘,结果应以简化的形式保存。

d)两个Rational值相除,结果应以简化的形式保存。

e)以a/b形式打印Rational值,其中a是分子,b是分母。

f)以浮点数的形式打印Rational值。

主函数分别输入两个分数的分子和分母,以及将要进行的运算。分别以分数形式和小数形式输出运算结果。

注意,负分数输出时负号后有一个空格,小数的负号后没有空格。

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

class Rational {
	
	private:
		void simplify() {
			int flag = abs(this->numerator);
			while(flag > 1) {
				if((this->numerator % flag) == 0 && (this->denominator % flag) == 0) {
					this->numerator /= flag;
					this->denominator /= flag;
				}
				flag--;
			} 
		}
	
	public:
		
		int numerator;
		int denominator;
		
		Rational(int c_num = 0, int c_den = 0) {
			
			if ((c_num > 0 && c_den > 0) || (c_num < 0 && c_den < 0)) {
				this->numerator = abs(c_num);
				this->denominator = abs(c_den);
			} else {
				this->numerator = -abs(c_num);
				this->denominator = abs(c_den);
			}
			
			c_num = abs(c_num);
			while(c_num > 1) {
				if((this->numerator % c_num) == 0 && (this->denominator % c_num) == 0) {
					this->numerator /= c_num;
					this->denominator /= c_num;
				}
				c_num--;
			}
		}
		
		void add(Rational a, Rational b) {
			this->numerator += (a.numerator * b.denominator + a.denominator * b.numerator);
			this->denominator += (a.denominator * b.denominator);
			this->simplify();
		}

		void minus(Rational a, Rational b) {
			this->numerator += (a.numerator * b.denominator - a.denominator * b.numerator);
			this->denominator += (a.denominator * b.denominator);
			this->simplify();
		}

		void multi(Rational a, Rational b) {
			this->numerator += (a.numerator * b.numerator);
			this->denominator += (a.denominator * b.denominator);
			this->simplify();
		}

		void divide(Rational a, Rational b) {
			this->numerator += (a.numerator * b.denominator);
			this->denominator += (a.denominator * b.numerator);
			this->simplify();
		}

		void printFormal() {
			cout << numerator << "/" << denominator << endl;
		}
		
		void printBoth() {
			cout << "the Formal format of the rational is : " << numerator << "/" << denominator << endl;
			cout << "the Fixed format of the rational is : " << setprecision(2) << fixed << (double)numerator/denominator << endl;
		}

};

int main() {
	int firstN,firstD,secondN,secondD;
	char op;

//    cout<<"Please input the numerator of first Rational: ";
	cin>>firstN;
//    cout<<"Please input the denominator of first Rational: ";
	cin>>firstD;
//    cout<<"Please input the numerator of second Rational: ";
	cin>>secondN;
//    cout<<"Please input the denominator of second Rational: ";
	cin>>secondD;


	Rational r1(firstN,firstD),r2(secondN,secondD),r3;


	cin>>op;

	cout<<"the Formal format of the first rational is : ";
	r1.printFormal();
	cout<<"the Formal format of the second rational is : ";
	r2.printFormal();
	cout<<endl;

	switch(op) {
		case '+' :
			r3.add(r1,r2);
			r3.printBoth();
			break;
		case '-' :
			r3.minus(r1,r2);
			r3.printBoth();
			break;
		case '*':
			r3.multi(r1,r2);
			r3.printBoth();
			break;
		case '/':
			r3.divide(r1,r2);
			r3.printBoth();
			break;
		default:
			cout<<"Invalid operator!";
	}

	return 0;
}

运行结果:

C++编程练习||创建一个名为Rational的类,进行分数运算。_#include

C++编程练习||创建一个名为Rational的类,进行分数运算。_c++_02

C++编程练习||创建一个名为Rational的类,进行分数运算。_分数_03

C++编程练习||创建一个名为Rational的类,进行分数运算。_ide_04

标签:denominator,void,编程,numerator,C++,flag,num,Rational
From: https://blog.51cto.com/u_16532251/9561059

相关文章

  • C++编程练习||Account类:创建一个名为Account的类,银行可以使用它表示客户的银行帐户||I
    1.Account类题目:创建一个名为Account的类,银行可以使用它表示客户的银行帐户。这个类应该包括一个类型为double的数据成员,表示帐户余额。这个类提供一个构造函数,它接受初始余额并用它初始化数据成员。这个构造函数应当确认初始余额的有效性,保证它大于或等于0。否则,余额应设置为0......
  • llvm官网上推荐的c++网站
    TheC++StandardTemplateLibraryLLVMmakesheavyuseoftheC++StandardTemplateLibrary(STL),perhapsmuchmorethanyouareusedto,orhaveseenbefore.Becauseofthis,youmightwanttodoalittlebackgroundreadinginthetechniquesusedandca......
  • CMU-15445(Fall 2023) Project0 C++ Primer 个人笔记
    CMU-15445Project0c++语法问题我直接问的gpt测试文件测试文件都存放在/bustub-private/test目录下,可以自己修改里边的测试方法并且查看有哪些特殊情况需要处理。Task1Get方法使用一个cur节点指向当前正在查找的节点,index指向当前当前正在查找的字符,在children_中查找key[......
  • vs2022支持c++20 import模块功能
    参考链接:https://blog.csdn.net/fellow1984/article/details/124819231工具->获取工具和功能->VisualStudioInstaller->单个组件:搜索C++模块,勾选项目属性对应项修改编译代码即可//helloworldimport<iostream>;intmain(){ std::cout<<"helloworld\n";......
  • c++结构体数组sort排序出错?(关于sort排序comp比较器的严格弱排序性质)
    在sort函数比较的时候,它会严格弱排序,比较a是否>=b,然后两个对象会进行交换,重新比较一遍,相当于这次比较的是b是否>=aa>=b?满足:trueb<=a?满足:true这样就出现了一个冲突,不管是a>=b还是b>=a都会返回true的情况,我们都知道sort中只要comp返回true,两个元素就会交换一次......
  • Windows内核开发-[6]、内核编程基础(3)
    内存分配在应用层编程时,系统提供了GlobalAlloc/HeapAlloc/LocalAlloc等函数。C/C++库提供了malloc函数,以及new操作符在堆上分配内存。在我前面一个关于Windows页交换文件的博客中,介绍了虚拟内存,虚拟内存是计算机系统内存管理的一种技术。它使得应用程序认为它拥有连续的可用的......
  • C++遴选出特定类型的文件或文件名符合要求的文件
      本文介绍基于C++语言,遍历文件夹中的全部文件,并从中获取指定类型的文件的方法。  首先,我们来明确一下本文所需实现的需求。现在有一个文件夹,其中包含了很多文件,如下图所示;我们如果想获取其中所有类型为.bmp格式的文件的名称,如果文件数量比较多的话,手动筛选就会很麻烦。而借......
  • canonical 在计算机编程领域的含义
    canonical在计算机编程领域中有多重含义,主要取决于上下文和所指的领域。以下是canonical在不同情境下的含义及相应示例:数据结构与算法:在数据结构与算法中,canonical常用来描述一个问题或者数据结构的标准或典型表达。这通常是指最常见或最经典的表达方式,可以作为学习和理解的......
  • Linux系统编程49 信号 - sigprocmask() 设置信号集当中信号的mask信号屏蔽字
    sigprocmask():虽然我不知道信号什么时候来,但是我可以决定什么时候响应信号信号集:NAMEsigemptyset,sigfillset,sigaddset,sigdelset,sigismember-POSIXsignalsetoperationsSYNOPSIS#include<signal.h>intsigemptyset(sigset_t*set);清空信号集intsigfi......
  • WebAssembly核心编程[3]: Module 与 Instance
    WebAssembly程序总是以模块来组织,模块是基本的部署、加载和编译单元。在JavaScript编程接口中,模块通过WebAssembly.Module类型表示。WebAssembly.Module通过加载的.wasm二进制文件创建而成,它承载了描述wasm模块的元数据,类似于描述程序集的Assembly对象。WebAssembly.Module自身是......