1、使用指针的指针
#include "stdafx.h"
#include <iostream.h>
int main(int argc, char* argv[])
{
int* pArray[3]; //定义一个指针数组
//为pArray中的元素赋值
int nArray[3] = {1, 2, 3}; //定义一个数组,包含3个元素
for(int i=0; i<3; i++) //使用指针遍历数组
{
pArray[i] = &nArray[i];
}
int** pIndex; //定义指针的指针
pIndex = pArray; //将指针数组赋值给pIndex
for (int j=0; j<3; j++)
{
cout << **pIndex << endl; //输出数组元素指向的数据
*pIndex++; //指向下一个数组元素
}
return 0;
}
2、使用引用对象代替目标对象
#include "stdafx.h"
#include <iostream.h>
int main(int argc, char* argv[])
{
int nKinds = 100; //定义一个整型变量
int& nRefKinds = nKinds; //定义一个引用对象,将其初始化为nKinds
cout << "nRefKinds的值:" << nRefKinds << endl; //输出nRefKinds
nRefKinds = 200;
cout << "nKinds的值:" << nKinds << endl; //输出nKinds
return 0;
}
3、使用异或运算实现两个变量值的互换
任何数据与0进行异或运算,结果仍为数据本身。
变量与自身进行异或运算,结果为0。
异或运算具有交换性,a^b^c = b^c^a = c^a^b
#include "stdafx.h"
#include "iostream.h"
void main()
{
int nLen = 50; //定义一个变量nLen,初始值为50
int nHeight = 40; //定义一个变量nHeight,初始值为40
cout << "转换前nLen = " << nLen << endl; //输出nLen
cout << "转换前nHeight = " << nHeight << endl; //输出nHeight
nLen = nLen ^ nHeight; //nLen与nHeight进行异或运算,结果赋值给nLen
nHeight = nHeight ^ nLen; //nHeight与nLen进行异或运算,结果赋值给nHeight
nLen = nLen ^ nHeight; //nLen与jvar进行异或运算,结果赋值给nLen
cout << "转换后nLen = " << nLen <<endl; //输出nLen
cout << "转换后nHeight = " << nHeight << endl; //输出nHeight
}
4、简易字符串加解密
#include "stdafx.h"
#include "iostream.h"
//加密函数
bool Encrypt(const char szText[], unsigned int nTextLen,
char szOutString[], unsigned int nOutLen)
{
if (nTextLen <=0 || nOutLen < nTextLen) //验证数组长度是否合法
{
return false;
}
char chLetter; //定义一个字符变量
for(int i=0; i<nTextLen-1; i++) //遍历szText字符串
{
chLetter = szText[i] + i + 10; //设置加密字符
szOutString[i] = chLetter;
}
szOutString[i] = '\0';
return true;
}
//解密函数
bool Decrypt(const char szText[], unsigned int nTextLen,
char szOutString[], unsigned int nOutLen)
{
if (nTextLen <=0 || nOutLen < nTextLen) //验证数组长度是否合法
{
return false;
}
char chLetter;
for(int i=0; i<nTextLen-1; i++) //遍历szText字符串
{
chLetter = szText[i] - i - 10; //设置解密字符
szOutString[i] = chLetter;
}
szOutString[i] = '\0';
return true;
}
int main(int argc, char* argv[])
{
char szText[] = "mrsoft"; //定义一个字符串
char szRet[sizeof(szText) / sizeof(char)] = {0}; //记录加密后的密文
char szDecrypt [sizeof(szText) / sizeof(char)] = {0}; //记录解密后的明文
if (Encrypt(szText, sizeof(szText), szRet,
sizeof(szRet) / sizeof(char))) //字符串加密
{
cout << szText << "的密文是:" << szRet <<endl; //输出密文
}
if (Decrypt(szRet, sizeof(szRet) / sizeof(char),
szDecrypt, sizeof(szDecrypt) / sizeof(char))) //字符串解密
{
cout << szRet << '\0' << "的明文是: " << szDecrypt << endl; //输出明文
}
return 0;
}
5、反转输出字符串
#include "stdafx.h"
#include "iostream.h"
void ReverseString(char szText[], int nLen, char szOutString[])
{
char *pLetter = szOutString + nLen - 2 ; //定位到szOutString的末尾指针
while (*szText != '\0') //遍历szText字符串
{
*pLetter = *szText; //读取源字符串中的字符
pLetter--; //指向前一个字符
szText++; //指向后一个字符
}
}
int main(int argc, char* argv[])
{
char szText[] = "mrsoft"; //定义一个字符数组
char szReverse[sizeof(szText) / sizeof(char)] = {0}; //定义目标字符数组
ReverseString(szText, sizeof(szText) / sizeof(char), szReverse); //调用ReverseString反转字符串
cout << "源字符串: " << szText << endl; //输出源字符串
cout << "目标字符串: " << szReverse << endl; //输出目标字符串
return 0;
}
6、学习使用系统时间
#include "stdafx.h"
#include "iostream.h"
#include <time.h>
int main(int argc, char* argv[])
{
cout << "请输入命令符:"; //输出字符串
char chCmd[256] = {0}; //定义一个字符数组
while (true)
{
cin.getline(chCmd, 255); //等待用于输入
if (chCmd[0] == '?') //判断用于输入的字符
{
//输出帮助信息
cout << "输入数字1显示系统时间, 输入数字2显示系统日期, 输出字母e退出系统!" << endl;
}
else if (chCmd[0] == 'e') //如果是命令字符e
{
return 0; //退出系统
}
else if (chCmd[0] == '1') //如果是命令字符1,显示系统时间
{
time_t nowTime;
time(&nowTime); //获取系统时间
struct tm *sysTime = localtime(&nowTime); //转换为系统时间
cout << "系统时间:" << sysTime->tm_hour << ":" <<
sysTime->tm_min << ":" << sysTime->tm_sec <<endl; //输出信息
}
else if (chCmd[0] == '2')
{
time_t nowTime;
time(&nowTime); //获取系统时间
struct tm *sysTime = localtime(&nowTime); //转换为系统时间
cout << "系统日期:" << 1900 + sysTime->tm_year << "-" <<
sysTime->tm_mon + 1 << "-" << sysTime->tm_mday <<endl;//输出信息
}
cout << "请输入命令符:";
}
return 0;
}
7、不使用系统函数,进行字符串拷贝
#include "stdafx.h"
#include "iostream.h"
void CopyString(const char* pszSrc, char* pszDes)
{
char *pChar = (char*)pszSrc;
char *pDes = pszDes;
while(*pChar != ' ')
{
*pDes = *pChar;
pDes ++;
pChar++;
}
}
int main(int argc, char* argv[])
{
char szDes[128] = {0};
CopyString("VC编程词典!", szDes);
cout << szDes << endl;
return 0;
}
8、已知两边和夹角,求第三条边
使用余弦定理
#include "stdafx.h"
#include "iostream.h"
#include "math.h"
const double PI = 3.1415926;
//根据三角形两边和夹角计算第3边
double CalcTriangle(double dbBorder_1, double dbBorder_2, double dbAngle)
{
double dbRadian = dbAngle * PI / 180;
double dbRet = sqrt(pow(dbBorder_1, 2) + pow(dbBorder_2, 2) - 2*dbBorder_1*dbBorder_2*cos(dbRadian));
return dbRet;
}
int main(int argc, char* argv[])
{
double dbRet = CalcTriangle(20, 10, 90);
cout << dbRet << endl;
return 0;
}
9、找到1998-2050之间的所有闰年
判断闰年,能被4整除不被100整除,或者能被100整除并能被400整除的年份。
#include "stdafx.h"
#include "iostream.h"
//判断某一年是否是闰年
bool IsLeapyear(int nYear)
{
if (nYear % 4 == 0 && nYear % 100 != 0)
{
return true;
}
else if (nYear % 100 == 0 && nYear % 400 == 0)
{
return true;
}
return false;
}
int main(int argc, char* argv[])
{
for(int nYear = 1998; nYear < 2051; nYear++)
{
if (IsLeapyear(nYear))
{
cout << nYear << endl;
}
}
return 0;
}