标签:指定 知识 tom names code com .com iostream mes
#ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 private: char symbol; int size; }; #endif
// 类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=1;i<=size;i++) { for(j=1;j<=size-i;j++) cout<<" "; for(k=1;k<=2*i-1;k++) cout<<symbol; cout<<endl; } }
#include <iostream> #include "graph.h" using namespace std; int main(){ Graph graph1(‘*‘,5); graph1.draw(); system("pause"); Graph graph2(‘$‘,7); graph2.draw(); return 0; }
#ifndef FRACTION_H #define FRACTION class Fraction{ public: Fraction(int top0,int bottom0); Fraction(); static Fraction add(Fraction a,Fraction b); static Fraction sub(Fraction a,Fraction b); static Fraction mul(Fraction a,Fraction b); static Fraction div(Fraction a,Fraction b); static +void compare(Fraction a,Fraction b); void sim(); void input(); void output(); private: int top; int bottom; }; #endif
#include "fraction.h" #include <iostream> #include <cmath> using namespace std; Fraction::Fraction(int top0,int bottom0):top(top0),bottom(bottom0){ if(bottom0=0){ cout<<"Wrong!"; exit(0); } } Fraction::Fraction(){} Fraction Fraction::add(Fraction a,Fraction b){ Fraction add1; add1.bottom=a.bottom*b.bottom; add1.top=a.top*b.bottom+b.top*a.bottom; add1.sim(); return add1; }//加法 Fraction Fraction::sub(Fraction a,Fraction b){ Fraction sub1; sub1.bottom=a.bottom*b.bottom; sub1.top=a.top*b.bottom-b.top*a.bottom; sub1.sim(); return sub1; } //减法 Fraction Fraction::mul(Fraction a,Fraction b){ Fraction mul1; mul1.bottom=a.bottom*b.bottom; mul1.top=a.top*b.top; mul1.sim(); return mul1; } //乘法 Fraction Fraction::div(Fraction a,Fraction b){ Fraction div1; div1.bottom=a.bottom*b.top; div1.top=a.top*b.bottom; div1.sim(); return div1; } //除法 void Fraction::compare(Fraction a,Fraction b){ a.sim(); b.sim(); if(a.top*b.bottom>a.bottom*b.top) cout<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl; else if(a.top*b.bottom<a.bottom*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::sim(){ int i; if(top!=0){ for(i=fabs(top);i>=1;i--){ if(top%i==0&&bottom%i==0) break; } top/=i; bottom/=i; } if(bottom*top<0){ top=-fabs(top); bottom=fabs(bottom); } else if(bottom*top>0){ top=fabs(top); bottom=fabs(bottom); } } //化简 void Fraction::input(){ cin>>top>>bottom; } //输入 void Fraction::output(){ if(top!=0){ if(bottom!=1) cout<<top<<"/"<<bottom; else if(bottom==1) cout<<top; } else cout<<"0"; } //输出
#include <iostream> #include "fraction.h" using namespace std; int main(){ Fraction m,n; Fraction c1,c2,c3,c4; cout<<"Enter numbers:"; m.input(); n.input(); cout<<"m="; m.output(); cout<<endl; cout<<"n="; n.output(); cout<<endl; c1=Fraction::add(m,n); cout<<"m+n="; c1.output(); cout<<endl; c2=Fraction::sub(m,n); cout<<"m-n="; c1.output(); cout<<endl; c3=Fraction::mul(m,n); cout<<"m*n="; c3.output(); cout<<endl; c4=Fraction::div(m,n); cout<<"m/n="; c4.output(); cout<<endl; Fraction::compare(m,n); system("pause"); return 0; }
实验总结:第二题用到了一些以前C语言中循环的知识,第三题让我感到困难不能够独立完成,希望自己以后多练习。
标签:指定 知识 tom names code com .com iostream mes
原文地址:https://www.cnblogs.com/mzx1999/p/10759632.html