C++基础2

本篇博客没有营养,仅仅作为个人笔记使用

#include<iostream>

using namespace std;

template<class T>

//顺序搜索

int sequentialSearch(T a[], int n, const T& x)
{//在数组a[0:n-1]中查找元素x
 //如果找到返回该元素位置,如果没找到范湖-1
 int i;
 for (i = 0;i < n && a[i] != x;i++);
    if(i==n)return -1;
    else return i;
}

//执行顺序搜索的递归函数

int rSequentialSearch(T a[],const T& x,int n)
{//在数组a[0:n-1]中查找元素x
 //如果找到返回该元素位置,如果没找到范湖-1
 if(n < 1)return - 1;
 if(a[n-1]==x)return n - 1;
 return rSequentialSearch(a,x,n-1);
}

//对多项式进行求值

T PolyEval(T coeff[],int n,const T& x)
{//计算n次多项式的值,coeff[0:n]为多项式的系数
    T y = 1,value = coeff[0];
    for(int i = 1;i <= n;i++)
    {
        y *=x;
        value += y * coeff[i];
    }
    return value;

}

//利用Horner法则对多项式进行求值

T Horner(T coeff[],int n,const T& x)
{//计算n次多项式的值,coeff[0:n]为多项式的系数
    T value = coeff[n];
    for(int i = 1;i <= n;i++)
        value = value * x + coeff[n-i];
    return value;

}

//计算名次

void Rank(T a[],int n,int r[])
{//计算a[0:n-1]中n个元素的排名
    for(int i = 1;i < n;i++)
        r[i] = 0;//初始化
    //逐对比较所有的元素
    for(int i = 1;i < n;i++)
        for(int j = 1;j < i;j++)
            if(a[j]<=a[i])r[i]++;
            else r[j]++;
}

//利用附加数组重排数组元素

void Rearrange(T a[],int n,int r[])
{
    T *u = new T[n+1];
    //在u中移动到正确的位置
    for(int i = 1;i < n;i++)
        u[r[i]] = a[i];
    //移动回a中
    for(int i = 1;i < n;i++)
        a[i] = u[i];
    delete []u;
}

//选择排序
int Max(T a[],int sizee){
    int maxv = a[0],k = 0;
    for(int i = 0;i < sizee;i++){
        if(maxv<=a[i])
            k = i;
    }
    return k;
}
void SelectionSort(T a[],int n)
{
    for(int sizee = n;sizee > 1;sizee--){
        int j = Max(a,sizee);
        swap(a[j],a[sizee-1]);
    }
}

//及时终止的选择排序

void selectionSort(T a[],int n)
{//及时终止的选择排序
    bool sorted = false;
    for(int sizee = n; !sorted && (sizee > 1);sizee--)
    {
        int indexOfMax = 0;
        sorted = true;
        for(int i = 1;i < sizee;i++)
            if (a[indexOfMax]<= a[i])indexOfMax = i;
            else sorted = false;
        swap(a[indexOfMax],a[sizee - 1]);
    }
}

//冒泡排序

//一次冒泡过程
void bubble(T a[],int n)
{
    for(int i = 0;i <  n - 1;i++)
        if(a[i] >  a[i+1]) swap(a[i],a[i+1]);
}
//冒泡排序
void BubbleSort(T a[],int n)
{
    for(int i = n;i > 1;i--)
        bubble(a,i);
}

//及时终止的冒泡排序
bool bubble(T a[],int n)
{
    bool swapped = false;//尚未发生交换
    for(int i = 0;i < n - 1;i++)
    if(a[i]>a[i+1]){
        swap(a[i],a[i+1])
        swapped = true;
    }
    return swapped;
}
void BubbleSort(T a[],int n)
{
    for(int i = n;i > 1&&bubble(a,i);i--);
}

//插入排序
//插入排序是逐步扩大有序数组的范围
void insertt(T a[],int n,const T& x)
{
    int i;
    for(i = n - 1;i >=0 && x < a[i];i--)
        a[i+1] = a[i];
    a[i+1] = x;
}
void InsertionSort(T a[],int n)
{
    for(int i = 1;i < n;i++){
        T t = a[i];
        insertt(a,i,t);
    }
}

//另一种插入排序
void InsertionSort(T a[],int n)
{
    for(int i = 1;i < n;i++){
        //将a[i]插入到a[0:i-1]
        T t = a[i];
        int j;
        for(j = i - 1;j >= 0 && t <  a[j];j--)
            a[j+1] = a[j];
        a[j+1] = t;
    }
}

currency类

// currency class using three data members
// (sign, dollars, and cents) to
// represent an instance/object

#ifndef currency_
#define currency_

#include <iostream>
#include "myExceptions.h"

using namespace std;

enum signType {pluss, minuss};

class currency
{
   friend ostream& operator<<(ostream&,const currency&);
   public:
      // constructor
      currency(signType theSign = pluss,
               unsigned long theDollars = 0,
               unsigned int theCents = 0);
      // destructor
      ~currency() {}
      void setValue(signType, unsigned long, unsigned int);
      void setValue(double);
      signType getSign() const {return sign;}
      unsigned long getDollars() const {return dollars;}
      unsigned int getCents() const {return cents;}
      currency add(const currency&) const;
      currency& increment(const currency&);
      void output() const;
   private:
      signType sign;           // sign of object
      unsigned long dollars;   // number of dollars
      unsigned int cents;      // number of cents
};

currency::currency(signType theSign, unsigned long theDollars,
                                     unsigned int theCents)
{// Create a currency object.
   setValue(theSign, theDollars, theCents);
}

void currency::setValue(signType theSign, unsigned long theDollars,
                                          unsigned int theCents)
{// Set currency value.
   if (theCents > 99)
       // too many cents
       throw illegalParameterValue("Cents should be < 100");

   sign = theSign;
   dollars = theDollars;
   cents = theCents;
}

void currency::setValue(double theAmount)
{// Set currency value.
   if (theAmount < 0) {sign = minuss;
                       theAmount = -theAmount;}
   else sign = pluss;

   dollars = (unsigned long) theAmount;
             // extract integer part
   cents = (unsigned int) ((theAmount + 0.001 - dollars) * 100);
             // get two decimal digits
}

currency currency::add(const currency& x) const
{// Add x and *this.
   long a1, a2, a3;
   currency result;
   // convert invoking object to signed integers
   a1 = dollars * 100 + cents;
   if (sign == minuss) a1 = -a1;

   // convert x to signed integer
   a2 = x.dollars * 100 + x.cents;
   if (x.sign == minuss) a2 = -a2;

   a3 = a1 + a2;

   // convert to currency representation
   if (a3 < 0) {result.sign = minuss; a3 = -a3;}
   else result.sign = pluss;
   result.dollars = a3 / 100;
   result.cents = a3 - result.dollars * 100;

   return result;
}

currency& currency::increment(const currency& x)
{// Increment by x.
   *this = add(x);
   return *this;
}

void currency::output() const
{// Output currency value.
   if (sign == minuss) cout << '-';
   cout << '$' << dollars << '.';
   if (cents < 10) cout << '0';
   cout << cents;
}
//ostream& operator<<(ostream& out, const currency& x)
//{
//    long theAmount = x.amount;
//    if(theAmount < 0){
//        out << '-';
//        theAmount = -theAmount;
//    }
//    long dollars = theAmount/100;
//    out << '$' << dollars <<'.';
//    int cents = theAmount - dollars * 100;
//    if (cents < 10) out << '0';
//    out<<cents;
//    return out;
//}
#endif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值