C语言小程序(六)、数组操作
对数组进行操作,查找、插入、删除。
#include <stdio.h>
#include <stdlib.h>
#include <time.h> int size = 0;
int flag = 0; void output(int *arry)
{
int i = 0;
for(i=0; i<size; i++)
{
printf("arry[%d]=%d\t",i,arry[i]);
if((i+1)%5 == 0)
printf("\n");
}
printf("\n");
} void getarry(int *arry)
{
int i = 0;
srand(time(NULL));
for(i=0; i<size; i++)
{
arry[i] = rand() % 100;
}
} void add(int *arry, int pos, int num)
{
int i = 0;
if(pos>=0 && pos<=size)
{
if(pos < size) //在中间插入
{
for(i=size; i>pos; i--)
{
arry[i] = arry[i-1];
}
arry[pos] = num;
}
else //在最后的位置插入
{
arry[size] = num;
}
size++;
}
else
printf("只能在0-%d的位置插入。\n",size);
} int search(int *arry, int num)
{
static int pos = 0;
if(flag)
pos++;
for(; pos<size; pos++)
{
if(arry[pos] == num)
{
flag = 0;
return pos;
}
}
return -1;
} void mod(int *arry, int pos, int num)
{
if(pos>=0 && pos<size)
{
arry[pos] = num;
}
else
{
printf("输入位置错误。\n");
}
} int del(int *arry, int num)
{
int count = 0;
int pos = 0;
int i = 0;
pos=search(arry, num);
while(pos+1)
{
for(i=pos; i<size; i++)
{
arry[i] = arry[i+1];
}
count++;
pos=search(arry, num);
} return count;
} int main()
{
//pos 0到size-1
int *arry = NULL;
int count = 0;
int pos = 0;
int num = 0; printf("输入要产生多少个随机数:");
scanf("%d",&size);
arry = malloc(2*size*sizeof(int)); getarry(arry);
output(arry); printf("输入要添加的位置(0-%d):",size);
scanf("%d",&pos);
printf("输入要添加的数字:");
scanf("%d",&num);
add(arry, pos, num);
output(arry); printf("输入要查找的数字:");
scanf("%d",&num);
pos=search(arry, num);
while(pos+1)
{
flag = 1;
count++;
printf("arry[%d]=%d\n",pos, num);
pos=search(arry, num);
}
printf("共找到%d个匹配数字\n",count); printf("输入要修改的位置:");
scanf("%d",&pos);
printf("输入要修改为数字:");
scanf("%d",&num);
mod(arry, pos, num);
output(arry); printf("输入要删除的数字:");
scanf("%d",&num);
del(arry, num);
output(arry); free(arry);
arry = NULL; return 0;
}
C语言小程序(六)、数组操作的更多相关文章
- 微信小程序之数组操作:push与concat的区别
微信小程序中需要用到数组的操作,push和concat二者功能很相像,但有两点区别. 先看如下例子: var arr = []; arr.push(); arr.push(); arr.push([, ...
- 【微信小程序】数组操作
Page({ data: { list:[{ id:1, name:'应季鲜果', count:1 },{ id:2, name:'精致糕点', count:6 },{ id:3, name:'全球美 ...
- 小程序JSON数组操作
- C语言小程序——推箱子(窄字符和宽字符)
C语言小程序——推箱子(窄字符Version) 推箱子.c #include <stdio.h> #include <conio.h> #include <stdlib. ...
- Linux下简单C语言小程序的反汇编分析
韩洋原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 写在开始,本文为因为参加MOO ...
- c语言小程序以及java生成注释文档方法
c语言小程序:sizeof和strlen() sizeof运算符以字节为单位给出数据的大小,strlen()函数以字符为单位给出字符串的长度,字符和字节不是一回事. char类型用于存储字母和标点符号 ...
- 通过反汇编C语言小程序学习Liunx汇编语言
大家好! 我是来自山东师范大学的吴乐. 今天在<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ...
- 微信小程序弹出操作菜单
微信小程序弹出操作菜单 比如在页面上放一个按钮,点击按钮弹出操作菜单,那么在按钮的 bindtap 事件里,执行下面的代码即可: wx.showActionSheet({ itemList: ['A' ...
- Linux C语言小程序
Linux C语言小程序 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include & ...
- 图解微信小程序---调用API操作步骤
图解微信小程序---调用API操作步骤 什么是API API(Application Programming Interface,应用程序编程接口:是一些预先定义的函数,目的是提供应用程序与开发人员基 ...
随机推荐
- Android蓝牙通信具体解释
蓝牙通信的大概过程例如以下: 1.首先开启蓝牙 2,搜索可用设备 3,创建蓝牙socket.获取输入输出流 4,读取和写入数据 5.断开连接关闭蓝牙 还要发送配对码发送进行推断! 以下是全部的源码:不 ...
- app 之间发送文件 ios
本文转载至 http://www.51094.com/?p=212 第一种: 发送一个正常的 pdf 文件,只要是能读取pdf 的都能得到响应 -(IBAction)openDocumentIn ...
- Myecplise Tomcat 启动很慢
今天突然遇到一个问题,tomcat在Myecplse启动非常慢,直接用tomcat自带的start.bat启动很快,如果通过Myeclipse启动会发现项目一直在实例化,最后发现是因为加了断点调试,断 ...
- easyUI参数传递Long型时,前台解析出错的问题——SKY
果发现datagrid在显示Long类型数据时有问题.问题如下:比如一个数据ID为20121229101239002,经过转换之后的JSON数据也没有问题,但是在显示的时候就会显示为201212291 ...
- elasticsearch从入门到出门-06-剖析Elasticsearch的基础分布式架构
这个图来自中华石杉:
- iOS 导航引发坐标高度问题
iOS7 后导航结构发生变化,有新的控制属性诞生,一下为两个属性引发的控制器视图高度问题 translucent = YES 导航透明 (默认) translucent = NO 导航 ...
- Is this its limit?
import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(c ...
- Bootstrap学习-菜单-按钮-导航
1.下拉菜单(基本用法) 在使用Bootstrap框架的下拉菜单时,必须调用Bootstrap框架提供的bootstrap.js文件.当然,如果你使用的是未编译版本,在js文件夹下你能找到一个名为“d ...
- 次小生成树Tree
次小生成树Treehttps://www.luogu.org/problemnew/show/P4180 题目描述 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正 ...
- 1.搭建Django开发环境
1.安装python(版本3.5.1) 官网下载:https://www.python.org/downloads/release/python-351/2.更新pip 命令:python -m pi ...