zoukankan      html  css  js  c++  java
  • 实验4 类与对象2)

    #include<iostream> 
    using namespace std;
    
    // 类Graph的声明 
    class Graph {
    	public:
    		Graph(char ch, int n);   // 带有参数的构造函数 
    		void draw(); 	// 绘制图形 
    	private:
    		char symbol;
    		int size;
    };
    
    // 类graph的实现
    
    
    // 带参数的构造函数的实现
    Graph::Graph(char ch, int n): symbol(ch), size(n) {
    }
    
    
    // 成员函数draw()的实现
    // 功能:绘制size行,显示字符为symbol的指定图形样式
    //       size和symbol是类Graph的私有成员数据
    void Graph::draw() {
    	// 补足代码,实现「实验4.pdf」文档中展示的图形样式
    	for(int j=1;j<=size;j++)  //j控制行 
    	{
    	   for(int i=1;i<=size-j+1;i++) cout<<' '; //i控制每行中的每个数 
    	   for(int i=1;i<=2*j-1;i++) cout<<symbol;
    	cout<<endl;
        }
    }
    
    
    
    int main() {
    	Graph graph1('*',5), graph2('$',7) ;  // 定义Graph类对象graph1, graph2 
    	graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 
    	graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
    	
    	return 0; 
    } 
    

    //graph.h
    #ifndef GRAPH_H
    #define GRAPH_H
    
    // 类Graph的声明 
    class Graph {
    	public:
    		Graph(char ch, int n);   // 带有参数的构造函数 
    		void draw(); 	// 绘制图形 
    	private:
    		char symbol;
    		int size;
    };
    
    #endif
    
    // 类graph的实现,graph.cpp
    
    #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 j=1;j<=size;j++)
    	{
    	for(int i=size;i>=1;i--) cout<<' ';
    	for(int i=1;i<=2*size-1;i++) cout<<symbol;
    	cout<<endl;
        }
    }
    
    //主函数,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()在屏幕上绘制图形
    
    	return 0;
    }
    

    2,

    #include<iostream> 
    using namespace std;
    class Fraction{
    	public:
    		Fraction();
    		Fraction(int t,int b);
    		Fraction(int b);
    		Fraction(Fraction &f0);
    		void show();
    		void add(Fraction &f0);
    		void min(Fraction &f0);
    		void mul(Fraction &f0);
    		void div(Fraction &f0);
    		void comp(Fraction &f0);
    		
    	private:
    		int top;
    		int bottom;
    };
    Fraction::Fraction():top(0),bottom(1){}
    Fraction::Fraction(int t,int b):top(t),bottom(b){}
    Fraction::Fraction(int b):top(1),bottom(b){}
    void Fraction::show(){
    	cout<<top<<"/"<<bottom<<endl;
    }
    void Fraction::add(Fraction &f0){
    	top=top*f0.bottom+f0.top*bottom;
    	bottom=bottom*f0.bottom;
    	show();
    }
    void Fraction::min(Fraction &f0){
    	top=top*f0.bottom-f0.top*bottom;
    	bottom=bottom*f0.bottom;
    	show();
    }
    void Fraction::mul(Fraction &f0){
    	top=top*f0.top;
    	bottom=bottom*f0.bottom;
    	show();
    }
    void Fraction::div(Fraction &f0){
    	top=top*f0.bottom;
    	bottom=bottom*f0.top;
    	show();
    }
    void Fraction::comp(Fraction &f0){
    	double i,j;
    	i=top/bottom;
    	j=f0.top/f0.bottom;
    	if(i>j) cout<<i<<">"<<j;
    	else if(i<j) cout<<i<<"<"<<j;
        else cout<<i<<"="<<j; 
    }
    int main(){
    	Fraction a;
    	Fraction b(3,4);
    	Fraction c(5);
    	a.show();
    	b.show();
    	c.show();
    	b.add(c);
    	b.min(c);
    	b.mul(c);
    	b.div(c);
    	b.comp(c);
    	return 0;
    }
    

    我的运行结果,,,前面都正常,但是后面的减法用的是上面那个加法的结果,乘法用的是减法的,除法用的是乘法的,求教大佬,如何让上一函数的结果不影响到下面?
    还有一种《我不写参量对象f0,在各类函数中具体化对象运算,应该就不会这么烦吧。。。》

    对上面的错误进行修正(1.题目看错,要求Fraction c(5);出来的结果是5/1,我搞成1/5了。 2.comp比较的话,之前总展示5>0,发现是我直接展示分数值大小,而没有用show()显示的格式

    #include<iostream>
    using namespace std;
    int g,h;    //用来存放初始值,并且起到赋值更新的用法
    class Fraction{     //类的定义
        public:          //外部接口
            Fraction();    //默认构造函数
            Fraction(int t,int b);  //构造函数
            Fraction(int t);        //同上,但是类似于上面的重载。
            Fraction(Fraction &f0);  //复制构造函数--对象的应用方面
            void show();             //show函数--显示分数(格式)
            void add(Fraction &f0);  //以下都是用对象作为形参
            void min(Fraction &f0);
            void mul(Fraction &f0);
            void div(Fraction &f0);
            void comp(Fraction &f0);
    
        private:
            int top;
            int bottom;
    };
    //类的实现,加上函数变量赋初值,以下
    Fraction::Fraction():top(0),bottom(1){}
    Fraction::Fraction(int t,int b):top(t),bottom(b){}
    Fraction::Fraction(int t):top(t),bottom(1){}
    void Fraction::show(){
        cout<<top<<"/"<<bottom<<endl;
    }
    void Fraction::add(Fraction &f0){
     g=top;h=bottom;  //把top值中的3赋给g,bottom=4赋给h--用于下一函数运算前,把变量更新一下
        top=top*f0.bottom+f0.top*bottom; //通分--列个式子就知道
        bottom=bottom*f0.bottom;
        show();  //以分数形式输出
    
    }
    
    void Fraction::min(Fraction &f0){
    	top=g;bottom=h; //更新一下,就是top重新变回3,bottom变回4
    
        top=top*f0.bottom-f0.top*bottom;
        bottom=bottom*f0.bottom;
        show();
    }
    void Fraction::mul(Fraction &f0){
    	top=g;bottom=h;    //下面都是如此,不多说。
        top=top*f0.top;
        bottom=bottom*f0.bottom;
        show();
    }
    void Fraction::div(Fraction &f0){
    	top=g;bottom=h;
        top=top*f0.bottom;
        bottom=bottom*f0.top;
        show();
    }
    void Fraction::comp(Fraction &f0){
    	top=g;bottom=h;
        double i,j;  //为了比较两个数,设置i,j存放分数值 
        i=f0.top/f0.bottom;
        j=top/bottom;
        if(i>j) cout<<f0.top<<"/"<<f0.bottom<<">"<<top<<"/"<<bottom; //显示出格式。 
        else if(i<j) cout<<f0.top<<"/"<<f0.bottom<<"<"<<top<<"/"<<bottom;
        else cout<<f0.top<<"/"<<f0.bottom<<"="<<top<<"/"<<bottom;
    }
    int main(){
        Fraction a;
        Fraction b(3,4);
        Fraction c(5);
        a.show();
        b.show();
        c.show();
        b.add(c);
        b.min(c);
        b.mul(c);
        b.div(c);
        b.comp(c);
        return 0;
    }
    

  • 相关阅读:
    C++ primer 简读
    C++函数及类方法(待补充)
    C++参数传递-复制和引用
    c++动态分配内存
    C++内存与指针
    c++循环
    C++容器vector及迭代器对象iterator
    c++字符类型一些知识点
    CNN中卷积运算转化成矩阵乘法的实现——img2col+GEMM大矩阵乘法
    046 LeetCode go
  • 原文地址:https://www.cnblogs.com/lixiaoyu-Judy/p/8921959.html
Copyright © 2011-2022 走看看