标签:cli include public amp none input cout ott min
1.graph类
#ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 private: char symbol; int size; }; #endif
// 类graph的实现 #include "graph.h" #include <iostream> using namespace std; // 带参数的构造函数的实现 Graph::Graph(char ch, int n) : symbol(ch), size(n) { } // 成员函数draw()的实现 // 功能:绘制size行,显示字符为symbol的指定图形样式 void Graph::draw() { int i, j; for (i = 1;i <= size;i++) { for (j = 0;j < size - i;j++) cout << " "; for (j = 1;j < 2 * i;j++) cout << symbol; cout << endl; } }
#include <iostream> #include "graph.h" #include <cstdlib> using namespace std; int main() { Graph graph1(‘*‘, 5); graph1.draw(); system("pause"); system("cls"); Graph graph2(‘$‘, 7); graph2.draw(); system("pause"); return 0; }
2.分数运算
#ifndef FRACTION_H #define FRACTION_H #include<iostream> class Fraction { public: Fraction(int ntop=0, int nbottom=1); void add(Fraction f1, Fraction f2);//f1+f2结果存放在f3 void minus(Fraction f1, Fraction f2);//f1-f2结果存放在f3 void mult(Fraction f1, Fraction f2);//f1*f2结果存放在f3 void divide(Fraction f1, Fraction f2);//f1/f2结果存放在f3 void simplify();//将f1化简 void input();//分数的输入 void output();//输出 void compare(Fraction f1, Fraction f2);//比较f1,f2的大小并输出 private: int top; int bottom; }; #endif
#include<iostream> #include"fraction.h" using std::cout; using std::cin; using std::endl; Fraction::Fraction(int ntop, int nbottom):top(ntop),bottom(nbottom) { } void Fraction::add(Fraction f1, Fraction f2) { Fraction f3; int top1,top2,bottom1; bottom = f1.bottom*f2.bottom; top1 = f1.top*f2.bottom; top2 = f2.top*f1.bottom; f3.top = top1 + top2; f3.bottom = bottom; f3.simplify(); top = f3.top; bottom = f3.bottom; } void Fraction::minus(Fraction f1, Fraction f2) { Fraction f3; int top1, top2, bottom1; bottom = f1.bottom*f2.bottom; top1 = f1.top*f2.bottom; top2 = f2.top*f1.bottom; f3.top = top1 - top2; f3.bottom = bottom; f3.simplify(); top = f3.top; bottom = f3.bottom; } void Fraction::mult(Fraction f1, Fraction f2) { Fraction f3; f3.top = f1.top*f2.top; f3.bottom = f1.bottom*f2.bottom; f3.simplify(); top = f3.top; bottom = f3.bottom; } void Fraction::divide(Fraction f1, Fraction f2) { if (f2.top == 0) cout << "error" << endl; else { Fraction f3; f3.top = f1.top*f2.bottom; f3.bottom = f1.bottom*f2.top; f3.simplify(); top = f3.top; bottom = f3.bottom; } } void Fraction::simplify() { int i,a=1; for (i = 1;i <= top;i++) { if (top%i == 0 && bottom%i == 0) a = i; } top /= a; bottom /= a; } void Fraction::input() { cout << "input top:"; cin >> top; cout << "input bottom:"; cin >> bottom; } void Fraction::output() { cout << top << "/" << bottom << endl; } void Fraction::compare(Fraction f1, Fraction f2) { if (f1.top*f2.bottom < f1.bottom*f2.top) { f1.output(); cout << "<"<<endl; f2.output(); cout << endl; } else if (f1.top*f2.bottom > f1.bottom*f2.top) { f1.output(); cout << ">"<<endl; f2.output(); cout << endl; } else if (f1.top*f2.bottom == f1.bottom*f2.top) { f1.output(); cout << "="<<endl; f2.output(); cout << endl; } }
#include<iostream> #include"fraction.h" #include<cstdlib> using namespace std; int main() { //加法 Fraction f1, f2, f3; cout << "add f1 and f2" << endl; cout << "f1:" << endl; f1.input(); cout << "f2:" << endl; f2.input(); f3.add(f1, f2); f3.output(); system("pause"); system("cls"); //减法 cout << "f1 minus f2" << endl; cout << "f1:" << endl; f1.input(); cout << "f2:" << endl; f2.input(); f3.minus(f1, f2); f3.output(); system("pause"); system("cls"); //乘法 cout << "f1 multiply f2" << endl; cout << "f1:" << endl; f1.input(); cout << "f2:" << endl; f2.input(); f3.mult(f1, f2); f3.output(); system("pause"); system("cls"); //除法 cout << "f1 divide f2" << endl; cout << "f1:" << endl; f1.input(); cout << "f2:" << endl; f2.input(); f3.divide(f1, f2); f3.output(); system("pause"); system("cls"); //比较大小 cout << "compare f1 and f2" << endl; cout << "f1:" << endl; f1.input(); cout << "f2:" << endl; f2.input(); f3.compare(f1, f2); system("pause"); system("cls"); }
标签:cli include public amp none input cout ott min
原文地址:https://www.cnblogs.com/Yyaoyyy/p/10751510.html