请用枚举法编写C++及C#代码,并附注详细注释
时间: 2024-03-08 10:49:18 浏览: 113
C++注释规范.pdf
C++代码:
```cpp
#include <iostream>
using namespace std;
// 枚举类型,表示星期几
enum Weekday {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
int main()
{
// 定义星期几变量并初始化为星期一
Weekday today = Monday;
// 输出今天是星期几
switch (today) {
case Sunday:
cout << "Today is Sunday." << endl;
break;
case Monday:
cout << "Today is Monday." << endl;
break;
case Tuesday:
cout << "Today is Tuesday." << endl;
break;
case Wednesday:
cout << "Today is Wednesday." << endl;
break;
case Thursday:
cout << "Today is Thursday." << endl;
break;
case Friday:
cout << "Today is Friday." << endl;
break;
case Saturday:
cout << "Today is Saturday." << endl;
break;
}
return 0;
}
```
C#代码:
```csharp
using System;
// 枚举类型,表示星期几
enum Weekday {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
class Program
{
static void Main(string[] args)
{
// 定义星期几变量并初始化为星期一
Weekday today = Weekday.Monday;
// 输出今天是星期几
switch (today) {
case Weekday.Sunday:
Console.WriteLine("Today is Sunday.");
break;
case Weekday.Monday:
Console.WriteLine("Today is Monday.");
break;
case Weekday.Tuesday:
Console.WriteLine("Today is Tuesday.");
break;
case Weekday.Wednesday:
Console.WriteLine("Today is Wednesday.");
break;
case Weekday.Thursday:
Console.WriteLine("Today is Thursday.");
break;
case Weekday.Friday:
Console.WriteLine("Today is Friday.");
break;
case Weekday.Saturday:
Console.WriteLine("Today is Saturday.");
break;
}
}
}
```
注释:
1. `enum Weekday`:定义枚举类型 Weekday,表示星期几。
2. `Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday`:枚举类型的值。
3. `Weekday today = Monday;`:定义变量 today,类型为 Weekday,初始值为 Monday。
4. `switch (today)`:根据变量 today 的值执行相应的分支。
5. `case Sunday:`:如果 today 的值为 Sunday,则执行下面的语句。
6. `cout << "Today is Sunday." << endl;`:输出今天是星期几。
7. `break;`:跳出 switch 语句。
8. 其他 case 分支和说明与上面类似。
9. `return 0;`:结束程序。
10. C#代码与C++代码类似,只是语法略有不同。
阅读全文