标签:ica include turn return div inf red code amp
2.代码:
#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 = 0; i < size; i++) { for (int j = i; j < (size-1); j++) {cout << " ";} for (int j = 0; j < i; j++) {cout << symbol;} cout << symbol; for (int j = 0; j < i; j++) {cout << symbol;} for (int j = i; j < (size-1); j++) {cout << " ";} cout << endl; } }
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1(‘*‘,5), graph2(‘$‘,7) ; graph1.draw(); graph2.draw(); int a; if (cin >> a) { return 0; } }
结果:
3.代码:
class Fraction{ public: Fraction(int a=0, int b=1); void compare(Fraction &p); void add(Fraction &p); void subtraction(Fraction &p); void multiplication(Fraction &p); void division(Fraction &p); void reduction(); void show(); private: int top; int bottom; };
#include<iostream> #include"fraction.h" using namespace std; Fraction::Fraction(int a, int b):top(a),bottom(b){ } void Fraction::compare(Fraction &p){ if(p.top>top){ cout<<"前者<后者"<<endl; } else if(p.top<=top){ cout<<"前者>=后者"<<endl; } } void Fraction::add(Fraction &p){ int b1=bottom,b2=p.bottom; top=top*b2; bottom=bottom*b2; p.top=p.top*b1; p.bottom=p.bottom*b1; top=top+p.top; } void Fraction::subtraction(Fraction &p){ int b1=bottom,b2=p.bottom; top=top*b2; bottom=bottom*b2; p.top=p.top*b1; p.bottom=p.bottom*b1; top=top-p.top; } void Fraction::multiplication(Fraction &p){ top=top*p.top; bottom=bottom*p.bottom; } void Fraction::division(Fraction &p){ top=top*p.bottom; bottom=bottom*p.top; } void Fraction::reduction(){ int a; if(top%bottom==0) a=bottom; else if(bottom%top==0) a=top; else for(int i=1;i<(bottom/2)&&i<(top/2);i++) { if(bottom%i==0&&top%i==0) a=i; } top/=a; bottom/=a; } void Fraction::show(){ cout<<"结果:"<<top<<"/"<<bottom<<endl; }
#include<iostream> #include"fraction.h" using namespace std; int main() { int m,n,x,y; cout<<"分数1:"; cin>>m>>n; Fraction a(m,n); a.show(); cout<<"分数2:"; cin>>x>>y; Fraction b(x,y); b.show(); a.compare(b); char input; cout<<"输入+ - * %:"; cin>>input; switch(input){ case ‘+‘: a.add(b); break; case ‘-‘: a.subtraction(b); break; case ‘*‘: a.multiplication(b); break; case ‘%‘: a.division(b); break; } a.reduction(); a.show(); return 0; }
结果
负数的计算没能想出来.
标签:ica include turn return div inf red code amp
原文地址:https://www.cnblogs.com/krousar/p/8922048.html