您的位置:首页 > 编程语言 > C语言/C++

关于软件技术基础上机报告(段景山 电子科技大学)若干问题漫谈

2015-04-04 20:21 459 查看
关于软件技术基础上机报告(段景山 电子科技大学)若干问题漫谈
本来准备就上机遇见的问题稍作分析,可时日已久,遂弃,放出代码,求大神斧正(IDE:vc++6.0,vs2013)

题目如文末图

附录一
程序代码:
/*ex1_1*/
#include<stdio.h>
#define MAXNUM 20
#define true 1
#define false 0
typedef struct
{
int data[MAXNUM];
int length;
}list_type;

void createlist(list_type *lp)
{
int i, elem;
lp->length=0;
printf("\nplease input datas of the list\n");
for(i=0; i< MAXNUM; i++)
{
scanf(" %d", &elem);
if(elem== -1) break;
lp->data[i]=elem;
lp->length++;
}
}

void showlist(list_type *lp)
{
int i;
printf("\nThese %d records are:\n", lp->length);
if(lp->length<=0)
{
printf("No data!\n");
return;
}
for(i=0; i<lp->length; i++)
printf(" %d ", lp->data[i]);
printf("\nlength of the list is:%d\n", lp->length);
}

int insertlist(list_type *lp, int new_elem, int i)
{  int j;
if(lp->length>=MAXNUM)
{
printf("the list is full,can not insert.");
return(false);
}
if(i<1 || i>lp->length+1)
{
printf("\n%d is invalid value",i);
return(false);
}
for(j=lp->length-1; j>=i-1; j--)
lp->data[j+1]=lp->data[j];
lp->data[i-1]=new_elem;
lp->length++;
return(true);
}

int deletelist(list_type *lp, int i)
{
int j;
if(i<1 || i>lp->length)
{
printf("elem not exist");
return(false);
}
for(j=i; j<lp->length; j++)
lp->data[j-1]=lp->data[j];
lp->length--;
return(true);
}

void delete_negative(list_type *lp)
{
int t,i,p=0;
for(i=p;i<lp->length;i++)
{
if(lp->data[i]<0&&i<lp->length-1)
{
p=i;
for(t=i; t<lp->length; t++)
lp->data[t]=lp->data[t+1];
i--;
lp->length--;
}
else if(lp->data[i]<0&&i==lp->length-1)
lp->length--;
}
}

void main( )
{  list_type list;
int i, data;
createlist(&list);
showlist(&list);
printf("\ninsert:Enter i and data :\n");
scanf("%d,%d", &i, &data);
insertlist(&list, data, i);
printf("\nlist after insert:\n");
showlist(&list);
printf("\ndelete:Enter i:\n");
scanf("%d", &i);
deletelist(&list, i);
printf("\nlist after delete:\n");
showlist(&list);
delete_negative(&list);
printf("\nlist after delete all negative:\n");
showlist(&list);
}

/*ex1-2*/
#include<stdio.h>
#define MAXNUM 10
#define elenum 15
#define true 1
#define false 0
typedef struct
{
int data[MAXNUM];
int length;
}list_type;
/*create a list*/
void createlist(list_type *lp)
{
int i, elem;
lp->length=0;
pri
4000
ntf("\nplease input datas of the list\n");
for(i=0; i< MAXNUM; i++)
{
scanf(" %d", &elem);
if(elem== -1) break;
lp->data[i]=elem;
lp->length++;
}
}

void showlist(list_type *lp)
{
int i;
printf("\nThese %d records are:\n", lp->length);
if(lp->length<=0)
{
printf("No data!\n");
return;
}
for(i=0; i<lp->length; i++)
printf(" %d ", lp->data[i]);
printf("\nlength of the list is:%d\n", lp->length);
}

int insertlist(list_type *lp, int new_elem)
{  int j,i;
if(lp->length>=MAXNUM)
{
printf("the list is full,can not insert.");
return(false);
}
for(j=0;new_elem>=lp->data[j]&&j<lp->length;j++){}
for(i=lp->length-1;i>=j;i--){
lp->data[i+1]=lp->data[i];
}
lp->data[i+1]=new_elem;
lp->length++;
return(true);
}

void main( )
{  list_type list;
int data;
createlist(&list);
showlist(&list);
printf("\ninsert:Enter data :\n");
scanf("%d", &data);
insertlist(&list, data);
printf("\nlist after insert:\n");
showlist(&list);
}
运行结果:
Ex1_1:

Ex1_2:

附录二
程序代码:
/*Ex2_1*/
#include<stdio.h>
#include <malloc.h>
typedef struct node_type      //定义链点
{
int data;
struct node_type *next;
}node_type;

typedef struct list_type      //定义链表
{
node_type *head;
node_type *tail;
int length;
} list_type;

int read()
{
int x;
scanf("%d",&x);
return x;
}
void error(int x)
{
switch(x)
{
case 1:
printf("\nthe place of the data is wrong ,please input the place again\n");break;
}
}

void creat_list(list_type *node)			//创建链表
{
node_type *p,*s;				//注意此处的指针要为链点结构体类型
int x;
node->head=(node_type*)malloc(sizeof(node_type));
node->length=0;
p=node->head;
while(node->length<5)			//输入五个数
{
scanf("%d",&x);
s=(node_type*)malloc(sizeof(node_type));
s->data=x;
p->next=s;
p=s;			//在链点连接上出现了问题导致后面显示链表时也出问题
node->length++;
}
p->next=NULL;
node->tail=p;
}
void show_list(list_type *node)				//把链表元素打印出来
{
node_type *p;
p=node->head->next;
printf("\nThe linked list is\n\n");
while(p!=NULL)
{
printf("%d   ",p->data);
p=p->next;					//p往下走一步
}
printf("\nThe length of this linked list is  %d\n",node->length);

}

void insert_list(list_type *node,int newdata,int place)
{
node_type *newnode,*p;
int i=0;
newnode=(node_type*)malloc(sizeof(node_type));  //动态申请内存
newnode->data=newdata;
p=node->head;
while(node->length+1<place||place<1)//判断插入的位置是否正确
{
error(1);
place=read();			//位置错误则重新输入位置
}
while(i<place-1)		//使p指针指向插入位置的前一个
{
p=p->next;
i++;
}
newnode->next=p->next;		//插入链点
p->next=newnode;
if(newnode->next==NULL)
{
node->tail=newnode;		//若插入的位置为表尾,则改变尾指针
}
node->length++;

}

void delete_list(list_type *node,int place)
{
int i=0;
node_type *p,*q;
p=node->head;

while(place>node->length||place<1)	//检查删除元素的位置是否符合要求
{
error(1);
place=read();
}
while(i<place-1)		//使p指针指向删除位置的前一个
{
p=p->next;
i++;
}
q=p->next;
p->next=p->next->next;		//删除链点
free(q);
if(p->next==NULL)			//若删除的是最后一个元素,则改变尾指针的指向
{
node->tail=p;
}
node->length--;		    	//链表长度自减一
}

void main()
{
list_type node;
int newdata,place;
int del_place;

printf("please input the the list\n");
creat_list(&node);			//创建链表 ,把链表这一包含头和尾的指针传过去
show_list(&node);			//而没有像书上一样传链点
printf("\nplease input the new data\n");
newdata=read();
printf("\nplease input the place of the new data where it should be\n");
place=read();
insert_list(&node,newdata,place);
show_list(&node);
printf("\nplease input the place of the number which will be deleted\n");
del_place=read();
delete_list(&node,del_place);
show_list(&node);
}

/*Ex2_2*/
#include<stdio.h>
#include <malloc.h>

typedef struct node_type      //定义链点
{
int data;
struct node_type *next;
}node_type;

typedef struct list_type      //定义链表
{
node_type *head;
node_type *tail;
int length;
} list_type;

int read()
{
int x;
scanf("%d",&x);
return x;
}
void error(int x)
{
switch(x)
{
case 1:
printf("\nthe place of the data is wrong ,please input the place again\n");break;
}
}

void creat_list(list_type *node)			//创建链表
{
node_type *p,*s;				//注意此处的指针要为链点结构体类型
int x;
node->head=(node_type*)malloc(sizeof(node_type));
scanf("%d",&x);
node->length=0;
p=node->head;
while(x!=0)			//判断是否继续输入
{
s=(node_type*)malloc(sizeof(node_type));
s->data=x;
p->next=s;
p=s;			//在链点连接上出现了问题导致后面显示链表时也出问题
node->length++;
scanf("%d",&x);
}
p->next=NULL;
node->tail=p;
}

void show_list(list_type *node)				//把链表元素打印出来
{
node_type *p;
p=node->head->next;
printf("\nThe linked list is\n\n");
while(p!=NULL)
{
printf("%d   ",p->data);
p=p->next;					//p往下走一步
}
printf("\nThe length of this linked list is  %d\n",node->length);

}

void insert_list(list_type *node,int newdata,int place)
{
node_type *newnode,*p;
int i=0;
newnode=(node_type*)malloc(sizeof(node_type));  //动态申请内存
newnode->data=newdata;
p=node->head;

while(node->length+1<place||place<1)//判断插入的位置是否正确
{
error(1);
place=read();			//位置错误则重新输入位置
}
while(i<place-1)		//使p指针指向插入位置的前一个
{
p=p->next;
i++;
}
newnode->next=p->next;		//插入链点
p->next=newnode;
if(newnode->next==NULL)
{
node->tail=newnode;		//若插入的位置为表尾,则改变尾指针
}
node->length++;

}

void delete_list(list_type *node,int place)
{
int i=0;
node_type *p,*q;
p=node->head;

while(place>node->length||place<1)	//检查删除元素的位置是否符合要求
{
error(1);
place=read();
}
while(i<place-1)		//使p指针指向删除位置的前一个
{
p=p->next;
i++;
}
q=p->next;
p->next=p->next->next;		//删除链点
free(q);
if(p->next==NULL)			//若删除的是最后一个元素,则改变尾指针的指向
{
node->tail=p;
}
node->length--;		    	//链表长度自减一
}

void search_list(list_type *node,int number )//寻找那个整数
{
node_type *p;
int i=0;
int m;
m=node->length;			//记录原来链表的长度
p=node->head;

while(p->next!=NULL)	//p如果指向最后一个元素,则跳出循环
{
while(p->next->data==number)
{
delete_list(node,i+1);//p的下一个元素如果是这个数,那么删除
if(p->next==NULL)//若p已经指向最后一个那么跳出删除的循环
{
break;
}
}
if(p->next!=NULL)	//若p没有指向最后一个那么让p指向下一个
{
p=p->next;
i++;
}
}

if(i==m)		//若链表中元素没有与指定数相同的,则将数插入链表
{
insert_list(node,number,m+1);
}
}

void main()
{
list_type node;
int number;

printf("please input the the list\n");
creat_list(&node);			//创建链表 ,把链表这一包含头和尾的指针传过去
show_list(&node);			//而没有像书上一样传链点
printf("\nplease input the number that you want to search\n");
number=read();
search_list(&node,number);
show_list(&node);
}

/*Ex2_3*/
#include<stdio.h>
#include <malloc.h>

typedef struct node_type      //定义链点
{
int data;
struct node_type *next;
}node_type;

typedef struct list_type      //定义链表
{
node_type *head;
int length;
} list_type;

int read()
{
int x;
scanf("%d",&x);
return x;
}

void creat_list(list_type *node)			//创建链表
{
node_type *p,*s;				//注意此处的指针要为链点结构体类型
int x;
node->head=(node_type*)malloc(sizeof(node_type));
scanf("%d",&x);
node->length=0;
p=node->head;
while(x!=0)			//判断是否继续输入
{
s=(node_type*)malloc(sizeof(node_type));
s->data=x;
p->next=s;
p=s;			//在链点连接上出现了问题导致后面显示链表时也出问题
node->length++;
scanf("%d",&x);
}
p->next=NULL;
}

void show_list(list_type *node)				//把链表元素打印出来
{
node_type *p;
p=node->head->next;
printf("\nThe linked list is\n\n");
while(p!=NULL)
{
printf("%d   ",p->data);
p=p->next;					//p往下走一步
}
printf("\nThe length of this linked list is  %d\n",node->length);

}

void insert_list(list_type *node,int newdata)
{
node_type *newnode,*p;
newnode=(node_type*)malloc(sizeof(node_type));  //动态申请内存
newnode->data=newdata;
p=node->head;
while(newdata > p->data&&p->next!=NULL)	//使p指针指向插入位置的前一个
{
p=p->next;
}
newnode->next=p->next;		//插入链点
p->next=newnode;
node->length++;
}

void main()
{
list_type node;
int number;
printf("please input the the list\n");
creat_list(&node);			//创建链表 ,把链表这一包含头和尾的指针传过去
show_list(&node);			//而没有像书上一样传链点
printf("\nplease input x\n");
number=read();
insert_list(&node,number);
show_list(&node);
}
运行结果:
Ex2_1:
Ex2_2:

Ex2_3:

附录三
程序代码:
/*Ex3_1*/
#include <stdio.h>
#include <stdlib.h>
#define true  1
#define false 0
typedef struct node
{
int data;
struct node *next;
}node_type;

typedef struct
{
node_type  *top;
int  length;
}lstack_type;

void push(lstack_type *lp,  int x)
{
node_type *p;
p=(node_type *)malloc(sizeof(node_type));
if(p!=NULL)
{
p->data = x;
p->next = lp->top;
lp-> top = p;
}
}
void pop(lstack_type *lp)
{
node_type *p;
if(lp->top==NULL)
{
return;
}
else
{
p = lp->top;
lp->top = lp->top->next;
free(p);
}
}

node_type *print( node_type *head )
{
node_type *temp;
temp=head;
while( temp!=NULL)
{
printf("%d	",temp->data);
temp=temp->next;
}
return 0;
}

void main()
{
int i,j;
i=0;j=0;
lstack_type M,*lp;
lp=&M;
lp->top=NULL;
printf("Please input the data(end by '0'):\n");
scanf("%d",&i);
while(i!=0)
{
push(lp,i);
scanf("%d",&i);
}
printf("All data is:\n");
print(lp->top);
printf("\nThe first pop:\n");
pop(lp);
print(lp->top);
printf("\nThe second pop:\n");
pop(lp);
print(lp->top);
}

/*Ex3_2*/
#include <stdio.h>
#define MAXNUM 20
#define true 1
#define false 0

typedef struct
{     int data[MAXNUM];
int front, rear;
}queue_type;

int  enqueue(queue_type *q,  int x)
{
if( (q->rear+1)%MAXNUM == q->front)
return(false);
else
{
q->data[q->rear]=x;	   /*新元素入队尾*/
q->rear=(q->rear+1)%MAXNUM; /*移rear*/
return 1;
}
}
int dequeue(queue_type *q)
{
if(q->rear==q->front) /*判队列是否为空*/
{
printf("queue is empty");
return 0; 		  /*返回空值*/
}
else
{
q->front=(q->front+1)%MAXNUM;
return(q->data[q->front-1]);
}
}
void aa(queue_type *q)
{
int i,j=0;
i=dequeue(q);
while(j<10)
{
if(i>0)
{
enqueue(q, i);
j++;
i=dequeue(q);
}
else
{
j++;
i=dequeue(q);
}
}
if(q->front>q->rear)
{
for(i=q->front;i<MAXNUM-1;i++)
{
printf("%d   ",q->data[i]);
}
for(i=0;i<q->rear;i++)
{
printf("%d   ",q->data[i]);
}
}
else
{
for(i=q->front-1;i<q->rear;i++)
{
printf("%d   ",q->data[i]);
}
}
}
void main()
{
queue_type M,*lp;
int i,a[]={2, 3, -4, 6, -5, 8, -9, 7, -10, 20};
lp=&M;
lp->front=0;
lp->rear=0;
for(i=0;i<10;i++)
{
enqueue(lp,a[i]);
}
printf("调用函数前队列元素为:\n");
for(i=0;i<10;i++)
{
printf("%d   ",lp->data[i]);
}
printf("\n调用函数后队列元素为:\n");
aa(lp);
}

/*Ex3_3*/
#include <stdio.h>
#define M 20
#define true 1
#define false 0
typedef struct{
int data[M];
int  top1,top2;
}stack_type;
int  push1(stack_type *s)
{
int x;
printf("请输入数据(以’0‘结束):\n");
scanf("%d",&x);
while(x!=0&&s->top1<=s->top2)
{
s->data[s->top1] = x;
s->top1 = s->top1+1;
scanf("%d",&x);
}
if(s->top1==s->top2)
printf("栈已满!\n");
return;
}
int  push2(stack_type *s)
{
int x;
printf("请输入数据(以’0‘结束):\n");
scanf("%d",&x);
while(x!=0&&s->top1<=s->top2)
{
s->data[s->top2] = x;
s->top2= s->top2-1;
scanf("%d",&x);
}
if(s->top1==s->top2)
printf("栈已满!\n");
return;
}
int pop1(stack_type *s)
{
int   out;
if(s->top1 < 0) return(false);
else
{
out = s->data[s->top1-1];
s->top1 = s->top1-1;
return(out);
}
}
int pop2(stack_type *s)
{
int   out;
if(s->top2 >=M) return(false);
else
{
out = s->data[s->top2+1];
s->top2= s->top2+1;
return(out);
}
}

void main()
{
int i,j,k;
stack_type as,*lp;
lp=&as;
lp->top1=0;
lp->top2=M-1;
while(1)
{
printf("请选择你要操作的栈:输入“1”或“2”\n");
printf("返回“0”\n");
scanf("%d",&i);
if(i==1)
{
printf("请选择你要进行的操作:入栈“1”;出栈“2”\n");
scanf("%d",&k);
switch(k)
{
case 1:push1(lp);
break;
case 2:j=pop1(lp);
printf("出栈元素为:%d\n",j);
break;
default:printf("输入错误!\n");
}
}
else if(i==2)
{
printf("请选择你要进行的操作:入栈“1”;出栈“2”\n");
scanf("%d",&k);
switch(k)
{
case 1:push2(lp);
break;
case 2:j=pop2(lp);
printf("出栈元素为:%d\n",j);
break;
default:printf("输入错误!\n");
}
}
else break;
}
}
/*Ex3_4*/
#include <stdio.h>
#define MAXNUM 5
#define true 1
#define false 0
typedef struct
{     int data[MAXNUM];
int front, rear,tag;
}queue_type;
int  enqueue(queue_type *q,  int x)
{

q->data[q->rear]=x;
q->rear=(q->rear+1)%MAXNUM;
return(true);

}
int dequeue(queue_type *q)
{
if(q->rear==q->front&&q->tag==0)
{
printf("queue is empty");
return(0);
}
else
{
q->front=(q->front+1)%MAXNUM;
return(q->data[q->front-1]);
}
}
void main()
{
queue_type M,*lp;
int i,j=0,k;
lp=&M;
lp->front=0;
lp->rear=0;
lp->tag=0;
printf("请输入元素(以-999结束):\n");
for(i=0;i<MAXNUM;i++)
{
scanf("%d",&k);
if(k!=-999)
{
enqueue(lp,k);
j++;
lp->tag=2;
if(j==MAXNUM)
{
lp->tag=1;
printf("队列已满!\n");
break;
}
}
else
break;
}
printf(
bbff
"队列元素为:\n");
if(lp->front>=lp->rear)
{
for(i=lp->front;i<MAXNUM;i++)
{
printf("%d   ",lp->data[i]);
}
for(i=0;i<lp->rear;i++)
{
printf("%d   ",lp->data[i]);
}
}
else
{
for(i=lp->front;i<lp->rear;i++)
{
printf("%d   ",lp->data[i]);
}
}
}
运行结果:
Ex3_1:

Ex3_2:

Ex3_3:

Ex3_4:

附录四
程序代码:
/*Ex4_1*/
#include<stdio.h>
#include<malloc.h>
#include<windows.h>
typedef struct bnode
{
int data;
struct bnode *LC, *RC;
}bonde;//树节点的定义
typedef struct tree_type{
bnode *root;
int num;
}tree_type;//树的定义
bnode *proof;
void creat_tree(bnode *p)
{
int num;
bnode *t;
p = NULL;/*初始化空树*/
if (scanf_s("%d", &num) && num != 0)
{
proof = (bnode *)malloc(sizeof(bnode));
proof->data = num;
proof->LC = NULL;
proof->RC = NULL;
}
while (scanf_s("%d", &num) && num != 0)
{
t = (bnode *)malloc(sizeof(bnode));
t->data = num;
t->LC = NULL;
t->RC = NULL;
p = proof;
while (p != NULL)
{
if (p->data>num)
{
if (p->LC != NULL)
p = p->LC;
else
{
p->LC = t;
p = NULL;/*插入左子树*/
}
}
else
{
if (p->RC != NULL) p = p->RC;
else
{
p->RC = t;
p = NULL;
}
}
}
}
}//创建二叉排序树
void process(bonde *root)
{
printf("%d,", root->data);
}//显示当前的指针所指的地址的数据
void inorder(bonde *root)
{
if (root->LC != NULL)
inorder(root->LC);
process(root);
if (root->RC != NULL)
inorder(root->RC);
}//中序
void postorder(bnode *root)
{
if (root->LC != NULL)
postorder(root->LC);
if (root->RC != NULL)
postorder(root->RC);
process(root);
}//后序
void preorder(bnode *root)
{
process(root);
if (root->LC != NULL)
preorder(root->LC);
if (root->RC != NULL)
preorder(root->RC);
}//先根
void main()
{
bnode *p;
p = NULL;
printf("input number:\n");
creat_tree(p);
printf("Now,it is the order after use the preorder\n");
preorder(proof);
printf("\n");
printf("Now,it is the order after use the inorder\n");
inorder(proof);
printf("\n");
printf("Now,it is the order after use the postorder\n");
postorder(proof);
system("pause");
}

/*Ex4_2*/
#include<stdio.h>

#include<windows.h>

#define MAX 30

#define inf 100000000

typedef char valType;

typedef int wghType;

struct HFMnode

{

valType data;

wghType weight;

int parent;

int lchild;

int rchild;

};

struct HFMcode

{

char code[MAX];

int start;

};

void createHFMtree(HFMnode *node, int n)

{

int i, m1, m2, l, r;

for (i = n + 1; i <= 2 * n - 1; i++)

{

m1 = m2 = inf;

l = r = 0;

int k;

for (k = 1; k <= i - 1; k++)

if (node[k].parent == 0)

{

if (node[k].weight<m1)

{

m2 = m1;

r = l;

m1 = node[k].weight;

l = k;

}

else if (node[k].weight<m2)

{

m2 = node[k].weight;

r = k;

}

}

node[i].weight = node[l].weight + node[r].weight;

node[i].lchild = l;

node[i].rchild = r;

node[l].parent = i;

node[r].parent = i;

}

}

void createHFMcode(HFMnode *node, HFMcode *hcode, int n)

{

int i;

for (i = 1; i <= n; i++)

{

HFMcode d;

d.start = n;

int num = i;

int father = node[num].parent;

while (father != 0)

{

if (node[father].lchild == num)

d.code[d.start--] = '0';

else d.code[d.start--] = '1';

num = father;

father = node[num].parent;

}

hcode[i] = d;

}

}

void printHFMcode(HFMnode * node, HFMcode * hcode, int n)

{

int i;

for (i = 1; i <= n; i++)

{

printf("%c: ", node[i].data);

for (int k = hcode[i].start + 1; k <= n; k++)

putchar(hcode[i].code[k]);

puts("");

}

}

void main()

{

HFMnode node[2 * MAX]; HFMcode hcd[MAX];

int n;

scanf_s("%d\n", &n);

for (int i = 1; i <= n; i++){

printf("输入第%d个节点的值\n", i);

scanf_s("%c", &node[i].data);

printf("输入它的权重\n");

scanf_s("%d\n", &node[i].weight);

}

for (int i = 1; i <= 2 * n - 1; i++)

node[i].parent = node[i].lchild = node[i].rchild = 0;

createHFMtree(node, n);

createHFMcode(node, hcd, n);

printHFMcode(node, hcd, n);

system("pause");
}
运行结果:
Ex4_1:

Ex4_1:

附录五
程序代码:
/*Ex5_1*/
#include<stdio.h>
#include<math.h>
int seq_search(int S[], int n, int k)
{
int i = 0;
S
= k;
while (S[i] != k)
i++;
if (i<n)
{
printf("searching success\n");
return(i);
}
else
{
printf("searching failed\n");
return(-1);
}
}
int bin_search(int S[], int k, int n)
{
int low, high, mid;
low = 0;
high = n - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (S[mid] == k)
{
printf("searching success\n");
return(mid);
}
else if (S[mid]<k)
low = mid + 1;
else
high = mid - 1;
}
printf("searching failed\n");
return(-1);
}
void main()
{
int S[8] = { 3, 10, 13, 17, 40, 43, 50, 70 };
int i, j, n, m;
i = seq_search(S, 8, 43);
printf("%d\n", i);
j = seq_search(S, 8, 5);
printf("%d\n", j);
n = bin_search(S, 43, 8);
printf("%d\n", n);
m = bin_search(S, 5, 8);
printf("%d\n", m);
}

/*Ex5_2*/
#include<stdio.h>
#include<windows.h>
#define MAXNUM 10
#define small s
typedef struct
{
int data;
}list_type;

void selectsort(list_type x[], int n)
{
list_type swap;
int i, j, small, k;
for (i = 0; i<n - 1; i++)
{
small = i;
for (j = i + 1; j<n; j++)
{
if (x[j].data<x[small].data)
small = j;
}
if (small != i)
{
swap = x[i];
x[i] = x[small];
x[small] = swap;
}
for (k = 0; k<n; k++)
{
printf(" %d ", x[k].data);
}
printf("\n");

}
}

void insertsort(list_type x[], int n)
{
list_type a;
int i, j, k;
for (i = 0; i<n - 1; i++)
{
a = x[i + 1];
j = i;
while (j>-1 && a.data<x[j].data)
{
x[j + 1] = x[j];
j--;

}

x[j + 1] = a;
for (k = 0; k<n; k++)
{
printf(" %d ", x[k].data);
}
printf("\n");
}

}

void bubblesort(list_type x[], int n)
{
int i, j, k, flag = 1;
list_type swap;
for (i = 0; i<n - 1 && flag == 1; i++)
{
flag = 0;
for (j = 0; j<n - i - 1; j++)
{
if (x[j].data>x[j + 1].data)
{
flag = 1;
swap = x[j];
x[j] = x[j + 1];
x[j + 1] = swap;
}

}
for (k = 0; k<n; k++)
{
printf(" %d ", x[k].data);
}
printf("\n");
if (flag == 0)
break;
}

}
void main()
{
list_type x[MAXNUM], y[MAXNUM], z[MAXNUM];
int i, b;
printf("\n please input datas(-1 at the end) \n");
for (i = 0; i< MAXNUM; i++)
{
scanf_s(" %d", &b);
if (b == -1) break;
x[i].data = b;
y[i].data = b;
z[i].data = b;
}
printf("\n the result of the selectsort is:\n");
selectsort(x, i);

printf("\n the result of the insertsort is:\n");
insertsort(y, i);
printf("\n the result of the bubblesort is:\n");
bubblesort(z, i);
system("pause");
}
运行结果:
Ex5_1:

Ex5_2:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  源码 c 软件 vc++ 段景山