CenTyger

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、tyger:

1: int getch(); 键盘读入一个字符;不在屏幕显示;读入enter为'\r'=13,制表符'\t'=9,空格' '=32,退格'\b'=9;没有用到缓冲区

* 2: int getche(); 键盘读入一个字符;并在屏幕显示;读入enter为‘\r’,...同上...;没有用到缓冲区

* 3: int getchar();缓冲区读一个字符;在屏幕显示;enter为'\n'=10;需要缓冲区;

* 4: int getc(FILE *stream); 从指定输入流(stdin,文件)读一个字符;

* 5: int fgetc(FILE *stream); 与getc相同;但:getc是宏操作,fgetc是函数;getc()可以作为其他函数参数;

* 6: int fgetchar(); 与getchar相同;区别参考getc和fgetc;

*

* 7: char* gets(char* buffer);从stdin流中读取字符串(换行\n,或EOF为结束),换行不为读取内容,读取的换行转为NULL;

* 8: char* fgets(char* str;int n,FILE* stream);从stream中读取n-1个字符,存入str为起始地址的空间内(换行\n或EOF结束),字符串包扩换行

*

*cout<<"\r";光标回到本行首

 1 /*
 2  * 1: int getch();  键盘读入一个字符;不在屏幕显示;读入enter为'\r'=13,制表符'\t'=9,空格' '=32,退格'\b'=9;没有用到缓冲区
 3  * 2: int getche(); 键盘读入一个字符;并在屏幕显示;读入enter为‘\r’,...同上...;没有用到缓冲区
 4  * 3: int getchar();缓冲区读一个字符;在屏幕显示;enter为'\n'=10;需要缓冲区;
 5  * 4: int getc(FILE *stream);   从指定输入流(stdin,文件)读一个字符;
 6  * 5: int fgetc(FILE *stream);  与getc相同;但:getc是宏操作,fgetc是函数;getc()可以作为其他函数参数;
 7  * 6: int fgetchar();   与getchar相同;区别参考getc和fgetc;
 8  *
 9  * 7: char* gets(char* buffer);从stdin流中读取字符串(换行\n,或EOF为结束),换行不为读取内容,读取的换行转为NULL;
10  * 8: char* fgets(char* str;int n,FILE* stream);从stream中读取n-1个字符,存入str为起始地址的空间内(换行\n或EOF结束),字符串包扩换行;
11  *
12  *cout<<"\r";光标回到本行首
13  * /
View Code
1 /*
2  *顺序表的相关操作演示
3  * 1)从键盘输入(或从文件读入)线性表的各个数据元素,创建一个顺序表;
4  * 2)查找顺序表中第i个数据元素,或查找所有值等于e的数据元素;
5  * 3)在顺序表的任意位置插入一个新元素;
6  * 4)删除顺序表中的任意一个元素;
7  * 5)打印顺序表中的各个元素的值;
8  *
9  */
线性表题目要求
 1 #include <iostream>
 2 #include <malloc.h>
 3 #include <conio.h>
 4 using namespace std;
 5 
 6 #define TURE    1
 7 #define FALSE   0
 8 #define OK      1
 9 #define ERROR   0
10 #define INFEASIBLE  -1      //不可行,
11 #define OVERFLOW    -2      //溢出
12 typedef int Status;
13 //*************************************************
14 #define DateType    char
15 #define LIST_INIT_SIZE  10
16 #define LIST_ADD_SIZE   10
17 typedef struct{
18     DateType *elem;
19     int length,size;
20 }List;
库、宏定义

 

操作函数:

 

 1 void ListInit(List &L);
 2 void ListDestroy(List &L);
 3 void ListInput(List &L);
 4 void ListPrint(List L);
 5 Status ListInsert(List &L,int pos,DateType e);
 6 Status ListFind_i(List L,int pos,DateType &e);
 7 Status ListFind_e(List L,int &pos,DateType e);
 8 Status ListDelete(List &L,DateType e);
 9 
10 char Init_ui(void);
函数

 

源代码实现

/*
 *顺序表的相关操作演示
 * 1)从键盘输入(或从文件读入)线性表的各个数据元素,创建一个顺序表;
 * 2)查找顺序表中第i个数据元素,或查找所有值等于e的数据元素;
 * 3)在顺序表的任意位置插入一个新元素;
 * 4)删除顺序表中的任意一个元素;
 * 5)打印顺序表中的各个元素的值;
 *
 */
#include <iostream>
#include <malloc.h>
#include <conio.h>
using namespace std;

#define TURE    1
#define FALSE   0
#define OK      1
#define ERROR   0
#define INFEASIBLE  -1      //不可行,
#define OVERFLOW    -2      //溢出
typedef int Status;
//*************************************************
#define DateType    char
#define LIST_INIT_SIZE  10
#define LIST_ADD_SIZE   10
typedef struct{
    DateType *elem;
    int length,size;
}List;

//**************************************************
void ListInit(List &L);
void ListDestroy(List &L);
void ListInput(List &L);
void ListPrint(List L);
Status ListInsert(List &L,int pos,DateType e);
Status ListFind_i(List L,int pos,DateType &e);
Status ListFind_e(List L,int &pos,DateType e);
Status ListDelete(List &L,DateType e);

char Init_ui(void);
//**************************************************
int main()
{
    List A;
    ListInit(A);
    while(1)
    {
        char xuanzhe=-2;
        xuanzhe=Init_ui();
        switch(xuanzhe)
        {
        case '0'://退出
            exit(1);
            break;
        case '1'://输入
            ListInput(A);
            break;
        case '2'://输出
            ListPrint(A);
            break;
        case '3'://插入元素
            cout<<"\n--------------任意位置插入一个元素----------------"<<endl;
            cout<<"输入插入位置和元素:";
            int pos;
            DateType e;
            scanf("%d %c",&pos,&e);
            //cin>>pos>>e;    //以空格隔开
            if(OK==ListInsert(A,pos,e))     cout<<"插入成功!"<<endl;
            else                            cout<<"插入失败!"<<endl;
            break;
        case '4'://查找
            cout<<"\n--------------查找------------------------------"<<endl;
            cout<<"\t1-以位置查找元素值"<<endl<<"\t2-查找元素所在的位置"<<endl;
            char ch;
            ch=getche();
            while(!(ch=='1' || ch=='2' )){
                cout<<"\r输入有误!重新输入:";
                ch=getche();
            }
            if(ch=='1'){
                int pos;    DateType e;
                cout<<"\n输入位置:";  scanf("%d",&pos);
                ListFind_i(A,pos,e);
                cout<<"查找结果:"<<endl;
                cout<<e<<endl;
            }
            else if(ch=='2'){
                DateType e;  int pos;
                cout<<"\n输入查找的元素:";   e=getche();
                ListFind_e(A,pos,e);
            }
            break;
        case '5'://删除元素
            cout<<"\n---------------------删除元素------------------"<<endl;
            DateType ee;
            cout<<"输入删除的元素:";
            ee=getche();
             if(ListDelete(A,ee)==OK)    cout<<"删除成功!"<<endl;
             else                   cout<<"删除失败!"<<endl;
            break;
        default:
            cout<<"OK!"<<endl;
        }

    }
    return 0;
}
//-------------------------------------------------
void ListInit(List &L)//表初始化
{
    //L.elem=new DateType[LIST_INIT_SIZE];
    L.elem=(DateType*)malloc(LIST_INIT_SIZE*sizeof(DateType));
    if(!L.elem)  exit(1);
    L.length=0;
    L.size=LIST_INIT_SIZE;
}

void ListDestroy(List &L) //表销毁
{
    if(L.elem){
        free(L.elem);
    }
    L.elem=NULL;
    L.length=0;
    L.size=0;
}

Status ListFind_i(List L,int pos,DateType &e)//查找位置i的值,以e返回
{
    if(pos<1 || pos>L.length)   return INFEASIBLE;
    e=L.elem[pos-1];
    return OK;
}
Status ListFind_e(List L,int &pos,DateType e)
{
    if(L.length==0)
        return INFEASIBLE;
    int i;
    pos=0;
    cout<<"\n查找结果:"<<endl;
    for(i=0;i<L.length-1;i++){
        if(L.elem[i]==e){
            if(pos==0)  pos=i+1;
            cout<<i+1<<" ";
        }
    }
    cout<<endl;
    return OK;
}

Status ListInsert(List &L,int pos,DateType e)//在pos位置插入元素e
{
    if(pos<1 || pos>L.length+1)
        return INFEASIBLE;      //不可行
    if(L.length>=L.size)        //当前容量不够
    {
        DateType *newelem;
        newelem=(DateType*)realloc(L.elem,LIST_ADD_SIZE+L.size);
        if(!newelem) return ERROR;//错误
        L.elem=newelem;
        L.size+=LIST_ADD_SIZE;
    }//容量足够
    int i;
    if(L.length==0) i=pos-1;
    else    i=L.length+1;
    while(i!=pos-1){
        L.elem[i]=L.elem[i-1];
        i--;
    }
    L.elem[pos-1]=e;
    ++L.length;
    return OK;
}

void ListInput(List &L)
{
    cout<<"\n输入数据(回车结束):"<<endl;
    DateType ch;
    L.length=0;
    while((ch=getche())!='\r' && L.length<L.size){
        L.elem[L.length++]=ch;
    }
}
Status ListDelete(List &L,DateType e)
{
    if(L.length==0) return INFEASIBLE;
    int pos,n=0;
    for(pos=0;pos+n<L.length;pos++){
        if(L.elem[pos]==e || L.elem[pos+1]==e){
            n++;
        }
        if(n!=0){
            L.elem[pos]=L.elem[pos+n];
        }
    }
    L.length-=n;
    return OK;
}
char Init_ui(void)
{
    char xuanzhe;
    cout << "\n********************** 顺序表菜单 ****************************" << endl;
    cout << "\t1: 输入顺序表" << endl;
    cout << "\t2: 输出顺序表" << endl;
    cout << "\t3: 插入数据" << endl;
    cout << "\t4: 查找" << endl;
    cout << "\t5: 删除数据" << endl;
    cout << "\t0- 退出" << endl;
    cout << "请选择:";
    xuanzhe=getche();
    while(!(xuanzhe=='0' || xuanzhe=='1' || xuanzhe=='2' || xuanzhe=='3' || xuanzhe=='4' || xuanzhe=='5'))
    {
        cout<<"\r输入有误!重新选择:";
       // cin.ignore(1024,'\n');      //清除缓冲区\n和之前的数据,或清除1024个数据
        xuanzhe=getche();
    }
    return xuanzhe;
}
void ListPrint(List L)//输出
{
    cout<<"\n顺序表为:"<<endl;
    if(!L.length)
        cout<<"NULL"<<endl;
    else
    {
        int i=0;
        do{
            cout<<L.elem[i++];
        }
        while(i<L.length);
    }
}
源代码实现

 

posted on 2016-07-08 23:22  CenTyger  阅读(199)  评论(0编辑  收藏  举报