zoukankan      html  css  js  c++  java
  • 实验4

    Graph

    • graph.h
    #ifndef GRAPH_H_
    #define GRAPH_H_
    class graph
    {
    public:
    	graph(char c, int num);
    	void draw()const;
    	void resetdata(char c, int num);
    	~graph();
    private:
    	char ch;
    	int number;
    };
    #endif // !GRAPH_H_
    
    • graph.cpp
    #include "stdafx.h"
    #include "graph.h"
    #include<iostream>
    #include<iomanip>
    using namespace std;
    graph::graph(char c,int num):ch(c),number(num){}
    graph::~graph(){}
    void graph::draw()const
    {
    	for(int i=0;i<number;i++)
    	{
    		int wid = (2*(number-i)+1)/2;
    		cout << setfill(' ') << setw(wid);
    		for (int j = 0; j <1+2*i; j++)
    		{
    			cout << ch;
    		}
    		cout << endl;
    	 }
    }
    void graph::resetdata(char c, int num)
    {
    	ch = c;
    	number = num;
    	this->draw();
    }
    
    • main.cpp
    #include "stdafx.h"
    #include"graph.h"
    int main()
    {
    	graph g1('&', 7);
    	g1.draw();
    	g1.resetdata('*', 5);
        return 0;
    }
    

    Fraction

    • 刚好之前学了运算符重载,就在这里当复习了。

    • fraction.h

    #ifndef FRACTION_H_
    #define FRACTION_H_
    class Fraction
    {
    	int top, bottom;
    public:
    	Fraction(int t=0, int b = 1);
    	~Fraction();
    	friend Fraction operator +(Fraction &f1,Fraction &f2);//重载“+”运算符,用于计算两个分数相加
    	friend Fraction operator +( int num, Fraction &f);//重载“+”运算符,用于一个整数与分数相加
    	friend Fraction operator -(Fraction &f1,Fraction &f2);//重载“-”运算符,用于计算两个分数相减
    	friend Fraction operator -( int num, Fraction &f);//重载“-”,用于一个整数减去一个分数
    	friend Fraction operator *(Fraction &f1,Fraction &f2);//重载“*”
    	friend Fraction operator *( int num, Fraction &f);//重载“*”,整数乘分数
    	friend Fraction operator /(Fraction &f1,Fraction &f2);//重载“/”
    	friend Fraction operator /(int num, Fraction &f);//重载“/”,整数除分数
    	bool operator <( Fraction &f);//重载“<”
    	friend bool operator <( int num, Fraction  &f);//重载“<”,整数与分数
    	bool operator >( Fraction &f);//重载“>”
    	friend bool operator >( int num, Fraction &f);
    	void setdata();//重新设置
    	void display();//显示分数
    };
    #endif // !FRACTION_H_
    
    • fraction.cpp
    #include "stdafx.h" 
    #include "Fraction.h"
    #include<iostream>
    #include<cmath>
    using namespace std;
    int Common_divisor(int top, int bottom)//用于求公约数
    {
    	int common_divisor;
    	while (1)
    	{
    		if (top%bottom == 0)
    		{
    			return bottom;
    		}
    		else {
    			common_divisor= bottom;
    			bottom = top % common_divisor;
    			top = common_divisor;
    		}
    	}
    	return common_divisor;
    }
    bool limited(int bottom) {   //用来判断一个分数是否可以化为有限小数输出
    	if (bottom == 1)
    	{
    		return true;
    	}
    	else if(bottom % 2 == 0)
    	{
    		return limited(bottom / 2);
    	}
    	else if (bottom % 5 == 0)
    	{
    		return limited(bottom / 5);
    	}
    	else {
    		return false;
    	}
    }
    //在构造分数时,直接按照要求进行修整,方便后面计算
    Fraction::Fraction(int t,int b):top(t),bottom(b){ 
    	if (top < 0 && bottom < 0)//分子分母都小于零,去符号
    	{
    		top = -top;
    		bottom = -bottom;
    	}
    	if (bottom < 0 && top>0)//分母小于零,分子大于零,交换符号
    	{
    		top = -top;
    		bottom = -bottom;
    	}
    	int temp_t, temp_b;
    	if (top < 0)//避免负数进行%运算时的干扰
    	{
    		temp_t = -top;
    		temp_b = bottom;
    	}
    	else {
    		temp_t = top;
    		temp_b = bottom;
    	}
    	bottom = bottom / Common_divisor(temp_t,temp_b);
    	top = top / Common_divisor(temp_t,temp_b);
    }
    //加减乘除操作基本相同,分母不同通分,找公约数,最简化
    Fraction operator+ (Fraction &f1,Fraction &f2) {
    	int temp_b,temp_t,temp_t_b,temp_t_t;//temp_b,temp_t用来储存通分后的分子分母,temp_t_b,temp_t_t用于计算公约数
    	if (f1.bottom != f2.bottom)//分母不同时,通分
    	{
    		temp_b= f1.bottom * f2.bottom;
    		temp_t=f1.top * f2.bottom+f2.top*f1.bottom;
    		if (temp_t < 0)//防止负数干扰求公约运算
    		{
    			temp_t_t = -temp_t;
    			temp_t_b = temp_b;
    		}
    		else {
    			temp_t_t = temp_t;
    			temp_t_b = temp_b;
    		}
    		return Fraction(temp_b/ Common_divisor(temp_t_t,temp_t_b),temp_t/ Common_divisor(temp_t_t,temp_t_b));//通分后除于公约数
    	}
    	else {//分母相同,分子直接相加
    		temp_b = f1.bottom;
    		temp_t = f1.top+f2.top;
    		if (temp_t < 0)
    		{
    			temp_t_t = -temp_t;
    			temp_t_b = temp_b;
    		}
    		else {
    			temp_t_t = temp_t;
    			temp_t_b = temp_b;
    		}
    		return Fraction(temp_b/ Common_divisor(temp_t_t,temp_t_b),temp_t/ Common_divisor(temp_t_t,temp_t_b));
    	}
    }
    Fraction operator +(int num, Fraction &f) {
    	int temp_b, temp_t, temp_t_t; //temp_t用来储存通分后的分子,temp_b, temp_t_t用于计算公约数
    	temp_b = f.bottom;
    	temp_t = num * f.bottom + f.top;
    	if (temp_t < 0)//防止负数干扰求公约运算
    	{
    		temp_t_t = -temp_t;
    	}
    	else {
    		temp_t_t = temp_t;
    	}
    	return Fraction(temp_t / Common_divisor(temp_t_t, temp_b), temp_b / Common_divisor(temp_t_t, temp_b));
    }
    Fraction operator -(Fraction &f1,Fraction &f2) {
    	int temp_b, temp_t, temp_t_b, temp_t_t; //temp_b, temp_t用来储存通分后的分子分母,temp_t_b, temp_t_t用于计算公约数
    	if (f1.bottom != f2.bottom)
    	{
    		temp_b = f1.bottom * f2.bottom;
    		temp_t = f1.top * f2.bottom - f2.top*f1.bottom;
    		if (temp_t < 0)//防止负数干扰求公约运算
    		{
    			temp_t_t = -temp_t;
    			temp_t_b = temp_b;
    		}
    		else {
    			temp_t_t = temp_t;
    			temp_t_b = temp_b;
    		}
    		return Fraction(temp_b / Common_divisor(temp_t_t,temp_t_b),temp_t / Common_divisor(temp_t_t,temp_t_b));
    	}
    	else {
    		temp_b = f1.bottom;
    		temp_t = f1.top - f2.top;
    		if (temp_t < 0)//防止负数干扰求公约运算
    		{
    			temp_t_t = -temp_t;
    			temp_t_b = temp_b;
    		}
    		else {
    			temp_t_t = temp_t;
    			temp_t_b = temp_b;
    		}
    		return Fraction(temp_b/ Common_divisor(temp_t_t,temp_t_b),temp_t/ Common_divisor(temp_t_t,temp_t_b));
    	}
    }
    Fraction operator -(int num, Fraction &f)
    {
    	int temp_b, temp_t, temp_t_t; //temp_t用来储存通分后的分子,temp_b, temp_t_t用于计算公约数
    	temp_b = f.bottom;
    	temp_t = num * f.bottom - f.top;
    	if (temp_t < 0)//防止负数干扰求公约运算
    	{
    		temp_t_t = -temp_t;
    	}
    	else {
    		temp_t_t = temp_t;
    	}
    	return Fraction(temp_t / Common_divisor(temp_t_t, temp_b), temp_b / Common_divisor(temp_t_t, temp_b));
    }
    Fraction operator *(Fraction &f1,Fraction &f2) {
    	int temp_b, temp_t, temp_t_b, temp_t_t;//temp_b,temp_t用来互乘后分子分母,temp_t_b,temp_t_t用于计算公约数
    	temp_b = f1.bottom * f2.bottom;
    	temp_t = f1.top * f2.top;
    	if (temp_t < 0)//防止负数干扰求公约运算
    	{
    		temp_t_t = -temp_t;
    		temp_t_b = temp_b;
    	}
    	else {
    		temp_t_t = temp_t;
    		temp_t_b = temp_b;
    	}
    	return Fraction(temp_b / Common_divisor(temp_t_t,temp_t_b),temp_t / Common_divisor(temp_t_t,temp_t_b));
    }
    Fraction operator *(int num, Fraction &f) {
    	int temp_b, temp_t, temp_t_t; //temp_t用来储存通分后的分子,temp_b, temp_t_t用于计算公约数
    	temp_b = f.bottom;
    	temp_t = num *f.top;
    	if (temp_t < 0)//防止负数干扰求公约运算
    	{
    		temp_t_t = -temp_t;
    	}
    	else {
    		temp_t_t = temp_t;
    	}
    	return Fraction(temp_t / Common_divisor(temp_t_t, temp_b), temp_b / Common_divisor(temp_t_t, temp_b));
    }
    Fraction operator / (Fraction &f1,Fraction &f2) {
    	int temp_b, temp_t, temp_t_b, temp_t_t;//temp_b,temp_sum用来储存乘倒数后的分子分母,temp_t_b,temp_t_sum用于计算公约数
    	temp_t = f1.top * f2.bottom;
    	temp_b = f1.bottom * f2.top;
    	if (temp_t < 0)//防止负数干扰求公约运算
    	{
    		temp_t_t = -temp_t;
    		temp_t_b = temp_b;
    	}
    	else {
    		temp_t_t = temp_t;
    		temp_t_b = temp_b;
    	}
    	return Fraction(temp_b / Common_divisor(temp_t_t,temp_t_b),temp_t / Common_divisor(temp_t_t,temp_t_b));
    }
    Fraction operator / (int num, Fraction &f){
    	int temp_b, temp_t, temp_t_t; //temp_t用来储存通分后的分子,temp_b, temp_t_t用于计算公约数
    	temp_b = f.top;
    	temp_t = num * f.bottom;
    	if (temp_t < 0)//防止负数干扰求公约运算
    	{
    		temp_t_t = -temp_t;
    	}
    	else {
    		temp_t_t = temp_t;
    	}
    	return Fraction(temp_t / Common_divisor(temp_t_t, temp_b), temp_b / Common_divisor(temp_t_t, temp_b));
    }
    bool Fraction::operator > (Fraction &f){
    	if (bottom == f.bottom)
    	{
    		if (top > f.top)
    		{
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    	else
    	{
    		int temp_t, temp_ft;
    		temp_t = top * f.bottom;
    		temp_ft = f.top*bottom;
    		if (temp_t > temp_ft)
    		{
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    }
    bool operator >(int num,Fraction &f){
    	if (num*f.bottom > f.top)
    	{
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    bool Fraction::operator < (Fraction &f) {
    	if (bottom == f.bottom)
    	{
    		if (top <f.top)
    		{
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    	else
    	{
    		int temp_t, temp_ft;
    		temp_t = top * f.bottom;
    		temp_ft = f.top*bottom;
    		if (temp_t < temp_ft)
    		{
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    }
    bool operator <(int num,Fraction &f){
    	if (num*f.bottom < f.top)
    	{
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    void Fraction::setdata()
    {
    	cout << "please enter top:";
    	cin >> top;
    	cout << "please enter bottom:";
    	cin >> bottom;
    	if (top < 0 && bottom < 0)
    	{
    		top = -top;
    		bottom = -bottom;
    	}
    	if (bottom < 0 && top>0)
    	{
    		top = -top;
    		bottom = -bottom;
    	}
    	int temp_t, temp_b,temp_t_t,temp_t_b;
    	temp_t = top;
    	temp_b = bottom;
    	if (temp_t < 0)//防止负数干扰求公约运算
    	{
    		temp_t_t = -temp_t;
    		temp_t_b = temp_b;
    	}
    	else {
    		temp_t_t = temp_t;
    		temp_t_b = temp_b;
    	}
    	bottom = temp_b / Common_divisor(temp_t_t,temp_t_b);
    	top = temp_t / Common_divisor(temp_t_t,temp_t_b);
    }
    void Fraction::display() {
    	if (limited(bottom))
    	{
    		double Frac_double;
    		Frac_double = double(top) / double(bottom);
    		cout << Frac_double << endl;
    	}
    	else
    	{
    		cout << top << "/" << bottom << endl;
    	}
    }
    Fraction::~Fraction(){} 
    
    • main.cpp
    #include "stdafx.h"
    #include"Fraction.h"
    #include<iostream>
    using namespace std;
    int main()
    {
    	Fraction f1(3, 4), f2(3, -7), f3(-5, -6), f4,f5(5,10),f6(9,3),check1,check2,check3;
    	cout << "f1 = ";
    	f1.display();
    	cout << "f2 = ";
    	f2.display();
    	cout << "f3 = ";
    	f3.display();
    	cout << "f5 = ";
    	f5.display();
    	cout << "f6 = ";
    	f6.display();
    	cout << "check1 = f1 + f2 = ";
    	check1 = f1 + f2;
    	check1.display();
    	cout << "check2 = f5 - f6 = ";
    	check2 = f5 - f6;
    	check2.display();
    	cout << "f3 = f3 * f1 = ";
    	f3 = f3 * f1;
    	f3.display();
    	cout << "check3 = f1 / f5 = ";
    	check3 = f1 / f5;
    	check3.display();
    	cout << "比较f1与f2的大小。" << endl;
    	if (f1 > f2)
    	{
    		cout << "较大的是f1:";
    		f1.display();
    	}
    	else {
    		cout << "较大的是f2:";
    		f2.display();
    	}
    	cout << "f1 = 7 + f2 = ";
    	f1 = 7 + f2;
    	f1.display();
    	cout << "f2 = 3 - f6 = ";
    	f2 = 3 - f6;
    	f2.display();
    	cout << "f3 = 8 * f1 = ";
    	f3 = 8 * f1;
    	f3.display();
    	cout << "f4 = 11 / f5 = ";
    	f4 = 11 / f5;
    	f4.display();
    	cout << "比较6与f2的大小。" << endl;
    	if (6 > f2)
    	{
    		cout << "较大的是6";
    	}
    	else {
    		cout << "较大的是f2:";
    		f2.display();
    	}
        return 0;
    }
    

  • 相关阅读:
    22.抽象类
    21.多态
    20.修饰符
    19.继承
    day46---MySQL数据库进阶(一)
    day45---mysql数据库基本操作初阶(二)
    day45---计算机安全项目(癞蛤蟆)
    day44---数据库初阶(一)
    day44---windows下安装MySQL-5.6.37
    day42---几种network IO模型
  • 原文地址:https://www.cnblogs.com/flyingbrid-nest/p/8909197.html
Copyright © 2011-2022 走看看