第 一 部 分 1.猜数字练习 #include <iostream> #include<string> using namespace std; int main(void) { int num = rand() % 100; cout << num << endl; int puT = 0; cout << "请你猜一下这个数是多少\n" << endl; while ((cin >> puT)) { if (puT > num) { cout << "猜大了\n" << endl; } else if (puT <= num / 2) { cout << "太小了\n" << endl; } else if (puT >= num / 2 && puT < num) { cout << "再大一点\n" << endl; } else if (num == puT) { cout << "猜对了\n" << endl; break; } } system("pause"); return 0; } 2.水仙花数练习 #include <iostream> using namespace std; int main(void) { int ge = 0; int shi = 0; int bai = 0; int i = 100; do { ge = i % 10; shi = (i / 10) % 10; bai = i / 100; if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) { cout << i << endl; } i++; } while (i < 1000); system("pause"); return 0; } 3.敲桌子练习 #include<iostream> using namespace std; int main(void) { for (int i = 1; i < 100; i++) { int ge = i % 10; int shi = (i /10)% 10; if (i % 7 == 0 || ge == 7 || shi == 7) { cout << i << endl; } } } 4.10*10的正方形 #include<iostream> using namespace std; int main(void) { for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) { cout << "* "; } cout << endl; } } 5.9*9的乘法表 #include<iostream> using namespace std; int main(void) { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { cout << j <<"*"<< i<<"="<< i* j<<" "; } cout << endl; } system("pause"); return 0; } 6.五只小猪谁最重 #include<iostream> using namespace std; int main(void) { int temp = 0; int Weight[5] = { 300,250,200,400,450 }; for (int i = 0; i < 5; i++) { if (Weight[i] > temp) { temp = Weight[i]; } } cout << "最重的小猪是" << temp << "kg"; return 0; } 7.冒泡排序 //排列这个数组{7,5,2,4,9,8,6,7,1} #include<iostream> using namespace std; int main(void) { //排序的总轮数=元素个数-1 //每轮对比的次数 = 元素个数- 排序轮数 int nums[9] = { 7,5,2,4,9,8,6,7,1 }; for (int i = 0; i < 8 ; i++) { //内层循环对比 for (int j = 0; j < 9 - i-1; j++) { //第一个数比第二个数大就交换他们两个的位置 int temp = 0; if (nums[j] > nums[j + 1]) { temp = nums[j+1]; nums[j + 1] = nums[j]; nums[j] = temp; } } } for (int i = 0; i < 9; i++) { cout << nums[i]; } return 0; } 8.结构体 #include<iostream> #include<string> #include<ctime> using namespace std; struct Student { string name; int age; int score; }; struct Teacher { string name; struct Student sArry[5]; }; void inPutInformation(struct Teacher tArry[], int len) { string Name = "ABCDE"; for (int i = 0; i < len; i++) { tArry[i].name = "Teacher_"; tArry[i].name += Name[i]; for (int j = 0; j < 5; j++) { tArry[i].sArry[j].name = "Student_"; tArry[i].sArry[j].name += Name[j]; int random = rand()% 60 +40; tArry[i].sArry[j].score = random; } } } void printInformation(struct Teacher tArry[],int len) { for (int i = 0; i < len; i++) { cout << "老师的姓名:" << tArry[i].name << endl; for (int j = 0; j < 5; j++) { cout << "\t学生的姓名:" << tArry[i].sArry[j].name << "考试分数:" << tArry[i].sArry[j].score << endl; } } } int main(void) { srand((unsigned int)time(NULL)); struct Teacher tArry[3]; int len = sizeof(tArry) / sizeof(tArry[0]); inPutInformation(tArry,len); printInformation(tArry,len); system("pause"); return 0; } 大 案 例 通 讯 录 管 理 系 统 #include<iostream> #include<string> #include<cstdlib> #define MAX 1000 using namespace std; struct Person { string name; int age; string sex; string phone; string addr; }; struct addreassbooks { struct Person personarry[MAX]; int m_Size; }; void mainMenu() { 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 addPerson(addreassbooks* abs) { if (abs->m_Size == MAX) { cout << "联系人已满,无法添加" << endl; return; } string name; cout << "请输入姓名" << endl; cin >> name; abs->personarry[abs->m_Size].name = name; string sex; cout << "请输入性别" << endl; cin >> sex; abs->personarry[abs->m_Size].sex = sex; int age; cout << "请输入年龄" << endl; cin >> age; abs->personarry[abs->m_Size].age = age; string phone; cout << "请输入电话" << endl; cin >> phone; abs->personarry[abs->m_Size].phone = phone; string addr; cout << "请输入地址" << endl; cin >> addr; abs->personarry[abs->m_Size].addr = addr; //更新通讯录人数 abs->m_Size++; cout << "添加成功" << endl; system("pause"); system("cls"); } void printPerson(addreassbooks* abs) { if (abs->m_Size == 0) { cout << "当前记录为空" << endl; } else { for (int i = 0; i < abs->m_Size; i++) { cout << "姓名\t" << abs->personarry[i].name << endl; cout << "性别\t" << abs->personarry[i].sex << endl; cout << "年龄\t" << abs->personarry[i].age << endl; cout << "电话\t" << abs->personarry[i].phone << endl; cout << "地址\t" << abs->personarry[i].addr << endl; cout << "\n"; } } system("pause"); system("cls"); } int checkPerson(addreassbooks* abs, string name) { for (int i = 0; i < abs->m_Size; i++) { if (abs->personarry[i].name == name) { return i; } } return -1; } void deletePerson(addreassbooks* abs) { string dname; cout << "请输入你要删除的人名" << endl; cin >> dname; int ret = checkPerson(abs, dname); if (ret == -1) { cout << "查无此人" << endl; } else { for (int i = ret; i < abs->m_Size; i++) { abs->personarry[i] = abs->personarry[i + 1]; } abs->m_Size--; cout << "删除成功" << endl; } system("pause"); system("cls"); } void findPerson(addreassbooks* abs) { string fname; cout << "请输入要查找的联系人姓名" << endl; cin >> fname; int result = checkPerson(abs, fname); if (result == -1) { cout << "查无此人" << endl; } else { cout << "姓名\t" << abs->personarry[result].name << endl; cout << "性别\t" << abs->personarry[result].sex << endl; cout << "年龄\t" << abs->personarry[result].age << endl; cout << "电话\t" << abs->personarry[result].phone << endl; cout << "地址\t" << abs->personarry[result].addr << endl; } system("pause"); system("cls"); } void modifyPerson(addreassbooks* abs) { string mname; cout << "请输入要修改的联系人姓名" << endl; cin >> mname; int result = checkPerson(abs, mname); if (result == -1) { cout << "查无此人" << endl; } else { string name; cout << "请输入姓名" << endl; cin >> name; abs->personarry[result].name = name; string sex; cout << "请输入性别" << endl; cin >> sex; abs->personarry[result].sex = sex; int age; cout << "请输入年龄" << endl; cin >> age; abs->personarry[result].age = age; string phone; cout << "请输入电话" << endl; cin >> phone; abs->personarry[result].phone = phone; string addr; cout << "请输入地址" << endl; cin >> addr; abs->personarry[result].addr = addr; cout << "修改成功" << endl; } system("pause"); system("cls"); } void cleanPerson(addreassbooks*abs)//逻辑清空 { abs->m_Size = 0; cout << "通讯录清空成功!" << endl; system("pause"); system("cls"); } int main(void) { //创建通讯录结构体变量 addreassbooks abs; //初始化通讯录中当前人员的个数 abs.m_Size = 0; int select = 0; while (1) { mainMenu(); cin >> select; switch (select) { case 1://添加联系人 addPerson(&abs); break; case 2://显示联系人 printPerson(&abs); break; case 3://删除联系人 deletePerson(&abs); break; case 4://查找联系人 findPerson(&abs); break; case 5://修改联系人 modifyPerson(&abs); break; case 6://清空联系人 cleanPerson(&abs); break; case 0://退出通讯录 cout << "欢迎下次使用" << endl; system("pause"); return 0; default: break; } } } 第 二 部 分 1.圆 #include<iostream> using namespace std; double pi = 3.14; //class 代表设计一个类,类后面紧跟着的就是类名称 class Circle { //访问权限 //公共权限 public: //属性 //半径 int c_r; //行为 //获取圆的周长 double calculateZC() { return 2 * pi * c_r; } }; int main(void) { //通过圆类创建具体的圆(对象) //实例化——通过一个类创建一个对象的过程 Circle c1; //给圆对象的属性进行赋值 c1.c_r = 10; cout << "圆的周长为" << c1.calculateZC() << endl; return 0; } 2.学生 #include<iostream> #include<string> using namespace std; class Student { public: string s_Name; int s_Id; void showStudent() { cout << "姓名: " << s_Name << "ID:" << s_Id << endl; } //赋值 void inputName(string name) { s_Name = name; } }; int main(void) { Student s1; //s1.s_Name = "张三"; s1.inputName("赵六"); s1.s_Id = 123456; s1.showStudent(); return 0; } 3.人 #include<iostream> #include<string> using namespace std; class Person { public: //设置姓名 void setName(string name) { p_name = name; } //获取姓名 string getName() { return p_name; } //获取年龄 int getAge() { return p_age; } //设置年龄 void setAge(int age) { p_age = age; if (age < 0 || age >150) { p_age = 0; cout << "鬼先生您好,再见!" << endl; return; } } //设置伙伴 void setLover(string lname) { lover = lname; } private: //姓名 可读可写 string p_name; //年龄 可读可写加个范围 int p_age; //伙伴 只写 string lover; }; int main(void) { Person p1; p1.setName("张三"); cout << "姓名:" << p1.getName() << endl; p1.setAge(18); cout << "年龄:" << p1.getAge() << endl; p1.setLover("赵四"); return 0; } 4.立方体 #include<iostream> using namespace std; class Cube { public: void setl(int l) { C_L = l; } int getl() { return C_L; } void setw(int w) { C_W = w; } int getw() { return C_W; } void seth(int h) { C_H= h; } int geth() { return C_H; } //表面积 int calculateS() { return 2 * C_L * C_W + 2 * C_L * C_H + 2 * C_W * C_H; } //体积 int calculateV() { return C_L * C_W * C_H; } //成员函数判断是否相等 bool issamebyClass(Cube &c) { if (C_H== c.geth() && C_L == c.getl() && C_W == c.getw()) { return true; } return false; } private: int C_L; int C_W; int C_H; }; //利用全局函数判断相等 bool issame(Cube &c1, Cube &c2) { if (c1.geth() == c2.geth() && c1.getl() == c2.getl() && c1.getw() == c2.getw()) { return true; } return false; } int main(void) { Cube c1; c1.seth(10); c1.setl(10); c1.setw(10); cout << c1.calculateS() << endl; cout << c1.calculateV() << endl; Cube c2; c2.seth(10); c2.setl(10); c2.setw(10); //判断是否相等 bool ret = issame(c1, c2); if (ret) { cout << "c1和c2相等" << endl; } else { cout << "c1和c2不相等" << endl; } //成员函数判断 bool ret2 = c1.issamebyClass(c2); if (ret2) { cout << "利用成员函数,c1和c2相等" << endl; } else { cout << "利用成员函数,c1和c2不相等" << endl; } system("pause"); return 0; }