C++实验三

Part2

#ifndef GRAPH_H
#define GRAPH_H

// 类Graph的声明 
class Graph {
    public:
        Graph(char ch, int n);   // 带有参数的构造函数 
        void draw();     // 绘制图形 
    private:
        char symbol;
        int size;
};


#endif
graph.h
#include <iostream>
#include "graph.h"
using namespace std;
int main() {
    Graph graph1('*',5);
    graph1.draw();
    
    system("pause");
    system("cls");
    
    Graph graph2('$',7);
    graph2.draw();
    system("pause");

    return 0; 
} 
main.cpp
// 类graph的实现
 
#include "graph.h" 
#include <iostream>
using namespace std;

// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}
// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式 
void Graph::draw() {
 int i,j,k; 
 for( i=0;i<size;i++)
 {
  for( j=0;j<=size-i;j++)
     cout<<" ";
  for( k=0;k<2*i-1;k++)
     cout<<symbol;
  cout<<endl;
 }
}
graph.cpp

 

 Part3

 

#include <iostream>
#include "fraction.h"
using namespace std;
int main()
{
 Fraction a;
 cout<<"a="; 
 a.show();
 Fraction b(3,4);
 cout<<"b=";
 b.show();
 Fraction c(5);
 cout<<"c=";
 c.show();
 Fraction d;
 cout<<"+:"<<endl;
    d.add(a,b);
    cout<<"-:"<<endl;
    d.sub(a,c);
    cout<<"*:"<<endl;
    d.mul(b,c);
    cout<<"/:"<<endl;
    d.div(c,b);
    c.com(a);

 return 0;
}
main.cpp
#include<iostream>
#include"fraction.h"
using namespace std;
void Fraction::show(){
  cout << top << "/" << bottom << endl;
}
void Fraction::add(Fraction &a, Fraction &b){
    fraction c;
    c.top=a.top*b.bottom+b.top*a.bottom;
    c.bottom=a.bottom*b.bottom;
    cout<<"add: "<<c.top<<"/"<<c.bottom<<endl;    
}
void Fraction::sub(Fraction &a, Fraction &b){
    int comm,t,r,f1,f2;
    if(a.bottom<b.bottom)
    {
      t=a.bottom;
      a.bottom=b.bottom;
      b.bottom=t;
      }
    r=a.bottom%b.bottom;
    while(r!=0)
    {
      a.bottom=b.bottom;
      b.bottom=r;
      r=a.bottom%b.bottom;
      }
    comm=a.bottom*b.bottom/b.bottom;
    f1=a.top*(comm/a.bottom)-b.top*(comm/b.bottom);
    f2=comm;
    cout<<"sub: "<<f1<<"/"<<f2<<endl;    
}
void Fraction::mul(Fraction &a, Fraction &b){
    int f1,f2;
    f1=a.top*b.top;
    f2=a.bottom*b.bottom;
    cout<<"mul: "<<f1<<"/"<<f2<<endl;
}
void Fraction::div(Fraction &a, Fraction &b){
    int f1,f2;
    f1=a.top*b.bottom;
    f2=b.top*a.bottom;
    cout<<"div: "<<f1<<"/"<<f2<<endl;
}

void Fraction::com(Fraction &a, Fraction &b){
    int comm,t,r,f1,f2;
    if(a.bottom<b.bottom)
    {
      t=a.bottom;
      a.bottom=b.bottom;
      b.bottom=t;
      }
    r=a.bottom%b.bottom;
    while(r!=0)
    {
      a.bottom=b.bottom;
      b.bottom=r;
      r=a.bottom%b.bottom;
      }
    comm=a.bottom*b.bottom/b.bottom;
    f1=a.top*(comm/a.bottom)-b.top*(comm/b.bottom);
    if(f1<0)
      cout<<"com: "<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl;
    else if(f1>0)
            cout<<"com: "<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl;
         else
            cout<<"com: "<<a.top<<"/"<<a.bottom<<"="<<b.top<<"/"<<b.bottom<<endl;
}
fraction.cpp
#ifndef Fraction_H
#define Fraction_H
class Fraction {
    public:
        Fraction(int i=0, int j=1){
            top=i;
            bottom=j;
        }
        void add(Fraction &a, Fraction &b);
        void sub(Fraction &a, Fraction &b);
        void mul(Fraction &a, Fraction &b);
        void div(Fraction &a, Fraction &b); 
        void com(Fraction &a, Fraction &b);
        void show();
    private:
        int top;
        int bottom;
};
#endif
fraction.h

 

实验小结

程序运行不出来,改了好几次也不行,不知道是dev不好还是程序本身的问题,最后一道题找公倍数的方法不会简化的办法,所以只能用以前学过的c语言来写。

转载于:https://www.cnblogs.com/fifi1224/p/10753767.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.声明一个动物基类Animal,私有整型成员变量年龄age,请定义一个派生类Dog,在其成员函数SetAge(int n)中直接给age赋值,测试下看是否会出问题?如何解决? 2.设计一个单基继承的类层次程序,用Person类派生出Student类,增加属性学号index和年级level。Person类中至少有姓名name、年龄age等数据成员,以及构造函数、输出函数等,其余成员函数根据需要添加。在主函数中进行测试。 3.定义一个学生类Student和教师类Teacher,学生类有姓名name、学号index等数据成员,教师类有姓名name、工作证号workID、职称title、课程course、周学时hoursPerWeek等数据成员。再定义一个助教类TeachingAssistant,多继承于学生类和教师类,该类可以使用学生类的全部数据成员,以及教师类的课程和周学时数据成员。要求:每个类提供自定义的构造函数和析构函数,并通过同名函数ShowInfo来显示全部数据成员的值。在主函数中进行测试。 4.声明一个Person,包含姓名name和年龄age等私有数据成员以及相关的成员函数;由它派生出领导类Leader,包含职务position和部门department私有数据成员以及相关的成员函数;再由Person派生出工程师类Engineer,包含职务position和专业speciality私有数据成员以及相关的成员函数;再由Leader和Engineer类派生出主任工程师类Chairman。在主函数中测试各类对象初始化和信息输出,查看是否会出问题?如何解决?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值