C++編程基礎一 34-總復習
阿新 • • 發佈:2018-07-21
end string cpp 應用程序 語句 core pac 應用 指向
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 Female17 }; 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 }
C++編程基礎一 34-總復習