经典C++源程序
- 格式:doc
- 大小:62.50 KB
- 文档页数:9
求数组最大值,最小值及其下标
# include
void cout_max(void*p,int x,int n)
{
int max=0,min=0;
if(x==4)
{
float*p1;
p1=(float*)p;
for(int i=1;i
{
if(p1[i]>p1[max])
max=i;
if(p1[i]
min=i;
}
cout<<"float型数组中:\n";
cout<<"最大值为:"<
cout<<"最小值为:"<
}
if(x==8)
{
double*p1;
p1=(double*)p;
for(int i=1;i
{
if(p1[i]>p1[max])
max=i;
if(p1[i]
min=i;
}
cout<<"double型数组中:\n";
cout<<"最大值为:"<
cout<<"最小值为:"<
}
}
void main()
{
float a[]={12.3,25.3,21,36,21.0,54.32,1,9,8.53,4};
double b[]={12.3,60,25.3,21,21.0,54.32,1,0.56,9,8.53,4};
cout_max(a,sizeof(a[0]),sizeof(a)/sizeof(a[0]));
cout_max(b,sizeof(b[0]),sizeof(b)/sizeof(b[0]));
}
统计字母,数字等各类字符 # include
# include
void choose(char*p,int n)
{
int sum1=0,sum2=0,sum3=0,sum4=0;
for(int i=0;i
{
if(p[i]>='A'&&p[i]<='z'||p[i]>='a'&&p[i]<='z')
sum1++;
if(p[i]==' ')
sum2++;
if(p[i]>='0'&&p[i]<='9')
sum3++;
else
sum4++;
}
cout<<"统计结果为:\n";
cout<<"字母总数为:"<
cout<<"空格总数为:"<
cout<<"数字总数为:"<
cout<<"其他字符总数为:"<
}
void main()
{
char ch[100];
cout<<"请输入一段字符";
cin.getline(ch,81);
choose(ch,strlen(ch));
}
用调用函数,实现从两个数为输出较大者(要求用指针变量传递参数值
# include
void cout_max(float*a,float*b)
{
cout<<"最大数为:"<<((*a>*b)?*a:*b);
cout<
}
void main()
{
float a,b;
cout<<"请输入要比较的两个数:\n";
cin>>a>>b; cout_max(&a,&b);
}
坐标系下的坐标转化为极坐标下的坐标
# include
# include
void cout_change(float&x,float&y)
{
float c,q;
x=sqrt(x*x+y*y);
y=atan(y/x);
}
void main()
{
float x,y;
cout<<"请输入直角坐标系下的坐标:\n";
cout<<"x=";
cin>>x;
cout<<"y=";
cin>>y;
cout_change(x,y);
cout<<"极坐标系下的坐标为:\n";
cout<<"c="<
cout<<"q="<
}
C++程序链表例题
# include
# include
struct node
{
char num[12];
char name[20];
int age;
node*next;
};
struct LikedList
{
node*head;
int size;
};
struct student
{
char num[12]; char name[20];
int age;
};
LikedList create()
{
LikedList list;
node*p;
p=new node;
list.head=p;
list.size=0;
strcpy(p->name,"0");
strcpy(p->num,"0");
p->age=0;
p->next=NULL;
return list;
}
LikedList create(student*a,int n)
{
LikedList list=create();
list.size=n;
node*p1,*p2;
p2=list.head;
for(int i=0;i
{
p1=p2;
p2=new node;
strcpy(p2->num,(a+i)->num);
strcpy(p2->name,(a+i)->name);
p2->age=(a+i)->age;
p1->next=p2;
}
p2->next=NULL;
return list;
}
void search(LikedList&list,int a)
{
node*p1,*p2;
p2=list.head;
int sum=0;
for(int i=0;p2->next!=NULL;i++)
{
p1=p2; p2=p2->next;
if(p2->age==a)
{
cout<<"在链表中找到一个这个年龄的学生,并已删除该结点!\n";
p1->next=p2->next;
delete p2;
p2=p1;
sum++;
list.size--;
}
}
if(sum!=0)
{
cout<<"共删除"<
}
else
{
cout<<"无该年龄的学生,该结点已插入到链表尾端。\n";
p1=new node;
p1->age=a;
p1->next=NULL;
p2->next=p1;
list.size++;
}
cout<<"链表长度为:"<
}void removeall(LikedList&list)
{
node*p;
while(list.head!=NULL)
{
p=list.head;
list.head=list.head->next;
delete p;
}
}
void main()
{
int x;
cout<<"请输入要查找的年龄:";
cin>>x;
student a[]={{"001","小赵",15},{"002","小钱",16},{"003","小孙",17},{"004","小李",18},{"005","小赵",15},{"006","小周",19},{"007","小吴",15}};
LikedList list=create(a,sizeof(a)/sizeof(student));