博文

练习:差分方程求值(用递归和非递归两种方法)(1)(2006-06-19 02:52:00)

摘要:差分方程求值(用递归和非递归两种方法)(1)
/*习题6
写出Fn(x)的递归函数 
               1                        n=0;
       Fn(x)=  2x                       n=1;
               2xFn-1(x)-2(n-1)Fn-2(x)  n>1
               

*/
#include <stack>

using namespace std;

typedef stack<float>  floats;

//递归形式 
float  fn(float x,int n)
{
       float ......

阅读全文(2589) | 评论:0

练习:将递归函数(testk())改写成非递归(test())(2006-06-19 02:37:00)

摘要:#include <cstdlib>
#include <iostream>
#include <stack>

using namespace std;

typedef stack<int> ints;
typedef deque<char>  chars;

int pn(int x,int n);

//习题1   将递归函数(testk())改写成非递归(test()) 
void testk (int &sum)
{
     int x;
     cin>>x;
     if(x==0)  sum=0;
     else{testk(sum);sum+=x;}
     cout<<sum<<endl;
}

void test(int &sum)
{
     ints L;
     int x=1;
     sum=0;
     while(x!=0)
     {
         cin>>x;
......

阅读全文(2792) | 评论:0

练习:求两个正整数m和n的最大公约数(三种方法)(2006-06-19 02:33:00)

摘要:/*
    求两个正整数m和n的最大公约数可用如下gcd(m,n)公式表示
               m           n=0
    gcd(m,n)= 
               gcd(n,m%n)  n>0
    */
    
#include <stack>    
using namespace std;

typedef stack<int>  ints;
//递归实现 
int gcd(int m,int n)
{
    int  res;
    if(n==0)
        res=m;
    else
        res=gcd(n,m%n);
    return res;
}

//非递归实现 
int gcd2(int m,int n......

阅读全文(6315) | 评论:0

练习:识别回文字符串(2006-06-19 02:29:00)

摘要:/*习题4
       识别依次读入的一个以字符序列是否为形为序列1&序列2模式的字符序列。
       其中序列1和序列2不含&,且序列2为序列1的逆序
       a+b&b+a是    a+b&b-a不是
*/ 
//devc++中通过
//2006.06.18
//Zyq


#include <iostream>
#include <stack>
#include <cstdlib>

using namespace std;

typedef stack<char>   chars;

 int check(char a[],int n)
 {
     chars S;
     for(int i=0;i<n/2;i++)
     S.push(*(a+i));
     char t;
     int j=1;
     int i=n/2+1;
        if(*(a+i-1)!='&')
     ......

阅读全文(2961) | 评论:0

练习:递归实现汉诺塔(2006-06-11 23:28:00)

摘要://递归实现汉诺塔 
//devcpp4.9.9.2环境
//2006 06 10 
//使用系统的运行时栈
#include <cstdlib>
#include <iostream>

using namespace std;

void hanoi(int n,char a,char b,char c)
{
     if(n==1)
         cout<<n<<" "<<a<<" "<<c<<endl;
       else
     {
         hanoi(n-1,a,c,b);
         cout<<n-1<<" "<<a<<" "<<c<<endl;
         hanoi(n-1,b,a,c);
     }
}    

int main(int argc, char *argv[])
{
  &nbs......

阅读全文(3115) | 评论:0

总结:函数模版(template)的使用(2006-06-11 23:18:00)

摘要:
1、c++是强类型语言,不容易实现“为每个函数实例都显式定义一个函数”
2、宏定义和模扳提供了这种方法
3、宏定义的方法
    #define min(a,b)   ((a)<(b)?(a):(b))
    这种方法对于简单的min()调用可以正常工作
    但是,复杂调用时行为不可预期!!因为它的机制并不像函数调用那样工作,而只是简单地提供参数的替换
    结果是,它的参数都被替换两次:一次在参数的测试中,另一次在宏的返回值被计算期间。
4、函数模版提供了一种用来自动生成各种类型函数实例的方法。
   程序员对于函数接口中的全部或者部分类型进行参数化(parameterize),而函数保持不变。

5、例1:求两个数中较小者
template <class Type>
    Type minm(Type a,Type b)
    {
         return a<b?a:b;
    }
    
int main()
{
    minm(10;20);
    minm(10.0,20,0);
    return 0;
}

6、例2    STL库中堆栈的用法   <stack>
///////6.1 ......

阅读全文(9352) | 评论:0

练习:顺序表的所有操作(2006-05-31 02:55:00)

摘要:/*
实验一
文件名:SeqList.cpp
该程序参考网上的一个实验要求编写,用于实验线形表的各种操作
在VC6.0中模拟C环境运行
作者:hanshuyujifen
日期:2006.05.29
*/

#include <cstdio>
#include <iostream>
using namespace std;

/* 定义DataType为int类型 */
typedef int DataType;

/*顺序表存储空间的总分配量*/
#define MAXSIZE 100

/* 顺序存储类型 */
typedef struct
{
    DataType data[MAXSIZE]; /*存放线性表的数组*/
    int length;               /* length是顺序表的长度*/
}SeqList;

 

/* 初始化顺序表 */
SeqList SeqListInit()
{
    SeqList L;
    L.length=0;
    return L;
}
 
/* 清空顺序表 */

SeqList ListClear(SeqList L)
{
    L.length=0;
&nb......

阅读全文(3255) | 评论:0

练习:将有序顺序表归并为一个有序顺序表(2006-05-31 02:52:00)

摘要:/*实验2  顺序表其它操作

实验目的

1.进一步掌握在线性表的顺序存储结构上的一些其它操作。

实验内容

程序2

已知两个非递减有序的线性表LA和LB,将LA和LB合并成一个线性表LC,LC也非递减有序。
*/


#include <iostream>
#include <cstdio>
using namespace std;

#define ElemType int
#define MAXSIZE 100

//顺序表的操作
/* 顺序存储类型 */
typedef struct
{ElemType data[MAXSIZE]; /*存放线性表的数组*/
 int length;               /* length是顺序表的长度*/
}SeqList;

/* 初始化顺序表 */
SeqList SeqListInit( )
{SeqList L;
 L.length=0;
 return L;
 }


/* 求顺序表长度 */
int ListLength(SeqList L)
 {return(L.length);}

/* 遍历顺序表 */
void ListTraverse(SeqList L)
{int i;
 if(L.length<=0) printf("顺序表为空\n");
&nb......

阅读全文(3956) | 评论:0

Array is not pointer(2006-05-29 08:47:00)

摘要:Array is not pointer
Published by siavoshkc
Last update on Feb 20, 2006 at 5:48amWith many thanks for your useful tutorials, I felt it's necessary to send this text about pointers and arrays. Unfortunately pulling out something wrong that is put in humans head is a bit difficult. So understanding the things correct and precise is very important to avoid further misconceptions.
An array is not equal to a pointer. It is more like a simple variable in definition.
When we write
int array[3];array[2]=666;
C compiler doesn't see array[0] as an addre......

阅读全文(403) | 评论:0

The difference between pointers and arra(2006-05-29 02:16:00)

摘要:The difference between pointers and arrays
I have seen in many places that an array is introduced as a pointer. This is technically not correct. Arrays are not pointer. So what is it? It is like any other variable in C++.
Take a look at this code:
int arr[3]={3,4,5};cout<<arr;
You may say: "Look there, arr is an address, who says it's not a pointer?"
And I say: this code prints out an address. So var is an address?
int var;cout<<&var;
Let me explain:
All the variables can be manipulated in the memory by their a......

阅读全文(412) | 评论:0