代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
static int m=0,i=0;
void create(int **v);
void input(int *v);
void insert(int *v);
void del(int *v);
void print(int *v);
void menue();
void main()
{
int *v;
int ch;
menue();
while(1)
{
scanf_s("%d",&ch);
switch (ch)
{
case 1:
create(&v);
break;
case 2:
input(v);
print(v);
break;
case 3:
insert(v);
print(v);
break;
case 4:
del(v);
print(v);
break;
case 5:
print(v);
break;
case 6:
exit(0);
}
}
}
void create(int **v)
{
int *p;
printf("Input the length:");
cin>>m;
p=(int *)malloc(sizeof(int)*m);
*v=p;
}
void input(int *v)
{
i=0;
char s[5],de[]="end";
cout<<"Input number(press \"end\" to end):"<<endl;
while(1)
{
if(i==m)
{
cout<<"Full!you can't input any number then!"<<endl;
break;
}
scanf_s("%d",v+i);
gets_s(s);
if(strcmp(s,de)==0)
break;
i++;
};
}
void insert(int *v)
{
int q,j,s;
if(i==m)
{
cout<<"The form is full!can't insert!\n";
return;
}
cout<<"Input a number you want to insert:";
cin>>q;
cout<<"Input a location you want to insert:";
cin>>j;
for(s=i-1;s>=j-1;s--)
{
*(v+s+1)=*(v+s);
}
*(v+j-1)=q;
i++;
return ;
}
void del(int *v)
{
int q,s;
if(m==0)
{
printf("The form is empty!can't delete!\n");
return;
}
cout<<"Input the location of number you want to delete:";
cin>>q;
for(s=q-1;s<=i-1;s++)
{
*(v+s)=*(v+s+1);
}
i--;
}
void print(int *v)
{
int q;
cout<<"After operation:"<<endl;
for(q=0;q<=i-1;q++)
cout<<*(v+q)<<endl;
}
void menue()
{
printf("Press 1 to create a form!\n");
printf("Press 2 to add number to the form!\n");
printf("Press 3 to insert a number!\n");
printf("Press 4 to delete a number!\n");
printf("Press 5 to print the form!\n");
printf("Press 6 to exit!\n");
}
函数input中,用scanf_s()可以正常运行,而用cin>>*(v+i);后可以输入但是执行到insert函数会报错,这是为啥呢?
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
static int m=0,i=0;
void create(int **v);
void input(int *v);
void insert(int *v);
void del(int *v);
void print(int *v);
void menue();
void main()
{
int *v;
int ch;
menue();
while(1)
{
scanf_s("%d",&ch);
switch (ch)
{
case 1:
create(&v);
break;
case 2:
input(v);
print(v);
break;
case 3:
insert(v);
print(v);
break;
case 4:
del(v);
print(v);
break;
case 5:
print(v);
break;
case 6:
exit(0);
}
}
}
void create(int **v)
{
int *p;
printf("Input the length:");
cin>>m;
p=(int *)malloc(sizeof(int)*m);
*v=p;
}
void input(int *v)
{
i=0;
char s[5],de[]="end";
cout<<"Input number(press \"end\" to end):"<<endl;
while(1)
{
if(i==m)
{
cout<<"Full!you can't input any number then!"<<endl;
break;
}
scanf_s("%d",v+i);
gets_s(s);
if(strcmp(s,de)==0)
break;
i++;
};
}
void insert(int *v)
{
int q,j,s;
if(i==m)
{
cout<<"The form is full!can't insert!\n";
return;
}
cout<<"Input a number you want to insert:";
cin>>q;
cout<<"Input a location you want to insert:";
cin>>j;
for(s=i-1;s>=j-1;s--)
{
*(v+s+1)=*(v+s);
}
*(v+j-1)=q;
i++;
return ;
}
void del(int *v)
{
int q,s;
if(m==0)
{
printf("The form is empty!can't delete!\n");
return;
}
cout<<"Input the location of number you want to delete:";
cin>>q;
for(s=q-1;s<=i-1;s++)
{
*(v+s)=*(v+s+1);
}
i--;
}
void print(int *v)
{
int q;
cout<<"After operation:"<<endl;
for(q=0;q<=i-1;q++)
cout<<*(v+q)<<endl;
}
void menue()
{
printf("Press 1 to create a form!\n");
printf("Press 2 to add number to the form!\n");
printf("Press 3 to insert a number!\n");
printf("Press 4 to delete a number!\n");
printf("Press 5 to print the form!\n");
printf("Press 6 to exit!\n");
}
函数input中,用scanf_s()可以正常运行,而用cin>>*(v+i);后可以输入但是执行到insert函数会报错,这是为啥呢?