实验4
Posted ditongwoshang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验4相关的知识,希望对你有一定的参考价值。
1.Graph文件
(1)graph.h
源码:
1 class Graph{ 2 public: 3 Graph(char a,int l); 4 void draw(); 5 private: 6 char appoint; 7 int linage; 8 };
(2)类的实现.cpp
源码:
#include"Graph.h" #include<iostream> using namespace std; Graph::Graph(char a,int l):appoint(a),linage(l){ } void Graph::draw() { for(int i=1;i<=linage;i++) { for(int j=linage-i;j>0;j--) { cout<<" "; } for(int j=1;j<=2*i-1;j++) { cout<<appoint; } cout<<endl; } }
(3)main.cpp
源码:
#include <iostream> #include"Graph.h" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int main(int argc, char** argv) { Graph graph1(‘*‘,5); graph1.draw(); Graph graph2(‘$‘,7); graph2.draw(); return 0; }
运行结果:
关于draw的算法:
1.输入的数字代表有几行
2.第n行字符前的空格数为linage-1,输出空格
3.输出字符,第n行的字符个数为2*n-1个,换行。
2.Fraction文件
(1)Fraction.h
源码:
1 class Fraction{ 2 public: 3 Fraction(); 4 Fraction(int t); 5 Fraction(int t,int b); 6 int gettop(){return top;} 7 int getbottom(){return bottom;} 8 void plus(Fraction); 9 void minus(Fraction); 10 void multiply(Fraction); 11 void divide(Fraction); 12 void output(); 13 private: 14 int top; 15 int bottom; 16 };
(2)类的实现.cpp
源码:
1 #include"Fraction.h" 2 #include<iostream> 3 using namespace std; 4 Fraction::Fraction():top(0),bottom(1){ 5 } 6 Fraction::Fraction(int t):top(t),bottom(1){ 7 } 8 Fraction::Fraction(int t,int b):top(t),bottom(b){ 9 } 10 void Fraction::output(){ 11 cout<<top<<"/"<<"("<<bottom<<")"; 12 } 13 void Fraction::plus(Fraction f){ 14 int rtop,rbottom; 15 rtop=top*f.getbottom()+f.gettop()*bottom; 16 rbottom=bottom*f.getbottom(); 17 Fraction result(rtop,rbottom); 18 result.output(); 19 } 20 void Fraction::minus(Fraction f){ 21 int rtop,rbottom; 22 rtop=top*f.getbottom()-f.gettop()*bottom; 23 rbottom=bottom*f.getbottom(); 24 Fraction result(rtop,rbottom); 25 result.output(); 26 } 27 void Fraction::multiply(Fraction f){ 28 int rtop,rbottom; 29 rtop=top*f.gettop(); 30 rbottom=bottom*f.getbottom(); 31 Fraction result(rtop,rbottom); 32 result.output(); 33 } 34 void Fraction::divide(Fraction f){ 35 int rtop,rbottom; 36 rtop=top*f.getbottom(); 37 rbottom=bottom*f.gettop(); 38 Fraction result(rtop,rbottom); 39 result.output(); 40 }
(3)main.cpp
源码:
#include <iostream> #include"Fraction.h" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int main(int argc, char** argv) { Fraction a; Fraction b(3,4); Fraction c(5); b.plus(c); cout<<endl; b.minus(c); cout<<endl; b.multiply(c); cout<<endl; b.divide(c); return 0; }
运行结果:
以上是关于实验4的主要内容,如果未能解决你的问题,请参考以下文章
[NTUSTISC pwn LAB 7]Return to libc实验(puts泄露libc中gadget片段定位)