主题:我用向量做的小程序,高手指点下
#include<stdio.h>
#include<malloc.h>
int A[10],N;
void create(int A[10],int N)
{int i;
for(i=0;i<N;++i)
scanf("%d",&A[i]);
for(i=0;i<N;++i)
printf("%d ",A[i]);
printf("\n");
printf("The length is %d\n",N);
}
void insert(int A[10],int n,int x)
{int i;
if(n<0||n>N)
printf("Wrong position!");
else
for(i=N-1;i>=n;--i)
A[i+1]=A[i];
A[n]=x;
N++;
for(i=0;i<N;++i)
printf("%d ",A[i]);
printf("\n");
printf("The length is %d\n",N);
}
void delete(int A[10],int n)
{int i;
if(n<0||n>N)
printf("Wrong position!");
else
for(i=N;i>n;--i)
A[i]=A[i-1];
A[N]=NULL;
N--;
for(i=0;i<N;++i)
printf("%d ",A[i]);
printf("\n");
printf("The length is %d",N);
}
void find(int A[10],int x)
{int i;
for(i=0;i<N;++i)
if(A[i]==x)
printf("It is in %d position",i);
else continue;
}
int main()
{ int i,n,x;
A[10]=(int)malloc(sizeof(int)*10);
printf("press 1 to create:\n");
printf("press 2 to insert:\n");
printf("press 3 to insert:\n");
scanf("%d",&i);
while(1)
switch(i)
{case 1:
{
printf("Create\n");
printf("Input a number below 10 for length:");
scanf("%d",&N);
create(A,N);
scanf("%d",&i);
} break;
case 2:
{
printf("Insert\n");
printf("Input two number:");
scanf("%d%d",&n,&x);
insert(A,n,x);
scanf("%d",&i);
} break;
case 3:
{printf("Find\n");
printf("Input what you want to find:");
scanf("%d",&x);
find(A,x);
scanf("%d",&i);
} break;
default :{printf("Input error!");
return 0;}
}
getch();
}
能运行的.
#include<malloc.h>
int A[10],N;
void create(int A[10],int N)
{int i;
for(i=0;i<N;++i)
scanf("%d",&A[i]);
for(i=0;i<N;++i)
printf("%d ",A[i]);
printf("\n");
printf("The length is %d\n",N);
}
void insert(int A[10],int n,int x)
{int i;
if(n<0||n>N)
printf("Wrong position!");
else
for(i=N-1;i>=n;--i)
A[i+1]=A[i];
A[n]=x;
N++;
for(i=0;i<N;++i)
printf("%d ",A[i]);
printf("\n");
printf("The length is %d\n",N);
}
void delete(int A[10],int n)
{int i;
if(n<0||n>N)
printf("Wrong position!");
else
for(i=N;i>n;--i)
A[i]=A[i-1];
A[N]=NULL;
N--;
for(i=0;i<N;++i)
printf("%d ",A[i]);
printf("\n");
printf("The length is %d",N);
}
void find(int A[10],int x)
{int i;
for(i=0;i<N;++i)
if(A[i]==x)
printf("It is in %d position",i);
else continue;
}
int main()
{ int i,n,x;
A[10]=(int)malloc(sizeof(int)*10);
printf("press 1 to create:\n");
printf("press 2 to insert:\n");
printf("press 3 to insert:\n");
scanf("%d",&i);
while(1)
switch(i)
{case 1:
{
printf("Create\n");
printf("Input a number below 10 for length:");
scanf("%d",&N);
create(A,N);
scanf("%d",&i);
} break;
case 2:
{
printf("Insert\n");
printf("Input two number:");
scanf("%d%d",&n,&x);
insert(A,n,x);
scanf("%d",&i);
} break;
case 3:
{printf("Find\n");
printf("Input what you want to find:");
scanf("%d",&x);
find(A,x);
scanf("%d",&i);
} break;
default :{printf("Input error!");
return 0;}
}
getch();
}
能运行的.