C++语言程序设计 五只小猪称体重(在一个数组中找到最大值) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std;int main () { int arr[5 ]={300 ,350 ,200 ,400 ,250 } int MAX=0 ; for (int i=0 ;i<5 ;i++) { if (arr[i]>MAX) { MAX=arr[i]; } } cout<<"最重的小猪体重为:" <<MAX<<endl; return 0 ; }
数组元素逆置(如原数组元素为1,2,3,4,5逆置后输出结果为5,4,3,2,1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <iostream> using namespace std;int main () { int start=0 ; int end=sizeof (arr)/sizeof (arr[0 ]-1 ); while (start<end) { int temp=arr[start]; arr[start]=arr[end]; arr[end]=temp; atart++; end--; } cout<<"数组元素更新后:" <<endl; for (int i=0 ;i<5 ;i++) { cout<<arr[i]<<endl; } system ("pause" ); return 0 ; }
结构体案例 案例1:学校正在做毕设项目,每名老师带领5个学生,总共有三名老师,需求如下 设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员,学生的成员有姓名,考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值,最终打印出老师数据以及老师所带的学生数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 #include <iosteam> using namespace std;#include <string> #include <Ctime> struct Student ;{ string sName; int score; } struct Teacher { string tName; struct Student sArray[5 ]; } void allocateSpace (struct Teacher tArray[],int len) { string nameSeed="ABCDE" ; for (int i=0 ;i<len;i++) { tArray[i].tName="Teacher_" ; tArray[i].tName+=nameSeed[i]; for (int j=0 ;j<5 ;j++) { tArray[i].sArray[j].sName="Student_" ; tArray[i].sArray[j].sname+=nameSeed[j]; int random=rand ()%61 +40 ; tArray[i].sArray[j].score=random; } } } void printinfo (struct Teacher tArray[],int len) { for (int i=0 ;i<len;i++) { cout<<"老师姓名:" <<tArray[i].tName<<endl; for (int j=0 ;j<5 ;j++) { cout<<"\t学生姓名:" <<tArray[i].sArray[j].sName<<" 考试分数:" <<tArray[i].sArray[j].score<<endl; } } } int main () { srand ((unsigned int )time (NULL )) struct Teacher tArray[3 ]; int len=sizeof (tArray)/sizeof (tArray[0 ]); allocateSpace (tArray,len); printInfo (); }
案例2:设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄,通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。 五名英雄信息如下:
1 2 3 4 5 {"刘备",23,"男"} {"关羽",22,"男"} {"张飞",20,"男"} {"赵云",21,"男"} {"貂蝉",19,"女"}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 #include <iostream> using namespace std;#include <string> struct Hero { string name; int age; string sex; }; void bubbleSort (struct Hero heroArray[],int len) { for (int i=0 ;i<len-1 ;i++) { for (int j=0 ;j<len-i-1 ;j++) { if (heroArray[j].age>heroArray[j+1 ],age) { struct Hero temp=heroArray[j]; heroArray[j]=heroArray[j+1 ]; heroArray[j+1 ]=temp; } } } } void printHero (struct Hero heroArray[],int len) { for (int i=0 ;i<len;i++) { cout<<"排序后排序:" <<"姓名:" <<heroArray[i].name<<"年龄:" <<heroArray[i].age<<"性别:" <<heroArray[i].sex<<endl } } int main () { struct Hero heroArray[5 ]= { {"刘备" ,23 ,"男" }, {"关羽" ,22 ,"男" }, {"张飞" ,20 ,"男" }, {"赵云" ,21 ,"男" }, {"貂蝉" ,19 ,"女" }, } int len=sizeof (heroArray)/sizeof (heroArray[0 ]); for (i=0 ;i<len;i++) { cout<<"姓名:" <<heroArray[i].name<<"年龄:" <<heroArray[i].age<<"性别:" <<heroArray[i].sex<<endl } bubbleSort (heroArray,len); printHero (HeroArray,len); }
通讯录管理系统
添加联系人:向通讯录中添加新人,信息包括(姓名,性别,年龄,联系电话,家庭住址)最多纪录1000人
显示联系人:显示通讯录中所有联系人信息
删除联系人:按照姓名进行删除指定联系人
查找联系人:按照姓名查看指定联系人信息
修改联系人:按照姓名重新修改指定联系人
清空联系人:清空通讯录里全部信息
退出通讯录:退出当前使用的通讯录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include <iostream> using namespace std;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; } int main () { struct Addressbooks abs; abs.m_Size=0 ; int select=0 ; while (true ){ showMenu (); cin>>select; switch (switch_on) { case 1 : addPerson (&abs); break ; case 2 : break ; case 3 : deletePerson (&abs); break ; case 4 : findPerson (&abs); break ; case 5 : modifyPerson (&abs); break ; case 6 : break ; case 0 : cout<<"欢迎下次使用" <<endl; system ("pause" ); return 0 ; break ; } } system ("pause" ); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 1. 设计联系人结构体:姓名,性别,年龄,联系电话,家庭住址2. 设计通讯录结构体3. main函数中创建通讯录4. 封装添加联系人函数5. 测试添加联系人功能#include <iostream> #include <string> using namespace std;#define MAX 1000 struct Person { string m_name; int m_Sex; int mPhone; string m_Addr; }; struct Addressbooks { struct Person personArrray[MAX]; int m_Size; } void addPerson (struct Addressbooks * abs) { if (abs->m_Size==MAX) { cout<<"通讯录已满,无法添加!" <<endl; return ; } else { string name; cout<<"请输入联系人:" <<endl; cin>>name; abs->personArray[abs->m_Size].m_Name=name; cout<<"请输入性别:" <<endl; cout<<"1----男" <<endl; cout<<"2----女" <<endl; int sex=0 ; if (sex==1 ||sex==2 ) { abs->personArray[abs->m_Size].m_Sex=sex; break ; } cout<<"请输入年龄:" <<endl; int age=0 ; cin>>age; abs->personArray[abs->m_Size].m_Age=age; cout<<"请输入联系电话:" <<endl; string Phone; cin>>phone; abs->persomArray[abs->m_Size].m_Phone=phone; cout<<"请输入家庭住址:" <<endl; string address; cin>>address; abs->personArray[abs->m_Size].m_Addr=address; abs->m_Size++; cout<<"添加成功" <<endl; system ("pause" ); system ("cls" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 void showPerson (Addressbooks * abs) { if (abs->m_Size==0 ) { cout<<"当前记录为空" <<endl; } else { for (int i=0 ;i<abs->m_Size;i++) { cout<<"姓名:" <<abs->personArray[i].m_Name<<"\t" ; cout<<"性别:" <<(abs->personArray[i].m_Sex==1 ?"男" :"女" )<<"\t" ; cout<<"年龄:" <<abs->personArray[i].m_Age<<"\t" ; cout<<"电话:" <<abs->personArray[i].m_Phone<<"\t" ; cout<<"住址:" <<abs->personArray[i].m_Addr<<endl; } } system ("pause" ); system ("cls" ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 int isExist (Addressbooks *abs,string name) { for (int i=0 ;i<abs->m_Size;i++) { if (abs->personArray[i].m_Name==name) { return i; } } return -1 ; } void deletePerson (Addressbooks * abs) { cout<<"请输入您要删除的联系人" <<endl; string name; cin>>name; int ret=isExist (abs,name); if (ret!-1 ) { for (int i=ret;i<abs->m_Size;i++) { abs->personArray[i]=abs->personArray[i+1 ]; } abs->personArray[i]=abs->personArray[i+1 ]; cout<<"删除成功" <<endl; } else { cout<<"未查到此人" <<endl; } system ("pause" ); system ("cls" ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 void findPerson (Addressbooks * abs) { cout<<"请输入您要查找的联系人" <<endl; string name; cin>>name; int ret=isExist (abs,name); if (ret!=-1 ) { cout<<"姓名:" <<abs->personArray[ret].m_Name<<"\t" ; cout<<"性别:" <<abs->personArray[ret].m_Sex<<"\t" ; cout<<"年龄:" <<abs->personArray[ret].m_Age<<"\t" ; cout<<"姓名:" <<abs->personArray[ret].m_Phone<<"\t" ; cout<<"姓名:" <<abs->personArray[ret].m_Addr<<"\t" ; } else { cout<<"查无此人" <<endl; } system ("pause" ); system ("cls" ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 void modifyPerson (Addressbooks * abs) { cout<<"请您输入要修改的联系人" <<endl; string name; cin>>name; int ret=isExist (abs,name); if (ret!=-1 ) { string name; cout<<"请输入姓名:" <<endl; cin>>name; abs->personArray[ret].m_Name=name; cout<<"请输入性别:" <<endl; cout<<"1---男" <<endl; cout<<"2---女" <<endl; int sex=0 ; while (true ) { cin>>sex; if (sex==1 ||sex==2 ) { abs->personArray[ret].m_Sex=sex; break ; } cout<<"输入有误,请重新输入" <<endl; } cout<<"请输入年龄:" <<endl; int age=0 ; cin>>age; abs->personArray[ret].m_Age=age; cout<<"请输入联系电话:" <<endl; string phone; cin>>phone; abs->personArray[ret].m_Phone=phone; cout<<"请输入家庭住址:" <<endl; string address; cin>>address; abs->personArray[ret].m_Adde=address; cout<<"修改成功!" <<endl; } else { cout<<"查无此人" <<endl; } }
1 2 3 4 5 6 7 8 9 void cleanPerson (Addressbooks * abs) { abs->m_Size=0 ; cout<<"通讯录已经清空" <<endl; system ("pause" ); system ("cls" ); }
设计立方体类 (求出立方体的面积和体积,分别用全局函数和成员函数判断两个立方体是否相等) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 class Cube { private : int m_L; int m_W; int m_H; public : void setW (int w) { m_L=w; } int getW () { return m_W; } void setL (int l) { m_L=l; } int getL () { return m_L; } void setL (int h) { m_H=h; } int getL () { return m_H; } int caculateS () { return 2 *m_L*m_W+2 *m_W*m_H+2 *m_L*m_H; } int calculateV () { return m_L*m_W*m_H; } bool isSameByClass (Cube &c) { if (m_L==c.getL ()m_W==c.getW ()&&m_H==c.getH ()) { return true ; } else { return false ; } } } bool isSame (Cube &c1,Cube &c2) { if (c1.getL ()==c2.getL ()&&c1.getW ()==c2.getW ()&&c1.getH ()==c2.getH ()) { return true ; } else { return false ; } } int main () { Cube c1; c1.setL (10 ); c1.setW (10 ); c1.steH (10 ); cout<<"c1的面积为:" <<c1.calculateS ()<<endl; cout<<"c1的体积为:" <<c1.calculateV ()<<endl; Cube c2; c2.setL (10 ); c2.setW (10 ); c2.steH (15 ); bool ret=isSame (c1,c2); if (ret) { cout<<"c1和c2是相等的" <<endl; } else { cout<<"c1和c2不相等" <<endl; } bool ret=isSame (c1,c2); if (ret) { cout<<"成员函数判断 c1和c2是相等的" <<endl; } else { cout<<"成员函数判断 c1和c2不相等" <<endl; } }
设计点和圆的关系
多态案例——-计算器类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 class Calculator { public : int getResult (string oper) { if (oper=="+" ) return m_Num1+m_Num2; if (oper=="-" ) return m_Num1-m_Num2; if (oper=="*" ) return m_Num1*m_Num2; } int m_Num1; int m_Num2; }; void text01{ Calculator c; c.m_Num1=10 ; c.m_Num2=10 ; cout<<c.m_Num1<<"+" <<c.m_Num2<<"=" <<c.getResult ("+" )<<endl; cout<<c.m_Num1<<"-" <<c.m_Num2<<"=" <<c.getResult ("-" )<<endl; cout<<c.m_Num1<<"*" <<c.m_Num2<<"=" <<c.getResult ("*" )<<endl; } main (){ text01 (); } class AbstractCalculator { public : virtual int getResult () { return 0 ; } int m_Num1; int m_Num2; }; class AddCalculator :public AbstractCalculator{ public : int getResult () { return m_Num1+m_Num2; } }; class SubCalculator :public AbstractCalculator{ public : int getResult () { return m_Num1-m_Num2; } }; class MulCalculator :public AbstractCalculator{ public : int getResult () { return m_Num1*m_Num2; } }; void text02 () { AbstractCalculator *abc=new AddCalculator; abc->m_Num1=10 ; abc->m_Num2=10 ; cout<<abc->m_Num1<<"+" <<abc->m_Num2<<"=" <<abc->getResult ()<<endl; delete abc; abc=new SubCalculator; abc->m_Num1=100 ; abc->m_Num2=100 ; cout<<abc->m_Num1<<"-" <<abc->m_Num2<<"=" <<abc->getResult ()<<endl; delete abc; abc=new SubCalculator; abc->m_Num1=100 ; abc->m_Num2=100 ; cout<<abc->m_Num1<<"*" <<abc->m_Num2<<"=" <<abc->getResult ()<<endl; delete abc; } main (){ text01 (); text02 (); }
多态案例——-制作饮品 大致流程:煮水 - 冲泡 - 倒入杯中 - 加入辅料
提供抽象制作饮品类,提供子类制作咖啡和茶叶 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 class AbstractDrinking { public : virtual void Boil () =0 ; virtual void Brew () =0 ; virtual void PourInCup () =0 ; virtual void PutSomething () =0 ; void makeDrink () ; { Boil (); Brew (); PourInCup (); PutSomething (); } }; class Coffee :public AbstractDrinking{ public : virtual void Boil () { cout<<"煮农夫山泉" <<endl; } virtual void Brew () { cout<<"冲泡咖啡" <<endl; } virtual void PourInCup () { cout<<"倒入杯中" <<endl; } virtual void PutSomething () { cout<<"加入糖和牛奶" <<endl; } }; class Tea :public AbstractDrinking{ public : virtual void Boil () { cout<<"煮矿泉水" <<endl; } virtual void Brew () { cout<<"冲泡茶叶" <<endl; } virtual void PourInCup () { cout<<"倒入杯中" <<endl; } virtual void PutSomething () { cout<<"加入枸杞" <<endl; } }; void dowork (AbstractDrinking *abs) { abs->makeDrink (); delete abs; } void text01 () { dowork (new Coffee); cout<<"----------------------" <<endl; dowork (new Tea); } main (){ text01 (); }
new创建对象是否调用构造函数和析构函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <iostream> using namespace std;class Point { public : static int number; public : point (){number++;} ~point (){number--;} }; int point::number=0 ;void main () { point *ptr; point A,B; { point *ptr_point=new point[3 ]; ptr=ptr_point; } point C; delete []ptr; cout<<point::number<<endl; }
多态案例——-电脑组装 案例描述: 电脑主要组成部件为CPU(用于计算),显卡(用于显示),内存条(用于存储)
将每个零件封装出抽象基类,并且提供不同的厂商生产不同的零件,例如intel厂商和lenovo厂商
创建电脑类提供让电脑工作的函数,并且调用每个零件工作的接口
测试时组转不同的三台电脑进行工作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 class CPU { public : virtual void calculate () =0 ; }; class VideoCard { public : virtual void display () =0 ; }; class Memory { public : virtual void storage () =0 ; }; class Computer { public : Computer (CPU *cpu,VideoCard *vc,Memory *mem) { m_cpu=cpu; m_vc=vc; m_mem=mem; } void work () { m_cpu->calculate; m_vc->display; m_mem->storage; } ~Computer () { if (m_cpu!=NULL ) { delete m_cpu; m_cpu=NULL ; } if (m_vc!=NULL ) { delete m_vc; m_vc=NULL ; } if (m_mem!=NULL ) { delete m_mem; m_mem=NULL ; } } private : CPU *m_cpu; VideoCard *m_vc; Memory *m_mem; }; class InterCPU :public CPU{ public : virtual void calculate () { cout<<"Inter的CPU开始计算了" <<endl; } }; class InterVideoCard :public VideoCard{ public : virtual void display () { cout<<"Inter的显卡开始显示了" <<endl; } }; class InterMemory :public Memory{ public : virtual void storage () { cout<<"Inter的内存条开始存储了" <<endl; } }; class LenovoCPU :public CPU{ public : virtual void calculate () { cout<<"Lenovo的CPU开始计算了" <<endl; } }; class LenovoVideoCard :public VideoCard{ public : virtual void display () { cout<<"Lenovo的显卡开始显示了" <<endl; } }; class LenovoMemory :public Memory{ public : virtual void storage () { cout<<"Lenovo的内存条开始存储了" <<endl; } }; void text01 () { CPU *interCpu=new InterCPU; VideoCard *interCard=new InterVideoCard; Memory *interMem=new InterMemory; cout<<"第一台电脑开始工作" <<endl; Computer *computer1=new Computer (interCpu,interCard,interMem); computer1->work (); delete computer1; cout<<"第二台电脑开始工作" <<endl; Computer *computer2=new Computer (new LenovoCPU,new LenovoVideoCard,new Lenovomemory); computer2->work (); delete computer2; cout<<"第三台电脑开始工作" <<endl; Computer *computer3=new Computer (new LenovoCPU,new InterVideoCard,new Lenovomemory); computer3->work (); delete computer3; } int main () { text01 (); }
定义一个描述矩形的类Rectangle,包括的数据成员有宽(width)和长(length):计算矩形周长;计算矩形面积;改变矩形大小 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <iostream> using namespace std;class Rectangle { public : Rectangle (int a,int b) { width=a; length=b; } int Area () { return width*length; } int Periment () { return 2 *(width+length); } void Changesize (int a,int b) { width=a; length=b; } void Print () ; private : int width,length; }; void Rectangle::Print () { cout<<"AREA=" <<Area ()<<endl; cout<<"PERIMENT=" <<Periment ()<<endl; } int main () { Rectangle r (5 ,8 ) ; r.Print (); r.Changesize (3 ,9 ); r.Print (); return 0 ; }
判断10~30内的质数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <cmath> #include <iomanip> #include <iostream> using namespace std;bool fun (long n) ;int main () { long a = 10 , b = 30 , l = 0 ; if (a % 2 == 0 ) a++; for (long m = a; m <= b;) if (fun (m)) { if (l++ % 10 == 0 ) cout << endl; cout << setw (5 ) << m; } return 0 ; } bool fun (long n) { int sqrtm = (int )sqrt (n); for (int i = 2 ; i <= sqrtm; i++) if (n % i == 0 ) return false ; return true ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <iostream> #include <math.h> using namespace std;int main () { int i=1 ,j=1 ,k.flag=1 ; k=sqrt (i); for (int i=1 ;i<=100 ;i++) { for (in j=1 ;j<=k;j++) { if (i%j==0 ) { flag=0 ; break ; } if (flag) { cout<<i<<"为素数" <<endl; } } } return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <iostream.h> bool prime (int n) ; void main () { int sum=0 ; for (int i=100 ;i<300 ;i++) if (prime (i)) sum+=i; cout<<"Sum=" <<sum<<endl; } bool prime (int n) {for (int i=2 ;i<n;i++) { if (n%i == 0 ) { return false ; } } return TRUE; }
输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字的立方和等于该数字本身。例如:153是一个“水仙花数”,因为:153=13+53+33 1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream.h> void main () {int i,j,k,n;for (n=100 ;n<1000 ;n++) { i=n/100 ; j=(n-i*100 )/10 ; k=n%10 ; if (n==i*i*i+j*j*j+k*k*k) cout<<n<<endl; } }
编写一个计算n!的函数,并在主程序中调用该函数,计算1-10的阶乘之和(即S=1!+2!+3!+…+10!) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <iostream> using namespace std;long fac (int n) { long f; if (n<0 ) cout<<"n<0,data error!" <<endl; else if (n==0 ) f=1 ; else f=fac (n-1 )*n; return (f); } void main () {int s=0 ;for (int i=1 ;i<=10 ;i++)s+=fac (i); cout<<”s=”<<s<<endl; }
从键盘输入10个正整数,输出它们中的最大值、最小值和平均值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std;void main () {int x,max,min,s=0 ;cout<<"please input 10 number:" ; cin>>x; max=min=x; for (int n=1 ;n<10 ;n++){ cin>>x; if (x>max) max=x; if (x<min) min=x; s+=x; } s=s/10 ; cout<<"max=" <<max<<endl; cout<<"min=" <<min<<endl; cout<<"s=" <<s<<endl; }
设计一个立方体类Box,它能计算并输出立方体的体积和表面积 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <iostream.h> class Box { float a; float volume; float area; public : Box (){} Box (float r){a=r;} void seta (float r) {a=r;} void getvolume () {volume=a*a*a;} void getarea () {area=6 *a*a;} void disp () { cout<<"体积:" <<volume<<",表面积:" <<area<<endl; } }; void main () { Box obj1 (4.5 ) ,obj2 ; obj2.seta (6.4 ); obj1.getvolume (); obj1.getarea (); cout<<"obj1=>" ; obj1.disp (); obj2.getvolume (); obj2.getarea (); cout<<"obj2=>" ; obj2.disp (); }
编写一个程序,设计一个点类Point,求两个点之间的距离 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <iostream> #include <math.h> #using namespace std; class point { private : int x,y; public : Point (int i,int j){x=i;y=j;} int getx (int x) { return x; } int gety (int y) { return y; } float distance (Point &x,Point &y) { float d; d=sqrt ((p1.getx ()-p2.getx ())*(p1.getx ()-p2.getx ())+(p1.gety ()-p2.gety ())*(p1.gety ()-p2.gety ())); return d; } }; void main () { Point p1 (2 ,2 ) ,p2 (5 ,5 ) ; p1.disp (); cout<<"与" ; p2.disp (); cout<<"之间距离=" <<distance (p1,p2)<<endl; }
编写一个简单程序,要求: 1用循环实现从键盘上输入20个整数并放入数组 2.求出数组元素的最大值及数组的平均值并输出 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <iostream.h> int sum (int p[]) ; int max (int p[]) ; void main () { int a[20 ],i=0 ; while (i<20 ) { cout<<"a[" <<i<<"]=" ; cin>>a[i]; i++; } cout<<"sum : " <<sum (a)<<endl; cout<<"max : " <<max (a)<<endl; } int sum (int p[]) { int s=0 ; for (int i=0 ;i<20 ;i++) s+=p[i]; return s; } int max (int p[]) { int maxv=p[0 ]; for (int i=0 ;i<20 ;i++) if (p[i]>maxv) maxv=p[i]; return maxv; }
编写两个函数,分别求两个数的最大公约数和最小公倍数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <iostream> using namespace std;int GCD (int u, int v) { while (u % v) { int t= u % v; u = v; v = t; } return v; } int LCM (int u, int v) { int gcd = GCD (u, v); return u * v / gcd; } void main () { int x,y;cin>>x>>y; cout<<”最大公约数为:”<<GCD (x,y)<<endl; cout<<”最小公倍数为:”<<LCM (x,y)<<endl; }
编写一个函数,实现用“冒泡法”对输入的10个整数按由小到大顺序排列 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include <iostream> #include <iomanip> using namespace std;void sort (int num[]) { int temp; for (int i=0 ;i<9 ;i++) { for (int j=0 ;j<9 -i;j++) { if (num[j]>num[j+1 ]) { temp=num[j]; num[j]=num[j+1 ]; num[j+1 ]=temp; } } } return ; } int main () { int num[10 ]; cout<<"请输入10个整数:" ; for (int i=0 ;i<10 ;i++) cin>>num[i]; cout<<"排序前:" ; for (int i=0 ;i<10 ;i++) cout<<setw (5 )<<num[i]; cout<<endl; sort (num); cout<<"排序后:" ; for (int i=0 ;i<10 ;i++) cout<<setw (5 )<<num[i]; cout<<endl; return 0 ; }