实验结论:
1)Part1 验证性内容:以多文件结构组织的项目文件示例:在画布上可以上下左右移动的小球
Ctrl C+V之后编译运行出现了如下界面:
(假装有图片)
2)Part2 基于已有信息,补足并扩充程序。 在graph文件夹里提供有三个文件: graph.h (类Graph的声明) graph.cpp (类Graph的实现) main.cpp (类Graph的测试: 定义Graph类对象,调用绘图接口绘制图形)
graph.h
#if !defined GRAPH_H #define GRAPH_H class Graph { public: Graph(char ch, int n); void draw(); private: char symbol; int size; }; #endif
graph.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(); system("pause"); return 0; }
main.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,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; } }
(继续假装有图片)
3)Part3 基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。 具体要求如下: 设计一个分数类 Fraction描述分数(两个整数的比值)
Fraction.h
#if !defined(FRACTION_H) #define FRACTION_H #include<iostream> #include<stdlib.h> using namespace std; namespace std{ class Fraction{ private: long num,den; public: Fraction(long p=0,long q=1); friend istream& operator>>(istream& istr,Fraction& x); Fraction operator+(Fraction& x)const; Fraction operator-(Fraction& x)const; Fraction operator*(Fraction& x)const; Fraction operator/(Fraction& x)const; Fraction operator-()const; int operator<(Fraction& x)const; int operator<=(Fraction& x)const; int operator>(Fraction& x)const; int operator>=(Fraction& x)const; int operator==(Fraction& x)const; int operator!=(Fraction& x)const; int getnum()const{ return num;} int getden()const{ return den;} void standardize(void); long gcd(long m,long n)const; void reduce(); }; Fraction::Fraction(long p,long q):num(p),den(q){ if(den==0){ cerr<<"分母不能为 0 !"<<endl; exit(1); } standardize(); reduce(); } void Fraction::standardize(){ if(den<0){ num=-num; den=-den; } } long Fraction::gcd(long m,long n)const{ static long x; if(m>n){ if(n==0) x=m; else gcd(n,m%n); } else{ if(m==0) x=n; else gcd(m,n%m); } return x; } istream& operator>>(istream& istr,Fraction& x){ char c; istr>>x.num>>c>>x.den; if(x.den==0){ cerr<<"分母不为 0 !"<<endl; exit(1); } x.standardize(); x.reduce(); return istr; } ostream& operator<<(ostream& ostr,const Fraction& x){ ostr<<x.getnum()<<'/'<<x.getden(); return ostr; } Fraction Fraction::operator+(Fraction& x)const{ Fraction temp=Fraction(num*x.den+den*x.num,den*x.den); temp.reduce(); return temp; } Fraction Fraction::operator-(Fraction& x)const{ Fraction temp=Fraction(num*x.den-den*x.num,den*x.den); temp.reduce(); return temp; } Fraction Fraction::operator*(Fraction& x)const{ Fraction temp=Fraction(num*x.num,den*x.den); temp.reduce(); return temp; } Fraction Fraction::operator/(Fraction& x)const{ Fraction temp=Fraction(num*x.den,den*x.num); temp.standardize(); temp.reduce(); return temp; } Fraction Fraction::operator-()const{ return Fraction(-num,den); } int Fraction::operator<(Fraction& x)const{ return(num*x.getden()<den*x.getnum()); } int Fraction::operator<=(Fraction& x)const{ return(num*x.getden()<=den*x.getnum()); } int Fraction::operator==(Fraction& x)const{ return(num*x.getden()==den*x.getnum()); } int Fraction::operator!=(Fraction& x)const{ return(num*x.getden()!=den*x.getnum()); } int Fraction::operator>(Fraction& x)const{ return(num*x.getden()>den*x.getnum()); } int Fraction::operator>=(Fraction& x)const{ return(num*x.getden()>=den*x.getnum()); } void Fraction::reduce(){ long bigdivisor,tempnumerator; tempnumerator=(num<0)?-num:num; if(num==0) den=1; else{ bigdivisor=gcd(tempnumerator,den); if(bigdivisor>1){ num/=bigdivisor; den/=bigdivisor; } } } /* 加加改改 可以输出带分数 void print(Fraction x){ long wholepart=long(x.getnum()/x.getden()); Fraction fractionpart=x-Fraction(wholepart); if(fractionpart==Fraction(0L)) cout<<wholepart<<" "; else{ fractionpart.reduce(); if(wholepart<0) fractionpart=-fractionpart; if(wholepart!=0) cout<<wholepart<<" "<<fractionpart<<" "; else cout<<fractionpart<<" "; } } */ } #endif
main.cpp
#include"Fraction.h" int main(){ Fraction r1,r2; cout<<"输入两个分数:"<<endl; cin>>r1>>r2; if(r1<r2) cout<<r1<<"<"<<r2<<endl; else if(r1==r2) cout<<r1<<"=="<<r2<<endl; else cout<<r1<<">"<<r2<<endl; cout<<"和为: "<<r1+r2<<endl; cout<<"积为: "<<r1*r2<<endl; cout<<"差为: "<<r1-r2<<endl; cout<<"商为: "<<r1/r2<<endl; return 0; }
实验总结:
1.需要掌握多文件组织结构
2.需要掌握运算符的重载
3.对于类的接口,也最好稍微注意一下
4.我是弟弟,是弟弟,前面都在Ctrl C+V,第三题分数的类,是看了一本书上的代码。