28.选择结构案例-三只小猪称体重
#include<iostream>
using namespace std;
int main(){
int a=0,b=0,c=0;
cout<<"输入小猪ABC的体重:"<<endl;
cout<<"小猪A的体重为:"<<endl;
cin>>a;
cout<<"小猪B的体重为:"<<endl;
cin>>b;
cout<<"小猪C的体重为:"<<endl;
cin>>c;
if (a>b)
{
if (a>c)
{
cout<<"小猪A最重"<<endl;
}
else
{
cout<<"小猪C最重"<<endl;
}
}
else
{
if (b<c)
{
cout<<"小猪C最重"<<endl;
}
else
{
cout<<"小猪b最重"<<endl;
}
}
}
34.循环结构案例-水仙花数
#include<iostream>
using namespace std;
int main()
{
int a=100;
do
{
int b=0;
b=a%10;
int c=0;
c=a/10%10;
int d=0;
d=a/100;
if (b*b*b+c*c*c+d*d*d==a)
{
cout<<a<<endl;
}
a++;
} while (a<1000);
return 0;
}
36.循环结构案例--敲桌子
#include<iostream>
using namespace std;
int main()
{
int a=1;
do
{
if (a%10==7||a/10==7||a%7==0)
{
cout<<"敲桌子!"<<endl;
}
else
{
cout<<a<<endl;
}
a++;
} while (a<101);
return 0;
}
38.嵌套循环案例-乘法口诀表
#include<iostream>
using namespace std;
int main(){
for (int i = 1; i < 10; i++)
{
for (int j = 1; j < i+1; j++)
{
cout<<j<<"*"<<i<<"="<<i*j<<" ";
}
cout<<endl;
}
return 0;
}
41.一元数组案例--元素逆置
#include<iostream>
using namespace std;
int main(){
int arr[]={1,3,5,2,4};
int temp=0;
for (int i = 0; i <(5-1)/2; i++)
{
temp=arr[i];
arr[i]=arr[5-1-i];
arr[5-1-i]=temp;
}
for (int i = 0; i <5; i++)
{
cout<<arr[i];
}
return 0;
}
44.一维数组案例-5只小猪城体重
#include<iostream>
using namespace std;
int main(){
int arr[]={300,205,250,310,246};
int max=0;
for (int i = 0; i < 5; i++)
{
if (max<arr[i])
{
max=arr[i];
}
}
cout<<"最终的小猪为:"<<max<<endl;
return 0;
}
49.二维数组案例-考试成绩统计
#include<iostream>
using namespace std;
int main(){
int arr[3][3]=
{
{100,100,100},
{90,50,100},
{60,70,80}
};
string brr[]={"张三","李四","王五"};
for (int i = 0; i < 3; i++)
{
int sum=0;
for (int j = 0; j < 3; j++)
{
sum+=arr[i][j];
}
cout<<brr[i]<<"的总成绩为:"<<sum<<endl;
}
return 0;
}
63.指针配合数组和函数案例
#include<iostream>
using namespace std;
void upsort(int *arr,int len)
{
int temp=0;
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len-i-1; j++)
{
if (arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for (int i = 0; i < 10-1; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main(){
int arr[]={1,2,3,4,5,7,6,9,8};
int len=sizeof(arr)/sizeof(arr[0]);
upsort(arr,len);
return 0;
}
70.结构体案例1
#include<iostream>
using namespace std;
#include<ctime>
struct student{
string s_name;
int s_score;
};
struct teacher
{
string t_name;
student s;
};
void fuzhi(teacher t[],int len){
string nameseed="ABCDE";
for (int i = 0; i < 3; i++)
{
t[i].t_name=nameseed[i];
cout<<"老师"<<t[i].t_name<<"的学生为:"<<endl;
for (int j = 0; j < 5; j++)
{
t[i].s.s_name=nameseed[j];
t[i].s.s_score=rand()%41+60;
cout<<"学生"<<t[i].s.s_name<<"分数为:"<<t[i].s.s_score<<" ";
}
cout<<endl;
}
}
int main(){
srand((unsigned int)time (NULL));
teacher t[3];
int len=sizeof(t)/sizeof(t[0]);
fuzhi(t,len);
return 0;
}
71.结构体案例2
#include<iostream>
using namespace std;
struct hero
{
string m_name;
int age;
};
void agesort(hero *h)
{
for (int i = 0; i < 3-1; i++)
{
hero temp;
for (int j = 0; j < 3-i-1; j++)
{
if(h[j].age<h[j+1].age)
{
temp=h[j];
h[j]=h[j+1];
h[j+1]=temp;
}
}
}
for (int i = 0; i < 3; i++)
{
cout<<h[i].m_name<<h[i].age<<endl;
}
}
int main(){
hero h[3]=
{
{"张三",18},
{"李四",38},
{"王五",28}
};
agesort(h);
return 0;
}
72-83 通讯录管理系统
#include<iostream>
using namespace std;
#define max 1000
struct person
{
string m_name;
int m_sex;
string m_phone;
};
struct books
{
person p[max];
int m_index;
};
void addperson(books *abs)
{ string name;
int sex=0;
string phone;
if(abs->m_index==max){cout<<"通讯录已满,无法添加!"<<endl;}
else{
cout<<"输入您要添加联系人的姓名:"<<endl;
cin>>name;
abs->p[abs->m_index].m_name=name;
cout<<"输入您要添加联系人的性别:"<<endl;
cout<<"1是男,2是女."<<endl;
while(true)
{
cin>>sex;
if (sex==1||sex==2)
{
abs->p[abs->m_index].m_sex=sex;
break;
}
else{cout<<"重新输入:"<<endl;}
}
abs->p[abs->m_index].m_sex=sex;
cout<<"输入您要添加联系人的电话:"<<endl;
cin>>phone;
abs->p[abs->m_index].m_phone=phone;
abs->m_index++;
cout<<"添加成功!"<<endl;
return ;}
}
void showperson(books *abs)
{ if (abs->m_index==0)
{
cout<<"通讯录为空!"<<endl;
}
else{
for (int i = 0; i < abs->m_index; i++)
{
cout<<"姓名:"<<abs->p[i].m_name<<"\t"
<<"性别:"<<(abs->p[i].m_sex==1?"男":"女")<<"\t"
<<"电话:"<<abs->p[i].m_phone<<endl;
}}
}
void deleteperson(books *abs)
{ string name;
cout<<"请输入您要删除的联系人姓名:"<<endl;
cin>>name;
int a=0;
for (int i = 0; i < abs->m_index; i++)
{
if (abs->p[i].m_name==name)
{
cout<<"找到此人!你确定要删除吗?"<<"\t"
<<"1.是"<<"\t"<<"2.否"<<endl;
int a=0;
cin>>a;
if (a==1)
{
abs->p[i]=abs->p[i+1];
abs->m_index--;
cout<<"删除成功!"<<endl;
}
return ;
a++;
}
}
if(a==0){cout<<"查无此人!"<<endl;}
}
void showmenu(){
cout<<"-------------------------"<<endl;
cout<<"------1.添加联系人--------"<<endl;
cout<<"------2.显示联系人--------"<<endl;
cout<<"------3.删除联系人--------"<<endl;
cout<<"------4.查找联系人--------"<<endl;
cout<<"------5.修改联系人--------"<<endl;
cout<<"------6.清空通讯录--------"<<endl;
cout<<"------0.退出通讯录--------"<<endl;
cout<<"-------------------------"<<endl;
}
void searchperson(books *abs )
{
cout<<"请输入您要查找联系人的姓名:"<<endl;
string name;
cin>>name;
int a=0;
for (int i = 0; i <abs->m_index; i++)
{
if (abs->p[i].m_name==name)
{
cout<<"此人的信息如下:"<<endl;
cout<<"姓名:"<<abs->p[i].m_name<<"\t"
<<"性别:"<<(abs->p[i].m_sex==1?"男":"女")<<"\t"
<<"电话:"<<abs->p[i].m_phone<<endl;
a++;
}
}
if(a==0){cout<<"查无此人!"<<endl; }
}
void reviseperson(books *abs)
{
cout<<"请输入您要修改联系人的姓名:"<<endl;
string nama;
int a=0;
cin>>nama;
int sex;
string name;
string phone;
for (int i = 0; i <abs->m_index; i++)
{
if (abs->p[i].m_name==nama)
{
cout<<"姓名修改为:"<<endl;
cin>>name;
abs->p[i].m_name=name;
cout<<"性别修改为:"<<endl;
cout<<"1是男,2是女."<<endl;
while(true)
{
cin>>sex;
if (sex==1||sex==2)
{
abs->p[i].m_sex=sex;
break;
}
else{cout<<"重新输入:"<<endl;}
}
abs->p[i].m_sex=sex;
cout<<"电话修改为:"<<endl;
cin>>phone;
abs->p[i].m_phone=phone;
cout<<"修改成功!"<<endl;
a++;
break;}}
if(a==0){cout<<"查无此人!"<<endl; }
}
int main(){
books abs;
abs.m_index=0;
int select=0;
while (true)
{
showmenu();
cin>>select;
switch (select)
{
case 1:
addperson(&abs);
break;
case 2:
showperson(&abs);
break;
case 3:
deleteperson(&abs);
break;
case 4:
searchperson(&abs );
break;
case 5:
reviseperson(&abs);
break;
case 6:
abs.m_index=0;
cout<<"通讯录已清空!"<<endl;
break;
case 0:
cout<<"欢迎下次使用!"<<endl;
return 0;
break;
default:
break;
}}
return 0;
}
100.封装案例--设计学生类
#include<iostream>
using namespace std;
class student
{
public:
void setname(string name)
{
m_name=name;
}
void setID(string ID)
{
m_ID=ID;
}
void showstudent(){
cout<<this->m_name<<this->m_ID<<endl;
}
string m_name;
string m_ID;
};
int main()
{
student s;
s.setID("123");
s.setname("张三");
s.showstudent();
return 0;
}