实验4
Posted lzy-123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验4相关的知识,希望对你有一定的参考价值。
#ifndef GRAPH_H #define GRAPH_H class Graph { public: Graph(char ch, int n); void draw(); private: char symbol; int size; }; #endif
#include "graph.h" #include <iostream> using namespace std; Graph::Graph(char ch, int n): symbol(ch), size(n) { } void Graph::draw() { for(int i=1;i<=size;i++) { for(int k=size-i;k>=0;k--) { cout<<" "; } for(int j=0;j<2*i-1;j++) { if(j<2*i-2) cout<<symbol; if(j==2*i-2) cout<<symbol<<endl; } } }
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1(‘*‘,5), graph2(‘$‘,7) ; graph1.draw(); graph2.draw(); return 0; }
class Fraction { public: Fraction(int p=0,int q=1):top(p),bottom(q){};
void plus(Fraction &a); void minus(Fraction &a); void multi(Fraction &a); void divi(Fraction &a); void opera(int p,int q); void compare(Fraction &a); void output(); private: int top; int bottom; };
#include"Fraction.h" #include<iostream> using namespace std; void Fraction::plus(Fraction &a) { top=a.top*bottom+a.bottom*top; bottom=a.bottom*bottom; opera(top,bottom);output(); } void Fraction::minus(Fraction &a) { top=a.top*bottom-a.bottom*top; bottom=a.bottom*bottom; opera(top,bottom);output(); } void Fraction::multi(Fraction &a) { top=a.top*top; bottom=a.bottom*bottom; opera(top,bottom);output(); } void Fraction::divi(Fraction &a) { top=a.top*bottom; bottom=a.bottom*top; opera(top,bottom);output(); } void Fraction::opera(int p,int q) { int i; for(i=p;i>0;i--) { if(p%i==0&&q%i==0) { break; } } if(q<0) { i=i*(-1); } top=top/i; bottom=bottom/i; } void Fraction::compare(Fraction &a) { int s=a.top*bottom-a.bottom*top; if(s>0) cout<<"a>b"<<endl; else if(s<0) cout<<"a<b"<<endl; else cout<<"a=b"<<endl; } void Fraction::output() { cout<<"The result is:"<<top<<"/"<<bottom<<endl; }
#include"Fraction.h" #include <iostream> using namespace std; int main() { int x,y; Fraction a;a.output(); Fraction b(3,4);b.output(); Fraction c(5);c.output(); b.compare(c); cout<<"Please input the top and the bottom:"; cin>>x>>y; Fraction d(x,y);d.opera(x,y);d.output(); return 0; }
以上是关于实验4的主要内容,如果未能解决你的问题,请参考以下文章
[NTUSTISC pwn LAB 7]Return to libc实验(puts泄露libc中gadget片段定位)