1 // 33-编程练习题二.cpp: 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include <iostream>
6 #include <climits>
7 #include <array>
8 #include <math.h>
9 #include <string>
10 using namespace std;
11
12 enum CH
13 {
14 A, //0
15 B, //1
16 C, //2
17 D, //3
18 E, //4
19 F, //5
20 Z //6
21 };
22
23 void Test1();
24 void Test2();
25 void Test3();
26 void Test4();
27 void Test5();
28
29 int main()
30 {
31 Test1();
32
33
34
35 int t;
36 cin >> t;
37 return 0;
38 }
39 //------------------------------练习----------------------------------
40 //1.用switch改写下面的代码片段:
41 //if (ch == 'A')
42 // a_grade++;
43 //else if (ch == 'B')
44 // b_grade++;
45 //else if (ch == 'C')
46 // c_grade++;
47 //else if (ch == 'D')
48 // d_grade++;
49 //else
50 // f_grade++;
51 void Test1()
52 {
53 bool isGrade = true;
54 int a_grade=0, b_grade=0, c_grade=0, d_grade=0, e_grade=0, f_grade = 0;
55 CH charValue = A;
56 while (isGrade)
57 {
58 char temp;
59 cout << "请输入最终评分(A-Z)";
60 cin >> temp;
61 charValue = CH(temp);
62 cout << "您输入的评分是:" << temp<< endl;
63 switch (charValue)
64 {
65 case 'A':
66 a_grade++;
67 cout << "A等级提高为:" << a_grade << endl;
68 break;
69 case 'B':
70 b_grade++;
71 cout << "B等级提高为:" << b_grade << endl;
72 break;
73 case 'C':
74 c_grade++;
75 cout << "C等级提高为:" << c_grade << endl;
76 break;
77 case 'D':
78 d_grade++;
79 cout << "D等级提高为:" << d_grade << endl;
80 break;
81 case 'Z':
82 cout << "淘汰出局!" <<endl;
83 isGrade = false;
84 break;
85 default:
86 f_grade++;
87 cout << "F等级提高为:" << f_grade << endl;
88 break;
89 }
90 }
91 Test2();
92
93 }
94 //2.求得20!(阶乘:4的阶乘相当于1*2*3*4)
95 void Test2()
96 {
97 int num;
98 cout<< "请输入一个整数求阶乘 :";
99 cin >> num;
100 long long result = 1;
101 for (int i = 1; i <= num;i++)
102 {
103 result = result * i;
104 }
105 cout << num<<"的阶乘为:"<< result << endl;
106 Test3();
107
108 }
109
110 //3.求得 1! + 2! +3! +.....+20! //输入一个整数,求得从1到这个数内所有整数阶乘的和。
111 void Test3()
112 {
113 int num;
114 cout << "请输入一个整数:";
115 cin >> num;
116 long long result=0;
117
118 for (int i = 1; i <= num; i++)
119 {
120 long long resultTemp=1;
121 for (int j=1;j<=i;j++)
122 {
123 resultTemp = resultTemp *j;
124 }
125
126 result += resultTemp;
127 }
128 cout << num << "以内所有整数的阶乘的和为:" << result<<endl;
129 Test4();
130
131 }
132
133 //4.实现函数判断一字符串是否是回文(顺读倒读都一样的字符串),若是回文,函数返回值为1,否则返回值为0。
134 void Test4()
135 {
136 bool isHui = true;
137 string str ;
138 cout << "请输入一字符串:";
139 cin >> str;
140 for (int i = 0; i < str.size()/2; i++)
141 {
142 if (str[i] != str[str.size() - 1 - i])
143 {
144 isHui = false;
145 }
146 }
147 if (isHui)
148 {
149 cout << "是回文" << endl;
150 }
151 else
152 {
153 cout << "不是回文" << endl;
154 }
155 Test5();
156 }
157 //5.输入三个整数,分别放在变量a,b,c中,然后把输入的数据重新按照由小到大的顺序放在变量a,b,c中,最后输出a,b,c的值。
158 void Test5()
159 {
160 int a,b,c;
161 cout << "请输入第一个整数:" << endl;
162 cin >> a;
163 cout << "请输入第二个整数:" << endl;
164 cin >> b;
165 cout << "请输入第三个整数:" << endl;
166 cin >> c;
167 while (true)
168 {
169 if (a >b)
170 {
171 int temp = a;
172 a = b;
173 b = temp;
174 }
175 if (b > c) //a>b 和b>c 是三个变量,不能用else if来比较。
176 {
177 int temp = b;
178 b = c;
179 c = temp;
180 }
181 if ((a < b) && (b < c))
182 {
183 break;
184 }
185 }
186 cout <<"a="<< a<<"\n"<<"b="<< b<<"\n" <<"c=" << c << endl;
187 }