2.图形
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1('*',5); graph1.draw(); system("pause"); system("cls"); Graph graph2('$',7); graph2.draw(); return 0; }
#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=0;i<size;i++) { for(j=1;j<=size+i;j++) { if(j<size-i) { printf(" "); } else { printf("%c",symbol); } } printf("\n"); } }
3.分数计算
#include <iostream> #include "未命名4.h" using namespace std; int main() { int j,k,l,m; cin>>j>>k>>l>>m; fs a(j,k); fs b(l,m); count c(a,b); c.add(); c.mul(); c.del(); c.divi(); c.bj(); }
#ifndef GRAPH_H #define GRAPH_H #include<iostream> using namespace std; class fs { public : fs(int a=0,int b=1) { x=a; y=b; } int x,y; private: }; class count { public : count(fs p3,fs p4) { p1=p3; p2=p4; } count(count &l); int add(); int del(); int mul(); int divi(); int bj(); fs p1,p2; private : }; #endif
#include "未命名4.h" #include <iostream> using namespace std; int count::add() { int i=0,j=0; i=(p1.x*p2.y+p2.x*p1.y); j=p2.y*p1.y; if(i==0) { j=0; } cout<<i<<"/"<<j<<endl; } int count::del() { int i=0,j=0; i=(p1.x*p2.y-p2.x*p1.y); j=p2.y*p1.y; if(i==0) { j=0; } cout<<i<<"/"<<j<<endl; } int count ::mul() { int i=0,j=0; i=(p1.x*p2.x); j=(p1.y*p2.y); if(i==0||j==0) { j=0; i=0; } cout<<i<<"/"<<j<<endl; } int count ::divi() { int i=0,j=0; i=(p1.y*p2.x); j=(p1.x*p2.y); if(i==0||j==0) { j=0; i=0; } cout<<i<<"/"<<j<<endl; } int count::bj() { if(p1.x*p2.y>p1.y*p2.x) { cout<<p1.x<<"/"<<p1.y<<">"<<p2.x<<"/"<<p2.y; } else if(p1.x*p2.y<p1.y*p2.x) { cout<<p1.x<<"/"<<p1.y<<"<"<<p2.x<<"/"<<p2.y; } else { cout<<p1.x<<"/"<<p1.y<<"="<<p2.x<<"/"<<p2.y; } }