Pages: 5/19 First page Previous page 1 2 3 4 5 6 7 8 9 10 Next page Final page [ View by Articles | List ]

实验七 栈的基本操作

| April 10, 2008 12:00 | timmy | Via Original
test7.h

void InitStack (Stack &S)    //构造一个空栈 S
{
  S.MaxSize=10;
  S.stack=new ElemType[S.MaxSize];
  if(!S.stack){
    cerr<<"fail"<<endl;
    exit(1);
  }
  S.top=-1;
}

int EmptyStack (Stack S)   //若栈S为空栈返回1,否则返回0
{
  return S.top==-1;
}

void Push(Stack &S, ElemType item)   //元素 item进栈
{
  if(S.top==S.MaxSize-1){
    int k=sizeof(ElemType);
    S.stack=(ElemType*)realloc(S.stack,2*S.MaxSize*k);
    S.MaxSize=2*S.MaxSize;
  }
  S.top++;
  S.stack[S.top]=item;
}

ElemType Pop(Stack &S)    //栈S的栈顶元素出栈并返回
{
  if(S.top==-1){
    cerr<<"Stack is empty!"<<endl;
    exit(1);
  }
  S.top--;
  return S.stack[S.top+1];
}

ElemType Peek(Stack S)   //取栈S的当前栈顶元素并返回
{
  if(S.top==-1){
    cerr<<"Stack is empty!"<<endl;
    exit(1);
  }
  return S.stack[S.top];
}

void ClearStack (Stack &S)   //清除栈s,使成为空栈
{
  if(S.stack){
    delete []S.stack;
    S.stack=0;
  }
  S.top=-1;
  S.MaxSize=0;
}

test7.cpp

#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
typedef char ElemType;
struct Stack{
    ElemType  *stack ;    // 存栈元素
    int  top;                // 栈顶指示器
    int MaxSize;            // 栈的最大长度
};
#include"test7.h"
int IsReverse(char *s)
{
  int i=0;
  Stack s1;
  InitStack(s1);
  while(s[i]!='\0'){
    Push(s1,s[i]);
    i++;
  }
  i=0;
  while(s[i]!='\0')
    if(Pop(s1)!=s[i])
      return 0;
    else
      i++;
                ClearStack(s1);
  return 1;
}
void main()
{
  char s[10];
  scanf("%s",s);
  if(IsReverse(s))
    cout<<"The string is reverse."<<endl;
  else
    cout<<"The string is not reverse."<<endl;
}

Download ( 0 downloads)
Only registered users can download this file. Please Register or Login
Tags: ,

实验四 线性表的顺序表示和实现

| March 20, 2008 12:09 | timmy | Via Original
test4.cpp

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
typedef int ElemType;
#define MAXSize  10;
#include "SeqList.h"
void main(void)
{
  SeqList myList;
  int i=1, x, sum=0, n;
  
  InitList (myList);
  scanf("%d", &x);
  while ( x!= -1 )
  {  
    if (InsertList (myList,x,i)==0) {
      printf("错误!\n");
      return ;
    }
       i++;  
     scanf("%d", &x);
  }
  n = LengthList (myList);
  for (i=1; i<=n; i++)
  {
    x=GetList(myList, i);
    sum =sum+ x;
  }
  printf("%d\n ", sum);
  if(DeleteElem(myList,10,100)==true){
    printf("After Deleting the digits between 10-100:");
    n = LengthList (myList);
    for (i=1; i<n; i++){
      cout<<myList.list[i]<<' ';
    }
    cout<<endl;
  }
  ClearList(myList);
}

seqlist.h

typedef struct List{
  ElemType *list;  
  int size;        
  int MaxSize;    
}SeqList;

void InitList(SeqList &L)
{  //初始化线性表      
  L.MaxSize=10;
  L.list=new ElemType[L.MaxSize];
  if(L.list==NULL){
    cout<<"exit running"<<endl;
    exit(1);
  }
  L.size=0;
}

void ClearList(SeqList &L)
{   //清除线性表
  if(L.list!=NULL){
    delete []L.list;
    L.list=NULL;
  }
  L.MaxSize=0;
  L.size=0;
}

int LengthList(SeqList L)
{  //求线性表长度
  return L.size;
}

bool InsertList(SeqList &L, ElemType item, int pos)
{   //按给定条件pos向线性表插入一个元素
  if(pos<-1||pos>L.size+1){
    cout<<"pos is unavailible"<<endl;
    return false;
  }
  int i;
  if(pos==0){
    for(i=0;i<L.size;i++)
      if(item<L.list[i])
        break;
    pos=i+1;
  }
  else if(pos==-1)
    pos=L.size+1;
  if(L.size==L.MaxSize){
    int k=sizeof(ElemType);
    L.list=(ElemType*) realloc(L.list,2*L.MaxSize*k);
    if(L.list==NULL){
      cout<<"exit running"<<endl;
      exit(1);
    }
    L.MaxSize=2*L.MaxSize;
  }
  for(i=L.size-1;i>=pos-1;i--)
    L.list[i+1]=L.list[i];
  L.list[pos-1]=item;
  L.size++;
  return true;
}

ElemType GetList(SeqList L, int pos)
{  //在线性表L中求序号为pos的元素,该元素作为函数值返回
  if(pos<1||pos>L.size)
  {
    cerr<<"pos is out range"<<endl;
    exit(1);
  }
  return L.list[pos-1];
}

bool DeleteList(SeqList &L, ElemType& item, int pos)
{
  if(L.size==0){
    cout<<"the list is null"<<endl;
    return false;
  }
  if(pos<-1||pos>L.size){
    cout<<"pos is unavailible"<<endl;
    return false;
  }
  int i;
  if(pos==0){
    for(i=0;i<L.size;i++)
      if(item==L.list[i])
        break;
    if(i==L.size)
      return false;
    pos=i+1;
  }
  else if(pos==-1) pos=L.size;
  item=L.list[pos-1];
  for(i=pos;i<L.size;i++)
    L.list[i-1]=L.list[i];
  L.list--;
  if(float(L.size)/L.MaxSize<0.4&&L.MaxSize>10){
    int k=sizeof(ElemType);
    L.list=(ElemType*) realloc(L.list,L.MaxSize*k/2);
    L.MaxSize=L.MaxSize/2;
  }
  return true;
}

bool DeleteElem(SeqList &L, int min, int max)
{
  if(L.size==0){
    cout<<"the list is null"<<endl;
    return false;
  }
  int i,j;
  for(i=0;i<L.size;i++)
    if(L.list[i]>=min&&L.list[i]<=max){
      for(j=i;j<L.size;j++)
        L.list[j]=L.list[j+1];
      L.size--;
    }
  if(float(L.size)/L.MaxSize<0.4&&L.MaxSize>10){
    int k=sizeof(ElemType);
    L.list=(ElemType*) realloc(L.list,L.MaxSize*k/2);
    L.MaxSize=L.MaxSize/2;
  }
  return true;
}


Download ( 0 downloads)
Only registered users can download this file. Please Register or Login

Tags: ,

试验三 算法与算法分析

| March 13, 2008 10:55 | timmy | Via Original
test3_1.cpp

#include<iostream.h>
void main()
{
  int x,y,z,t;
  cin>>x>>y>>z;
  if(x>y){
    t=x;
    x=y;
    y=t;
  }
  if(x>z){
    t=x;
    x=z;
    z=t;
  }
  if(y>z){
    t=y;
    y=z;
    z=t;
  }
  cout<<x<<y<<z<<endl;
}

test3_2.cpp

# include <stdio.h>
# include <sys/timeb.h>  //时间函数  
void main()
{
     timeb t1, t2;
     long t;
     double x, sum=1, sum1;
     int i, j, n;
     printf("请输入x,n:") ;
     scanf("%lf%d", &x, &n) ;
     ftime(&t1) ;        // 求得当前时间
     for(i=1; i<=n; i++)
     {
      sum1=1;
      for(j=1; j<=i; j++)
        sum1=sum1*(-1.0/x) ;
        sum+=sum1;
    }
    ftime(&t2) ;        // 求得当前时间
    t=(t2.time-t1.time)*1000+(t2.millitm-t1.millitm) ; //计算时间差,转换成毫秒
    printf("sum=%lf 用时%ld毫秒\n", sum, t) ;
}

test3_3.cpp

  # include <stdio.h>
  # include <sys/timeb.h>
  void main()
  {
    timeb t1, t2;
    long t;
    double x, sum1=1, sum=1;
    int i, n;
    printf("请输入x,n: ") ;
    scanf("%lf%d", &x, &n) ;
    ftime(&t1) ;          // 求得当前时间
    for(i=1;i<=n;i++)
    {
      sum1*=-1.0/x;
      sum+=sum1;
    }
    ftime(&t2) ;           // 求得当前时间
    t=(t2.time-t1.time)*1000+(t2.millitm-t1.millitm) ; // 计算时间差,转换成毫秒
    printf("sum=%lf 用时%ld毫秒\n", sum, t) ;
  }

Download ( 0 downloads)
Only registered users can download this file. Please Register or Login
Tags: ,

实验二  抽象数据类型的表示和实现

| March 6, 2008 11:06 | timmy | Via Original
test_main.cpp

  # include <iostream.h>  // 包含输入(cin)、输出(cout)的头文件,
  # include <stdio.h>     //若采用printf和scanf,则用stdio.h库函数
  # include <stdlib.h>      //常用函数的头文件
  typedef int ElemType;  // 定义三元组元素类型ElemType为整型
  typedef ElemType *Triplet; //定义动态分配的三元组类型, 指针Triplet
//指向ElemType类型元素的地址。初始化操作分配3个元素的存储空间
  # include "test2_function.h"  // 包含三元组基本操作的头文件
  void main()
  {
    Triplet T;        // _定义T为Triplet类型的变量
    ElemType m;
    int i;
    i=InitTriplet(T, 1, 3, 5);  //__调用InitTriplet函数初始化即构造三元组T,并赋三个初值为1 3 5
    printf("调用初始化函数后,i=%d (1:成功;否则:不成功) ", i);
    printf("\n三元组中三个元素的值分别为:\n");
    printf("T[0]=%d, T[1]=%d, T[2]=%d", T[0], T[1], T[2]);
  if(IsAscending(T))   //调用IsAscending()函数判断三元组中元素是否为升序排列
    printf("\nT中三个元素按升序排列");
  if(IsDecending(T))   //调用IsDecending()函数判断三元组中元素是否为升序排列
    printf("\nT中三个元素按降序排列");
    i=Get(T, 2, m);  // 调用Get()将T的第2元的值赋值给m
    if (i==1)        // 调用Get() 成功
      printf("\nT的第2个值为:%d", m) ;
    i=Put(T, 2, 6);      // _调用Put()改变T的第2元的值为6
    if (i==1)        // 调用Put() 成功
      printf("\n改变后T的3个值为:%d, %d, %d", T[0], T[1], T[2]) ;
    Max(T, m);      // _调用Max()返回T中的最大值并赋值给m
    printf("\nT中的最大值为:%d", m) ;
    Min(T, m);
    printf("\nT中的最小值为:%d", m);
    DestroyTriplet(T);    //_调用DestroyTriplet()销毁T
    printf("\n销毁T后,T=%d\n", T);
  }

test2_function.h

  int InitTriplet(Triplet &T, ElemType v1, ElemType v2, ElemType v3)
  { // 操作结果:构造三元组T,依次置T的3个元素的初值为v1,v2和v3
// (见图2.3) ,经过此操作,系统将分配给三元组T一个起始地址
    if (!(T=(ElemType *) malloc (3*sizeof(ElemType))))
        exit(0);        // 申请空间失败,退出系统
    T[0]=v1; T[1]=v2; T[2]=v3;
    return 1;         // 若返回值为1,则初始化成功
  }
  void DestroyTriplet(Triplet &T)
  { // 操作结果:三元组T被销毁
    free(T);
    T=NULL;
    return;
  }
int Get(Triplet T,int i,ElemType &e)
{
  if(T!=NULL&&(i>=1&&i<=3)){
    e=T[i-1];
    return 1;
  }
  else
    exit(0);
}
int IsAscending(Triplet T)
{
  if(T!=NULL&&(T[0]<=T[1]&&T[1]<=T[2]))
    return 1;
  else
    return 0;
}
int IsDecending(Triplet T)
{
  if(T!=NULL&&(T[0]>=T[1]&&T[1]>=T[2]))
    return 1;
  else
    return 0;
}
int Put(Triplet &T,int i,ElemType e)
{
  if(T!=NULL&&(i>=1&&i<=3)){
    T[i-1]=e;
    return 1;
  }
  else
    exit(0);
}
void Max(Triplet T,ElemType &e)
{
  int i;
  if(T!=NULL){
    e=T[0];
    for(i=1;i<3;i++)
      if(e<T[i])
        e=T[i];
  }
  else
    exit(0);
}  
void Min(Triplet T,ElemType &e)
{
  int i;
  if(T!=NULL){
    e=T[0];
    for(i=1;i<3;i++)
      if(e>T[i])
        e=T[i];
  }
  else
    exit(0);
}  

Download ( 0 downloads)
Only registered users can download this file. Please Register or Login
Tags: ,

实验1__熟悉Project组织应用程序

| February 28, 2008 12:06 | timmy | Via Original
test1.cpp

#include<iostream.h>
#include<math.h>
#include "test1.h"
void main()
{
  int i,a[10],N;
  int res_max,res_min,res_aver;
  int max,min,aver;
  N=10;
  for(i=0;i<N;i++)
    cin>>a[i];
  cout<<"Testing Function aMAX:";
  res_max=aMAX(a,N);
  cout<<"max="<<res_max<<endl;
  cout<<"Testing Function aMIN:";
  res_min=aMIN(a,N);
  cout<<"min="<<res_min<<endl;
    cout<<"Testing Function aAVE:";
  res_aver=aAVE(a,N);
  cout<<"aver="<<res_aver<<endl;
  cout<<"Testing Function aMAX_MIN_AVE:";
  aMAX_MIN_AVE(a,N,max,min,aver);
  cout<<"Prime_sum="<<prime_SUM(a,N)<<endl;
  cout<<"After sort:"<<endl;
  aSORT(a,N);
}

test1.h

int aMAX(int *a,int n)
{
  int i,res;
  res=a[0];
  for(i=1;i<n;i++)
    if(res<a[i]) res=a[i];
  return res;
}
int aMIN(int *a,int n)
{
  int i,res;
  res=a[0];
  for(i=1;i<n;i++)
    if(res>a[i]) res=a[i];
  return res;
}
int aAVE(int *a,int n)
{
  int res,i;
  res=0;
  for(i=0;i<=n;i++)
    res+=a[i];
  res/=n;
  return res;
}
void aMAX_MIN_AVE(int *a, int n, int &max, int &min, int &aver)
{
  int i;
  max=min=aver=a[0];
  for(i=1;i<n;i++){
    if(max<a[i]) max=a[i];
    if(min>a[i]) min=a[i];
    aver+=a[i];
  }
  aver/=n;
  cout<<"max="<<max<<" "<<"min="<<min<<" "<<"aver="<<aver<<endl;
}
int prime(int m)
{
  int i,y;
  if(m!=1){
    for(i=2;i<=sqrt(m);i++)
      if(m%i==0) break;
      if(i>sqrt(m))
        y=1;  
      else
        y=0;
  }
  else
    y=0;
  return y;
}
int prime_SUM(int *a, int n)
{
  int i,res;
  res=0;
  for(i=0;i<n;i++)
    if(prime(a[i]))
      res+=a[i];
  return res;
}
void aSORT(int *a,int n)
{
  int k,i,index,temp;
  for(k=0; k<n; k++){
    index=k;
    for(i=k+1;i<n;i++)
      if(a[i]<a[index])
        index=i;
    temp=a[index];
        a[index]=a[k];
        a[k]=temp;
    }
  for(i=0;i<n;i++)
    cout<<a[i]<<" ";
  cout<<endl;
}

Download ( 4 downloads)
Only registered users can download this file. Please Register or Login

Tags: ,
Pages: 5/19 First page Previous page 1 2 3 4 5 6 7 8 9 10 Next page Final page [ View by Articles | List ]