• 实践练习四


    题一:graph

    以下为题目截图:


    将draw函数补充如下:

    	for (int i = 0; i < size; i++)
    	{
    		for (int j = 0; j < size - i - 1; j++)
    			cout << " ";
    		for (int j = 0; j < 2 * i + 1; j++)
    			cout << symbol;
    		for (int j = 0; j < size - i - 1; j++)
    			cout << " ";
    		cout << endl;
    	}
    

    下面是选做题目一:支持重新设置显示的字符、尺寸,每次重新设置后,自动重绘图形。

    文件graph.h

    #ifndef GRAPH_H
    #define GRAPH_H
    
    // 类Graph的声明 
    class Graph {
    	public:
    		Graph(char ch, int n);   // 带有参数的构造函数 
    		void draw(); 	// 绘制图形 
    		void input();	// 录入字符和尺寸
    	private:
    		char symbol;
    		int size;
    };
    #endif
    

    文件graph.cpp

    // 类graph的实现
     
    #include "graph.h" 
    #include <iostream>
    using namespace std;
    
    // 带参数的构造函数的实现 
    Graph::Graph(char ch, int n): symbol(ch), size(n) {
    }
    
    
    // 成员函数draw()的实现
    // 功能:绘制size行,显示字符为symbol的指定图形样式 
    //       size和symbol是类Graph的私有成员数据 
    void Graph::draw() {
    	// 补足代码,实现「实验4.pdf」文档中展示的图形样式 
    	for (int i = 0; i < size; i++)
    	{
    		for (int j = 0; j < size - i - 1; j++)
    			cout << " ";
    		for (int j = 0; j < 2 * i + 1; j++)
    			cout << symbol;
    		for (int j = 0; j < size - i - 1; j++)
    			cout << " ";
    		cout << endl;
    	}		
    	}
    	
    void Graph::input()
    {
    	cin >> symbol >> size;
    	draw();			//自动绘图
    }
    

    文件main.cpp

    #include <iostream>
    #include "graph.h"
    using namespace std;
    
    
    int main() {
    	Graph graph1('*',5), graph2('$',7) ;  // 定义Graph类对象graph1, graph2 
    	graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 
    	graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
    	
    	while (true)
    	{
    		cout << "请输入指定的字符及尺寸,中间以空格隔开:";
    		graph1.input();
    	}
    
    	return 0; 
    } 
    

    结果截图如下:

    题二:fraction

    以下为题目截图:

    fraction.h文件

    #pragma once
    #ifndef FRACTION_H
    #define FRACTION_H
    class fraction
    {
    public:
    	fraction(int t0,int b0);
    	fraction(int t0);
    	fraction();
    	~fraction();
    	void add(fraction &f1);			//加
    	void subtract(fraction &f1);	//减
    	void multiply(fraction &f1);	//乘
    	void divide(fraction &f1);		//除
    	void compare(fraction &f1);	//比较大小
    	void input();
    	void output();
    
    private:
    	int top;
    	int bottom;
    };
    #endif // !FRACTION_H
    

    fraction.cpp文件

    #include "fraction.h"
    #include <iostream>
    using namespace std;
    
    fraction::fraction(int t0,int b0):top(t0),bottom(b0)
    {}
    
    fraction::fraction(int t0):top(t0)
    {
    	bottom = 1;
    }
    fraction::fraction()
    {
    	top = 0;
    	bottom = 1;
    }
    
    fraction::~fraction()
    {}
    
    void fraction::add(fraction &f1)			//加
    {
    	cout << top * f1.bottom + f1.top * bottom
    		<< "/" << bottom * f1.bottom << endl;
    }
    
    void fraction::subtract(fraction &f1)		//减
    {
    	cout << top * f1.bottom - f1.top * bottom
    		<< "/" << bottom * f1.bottom << endl;
    }
    
    void fraction::multiply(fraction &f1)		//乘
    {
    	cout << top * f1.top << "/"
    		<< bottom * f1.bottom << endl;
    }
    
    void fraction::divide(fraction &f1)		//除
    {
    	cout << top * f1.bottom << "/"
    		<< bottom * f1.top << endl;
    }
    
    void fraction::compare(fraction &f1)		//比较大小
    {
    	if (top * f1.bottom > bottom * f1.top)
    		cout << top << "/" << bottom << endl;
    	else if (top * f1.bottom < bottom * f1.top)
    		cout << f1.top << "/" << f1.bottom << endl;
    	else if (top * f1.bottom == bottom * f1.top)
    		cout << "一样大" << endl;
    }
    
    void fraction::input()
    {
    	cin >> top >> bottom;
    }
    void fraction::output()
    {
    	cout << top << "/" << bottom << endl;
    }
    

    main.cpp文件

    // Fraction.cpp: 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include "fraction.h"
    using namespace std;
    
    int main()
    {
    	fraction a;
    	fraction b(3,4);
    	fraction c(5);
    
    	cout << "函数输出测试" << endl;
    	cout << "分数a为:";	a.output();
    	cout << "分数b为:";	b.output();
    	cout << "分数c为:";	c.output();
    	
    	cout << "加减乘除比较测试" << endl;
    	cout << "a + b = ";		a.add(b);
    	cout << "a - b = ";		a.subtract(b);
    	cout << "b * c = ";		b.multiply(c);
    	cout << "b / c = ";		b.divide(c);
    	cout << " a和b中较大的是:";	 a.compare(b);
    	cout << "c和c比较:";		c.compare(c);
    
    	cout << "请输入分数a(分子和分母中间以空格分隔):";
    	a.input();
    	cout << "a的大小为:";		a.output();
    
        return 0;
    }
    

    运行结果截图如下:

    关于fraction的选做任务没有做,不过有一些思路

    1.规范化处理:在进行加减乘除运算的时候,不采用cout<<分子<<分母一行输出的方法,先将分子和分母都暂存至临时变量a,b。然后对用if对判断,若b<0;则a变为-a
    2.分数化简:分子分母同时除以他们的最大公因数
    3.分数转为十进制:新建double类型变量,值为分子除以分母

  • 相关阅读:
    二分思想判断数组中是否有两数和为sum
    VC中#pragma warning指令
    (转)预编译头文件
    成为一名优秀程序员所需要知道的那些事
    SetThreadAffinityMask设置使用多核CPU的哪个核心
    DirectX 3D 设备丢失(lost device)的处理
    转载 CreateWaitableTimer和SetWaitableTimer函数
    转赵青《剑侠情缘网络版》开发回顾
    转载使用PostThreadMessage在Win32线程间传递消息
    VC使用CRT调试功能检测内存泄漏
  • 原文地址:https://www.cnblogs.com/MrWang-nextdoor/p/8908847.html
Copyright © 2020-2023  润新知