标签:void add har .sh use out private for 比较
graph项目:
graph.h
#ifndef GRAPH_H #define GRAPH_H class Graph { public: Graph(char ch,int n); void draw(); private: char symbol; int size; }; #endif
graph.cpp
#include "graph.h" #include <iostream> using namespace std; Graph::Graph(char ch,int n):symbol(ch),size(n){ } void Graph::draw() { int i,j; for(i=1;i<=size;i++) {for(j=1;j<=size-i;j++) cout<<" "; for(j=1;j<=2*i-1;j++) cout<<symbol; cout<<endl; } }
main.cpp
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1(‘*‘,5); graph1.draw(); system("pause"); system("cls"); Graph graph2(‘$‘,7); graph2.draw(); return 0; }
Fraction项目:
fraction.h
#ifndef FRACTION_H #define FRACTION_H class Fraction{ public: Fraction(int top1=0,int bottom1=1):top(top1),bottom(bottom1) { } void add(Fraction a,Fraction b); void min(Fraction a,Fraction b); void mul(Fraction a,Fraction b); void div(Fraction a,Fraction b); void compare(Fraction a,Fraction b); void show(); private: int top; int bottom; }; #endif
fraction.cpp
#include "fraction.h" #include <iostream> using namespace std; void Fraction::add(Fraction a,Fraction b) { Fraction sum; sum.top=a.top*b.bottom+b.top*a.bottom; sum.bottom=a.bottom*b.bottom; cout<<"加:"<<sum.top<<"/"<<sum.bottom<<endl; } void Fraction::min(Fraction a,Fraction b) { Fraction min; min.top=a.top*b.bottom-b.top*a.bottom; min.bottom=a.bottom*b.bottom; cout<<"减:"<<min.top<<"/"<<min.bottom<<endl; } void Fraction::mul(Fraction a,Fraction b) { Fraction mul; mul.top=a.top*b.top; mul.bottom=a.bottom*b.bottom; cout<<"乘:"<<mul.top<<"/"<<mul.bottom<<endl; } void Fraction::div(Fraction a,Fraction b) { Fraction div; div.top=a.top*b.bottom; div.bottom=a.bottom*b.top; cout<<"除:"<<div.top<<"/"<<div.bottom<<endl; } void Fraction::compare(Fraction a,Fraction b) { if(a.bottom==b.bottom) {if(a.top>b.top) cout<<"两数比较:"<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl; else cout<<"两数比较:"<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl; } else {a.bottom=a.bottom*b.bottom; a.top=a.top*b.bottom; b.bottom=a.bottom*b.bottom; b.top=b.top*a.bottom; if(a.top>b.top) cout<<"两数比较:"<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl; else cout<<"两数比较:"<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl; } } void Fraction::show() { cout<<top<<"/"<<bottom<<endl; }
main.cpp
#include <iostream> #include "Fraction.h" using namespace std; int main() { Fraction a; a.show(); Fraction b(3,4); b.show(); Fraction c(5); b.show(); Fraction example; example.add(b,c); example.min(b,c); example.mul(b,c); example.div(b,c); example.compare(b,c); }
实验总结与体会:
通过本次实验,我对类的应用更加熟练,但是part1中让小球左右移动还是不知道如何编写,在改变x和y的坐标的过程中遇到瓶颈,看了同学的程序还是不明白如何实现。
part3中将分数最简化也还未做到,我会再尝试一下的。
标签:void add har .sh use out private for 比较
原文地址:https://www.cnblogs.com/mzy-1229/p/10745732.html