实验4
实验结论
实验内容2
graph.h
#ifndef GRAPH_H #define GRAPH_H class Graph { public: Graph(char ch, int t); void draw(); private: char symbol; int size; }; #endif
graph.cpp
#include "graph.h" #include <iostream> using namespace std; Graph::Graph(char ch, int t): symbol(ch), size(t) { } void Graph::draw() { int i,j,k; for(i=1;i<=size;i++) { for(k=size-i;k>=0;k--) { cout<<" "; } for(j=0;j<2*i-1;j++) { if(j<2*i-2) cout<<symbol; if(j==2*i-2) cout<<symbol<<endl; } } }
main.cpp
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1('*',5), graph2('$',7) ; graph1.draw(); graph2.draw(); return 0; }

实验内容3
fraction.h
#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 &f); void multiply(Fraction &f); void divide(Fraction &f); void compare(Fraction &f); void input(); void output(); private: int top; int bottom; }; #endif
fraction.cpp
using namespace std; Fraction::Fraction(int t,int b):top(t),bottom(b){ } Fraction::Fraction(int t):top(t) { bottom = 1; } Fraction::Fraction() { top = 0; bottom = 1; } Fraction::~Fraction(){ } void Fraction::add(Fraction &f) //加 { cout << top * f.bottom + f.top * bottom << "/" << bottom * f.bottom << endl; } void Fraction::subtract(Fraction &f) //减 { cout << top * f.bottom - f.top * bottom << "/" << bottom * f.bottom << endl; } void Fraction::multiply(Fraction &f) //乘 { cout << top * f.top << "/" << bottom * f.bottom << endl; } void Fraction::divide(Fraction &f) //除 { cout << top * f.bottom << "/" << bottom * f.top << endl; } void Fraction::compare(Fraction &f) //比较大小 { if (top * f.bottom > bottom * f.top) cout << top << "/" << bottom << endl; else if (top * f.bottom < bottom * f.top) cout << f.top << "/" << f.bottom << endl; else if (top * f.bottom == bottom * f.top) cout << "一样大" << endl; } void Fraction::input() { cin >> top >> bottom; } void Fraction::output() { cout << top << "/" << bottom << endl; }
main.cpp
#include <iostream> #include "fraction.h" using namespace std; int main() { Fraction a; Fraction b(3,4); Fraction c(5); 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; }

实验总结与体会
c++的学习很困难,需要花时间钻研
浙公网安备 33010602011771号