实验三
graph.cpp:
#include "graph.h" #include <iostream> using namespace std; Graph::Graph(char ch, int n): symbol(ch), size(n) { } void Graph::draw() { int length=2*size-1,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; } }
graph.h:
#ifndef GRAPH_H #define GRAPH_H class Graph { public: Graph(char ch, int n); void draw(); private: char symbol; int size; }; #endif
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.cpp
#include "fraction.h" #include<iostream> using namespace std; void fraction::add(fraction a , fraction b){ fraction c; c.shang=a.shang*b.xia+b.shang*a.xia; c.xia=a.xia*b.xia; cout<<"两数相加:"<<c.shang<<"/"<<c.xia<<endl; } void fraction::min(fraction a , fraction b){ fraction c; c.shang=a.shang*b.xia-b.shang*a.xia; c.xia=a.xia*b.xia; cout<<"两数相减:"<<c.shang<<"/"<<c.xia<<endl; } void fraction::mul(fraction a , fraction b){ fraction c; c.shang=a.shang*b.shang; c.xia=a.xia*b.xia; cout<<"两数相乘:"<<c.shang<<"/"<<c.xia<<endl; } void fraction::div(fraction a , fraction b){ fraction c; c.shang=a.shang*b.xia; c.xia=a.xia*b.shang; cout<<"两数相除:"<<c.shang<<"/"<<c.xia<<endl; } void fraction::compare(fraction a , fraction b){ int c; c=a.shang*b.xia-b.shang*a.xia; if (c<0) cout<<"两数相较:"<<a.shang<<"/"<<a.xia<<"<"<<b.shang<<"/"<<b.xia<<endl; else if (c>0) cout<<"两数相较:"<<a.shang<<"/"<<a.xia<<">"<<b.shang<<"/"<<b.xia<<endl; else cout<<"两数相较:"<<a.shang<<"/"<<a.xia<<"="<<b.shang<<"/"<<b.xia<<endl; } void fraction::show(){ cout<<shang<<"/"<<xia<<endl; }
#ifndef FRACTION_H #define FRACTION_H class fraction{ public: fraction(int shang0 = 0 , int xia0 = 1):shang(shang0),xia(xia0){} 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 shang; int xia; }; #endif
#include<iostream> #include"Fraction.h" using namespace std; int main(){ fraction a; a.show(); fraction b(3,4); b.show(); fraction c(5); c.show(); fraction e; e.add(b,c); e.min(b,c); e.mul(b,c); e.div(b,c); e.compare(b,c); }