C++编程基础一 34-总复习
程序员文章站
2022-05-23 21:30:06
1 // 01-C++基础复习一.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include 6 #include 7 #include 8 #include 9 #include 10 using namespace std; 11 12... ......
1 // 01-C++基础复习一.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <iostream> 6 #include <string> 7 #include <array> 8 #include <climits> 9 #include <math.h> 10 using namespace std; 11 12 //枚举 13 enum Gender //枚举类型首字母也大写 14 { 15 Male, 16 Female 17 }; 18 19 //结构体 20 struct Student //一般结构体 首字母大写 21 { 22 string name; 23 int age; 24 Gender gender; 25 }; 26 int main() 27 { 28 //输出和输入 29 int i; 30 std::cout << "Hello" << endl; 31 cin >> i; 32 cout << i << endl; 33 34 //整型数据 35 int score = 100; 36 short age = 17; 37 long money = 1000000000; 38 cout << score << "/" << age << "/" << money << "/" << endl; 39 40 //浮点类型 41 float weight = 12.4; 42 double pai = 3.1515926; 43 44 //布尔类型 45 bool isPassed = false; //0是false 1是true 非0情况都是true 46 cout << isPassed << endl; 47 48 //字符类型 49 char c = 'a'; //ASCII码表 50 cout << c << endl; 51 cout << int('a') << endl; 52 53 //数组 54 int arr1[5] = { 1,2,3,4, };//初始化少一个值没事,系统会自动初始化值为0,多值就不行。 55 cout << arr1[0] << "/" << arr1[4] << endl; 56 float arr2[3]{ 1.2 }; 57 58 //字符串 59 string name = "uimodel"; 60 cout << name << endl; 61 62 //结构体 63 Student stu1 = { "uimodel",20,Female }; 64 cout << stu1.name << "的年龄是:" << stu1.age << endl; 65 66 //枚举类型 67 Gender g = Male; 68 69 //指针 70 int* pi = &score; 71 cout << "地址" << pi << "/" << "指向的对象的值:" << *pi << endl; 72 *pi = 200; 73 cout << "score:" << score << endl; 74 75 //算数运算符 76 int num1 = 15, num2 = 4; 77 cout << num1 + num2 << " " << num1 - num2 << " " << num1 + num2 << " " << float(num1) / num2 << " " << num1 % num2 << endl; 78 79 //关系运算符 80 cout << (num1 == num2) << " " << (num1 > num2) << " " << (num1 <= num2) << " " << (num1 != num2) << endl; 81 82 //逻辑运算符 或与非 83 cout << ((num1 == num2) || (num1 > num2)) << endl;//1 84 cout << ((num1 != num2) || (num1 > num2)) << endl;//1 85 cout << ((num1 == num2) || (num1 < num2)) << endl;//0 86 cout << ((num1 == num2) && (num1 > num2)) << endl;//0 87 cout << ((num1 != num2) && (num1 > num2)) << endl;//1 88 cout << ((num1 == num2) && (num1 < num2)) << endl;//0 89 90 //条件运算符 91 int max = num1 > num2 ? num1 : num2; 92 cout << num1 << "和" << num2 << "中较大的值为:" << max << endl; 93 int min = num1 < num2 ? num1 : num2; 94 cout << num1 << "和" << num2 << "中较小的值为:" << min << endl; 95 96 //if语句 97 if (num1 > num2) 98 { 99 cout << num1 << "和" << num2 << "中较大的值为:" << num1 << endl; 100 } 101 else if ((num1 < num2)) 102 { 103 cout << num1 << "和" << num2 << "中较大的值为:" << num2 << endl; 104 } 105 else 106 { 107 cout << num1 << "等于" << num2 << endl; 108 109 } 110 111 //switch语句 112 switch (stu1.gender) 113 { 114 case Male: 115 cout << stu1.name << "是男生" << endl; 116 break; 117 case Female: 118 cout << stu1.name << "是女生" << endl; 119 break; 120 default: 121 break; 122 } 123 124 //for循环 125 for (int i = 0; i<(end(arr1) - begin(arr1)); i++) 126 { 127 cout << arr1[i] << " "; //1 2 3 4 0 128 } 129 cout << endl; 130 131 //while循环 132 133 //while (num1>=num2) 134 //{ 135 // num1 -= 2; 136 // cout << num1 << " "; 137 //} 138 //cout << endl; 139 140 //do while循环 141 do 142 { 143 num1 -= num2; 144 cout << num1 << " "; 145 146 } while (num1 >= num2); 147 148 149 return 0; 150 151 }
上一篇: 好像你考完不用挨打似的!