⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clr.cpp

📁 控制台应用程序:ManagedNUnmanaged 项目 应用程序向导已为您创建了此 ManagedNUnmanaged 应用程序。 本文件概要介绍组成 ManagedNUnmanaged
💻 CPP
字号:
// CLR.cpp: 主项目文件。

#include "stdafx.h"
using namespace System;

/**
// 打印一维数组
void display_string_array1(array<String^>^ args)
{
	for (int i=0;i<args->Length ;i++)
    Console::Write(L"{0} ", args[i]);
  Console::WriteLine();
}

// 打印二维数组

void display_string_array2(array<String^,2>^ args)
{
  for (int i=args->GetLowerBound(0);i<=args->GetUpperBound(0);i++)
  {
    for (int j=args->GetLowerBound(1);j<=args->GetUpperBound(1);j++)
      Console::WriteLine(L"arr[{0},{1}] = {2}", 
                    i.ToString(), j.ToString(), args[i,j]);
  }
}

// Test function to show how to create an array of strings
// Be that single dimensional or multi dimensional
void test1()
{
  // 创建长度为3的一维数组,数组元素为String
  array<String^>^ names = gcnew array<String^>(3);

  // 初始化数组
  names[0] = L"Hello";
  names[1] = L"World";
  names[2] = L"of Wonders!";

  display_string_array1(names);

  // 定义3行2列的二维数组
  array<String^, 2>^ arr2 = gcnew array<String^,2>(3,2);

  arr2[0,0] = L"First1";
  arr2[0,1] = L"Last1";

  arr2[1,0] = L"First2";
  arr2[1,1] = L"Last2";

  arr2[2,0] = L"First3";
  arr2[2,1] = L"Last3";

  display_string_array2(arr2);

  //对数组进行排序
  Array::Sort(names);
  Console::WriteLine("After sorted:");
  display_string_array1(names);

  //对数组进行搜索
  System::String^ hello = L"Hello";
  int position = Array::BinarySearch(names, hello);
  if(position >=0)
  {
	  Console::WriteLine(L"{0} was found at index position {1}.",hello, position);
  }
  else
  {
	  Console::WriteLine(L"{0} was not found.", hello);
  }
}


void joinStrings()
{

	String^ str1 = L"Value:";
	
	//字符串的拼接
	String^ str2 = str1 + 2.5;		//"Value:2.5"
	String^ str3 = str1 + 25;		//"Value:25"
	String^ str4 = str1 + true;		//"Value:true"

	//以数组的方式操作字符串
	Console::WriteLine("the second character in the str1 is:'{0}'.", str1[1]);
	Console::WriteLine("the length of  the str1 is:{0}.", str1->Length);

	//字符串的拼接
	array<String^>^ marks = {"A","B","C","D","E"};
	String^ joinString = String::Join(L";",marks);
	Console::WriteLine(L"the joinString is:{0}.",joinString);
}

void modifyString()
{
	String^ str1 = L"           Hello World of wanders               ";
	Console::WriteLine("str1 is: '{0}'.", str1);
	//修改字符串
	String^ newString = str1->Trim();
	Console::WriteLine("newString is: '{0}'.", newString);

}

//定义了枚举类型
enum class Color{Red, Blue, Orange,White};
void enumSample()
{
	Color myColor = Color::Blue;
	int value = safe_cast<int>(myColor);
	Console::WriteLine(L"Color is:{0},and the value is:{1}.",myColor, value);

	myColor = Color::Red;
	value = safe_cast<int>(myColor);
	Console::WriteLine(L"Color is:{0},and the value is:{1}.",myColor, value);
	
	myColor = Color::Orange;
	value = safe_cast<int>(myColor);
	Console::WriteLine(L"Color is:{0},and the value is:{1}.",myColor, value);
	
	myColor = Color::White;
	value = safe_cast<int>(myColor);
	Console::WriteLine(L"Color is:{0},and the value is:{1}.",myColor, value);

}

//定义托管值结构体
value struct LargeInteger
{
	System::Int32 lowPart;
	System::Int32 highPart;
	System::Int64 quadPart;  
};

//定义引用结构体
ref struct RefLargeInteger
{
	System::Int32 lowPart;
	System::Int32 highPart;
	System::Int64 quadPart; 
};

void structExample()
{
	LargeInteger li;//注意比较ref struct变量的定义。
	li.quadPart = 0x22;
	Console::WriteLine("Using value struct:{0:X}", li.quadPart.ToString());

	RefLargeInteger^ refli = gcnew RefLargeInteger;
	refli->quadPart = 0x22;
	Console::WriteLine("Using ref struct:{0:X}", refli->quadPart.ToString());
}
*/

value class Height
{
private:
	// 记录高度,英尺、英寸。
	int feet;
	int inches;
	//定义两个常量
	literal int inchesPerFoot = 12;
	literal double inchesToMeters = 2.54/100;
public:
	// 带参构造函数
	Height(int ins)
	{
	feet = ins / inchesPerFoot;
	inches = ins % inchesPerFoot;
	}
	// 带参构造函数
	Height(int ft, int ins) : feet(ft), inches(ins){}
	// 以米单位计算高度
	property double meters // 标量属性
	{
	//返回属性值
	double get()
	{
	return inchesToMeters*(feet*inchesPerFoot+inches);
	}
	
	}
	// 重载ToString方法。
	virtual String^ ToString() override
	{
	return feet + L" feet "+ inches + L" inches";
	}
};

// 定义体重
value class Weight
{
private:
	int lbs;
	int oz;
	literal int ouncesPerPound = 16;
	literal double lbsToKg = 1.0/2.2;
public:
	Weight(int pounds, int ounces)
	{
	lbs = pounds;
	oz = ounces;
	}
	property int pounds // 标量属性
	{
	int get() { return lbs; }
	void set(int value) { lbs = value; }
	}
	property int ounces // 标量属性
	{
	int get() { return oz; }
	void set(int value) { oz = value; }
	}
	property double kilograms // 标量属性
	{
	double get() { return lbsToKg*(lbs + oz/ouncesPerPound); }
	}
	virtual String^ ToString() override
	{ return lbs + L" pounds " + oz + L" ounces"; }
};

//
ref class Person
{
private:
	Height ht;
	Weight wt;
public:
	property String^ Name; // 细小标量属性
	Person(String^ name, Height h, Weight w) : ht(h), wt(w)
	{
	Name = name;
	}
	Height getHeight(){ return ht; }
	Weight getWeight(){ return wt; }
};


ref class Name
{
private:
	array<String^>^ Names;
public:
	Name(...array<String^>^ names) : Names(names) {}
	// 标量属性返回数组长度
	property int NameCount
	{
		int get() {return Names->Length; }
	}
	// 索引属性返回名字
	property String^ default[int]
	{
		String^ get(int index)
		{
			if(index >= Names->Length)
			throw gcnew Exception(L"Index out of range");
			return Names[index];
		}
	}
};

value class Length
{
private:
	int feet;
	int inches;
public:
	static initonly int inchesPerFoot = 12;
	//构造函数
	Length(int ft, int ins) : feet(ft), inches(ins){ }
	//重载tostring方法
	virtual String^ ToString() override
	{ 
		return feet+L" feet " + inches + L"inches"; 
	}
	//重载“+"操作符。
	Length operator+(Length len)
	{
		int inchTotal = inches+len.inches+inchesPerFoot*(feet+len.feet);
		return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot);
	}
	//重载“/"操作符。
	static Length operator/(Length len, double x)
	{
		int ins = safe_cast<int>((len.feet*inchesPerFoot + len.inches)/x);
		return Length(ins/inchesPerFoot, ins%inchesPerFoot);
	}
	static Length operator*(double x, Length len); //double型作为被乘数
	static Length operator*(Length len, double x); // double型作为乘数
};
Length Length::operator *(double x, Length len)
{
	int ins = safe_cast<int>(x*len.inches +x*len.feet*inchesPerFoot);
	return Length(ins/inchesPerFoot, ins%inchesPerFoot);
}
Length Length::operator *(Length len, double x)
{ 
	return operator*(x, len); 
}


int main(array<System::String ^> ^args)
{
	 int dogs,cats;
	int animal;
	double dogweight = 10.1;
	double catweight = 5.1;
	dogs = 5;
	cats = 6;
	animal = dogs + cats;
	//普通输出
	Console::WriteLine(L"\n Dogs are not the only animal.");
	Console::Write(L"\n And we have ");
	Console::Write(animal);
	Console::Write(L" animal in all.");
	Console::WriteLine("\n");
	//格式化输出
	Console::WriteLine(L"Dogs are not the only animal. And we have {0} animal in all.", animal);
	Console::WriteLine(L"Dog weight is: {0:F2}.Cat weight is: {1:F3},the bigger animal weight is:{0:F3}",dogweight,catweight);
	Console::ReadKey();
    return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -