浙江大学PAT_甲级_1028. List Sorting (25)

本文介绍了一段使用C++实现的学生记录排序代码,通过输入文件读取学生ID、姓名和成绩,根据指定的列进行排序输出。

题目链接:点击打开链接

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input

Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student's record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output

For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID's in increasing order.

Sample Input 1
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90
我的c++代码:

#include<iostream>
#include<algorithm>  
#include<vector>  
#include<string.h>
using namespace std;
struct student
{
	int id; //为节省时间,id用Int类型,不用String
	char name[9];//姓名用char,
	int grade;//分数
};
int c = 1;//控制比较方法
bool compare(student a, student b)//比较方法
{
	if (c == 1)//比较id
	{
		return(a.id < b.id);
	}
	else if (c == 2)//比较名字
	{
		if (strcmp(a.name, b.name) == 0)//名字相同,同一个人,比较id
			return a.id < b.id;
		else
			return strcmp(a.name, b.name) < 0;
	}
	else//比较分数
	{
		if (a.grade == b.grade)//分数相同,比较id
			return a.id < b.id;
		else
			return(a.grade < b.grade);
	}
}
int main()
{
	int n = 0,i=0;//n学生人数
	scanf("%d %d", &n, &c);
	vector<student>stu_vec(n);//要分配vecto的size
	for (i = 0; i < n; i++)
	{
		cin >> stu_vec[i].id>> stu_vec[i].name >> stu_vec[i].grade;
	}
	sort(stu_vec.begin(),stu_vec.end(),compare);
	for (auto j : stu_vec)//遍历vec
	{
		printf("%0.6d %s %d\n", j.id,j.name,j.grade);//%0.6d把id补足为6个长度
	}
	//system("pause");
	return 0;
}


public static void InitMenu(Form mainForm) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Unknown result type (might be due to invalid IL or missing references) //IL_0015: Unknown result type (might be due to invalid IL or missing references) LanguageConfig.CurrentLanguageType = DictIBLL.Instance.CurrentLanguage; GlobalHelper.CurrentLanguage = LanguageConfig.CurrentLanguageType; MenuService.m__00A0 = mainForm; MenuService.m__00A0.Add(0, MenuService.m__00A0); MenuService.m__00A0.Add(MenuService.m__00A0.GetType().FullName, MenuService.m__00A0); MenuService.m__00A0 = MenuService.m__00A0.Controls.Find(_10C161DD_002D44FC_002D490D_002D8979_002DB5CC1BEAB6EF._205D(), searchAllChildren: true)[0] as Panel; MenuService.m__00A0 = MenuService.m__00A0.Controls.Find(_10C161DD_002D44FC_002D490D_002D8979_002DB5CC1BEAB6EF._205E(), searchAllChildren: true)[0] as ToolStrip; MenuService.m__1680 = MenuService.m__00A0.Controls.Find(_10C161DD_002D44FC_002D490D_002D8979_002DB5CC1BEAB6EF._205F(), searchAllChildren: true)[0] as ToolStrip; MenuService.m__00A0.Renderer = new _00A0(); MenuService.m__1680.Renderer = new _00A0(); MenuService.m__1680.Items.Clear(); MenuService.m__00A0 = (from P_0 in AppDomain.CurrentDomain.GetAssemblies() where P_0.FullName.Contains(_10C161DD_002D44FC_002D490D_002D8979_002DB5CC1BEAB6EF._1680_204F()) select P_0).ToList(); if (LoginInfo.IsDebug) { MenuList = MenuIBLL.Instance.GetSystemMenuList(); } else { MenuList = MenuIBLL.Instance.GetMenuListByRoleId(0); } ParentMenuList = (from P_0 in MenuList where P_0.ParentId == 0 orderby P_0.Sorting select P_0).ToList(); ParentMenuList.ForEach(delegate (MenuEntity P_0)dh
最新发布
03-27
<think>好的,用户的问题是关于C#中InitMenu方法遇到的编译器生成的未知类型问题,涉及IL代码的解决方案。我需要先理解这个问题的背景。 首先,用户提到InitMenu方法,这通常与菜单初始化相关,可能是在Windows Forms或WPF应用程序中。编译器生成的未知类型可能指的是自动生成的代码,比如设计器文件或某些特性生成的类。当查看IL代码时,这些自动生成的类型可能不容易直接对应到源代码,导致困惑或错误。 接下来,用户提到IL代码,说明问题可能出现在反编译或分析中间语言时,无法识别某些类型。这时候需要检查几个方面:设计器文件是否正确生成,是否有部分代码被优化或混淆,或者是否使用了某些代码生成工具或框架导致类型无法识别。 根据引用[2],用户可能在处理控件设置时遇到问题,特别是ContextMenuStrip控件,这可能在设计器文件中生成相关代码。如果设计器文件未正确更新或存在错误,可能导致编译器生成未知类型。此外,引用[3]提到的代码重构和模块化也可能相关,需要检查代码结构是否合理,是否存在冗余或错误生成的代码。 解决方案方面,首先应检查设计器文件,确保自动生成的代码没有错误。清理和重新生成项目可以解决一些生成问题。如果问题依旧,可能需要手动检查IL代码中的类型,使用ildasm或反编译工具查看生成的类型来源。另外,检查项目引用和NuGet包是否完整,避免缺少依赖导致类型无法解析。 此外,用户可能需要检查是否使用了动态编译或反射来生成类型,这些情况下需要确保类型名称正确且存在。如果问题涉及第三方库,可能需要更新或重新安装相关包。 最后,确保代码中没有使用过时的特性或编译器指令,这些可能导致生成意外的IL代码。如果所有方法都尝试后问题仍存在,可能需要简化代码,逐步排除,定位具体引发问题的部分。</think>### C#中InitMenu方法遇到编译器生成未知类型的解决方案 在C#中,`InitMenu`方法通常与菜单初始化相关,若遇到编译器生成的未知类型问题(尤其是涉及IL代码时),可通过以下步骤排查和解决: --- #### **1. 理解问题背景** - **现象**:编译或反编译时提示"未知类型",但源代码中未明确定义该类型。 - **原因**:可能是由设计器自动生成代码、编译器优化(如闭包/迭代器)或第三方工具生成的中间代码引起[^2][^3]。 --- #### **2. 排查设计器生成代码** - **步骤**: 1. 在解决方案资源管理器中展开`YourForm.cs`,找到对应的`YourForm.Designer.cs`文件。 2. 检查`InitializeComponent`方法中与菜单相关的代码(如`ContextMenuStrip`控件)。 3. 确保设计器文件与窗体代码同步(右键设计器界面 → 查看代码)。 - **示例代码**: ```csharp // YourForm.Designer.cs private void InitializeComponent() { this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(); // ...其他控件初始化 } ``` > 若此处类型未正确引用(如`System.Windows.Forms`缺失),需检查项目引用。 --- #### **3. 处理编译器生成的类型** 若涉及闭包、迭代器或异步方法: - **分析IL代码**: - 使用工具查看生成的IL代码(如`ildasm.exe`或JetBrains dotPeek)。 - 查找类似`<>c__DisplayClass`或`<InitMenu>b__0`的编译器生成类型。 - **解决方案**: - 避免直接依赖编译器生成类型名称(因其不稳定)。 - 重构代码:将嵌套逻辑提取为独立方法,减少闭包使用。 --- #### **4. 检查第三方工具干扰** - **可能场景**: - 使用代码生成器(如T4模板)或AOP框架(如PostSharp)。 - 动态编译(如`CSharpCodeProvider`)。 - **应对措施**: - 检查生成代码的完整性。 - 更新工具版本或调整生成逻辑。 --- #### **5. 修复步骤总结** 1. **清理并重新生成项目**: ```bash msbuild /t:Clean msbuild /t:Build ``` 2. **验证引用完整性**: - 检查NuGet包和DLL引用是否缺失。 3. **简化代码**: - 注释可疑代码块,逐步排查问题点。 4. **检查编译器选项**: - 禁用优化(项目属性 → 生成 → 取消勾选"优化代码")。 --- #### **示例:处理闭包生成的类型** ```csharp // 原始代码(可能生成闭包类) private void InitMenu() { var counter = 0; menuItem.Click += (s, e) => { counter++; MessageBox.Show($"Clicked {counter} times"); }; } // 重构后(避免闭包) private int _counter = 0; private void MenuItem_Click(object sender, EventArgs e) { _counter++; MessageBox.Show($"Clicked {_counter} times"); } private void InitMenu() { menuItem.Click += MenuItem_Click; } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值