part 2
#ifndef GRAPH_H 2 #define GRAPH_H 3 4 5 class Graph { 6 public: 7 Graph(char ch, int n); 8 void draw(); 9 private: 10 char symbol; 11 int size; 12 }; 13 14 15 #endif 16 17 18 #include "graph.h" 19 #include <iostream> 20 using namespace std; 21 Graph::Graph(char ch, int n): symbol(ch), size(n) { 22 } 23 24 void Graph::draw() { 25 int x=size*2-1; 26 int i,line; 27 for(line=1;line<=size;line++) 28 { 29 for(i=1;i<=x;i++) 30 { 31 if(i>size-line&&i<size+line) 32 cout<<symbol; 33 else cout<<" "; 34 } 35 cout<<endl; 36 } 37 } 38 39 #include <iostream> 40 #include "graph.h" 41 using namespace std; 42 43 int main() { 44 Graph graph1(‘*‘,5); 45 graph1.draw(); 46 47 system("pause"); 48 system("cls"); 49 50 Graph graph2(‘$‘,7); 51 graph2.draw(); 52 53 return 0; 54 }
part 1
part 3
#include<iostream> using namespace std; class Fraction { public: Fraction(int top0=0,int bottom0=1){ top=top0; bottom=bottom0; cout<<"top="<<top<<" "<<"bottom="<<bottom<<endl; } void show(); Compare(Fraction & c); private: int top; int bottom; }; void Fraction::show(){ int add,sub,mul,div; add=top+bottom; sub=top-bottom; mul=top*bottom; div=top/bottom; cout<<"add="<<add<<endl; cout<<"subtract="<<sub<<endl; cout<<"multiply="<<mul<<endl; cout<<"divide="<<div<<endl; cout<<"fraction="<<top<<"/"<<bottom<<endl; } Fraction::Compare(Fraction & c){ if(top/bottom>c.top/c.bottom) cout<<top/bottom<<">"<<c.top/c.bottom<<endl; else cout<<top/bottom<<"<"<<c.top/c.bottom<<endl; } int main() { Fraction a; a.show(); Fraction b(3,4); b.show(); Fraction c(5); c.show(); c.Compare(b); return 0; }
总结:本次实验主要是对多文件的调试和编辑,正确使用多文件的方式会使代码变得整洁清晰并且方便许多。
原文:https://www.cnblogs.com/suifeng823/p/10725816.html