随笔记---杨辉三角

关键是记录一下Xcode的代码方式。

triangle.hpp

#ifndef triangle_hpp
#define triangle_hpp

#include <iostream>

#endif /* triangle_hpp */
using namespace std;
class Node{
public:
    void setData(int d);
    void setNext(Node*  n);
    int getData();
    Node* getNext();
private:
    int data;
    Node *next;
};
class Queue{
public:
    Queue();
    void addNode(int data);
    int popNode();
    bool isEmpty();
    void printQueue();
private:
    Node *first;
    Node *last;
};
//杨辉三角
void triangle(int rank);

triangle.cpp

//
//  triangle.cpp
//  algri
//
//  Created by CTK-Mac on 16/8/22.
//  Copyright © 2016年 CTK-Mac. All rights reserved.
//

#include "triangle.hpp"
//Node成员函数
void Node::setData(int d){
        data=d;
    }
void Node::setNext(Node*  n){
        next=n;
    }
int Node::getData(){
        return data;
    }
Node* Node::getNext(){
        return next;
    }
//Queue构造函数
Queue::Queue(){
        first=new Node();
        last=new Node();
        first->setNext(last);
        last->setNext(nullptr);
    }
//Queue成员函数
void Queue::addNode(int data){
        Node *n=new Node();
        n->setData(data);
        if(first->getNext()==last){
            first->setNext(n);
            n->setNext(last);
        }else{
            Node *p=first->getNext();
            while(p->getNext()!=last)
                p=p->getNext();
            p->setNext(n);
            n->setNext(last);
        }
    }
int Queue::popNode(){
        int data=-1;
        if(first->getNext()!=last){
            Node *p=first->getNext();
            first->setNext(p->getNext());
            data=p->getData();
            delete(p);
        }
        return data;
    }
bool Queue::isEmpty(){
        if(first->getNext()==last)
            return true;
        else
            return false;
    }
void Queue::printQueue(){
        Node *p=first->getNext();
        while(p!=last)
        {
            cout<<p->getData()<<" ";
            p=p->getNext();
        }
        cout<<endl;
    }

void triangle(int rank){
    int mark=0;
    int result=0;
    Queue save;
    for(int i=0;i<rank;i++){
        for(int j=0;j<=i;j++){
            if(i==0){
                cout<<1;
            }else if(i==1){
                save.addNode(1);
                cout<<1<<" ";
            }else{
                if(j==0)
                {
                    cout<<1<<" ";
                    save.addNode(1);
                    mark=save.popNode();
                }else if(j!=i){
                    result+=mark;
                    mark=save.popNode();
                    result+=mark;
                    save.addNode(result);
                    cout<<result<<" ";
                    result=0;
                }else{
                    cout<<1;
                    save.addNode(1);
                }
            }
            
        }
        cout<<endl;
    }
}

main.cpp

#include "triangle.hpp"

int main(int argc, const char * argv[]) {
    // insert code here...
    triangle(5);
    return 0;
}
原文地址:https://www.cnblogs.com/chentingk/p/5795191.html