c语言插入的函数 c语言加入数学函数

C语言数组中插入元素的函数

void  insert( int a[ ], int  n, int  number )

成都创新互联从2013年开始,先为松阳等服务建站,松阳等地企业,进行企业商务咨询服务。为松阳企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

{

for (int i=0;in;i++)

{

if (a[i]number)

continue;

else 

{

for (int j=n;ji;j--)

{

a[j]=a[j-1];

}

a[i]=number;

break;

}

}

if (i==n)

a[i]=number;

}

C语言,insert函数

表示“插入”,它并不是C语言的一部分,用在函数名中可以表示插入元素。

例题:

#include iostream

#include vector

using namespace std;

int main()

{

vectorint intArray;

int i;

for(i=0;i10;i++)

{

intArray.push_back(i);

coutintArray[i]" ";

}

coutendl;

intArray.insert(intArray.begin()+1,intArray.begin()+3,intArray.begin()+5);

for(i=0;iintArray.size();i++)

coutintArray[i]" ";

system("pause");

return 0;

}

用C语言怎么写个 字符串插入函数

程序的大体思路可以是这样:

str1是原字符串,str2是待插入的字符串,position是待插入的位置,我们可以这样,用一个指针p_cur指向字符串1 str1中的待插入位置position,另一个指针p_end指向字符串1 str1的尾部,每次插入字符前,把str1中从当前位置开始一直到结束字符全部后移一个位置,空出当前位置,然后把要插入的字符放进这个位置,这样就完成了一个字符的插入,重复这个步骤,直到str2被完全插入。

代码如下:

#include stdio.h

#include string.h

void insert_str(char str1[],char str2[],int position)

{

/*

insert_str()函数

功能:将字符串str2插入到str1的position位置处

参数:char str1,char str2 ,int position

返回值:无

*/

int i;

char *p_end,*p_cur,*p;/*p_end指向第一个字符串的尾部,p_cur指向被插入的位置*/

p_end=str1+strlen(str1)-1;

p_cur=str1+position-1;

for(i=0;str2[i]!='\0';i++)

{

for(p=p_end;p=p_cur;p--)

{

*(p+1)=*p;/*从p_cur到p_end的全部元素后移一个位置,此时p_cur指向的位置就空出来了*/

}

*p_cur=str2[i];/*把字符串2中的字符插入空出来的位置*/

p_cur++;/*p_cur下移一个位置*/

p_end++;/*多了一个字符,因此p_end也下移一个位置*/

}

}

void main()

{

char s1[100],s2[20];

int position;

printf("输入字符串1:\n");

gets(s1);

printf("输入插入位置:");

do

{

scanf("%d",position);

while(getchar()!='\n');/*这一句可以把输入position的时候输入的回车去掉*/

}while(position0||positionstrlen(s1));

printf("输入字符串2:\n");

gets(s2);

insert_str(s1,s2,position);

printf("字符串被插入后变成:\n");

puts(s1);

}

C语言编写一个插入删除函数

一般呢,插入和删除函数是分开写的,还有分成两种存储结构,1.顺序表,2.链表,我给你一个我上数据结构时候写的链表的操作,里面全都有,如果不会用,追问我

#includestdio.h

#includemalloc.h

#includeWindows.h

#includeconio.h

#includestdlib.h

typedef struct

{

int data;

struct LNode *next;

}LNode;

LNode *Listinit(LNode *L)//初始化链表返还头指针

{

L = (LNode *)malloc(sizeof(LNode));

if (!L)return 0;

L-next = NULL;

return L;

}

int GetElem_L(LNode *L, int i, int *e)//取第i个元素

{

int j;

LNode *p;

p=L-next;j=1;

while(pji)

{

p=p-next;++j;

}

if(!p||ji) return 0;//i超过表长

*e=p-data;

return 1;

}

int ListInsert_L(LNode *L, int i, int e)//插入数据元素

{

LNode *p1 = L,*p2=L;

int j = 0;

if (i-1 LinkLength(L))

return 2;

while(p1!=NULL ji-1)

{

p1 = p1-next;

j++;

}

p2 = (LNode *)malloc(sizeof(LNode));

if (!p2)

return 0;

p2-data = e;

p2-next = p1-next;

p1-next = p2;

return 1;

}

void ClearList(LNode *L)//重置为空表

{

LNode *p;

while(L-next)

{

p=L-next;

L-next=p-next;

free(p);

}

}

void print_link(LNode *L)//输出函数

{

LNode *p = L;

p = p-next;

while (p != NULL)

{

printf("%5d", p-data);

p = p-next;

}

}

int ListDlete_L(LNode *L, int i, int *e)//删除L中I,并用e返回

{

int j = 0;

LNode *p1 = NULL, *p2 = NULL;

p1 = L;

while (p1-next != NULL j i - 1)

{

p1 = p1-next;

j++;

}

if (p1-next == NULL || j i - 1)

return 0;

p2 = p1-next;

p1-next = p2-next;

free(p2);

return 1;

}

int LinkLength(LNode *L)//链表的长度

{

int i = 0;

LNode *p = L-next;

while (p != NULL)

{

i++;

p = p-next;

}

return i;

}


新闻标题:c语言插入的函数 c语言加入数学函数
标题网址:http://pwwzsj.com/article/ddedgde.html