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

📄 e0201.cpp

📁 Visualc++6.0自学手册第2章
💻 CPP
字号:
// E0201.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "E0201.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

/* 2.5.1 
void HandWin(CString hand1, CString hand2)
{
	if (hand1 == hand2)
	{
		cout << "平局……" << endl;
	}
	else
	{
		if (hand1 == "剪刀")
		{
			if (hand2 == "石头")
				cout << "选手2获胜(剪刀 < 石头)" << endl;
			else
				cout << "选手1获胜(剪刀 > 布)" << endl;
		}
		else if (hand1 == "石头")
		{
			if (hand2 == "剪刀")
				cout << "选手1获胜(石头 > 剪刀)" << endl;
			else
				cout << "选手2获胜(石头 < 布)" << endl;
		}
		else
		{
			if (hand2 == "石头")
				cout << "选手1获胜(布 > 石头)" << endl;
			else
				cout << "选手2获胜(布 < 剪刀)" << endl;
		}
	}
}
//*/

//* 2.5.2
char JudgeMark(int grade)  // 通过分数判定成绩
{
	if (grade<0 || grade>100)  // 无效分数
		return 'I';
	else
	{
		if (grade>=90)  // 90分以上
			return 'E';
		else if (grade >= 70)  // 70分以上
			return 'A';
		else if (grade >= 60)  // 60分以上
			return 'P';
		else  // 60分以下
			return 'N';
	}
}

void JudgeGrade(char mark)
{
	switch (mark)  // switch语句
	{
	case 'I':
		cout<<"无效"<<endl;
		break;
	case 'E':
		cout<<"优秀"<<endl;
		break;
	case 'A':
		cout<<"良好"<<endl;
		break;
	case 'P':
		cout<<"通过"<<endl;
		break;
	case 'N':
		cout<<"未通过"<<endl;
		break;
	default:
		cout<<"无效"<<endl;
		break;
	}
}
//*/

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	/* 2.1.1
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		CString strHello;
		strHello.LoadString(IDS_HELLO);
		cout << (LPCTSTR)strHello << endl;
	}
	//*/

	/* 2.1.4 
	// 测试代码开始
	CString strTest;  // 声明字符串变量
	strTest.Format(_T("Hello,World!"));  // 格式化字符串
	cout << (LPCTSTR)strTest << endl;  // 输出字符串
	// 测试代码结束
	//*/

	/* 2.2.1
	char c;  // 字符变量
	cin >> c;  // 输入字符
	cout << "对应的整数为:" << int(c) << endl;  // 显示对应的整数
	//*/

	/* 2.2.4
	struct STUD // 定义结构STUD
	{
		char * name;  // char指针
		int age;  // int型
	};
	STUD Stud, *pStud;  // 声明STUD型变量Stud和指针pStud

	Stud.name = "白儒";  // 使用操作符"."访问成员变量
	Stud.age = 18;
    cout << Stud.name << "-" << Stud.age << '\n' << endl;  // 输出变量Stud的成员
	
	pStud = new STUD;  // 初始化STUD型指针
	pStud->name = "黑儒";  // 使用操作符"->"访问成员变量
	pStud->age = 20; 
	cout << pStud->name << "-" << pStud->age << '\n' << endl;  // 输出pStud指向的STUD对象的成员

	cout << sizeof(STUD) << endl;  // 输出结构STUD的大小
	cout << sizeof(char *) << endl;  // 输出类型char *的大小
	cout << sizeof(int) << endl;  // 输出类型int的大小
	//*/

	/* 2.2.4
	enum Year {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec} Month; // 声明一个枚举类型
	enum Week {Sun,Mon=1,Tue,Wed,Thu,Fri,Sat} Day;  // 显式初始化声明

	enum Year Month1 = May;  // 正确,为枚举变量Month1赋枚举常量May
	enum Year Month2 = 5;  // 错误,为枚举变量Month2赋常数

	enum Week Day1 = Wed;  // 正确,为枚举变量Day1赋枚举常量Wed
	enum Week Day2 = 4;  // 错误,为枚举变量Day2赋常数
	//*/

	/* 2.4.1
	cout<<"sizeof(char):"<<sizeof(char)<<endl;
	cout<<"sizeof(int):"<<sizeof(int)<<endl;
	cout<<"sizeof(float):"<<sizeof(float)<<endl;
	cout<<"sizeof(double):"<<sizeof(double)<<endl;
	struct MyStruct
	{
		char a;
		int b;
		float c;
		double d;
		void * e;
	};
	cout<<"sizeof(MyStruct):"<<sizeof(MyStruct)<<endl;
	char str[7] = "Hello!";
	cout<<"sizeof(str[]):"<<sizeof(str)<<endl;
	int array[] = {1,2,3,4,5,6,7,8,9};
	cout<<"sizeof(array[]):"<<sizeof(array)<<endl;
	//*/

	/* 2.4.2
	// ① 先声明指针,再为其分配内存空间
	char *pFileName;  // 声明一个指针变量
	pFileName = new char[255];  // 分配内存空间
	if (pFileName != NULL)  // 如果分配成功
	{
		// 使用pFileName指针
	}
	// 其它操作
	delete pFileName;  // 释放内存空间

	// ② 声明指针的同时分配内存空间
	char * pPathName = new char[255]; // 声明指针变量并分配内存空间
	if (pPathName != NULL)  // 如果分配成功
	{
		//使用pPathName指针
	}
	// 其它操作
	delete [] pPathName; // 释放内存空间
	//*/

	/* 2.5.1
    HandWin("剪刀", "剪刀");
	HandWin("石头", "剪刀");
	HandWin("布", "剪刀");
	HandWin("石头", "布");
	HandWin("石头", "石头");
	//*/

    cout<<"分数为:99;成绩为:"; JudgeGrade(JudgeMark(99));
	cout<<"分数为:88;成绩为:"; JudgeGrade(JudgeMark(88));
	cout<<"分数为:66;成绩为:"; JudgeGrade(JudgeMark(66));
	cout<<"分数为:59;成绩为:"; JudgeGrade(JudgeMark(59));
	cout<<"分数为:-8;成绩为:"; JudgeGrade(JudgeMark(0-8));

	return 0;
}


⌨️ 快捷键说明

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