实验4
Posted flyingbrid-nest
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验4相关的知识,希望对你有一定的参考价值。
Graph
graph.h
#ifndef GRAPH_H_ #define GRAPH_H_ class graph { public: graph(char c, int num); void draw()const; void resetdata(char c, int num); ~graph(); private: char ch; int number; }; #endif // !GRAPH_H_
graph.cpp
#include "stdafx.h" #include "graph.h" #include<iostream> #include<iomanip> using namespace std; graph::graph(char c,int num):ch(c),number(num){} graph::~graph(){} void graph::draw()const { for(int i=0;i<number;i++) { int wid = (2*(number-i)+1)/2; cout << setfill(‘ ‘) << setw(wid); for (int j = 0; j <1+2*i; j++) { cout << ch; } cout << endl; } } void graph::resetdata(char c, int num) { ch = c; number = num; this->draw(); }
main.cpp
#include "stdafx.h" #include"graph.h" int main() { graph g1(‘&‘, 7); g1.draw(); g1.resetdata(‘*‘, 5); return 0; }
Fraction
刚好之前学了运算符重载,就在这里当复习了。
fraction.h
#ifndef FRACTION_H_ #define FRACTION_H_ class Fraction { int top, bottom; public: Fraction(int t=0, int b = 1); ~Fraction(); Fraction operator +(Fraction &f);//重载“+”运算符,用于计算两个分数相加 friend Fraction operator +( int num, Fraction &f);//重载“+”运算符,用于一个整数与分数相加,为非成员函数 Fraction operator -(Fraction &f);//重载“-”运算符,用于计算两个分数相减 friend Fraction operator -( int num, Fraction &f);//重载“-”,用于一个整数减去一个分数,非成员函数 Fraction operator *(Fraction &f);//重载“*” friend Fraction operator *( int num, Fraction &f);//重载“*”,整数乘分数,非成员函数 Fraction operator /(Fraction &f);//重载“/” friend Fraction operator /(int num, Fraction &f);//重载“/”,整数除分数,非成员函数 bool operator <( Fraction &f);//重载“<” friend bool operator <( int num, Fraction &f);//重载“<”,整数与分数比较,非成员函数 bool operator >( Fraction &f);//重载“>” friend bool operator >( int num, Fraction &f);//重载“>”,整数与分数比较,非成员函数 void setdata();//重新设置 void display();//显示分数 }; #endif // !FRACTION_H_
fraction.cpp
#include "stdafx.h" #include "Fraction.h" #include<iostream> #include<cmath> using namespace std; int Common_divisor(int top, int bottom)//用于求公约数 { int common_divisor; while (1) { if (top%bottom == 0) { return bottom; } else { common_divisor= bottom; bottom = top % common_divisor; top = common_divisor; } } return common_divisor; } bool limited(int bottom) { //用来判断一个分数是否可以化为有限小数输出 if (bottom == 1) { return true; } else if(bottom % 2 == 0) { return limited(bottom / 2); } else if (bottom % 5 == 0) { return limited(bottom / 5); } else { return false; } } //在构造分数时,直接按照要求进行修整,方便后面计算 Fraction::Fraction(int t,int b):top(t),bottom(b){ if (top < 0 && bottom < 0)//分子分母都小于零,去符号 { top = -top; bottom = -bottom; } if (bottom < 0 && top>0)//分母小于零,分子大于零,交换符号 { top = -top; bottom = -bottom; } int temp_t, temp_b; if (top < 0)//避免负数进行%运算时的干扰 { temp_t = -top; temp_b = bottom; } else { temp_t = top; temp_b = bottom; } bottom = bottom / Common_divisor(temp_t,temp_b); top = top / Common_divisor(temp_t,temp_b); } //加减乘除操作基本相同,分母不同通分,找公约数,最简化 Fraction Fraction::operator+ (Fraction &f) { int temp_b,temp_t,temp_t_b,temp_t_t;//temp_b,temp_t用来储存通分后的分子分母,temp_t_b,temp_t_t用于计算公约数 if (bottom != f.bottom)//分母不同时,通分 { temp_b= bottom * f.bottom; temp_t=top * f.bottom+f.top*bottom; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; temp_t_b = temp_b; } else { temp_t_t = temp_t; temp_t_b = temp_b; } bottom =temp_b/ Common_divisor(temp_t_t,temp_t_b);//通分后分母除于公约数 top = temp_t/ Common_divisor(temp_t_t,temp_t_b);//通分后分子除于公约数 } else {//分母相同,分子直接相加 temp_b = bottom; temp_t = top+f.top; if (temp_t < 0) { temp_t_t = -temp_t; temp_t_b = temp_b; } else { temp_t_t = temp_t; temp_t_b = temp_b; } bottom = bottom/ Common_divisor(temp_t_t,temp_t_b); top = temp_t/ Common_divisor(temp_t_t,temp_t_b); } return Fraction(top, bottom); } Fraction operator +(int num, Fraction &f) { int temp_b, temp_t, temp_t_t; //temp_t用来储存通分后的分子,temp_b, temp_t_t用于计算公约数 temp_b = f.bottom; temp_t = num * f.bottom + f.top; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; } else { temp_t_t = temp_t; } return Fraction(temp_t / Common_divisor(temp_t_t, temp_b), temp_b / Common_divisor(temp_t_t, temp_b)); } Fraction Fraction::operator -(Fraction &f) { int temp_b, temp_t, temp_t_b, temp_t_t; //temp_b, temp_t用来储存通分后的分子分母,temp_t_b, temp_t_t用于计算公约数 if (bottom != f.bottom) { temp_b = bottom * f.bottom; temp_t = top * f.bottom - f.top*bottom; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; temp_t_b = temp_b; } else { temp_t_t = temp_t; temp_t_b = temp_b; } bottom = temp_b / Common_divisor(temp_t_t,temp_t_b); top = temp_t / Common_divisor(temp_t_t,temp_t_b); } else { temp_b = bottom; temp_t = top - f.top; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; temp_t_b = temp_b; } else { temp_t_t = temp_t; temp_t_b = temp_b; } bottom = bottom / Common_divisor(temp_t_t,temp_t_b); top = (top - f.top) / Common_divisor(temp_t_t,temp_t_b); } return Fraction(top, bottom); } Fraction operator -(int num, Fraction &f) { int temp_b, temp_t, temp_t_t; //temp_t用来储存通分后的分子,temp_b, temp_t_t用于计算公约数 temp_b = f.bottom; temp_t = num * f.bottom - f.top; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; } else { temp_t_t = temp_t; } return Fraction(temp_t / Common_divisor(temp_t_t, temp_b), temp_b / Common_divisor(temp_t_t, temp_b)); } Fraction Fraction::operator *(Fraction &f) { int temp_b, temp_t, temp_t_b, temp_t_t;//temp_b,temp_t用来互乘后分子分母,temp_t_b,temp_t_t用于计算公约数 temp_b = bottom * f.bottom; temp_t = top * f.top; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; temp_t_b = temp_b; } else { temp_t_t = temp_t; temp_t_b = temp_b; } bottom = temp_b / Common_divisor(temp_t_t,temp_t_b); top = temp_t / Common_divisor(temp_t_t,temp_t_b); return Fraction(top, bottom); } Fraction operator *(int num, Fraction &f) { int temp_b, temp_t, temp_t_t; //temp_t用来储存通分后的分子,temp_b, temp_t_t用于计算公约数 temp_b = f.bottom; temp_t = num *f.top; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; } else { temp_t_t = temp_t; } return Fraction(temp_t / Common_divisor(temp_t_t, temp_b), temp_b / Common_divisor(temp_t_t, temp_b)); } Fraction Fraction::operator / (Fraction &f) { int temp_b, temp_t, temp_t_b, temp_t_t;//temp_b,temp_sum用来储存乘倒数后的分子分母,temp_t_b,temp_t_sum用于计算公约数 temp_t = top * f.bottom; temp_b = bottom * f.top; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; temp_t_b = temp_b; } else { temp_t_t = temp_t; temp_t_b = temp_b; } bottom = temp_b / Common_divisor(temp_t_t,temp_t_b); top = temp_t / Common_divisor(temp_t_t,temp_t_b); return Fraction(top, bottom); } Fraction operator / (int num, Fraction &f){ int temp_b, temp_t, temp_t_t; //temp_t用来储存通分后的分子,temp_b, temp_t_t用于计算公约数 temp_b = f.top; temp_t = num * f.bottom; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; } else { temp_t_t = temp_t; } return Fraction(temp_t / Common_divisor(temp_t_t, temp_b), temp_b / Common_divisor(temp_t_t, temp_b)); } bool Fraction::operator > (Fraction &f){ if (bottom == f.bottom) { if (top > f.top) { return true; } else { return false; } } else { int temp_t, temp_ft; temp_t = top * f.bottom; temp_ft = f.top*bottom; if (temp_t > temp_ft) { return true; } else { return false; } } } bool operator >(int num,Fraction &f){ if (num*f.bottom > f.top) { return true; } else { return false; } } bool Fraction::operator < (Fraction &f) { if (bottom == f.bottom) { if (top <f.top) { return true; } else { return false; } } else { int temp_t, temp_ft; temp_t = top * f.bottom; temp_ft = f.top*bottom; if (temp_t < temp_ft) { return true; } else { return false; } } } bool operator <(int num,Fraction &f){ if (num*f.bottom < f.top) { return true; } else { return false; } } void Fraction::setdata() { cout << "please enter top:"; cin >> top; cout << "please enter bottom:"; cin >> bottom; if (top < 0 && bottom < 0) { top = -top; bottom = -bottom; } if (bottom < 0 && top>0) { top = -top; bottom = -bottom; } int temp_t, temp_b,temp_t_t,temp_t_b; temp_t = top; temp_b = bottom; if (temp_t < 0)//防止负数干扰求公约运算 { temp_t_t = -temp_t; temp_t_b = temp_b; } else { temp_t_t = temp_t; temp_t_b = temp_b; } bottom = temp_b / Common_divisor(temp_t_t,temp_t_b); top = temp_t / Common_divisor(temp_t_t,temp_t_b); } void Fraction::display() { if (limited(bottom)) { double Frac_double; Frac_double = double(top) / double(bottom); cout << Frac_double << endl; } else { cout << top << "/" << bottom << endl; } } Fraction::~Fraction(){}
main.cpp
#include "stdafx.h" #include"Fraction.h" #include<iostream> using namespace std; int main() { Fraction f1(3, 4), f2(3, -7), f3(-5, -6), f4,f5(5,10),f6(9,3); cout << "f1 = "; f1.display(); cout << "f2 = "; f2.display(); cout << "f3 = "; f3.display(); cout << "f5 = "; f5.display(); cout << "f6 = "; f6.display(); cout << "f1 = f1 + f2 = "; f1 = f1 + f2; f1.display(); cout << "f5 = f5 - f6 = "; f5 = f5 - f6; f5.display(); cout << "f3 = f3 * f1 = "; f3 = f3 * f1; f3.display(); cout << "f1 = f1 / f5 = "; f1 = f1 / f5; f1.display(); cout << "比较f1与f2的大小。" << endl; if (f1 > f2) { cout << "较大的是f1:"; f1.display(); } else { cout << "较大的是f2:"; f2.display(); } cout << "f1 = 7 + f2 = "; f1 = 7 + f2; f1.display(); cout << "f2 = 3 - f6 = "; f2 = 3 - f6; f2.display(); cout << "f3 = 8 * f1 = "; f3 = 8 * f1; f3.display(); cout << "f4 = 11 / f5 = "; f4 = 11 / f5; f4.display(); cout << "比较6与f2的大小。" << endl; if (6 > f2) { cout << "较大的是6"; } else { cout << "较大的是f2:"; f2.display(); } return 0; }
以上是关于实验4的主要内容,如果未能解决你的问题,请参考以下文章
[NTUSTISC pwn LAB 7]Return to libc实验(puts泄露libc中gadget片段定位)