2007年08月27日

#include "iostream.h"
int arr[]={0,-9,-1,-78,31,2,1,39,91,3,9,1,9,4,5,-9,-1,-78,31,2,1,39,91,3};
int alen =sizeof(arr)/sizeof(int)-1;
void OutPut(int *arr)
{
 while (alen)
 {
  cout<<arr[--alen]<<" ";
 }
}
void InsertSort(int  *R)
{
    int i,j=0;
 for(i=1;i<=alen;i++)
  if(R[i] >R[i-1] )
  {
   //存储要插入的元素
      int Insert = R[i];
   j=i-1;
   do
   {
    if (j==-1)
    {
                   R[0] = Insert;
       break;
    }
    else
    {
     R[j+1]=R[j];
     j– ;
    }
   }while( Insert > R[j]);
   R[j+1]=Insert;
  }
}
void main()
{
 InsertSort(arr);
 OutPut(arr);
}
result:

-78 -9 -9 -1 -1 0 1 1 1 2 2 3 3 4 5 9 9 31 31 39 39 91 91

 

2007年08月26日

#include "iostream.h"
#include "string.h"
#include "iomanip.h"
#define for if(0); else for
int R[]={-1,-2,3,2,1,4,18,3,3,3,3,2,2,3,2,1,4,18,13,-1,-1,1,1,-1,2,2,-3,-4,-5};
int rlen=sizeof(R)/sizeof(int);
//选择排序
void SelectSort(int *r,int n)
{
 for (int i= 0;i<n;i++)
 {
  int k = i;
  for (int j = k+1; j< n; j++ )
  {
   if (r[j]<r[k])
   {
    k =j;
   }
  }
  if (k!=i)
  {
      int tmp = r[i];r[i] = r[k];r[k]= tmp;
  }
 }
}
//确定排序好的数组中第二个小的数共有end -star +1个
void SecondNum(int* R,int n,int *star,int *end,int nSort,int *iRet)
{
 if (nSort <= 0 || nSort >n)
 {
  cout<<"logic is invalid !    ";
  return;
 }
 int i =0;
 int count = 0;
 if (nSort == 1)
 {
  *star = 0;
  do
  {
   i++;
  } while(R[i] == R[0]);
  *end = i;
 }
 else
 {
  for (int j = i+1;j<n+1;j++)
  { 
   if (R[i] == R[j] )
   {
    i++;
   }
   else
   {
    i++;
    count++;
    int result = i;
    if (count == nSort-1)
    {  
     *star = result;
    }
    if (count == nSort)
    {
     *end = result;
     break;
    }
   }
  }
 }
    *iRet = i;
}
//输出数组的元素
void output(int *R,int star,int end)
{
 cout<<"result is–〉";
 for (int i = star ; i < end; i ++)
 {
  cout<<R[i]<<"   ";
 }
}
void main()
{
 int *iRet = new int;
 int *star = new int;
 int *end  = new int;
  int nSort = 0;
 *iRet = 0;
 SelectSort(R,rlen);
 output(R,0,rlen);
 cout<<endl;
 while (1)
 {
  if (R[*iRet - 1] == R[rlen-1])
  {
   cout<<endl;
      cout<<"THE ARRAY IS ALREADY END!";
  }
  cout<<endl<<"please input your serialNumber"<<endl;
  cin>>nSort;
  SecondNum(R,rlen,star,end,nSort,iRet);
  output(R,*star,*end);
 }
}

运行结果:

result is–〉-5   -4   -3   -2   -1   -1   -1   -1   1   1   1   1   2   2   2
 2   2   2   3   3   3   3   3   3   4   4   13   18   18

please input your serialNumber
-34
logic is invalid !    result is–〉
please input your serialNumber
0
logic is invalid !    result is–〉
please input your serialNumber
1
result is–〉-5
please input your serialNumber
2
result is–〉-4
please input your serialNumber
3
result is–〉-3
please input your serialNumber
4
result is–〉-2
please input your serialNumber
5
result is–〉-1   -1   -1   -1
please input your serialNumber
6
result is–〉1   1   1   1
please input your serialNumber
7
result is–〉2   2   2   2   2   2
please input your serialNumber
8
result is–〉3   3   3   3   3   3
please input your serialNumber
9
result is–〉4   4
please input your serialNumber
10
result is–〉13
please input your serialNumber
11
result is–〉18   18
THE ARRAY IS ALREADY END!
please input your serialNumber
12
result is–〉
THE ARRAY IS ALREADY END!
please input your serialNumber

#include "iostream.h"
#include "string.h"
#define for if(0); else for
int R[]={-1,-2,3,2,1,4,8,2,2,13,3,2,1,4,8,13,-1,-1,1,1,-1,2,2,};
const int rlen=sizeof(R)/sizeof(int);
//选择排序
void SelectSort(int *r,int n)
{
 int tmp ;
 for (int i= 0;i<n;i++)
 {
  int k = i;
  for (int j = k+1; j< n; j++ )
  {
   if (r[j]<r[k])
   {
    k =j;
   }
  }
  if (k!=i)
  {
   tmp = r[i];r[i] = r[k];r[k]= tmp;
  }
 }
}
//确定排序好的数组中第二个小的数共有end -star +1个
void SecondNum(int* R,int n,int *star,int *end)
{
 int tmp = R[0];
 int count = 0;
 for (int i = 0, j=1;j<n-1;j++)
 { 
  if (tmp == R[j] )
  {
   i++;
  }
        else
  {
   count++;
   if (count == 1)
   {
    tmp = R[i+1];
    *star = 1+i;
   }
            if (count == 2)
            {
    *end = j;
            }
  }
 }
}
//输出数组的元素
void output(int *R,int star,int end)
{
 for (int i = star; i < end; i ++)
 {
  cout<<R[i]<<" ";
 }
 cout<<endl;
}
void main()
{
 SelectSort(R,rlen);
 output(R,0,rlen);
 int *star = new int;
 int *end  = new int;
    SecondNum(R,rlen,star,end);
 output(R,*star,*end);
}

rusult:

-2 -1 -1 -1 -1 1 1 1 1 2 2 2 2 2 2 3 3 4 4 8 8 13 13
-1 -1 -1 -1

2007年08月25日

#include<iostream.h>
#include<string.h>
#define MAX 10000
char *sum = new char[MAX];
char *result = new char[MAX];

void swap(char &a,char &b)
{
 int tmp = a;
    a = b;
 b = tmp;
}

char* ReverseStr(char *str)
{
 int len = strlen(str) – 1;
 for (int i = 0; i <= len/2 ;i++)
 {
  swap(str[i],str[len -i]);
 }
 return str;
}

char* BigIntMulti(char *s1,char *s2)
{
 int i=0,j,p,q,m;
 int k=0,flag;
 char mid[MAX];
 ReverseStr(s1);
 ReverseStr(s2);
 //结果字符串赋零值以免出现随机数
 for(i=0;i<MAX;i++)
 {
  sum[i]=’0′;
 }
    i=j=k=0;
 p=0;q=0;
 while(s2[i]!=’\0′)
 {
  flag=0;
  //q的值就是移位的数量
  for(m=0;m<q;m++)
  {
   mid[m]=’0′;
  }
  p=q;
  //计算每一位的结果
        while(s1[j]!=’\0′)
  {
   k=(s1[j]-’0′)*(s2[i]-’0′);
   mid[p]=(flag+k)%10+’0′;
   flag=(flag+k)/10;
   j++;p++;
  }
  if(flag!=0)
  {
   mid[p++]=flag+’0′;
   flag=0;
  }
  //在字符串结尾添加标志
        mid[p]=’\0′;
  p=0;
  while(mid[p]!=’\0′)
  {
   //累计相加
   k=(sum[p]-’0′)+(mid[p]-’0′);
   sum[p]=(k+flag)%10+’0′;
   flag=(k+flag)/10;
   p++;
  }
  //处理标志位
  if(flag!=0)
  {
   sum[p++]=flag+’0′;
   flag=0;
  }
  j=0;
  i++;q++;
 }
 sum[p]=’\0′;
 return  ReverseStr(sum); 
}
int main()
{
 char s1[] = "999999999999999999";
 char s2[] = "98091999999999999999";
 cout<<BigIntMulti(s1,s2)<<endl;
 return 0;
}
result:                      98091999999999999900908000000000000001

#include "iostream.h"
#include "string.h"
#define  MAX  100
char rstr[MAX];

//交换两个字符

void swap(char &a,char &b)
{
 int tmp = a;
    a = b;
 b = tmp;
}

//反转字符串
void ReverseStr(char str[])
{
 int len = strlen(str) – 1;
 for (int i = 0; i <= len/2 ;i++)
 {
  swap(str[i],str[len -i]);
 }
}

//大数相加函数

char* BigIntAdd(char stra[],char strb[])
{
 int alen = strlen(stra);
 int blen = strlen(strb);
 //ReverseStr the str
 ReverseStr(stra);
    ReverseStr(strb);
 int flag = 0; //标志是否有进位
 int i = 0;
 int j = 0;
 do
 {

//异常处理两个数的结尾,如果字符串有一个结束最后一位用’0′字符代替
  if (i >= alen)
        {
   stra[i] = ‘0′;
        }
  if (j >= blen)
  {
      strb[j] = ‘0′;
  }

//结果字符串rstr
  rstr[i] = ((stra[i] – ‘0′) + (strb[i] – ‘0′)+flag)%10 +’0′;
  flag = ((stra[i] – ‘0′) + (strb[i] – ‘0′)+flag)/10;//进位的处理
        i++;
     j++;
 }while (i < alen || j < blen);
 ReverseStr(rstr);
 rstr[MAX] = ‘\0′;
 return rstr;
}
void main()
{         
 char stra[] = "666666666666666666666666987986986666666666666668799898989898676";
 char strb[] = "987986986666666666666668799898989898676666666666666666698798";
 char *RetStr = BigIntAdd(stra,strb);
    cout<<RetStr<<endl;
 
}

result:667654653653333333333333656786885656565343333335466565656597474