zoukankan      html  css  js  c++  java
  • C++程序设计实验-4

     

    南京信息工程大学实验报告

    实验名称 C++简单程序设计-1 实验日期 2018-4-16 得分  指导教师  耿学华     

       计软院   专业计科    年级  2016     班次  (2)  姓名    余佳奇    学号  20161326022    

     一、实验结论

    1.创建多文件结构工程 4-20

    点击文件,新建项目,在项目中添加complex.h complex.cpp main.cpp文件。

    2. 实验内容 2

    (1)类 Graph 的成员 draw():假定有L行,第i行首先显示的空格数为L-i,然后显示2i-1个符号,再显示L-i个空格。,

    (2)Code:

    Graph.h

    1 class Graph{     
    2 public:
    3 Graph(char s,int l);
    4 void draw();
    5 private:
    6 char symbol;
    7 int line; 
    8 };

    Graph.cpp

     1 #include<iostream>
     2 #include"Graph.h"
     3 using namespace std;
     4 Graph::Graph(char s,int l){
     5 symbol=s;
     6 line=l;
     7 }
     8 void Graph::draw(){
     9 int i;
    10 for(i=1;i<=line;++i){
    11     int sp=line-i;
    12     int j;
    13     for(j=1;j<=sp;++j)
    14         cout<<" ";
    15     for(j=1;j<=2*i-1;++j)
    16         cout<<symbol;   17         cout<<endl;
    18 }
    19 return ;
    20 }

    Main.cpp

    1 #include "Graph.h"
    2 int main(){
    3 Graph graph1('*',5);
    4 graph1.draw();
    5 Graph graph2('$',7);
    6 graph2.draw();
    7 return 0;
    8 }

    (3)

    运行环境 DevC++5.11

    运行结果:

     

    3. 实验内容 3

    (1)类 Fraction 的类图

                                                  Fraction

    #top:int

    #bottom:int

    <<create>>-fraction()

    <<create>>-fraction(num:int)

    <<create>>-fraction(num:int,

    +gcd(t,b):int

    +simplify():void

    +add(c0:fraction):void

    +subtract(c0:fraction):void

    +multiple(c0:fraction):void

    +divide(co:fraction):void

    +compare(c0:fraction):void

    +readln():void

    +writeln():void

    (2)code:

    Fraction.h

     1 #include<iostream>
     2 #include<cmath>
     3 using namespace std;
     4 class Fraction
     5 {
     6 private:
     7 int top;
     8 int bottom;
     9 public:
    10 Fraction(int t, int b);
    11 Fraction(int t);
    12 Fraction();
    13 int gcd(int t, int b);
    14 void simplify();
    15 void add(Fraction c0);
    16 void subtract(Fraction c0);
    17 void multiple(Fraction c0);
    18 void divde(Fraction c0);
    19 void compare(Fraction c0);
    20 void readln();
    21 void writeln();
    22 };

    Fraction.cpp

     1 #include"Fraction.h" 
     2 Fraction::Fraction(int t, int b) {               //定义分数对象时,如果提供两个初始化参数,代表分数3/4// 
     3 top = t; 
     4 bottom = b;
     5 }
     6 Fraction::Fraction(int t) {                      //定义分数对象时,如果提供一个初始化参数,代表分数5/1// 
     7 top = t;
     8 bottom = 1;
     9 }
    10 Fraction::Fraction() {                          //默认没有提供任何初始化数据时,分数的对象默认值为0/1// 
    11 top = 0;
    12 bottom = 1;
    13 }
    14 int Fraction::gcd(int t, int b) {                 //求两个数的最小公约数// 
    15 return t % b == 0 ? b : gcd(b, t%b);
    16 }
    17 
    18 void Fraction::simplify() {                     //用于分数的化简// 
    19 if(bottom < 0) {
    20 top = - top;
    21 bottom = - bottom;
    22 }
    23 int g = gcd(abs(top),abs(bottom));             //求出最大公约数,分别用分子分母做除// 
    24 top /= g;
    25 bottom /= g;
    26 }
    27 void Fraction::add(Fraction c0) {              //求分数的和// 
    28 bottom = bottom * c0.bottom;
    29 top = top * c0.bottom + c0.top * bottom;
    30 simplify();
    31 }
    32 void Fraction::subtract(Fraction c0) {         //求分数的差// 
    33 bottom = bottom * c0.bottom;
    34 top = top * c0.bottom - c0.top * bottom;
    35 simplify();
    36 }
    37 void Fraction::multiple(Fraction c0) {        //求分数的积// 
    38 top *= c0.top;
    39 bottom *= c0.bottom;
    40 simplify();
    41 }
    42 void Fraction::divde(Fraction c0) {           //求分数的商//
    43 if(c0.top == 0) {
    44 cout << "错误,0不能作除数.
    ";
    45 return ;
    46 }
    47 top *= c0.bottom;
    48 bottom *= c0.top;                            //两个数相除就相当于一个数乘另一个数的倒数// 
    49 simplify();
    50 }
    51 void Fraction::compare(Fraction c0) {        //比较分数大小// 
    52 int tmp=top * gcd(bottom, c0.bottom) - c0.top* gcd(bottom, c0.bottom);
    53 if(tmp==0)
    54 cout<<"两数大小相等!"<<endl;
    55 else if(tmp<0) cout<<"第一个数较大!" <<endl;
    56 else if(tmp>0) cout<<"第二个数较大!" <<endl;
    57 }
    58 void Fraction::readln() {                    //读入分数// 
    59 cout << "请输入分子和分母" << endl;
    60 cin >> top;
    61 int ans;
    62 cin >> ans;
    63 while (ans == 0) {
    64 cout << "0不能作分母,请重试!" << endl;
    65 cin >> ans;
    66 }
    67 bottom = ans;
    68 }
    69 void Fraction::writeln() {                   //输出分数// 
    70 if(bottom != 1) 
    71 cout << top << "/" << bottom << endl;
    72 else cout << top <<endl;
    73 }

    Main.cpp

     1 #include "Fraction.h"
     2 #include <iomanip>
     3 int main() {
     4 Fraction c1 (6,-12);
     5 Fraction c2 (2);
     6 Fraction c3 ;
     7 c1.writeln();
     8 c1.add(c2);
     9 c1.writeln();
    10 c1.subtract(c2);
    11 c1.writeln();
    12 c1.multiple(c2);
    13 c1.writeln();
    14 c1.divde(c2);
    15 c1.writeln();
    16 c1.readln() ;
    17 c1.writeln();
    18 c2.readln();
    19 c2.writeln();
    20 c1.compare(c2); 
    21 return 0;
    22 }

    (3)运行环境 DevC++ 5.11

      运行结果:

    二、实验总结与体会

      本次实验学会了用C++多文件结构工程模式编写代码,感觉整体和java语言更像了。这样的写法让整个项目更清晰,能够让人轻松的读懂代码。本次实验第二题和第三题更多的是考验对基础数学的掌握,第二题要观察特性总结规律,第三题要注重很多关于分数的细节。数学和编程紧密结合,而本次的两个实验就相当于以后做其他大型项目的缩写,以后运用的规律,和数学基本知识将会更多更难,结合到代码之中细节也会更多,需要仔细斟酌的地方也越多。

  • 相关阅读:
    使用element-ui是下拉筛选选择
    vue 组件传值
    vue element 地址联动的使用
    vux scroller
    实时监听组件中路由的变化
    vuex的使用
    对移动端滚动高度的获取
    【转】ACM 取石子问题
    【转】ACM博弈知识汇总
    EOJ 2857 编辑距离
  • 原文地址:https://www.cnblogs.com/yjqi/p/8858750.html
Copyright © 2011-2022 走看看