概述
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int len=0;
#define MAXSIZE 13
int main()
{
int a[10];
void start(int *a);
void create_array(int *a);
create_array(a);
start(a);
//out(a);
getchar();
return 0;
}
/*随机生成0-100的整数*/
void create_array(int *a)
{
srand(time(NULL));
int i;
for(i=0;i<10;i++)
{
a[i]=rand()%100+1;
}
len=10;
}
void out(int *a)
{
char s[5];
int i;
int x,y;
void menu(int *a);
x=0;
y=0;
initgraph(800,600);
setbkcolor(RGB(211,51,29));
cleardevice();
MOUSEMSG m;
FlushMouseMsgBuffer();
setfillcolor(WHITE);
fillrectangle(50,240,729,250);
fillrectangle(50,300,729,310);
fillrectangle(40,240,50,310);
fillrectangle(93,240,103,310);
fillrectangle(146,240,156,310);
fillrectangle(199,240,209,310);
fillrectangle(252,240,262,310);
fillrectangle(305,240,315,310);
fillrectangle(358,240,368,310);
fillrectangle(411,240,421,310);
fillrectangle(464,240,474,310);
fillrectangle(517,240,527,310);
fillrectangle(570,240,580,310);
fillrectangle(623,240,633,310);
fillrectangle(676,240,686,310);
fillrectangle(729,240,739,310);
settextcolor(WHITE);
setbkcolor(RGB(211,51,29));
settextstyle(36,18, _T("宋体"));
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(221,100,83));
solidroundrect(275,410,525,500,80,160);
setbkcolor(RGB(221,100,83));
setfillcolor(RGB(221,100,83));
outtextxy(310,437,"返回主菜单");
settextstyle(18, 8, _T("宋体"));
setbkcolor(RGB(211,51,29));
settextcolor(WHITE);
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(211,51,29));
for(i=0;i<len;i++)
{
sprintf(s, "%d", a[i]);
x = 64+i*53;
y = 270;
outtextxy(x,y,s);
}
while(1)
{
while (MouseHit())
{
m = GetMouseMsg();
if(m.x<=525 && m.x>=275 && m.y<=500 && m.y>=410)
{
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
menu(a);
break;
default:
break;
}
}
}
}
}
int check_button(int x,int y)
{
if(x>150 && x<650 && y>100 && y<200)
return 1;
else if(x>150 && x<650 && y>200 && y<300)
return 2;
else if(x>185 && x<615 && y>300 && y<400)
return 3;
else if(x>400 && x<600 && y>400 && y<500)
return 4;
else if(x>750 && x<800 && y>0 && y<50)
return 5;
else
return -1;
}
void event_mouse(int *a)
{
MOUSEMSG m;
char value[4];
int t;
void change(int *a);
int binarySearch(int *a, int value);
void out_havefind(int *a,int t);
void out_notfind(int *a);
void insert(int a[], int value);
void selectsort(int a[]);
while(1)
{
m=GetMouseMsg();
if(m.uMsg == WM_LBUTTONDOWN)
if(check_button(m.x,m.y)==1)
{
change(a);
out(a);
//menu();
closegraph();
break;
}
else if(check_button(m.x,m.y)==2)
{
int m ;
InputBox(value,20,"请输入查找元素的值:");
m = atoi(value);
t = binarySearch(a,m);
if(t==-1)
{
out_notfind(a);
}
else
{
out_havefind(a,t);
}
break;
}
else if(check_button(m.x,m.y)==3)
{
selectsort(a);
out(a);
break;
}
else if(check_button(m.x,m.y)==4)
{
int m ;
InputBox(value,20,"请输入插入元素的值:");
m = atoi(value);
insert(a,m);
break;
}
else if(check_button(m.x,m.y)==5)
{
exit(0);
closegraph();
break;
}
}
}
void menu(int *a)
{
void event_mouse(int *a);
initgraph(800,600);
setbkcolor(WHITE);
cleardevice();
//MOUSEMSG m;
//FlushMouseMsgBuffer();
setfillcolor(RGB(191,191,191));
setlinecolor(RGB(191,191,191));
ellipse(150,100,650,500);
line(150,300,650,300);
line(185,200,615,200);
line(185,400,615,400);
settextcolor(WHITE);
settextstyle(36,18, _T("宋体"));
setfillcolor(RGB(250,209,150));
setlinecolor(RGB(250,209,150));
floodfill(250,250,RGB(191,191,191));
setbkcolor(RGB(250,209,150));
outtextxy(270,230,"2.查找元素");
setfillcolor(RGB(158,213,232));
setlinecolor(RGB(158,213,232));
floodfill(380,160,RGB(191,191,191));
setbkcolor(RGB(158,213,232));
outtextxy(270,140,"1.最值交换");
setfillcolor(RGB(136,177,163));
setlinecolor(RGB(136,177,163));
floodfill(500,350,RGB(191,191,191));
setbkcolor(RGB(136,177,163));
outtextxy(270,330,"3.从小到大排序");
setfillcolor(RGB(249,146,145));
setlinecolor(RGB(249,146,145));
floodfill(450,450,RGB(191,191,191));
setbkcolor(RGB(249,146,145));
outtextxy(270,430,"4.插入整数");
setfillcolor(RGB(213,23,23));
fillrectangle(750,0,800,50);
setfillcolor(WHITE);
POINT pts1[] = { {751,0},{800,49},{799, 50}, {750,1},{751,0}};
solidpolygon(pts1, 4);
POINT pts2[] = { {799,0},{800,1},{751, 50}, {750,49},{799,0}};
solidpolygon(pts2, 4);
event_mouse(a);
}
void change(int *a)
{
void out(int *a);
int min,max;
int minp,maxp;
int i;
int t;
min = a[0];
minp = 0;
max = a[0];
maxp = 0;
for (i = 0; i<len; i++)
{
if (a[i] < min)
{
min = a[i];
minp = i;
}
if (a[i] > max)
{
max = a[i];
maxp = i;
}
}
t = a[maxp];
a[maxp] = a[minp];
a[minp] = t;
}
int binarySearch(int *a, int value)
{
void selectsort(int a[]);
int low = 0;
int high = len - 1;
int mid;
int midVal;
selectsort(a);
while(low <= high)
{
mid = (low + high)/2;
midVal = a[mid];
if(midVal < value)
low = mid + 1;
else if(midVal > value)
high = mid - 1;
else
return mid;
}
return -1;
}
void out_notfind(int *a)
{
char s[5];
int i;
int x,y;
void menu(int *a);
x=0;
y=0;
initgraph(800,600);
setbkcolor(RGB(211,51,29));
cleardevice();
MOUSEMSG m;
FlushMouseMsgBuffer();
setfillcolor(WHITE);
fillrectangle(50,240,729,250);
fillrectangle(50,300,729,310);
fillrectangle(40,240,50,310);
fillrectangle(93,240,103,310);
fillrectangle(146,240,156,310);
fillrectangle(199,240,209,310);
fillrectangle(252,240,262,310);
fillrectangle(305,240,315,310);
fillrectangle(358,240,368,310);
fillrectangle(411,240,421,310);
fillrectangle(464,240,474,310);
fillrectangle(517,240,527,310);
fillrectangle(570,240,580,310);
fillrectangle(623,240,633,310);
fillrectangle(676,240,686,310);
fillrectangle(729,240,739,310);
settextcolor(WHITE);
setbkcolor(RGB(211,51,29));
settextstyle(36,18, _T("宋体"));
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(221,100,83));
solidroundrect(275,410,525,500,80,160);
setbkcolor(RGB(221,100,83));
setfillcolor(RGB(221,100,83));
outtextxy(310,437,"返回主菜单");
settextstyle(18, 8, _T("宋体"));
setbkcolor(RGB(211,51,29));
settextcolor(WHITE);
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(211,51,29));
for(i=0;i<len;i++)
{
sprintf(s, "%d", a[i]);
x = 64+i*53;
y = 270;
outtextxy(x,y,s);
}
settextcolor(WHITE);
settextstyle(36,18, _T("宋体"));
setbkcolor(RGB(211,51,29));
outtextxy(300,150,"Not Find!");
while(1)
{
while (MouseHit())
{
m = GetMouseMsg();
if(m.x<=525 && m.x>=275 && m.y<=500 && m.y>=410)
{
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
menu(a);
break;
default:
break;
}
}
}
}
}
void out_full(int *a)
{
char s[5];
int i;
int x,y;
void menu(int *a);
x=0;
y=0;
initgraph(800,600);
setbkcolor(RGB(211,51,29));
cleardevice();
MOUSEMSG m;
FlushMouseMsgBuffer();
setfillcolor(WHITE);
fillrectangle(50,240,729,250);
fillrectangle(50,300,729,310);
fillrectangle(40,240,50,310);
fillrectangle(93,240,103,310);
fillrectangle(146,240,156,310);
fillrectangle(199,240,209,310);
fillrectangle(252,240,262,310);
fillrectangle(305,240,315,310);
fillrectangle(358,240,368,310);
fillrectangle(411,240,421,310);
fillrectangle(464,240,474,310);
fillrectangle(517,240,527,310);
fillrectangle(570,240,580,310);
fillrectangle(623,240,633,310);
fillrectangle(676,240,686,310);
fillrectangle(729,240,739,310);
settextcolor(WHITE);
setbkcolor(RGB(211,51,29));
settextstyle(36,18, _T("宋体"));
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(221,100,83));
solidroundrect(275,410,525,500,80,160);
setbkcolor(RGB(221,100,83));
setfillcolor(RGB(221,100,83));
outtextxy(310,437,"返回主菜单");
settextstyle(18, 8, _T("宋体"));
setbkcolor(RGB(211,51,29));
settextcolor(WHITE);
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(211,51,29));
for(i=0;i<len;i++)
{
sprintf(s, "%d", a[i]);
x = 64+i*53;
y = 270;
outtextxy(x,y,s);
}
settextcolor(WHITE);
settextstyle(36,18, _T("宋体"));
setbkcolor(RGB(211,51,29));
outtextxy(270,150,"数组已满,无法插入!");
while(1)
{
while (MouseHit())
{
m = GetMouseMsg();
if(m.x<=525 && m.x>=275 && m.y<=500 && m.y>=410)
{
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
menu(a);
break;
default:
break;
}
}
}
}
}
void selectsort(int a[])
{
int i,j,k,temp;
for(i=0;i<len-1;i++)
{
k=i;
for(j=i+1;j<len;j++)
{
if(a[j]<a[k])
k=j;
}
if(k!=i)
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
void out_havefind(int *a,int t)
{
char s[5];
int i;
int x,y;
void menu(int *a);
x=0;
y=0;
initgraph(800,600);
setbkcolor(RGB(211,51,29));
cleardevice();
MOUSEMSG m;
FlushMouseMsgBuffer();
setfillcolor(WHITE);
fillrectangle(50,240,729,250);
fillrectangle(50,300,729,310);
fillrectangle(40,240,50,310);
fillrectangle(93,240,103,310);
fillrectangle(146,240,156,310);
fillrectangle(199,240,209,310);
fillrectangle(252,240,262,310);
fillrectangle(305,240,315,310);
fillrectangle(358,240,368,310);
fillrectangle(411,240,421,310);
fillrectangle(464,240,474,310);
fillrectangle(517,240,527,310);
fillrectangle(570,240,580,310);
fillrectangle(623,240,633,310);
fillrectangle(676,240,686,310);
fillrectangle(729,240,739,310);
settextcolor(WHITE);
setbkcolor(RGB(211,51,29));
settextstyle(36,18, _T("宋体"));
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(221,100,83));
solidroundrect(275,410,525,500,80,160);
setbkcolor(RGB(221,100,83));
setfillcolor(RGB(221,100,83));
outtextxy(310,437,"返回主菜单");
settextstyle(18, 8, _T("宋体"));
setbkcolor(RGB(211,51,29));
settextcolor(WHITE);
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(211,51,29));
for(i=0;i<len;i++)
{
setbkcolor(RGB(211,51,29));
settextcolor(WHITE);
sprintf(s, "%d", a[i]);
x = 64+i*53;
y = 270;
if(i==t)
{
setbkcolor(YELLOW);
settextcolor(BLACK);
outtextxy(x,y,s);
}
outtextxy(x,y,s);
}
settextcolor(WHITE);
settextstyle(36,18, _T("宋体"));
setbkcolor(RGB(211,51,29));
outtextxy(300,150,"Have Find!");
while(1)
{
while (MouseHit())
{
m = GetMouseMsg();
if(m.x<=525 && m.x>=275 && m.y<=500 && m.y>=410)
{
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
menu(a);
break;
default:
break;
}
}
}
}
}
void insert_success(int *a,int value)
{
char s[5];
int i;
int x,y;
void menu(int *a);
x=0;
y=0;
initgraph(800,600);
setbkcolor(RGB(211,51,29));
cleardevice();
MOUSEMSG m;
FlushMouseMsgBuffer();
setfillcolor(WHITE);
fillrectangle(50,240,729,250);
fillrectangle(50,300,729,310);
fillrectangle(40,240,50,310);
fillrectangle(93,240,103,310);
fillrectangle(146,240,156,310);
fillrectangle(199,240,209,310);
fillrectangle(252,240,262,310);
fillrectangle(305,240,315,310);
fillrectangle(358,240,368,310);
fillrectangle(411,240,421,310);
fillrectangle(464,240,474,310);
fillrectangle(517,240,527,310);
fillrectangle(570,240,580,310);
fillrectangle(623,240,633,310);
fillrectangle(676,240,686,310);
fillrectangle(729,240,739,310);
settextcolor(WHITE);
setbkcolor(RGB(211,51,29));
settextstyle(36,18, _T("宋体"));
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(221,100,83));
solidroundrect(275,410,525,500,80,160);
setbkcolor(RGB(221,100,83));
setfillcolor(RGB(221,100,83));
outtextxy(310,437,"返回主菜单");
settextstyle(18, 8, _T("宋体"));
setbkcolor(RGB(211,51,29));
settextcolor(WHITE);
setlinecolor(RGB(211,51,29));
setfillcolor(RGB(211,51,29));
for(i=0;i<len;i++)
{
setbkcolor(RGB(211,51,29));
settextcolor(WHITE);
sprintf(s, "%d", a[i]);
x = 64+i*53;
y = 270;
if(a[i]==value)
{
setbkcolor(YELLOW);
settextcolor(BLACK);
outtextxy(x,y,s);
}
outtextxy(x,y,s);
}
settextcolor(WHITE);
settextstyle(36,18, _T("宋体"));
setbkcolor(RGB(211,51,29));
outtextxy(300,150,"插入成功!");
while(1)
{
while (MouseHit())
{
m = GetMouseMsg();
if(m.x<=525 && m.x>=275 && m.y<=500 && m.y>=410)
{
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
menu(a);
break;
default:
break;
}
}
}
}
}
void insert(int a[], int value)
{
int i;
int j;
void out_full(int *a);
void selectsort(int a[]);
void insert_success(int *a,int value);
selectsort(a);
if(len < MAXSIZE) // 判断是否超出数组的最大容量
{
if (value >= a[len-1])
{
a[len] = value;
len++;
}
else
{
for(i=0; i<len; i++)
{
if(value < a[i])
{
for(j=len;j>i;j--)
{
a[j] = a[j-1];
}
a[i] = value;
break;
}
}
len++;
}
insert_success(a,value);
}
else
out_full(a);
}
void start(int *a)
{
MOUSEMSG m;
FlushMouseMsgBuffer();
initgraph(800,600);
setbkcolor(WHITE);
cleardevice();
setfillcolor(RGB(255,239,0));
POINT pts1[] = { {400,50},{463,222},{650,229}, {500,350},{550,523},{400,422}, {250,522},{300,350},{150,228},{335,225},{400,50}};
solidpolygon(pts1, 10);
setbkcolor(RGB(255,239,0));
settextcolor(RED);
settextstyle(30,15, _T("宋体"));
outtextxy(310,300,"生成随机数组");
while(1)
{
while (MouseHit())
{
m = GetMouseMsg();
if(m.x<=600 && m.x>=200 && m.y<=450 && m.y>=250)
{
switch(m.uMsg)
{
case WM_LBUTTONDOWN:
out(a);
break;
default:
break;
}
}
}
}
}
最后
以上就是细腻铃铛为你收集整理的C语言数组的插入删除-easyx的全部内容,希望文章能够帮你解决C语言数组的插入删除-easyx所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复