📄 common.cpp
字号:
////////////////////////
// //
// Common.cpp //
// 公用函数集合 //
// //
////////////////////////
#include <bios.h>
#include <dos.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include "Common.h"
#include "Paint.h"
void Receive(int &Key)
{
// 从键盘读入字符
while (bioskey(1) == 0);
Key = bioskey(0);
}
void Sound()
{
// 警告音
sound(1000);
delay(100);
nosound();
}
void GetPart
(
const char Source[MaxY], // 源字符串
const int n, // 第n部分
const char Separator, // 分隔符
char *Return // 返回部分
)
{
// 返回指定字符串的某一部分
// 返回部分清零
Return[0] = '\0';
// 源字符串长度
int Len = strlen(Source);
// 序号清零
int No = 0;
for (int i = 0; i <= Len; i++)
{
if (Source[i] != Separator && i != Len)
{
// 非分隔符,且字符串未扫描完毕
if (Return[0] == '\0')
{
No++;
}
// 将当前字符加入单词
strcat(Return, " ");
Return[strlen(Return) - 1] = Source[i];
}
else
{
// 当前是分隔符
if ( No >= n )
{
// 已经找到
return;
}
// 单词清空
Return[0] = '\0';
}
}
}
void GetBack
(
const char Source[MaxY], // 源字符串
const int n, // 序号
const char Separator, // 分隔符
char *Return // 返回部分
)
{
// 返回字符串的后面部分
// 源字符串长度
int Len = strlen(Source);
// 序号清零
int No = 0;
int i = 0;
while (i < Len && No < n)
{
if (Source[i] == Separator && (i == 0 || Source[i - 1] != Separator))
{
// 当前是分隔符,且前一个不是分隔符
No++;
}
i++;
}
if (i < Len)
{
// 找到返回部分
// 从源字符串中截取后部
GetMid(Source, i, Len, Return);
// 清空前部和后部的空格
ClearHeadBlank(Return);
ClearTailBlank(Return);
}
else
{
// 未找到,返回空字符串
Return[0] = '\0';
}
}
void GetMid
(
const char Source[MaxY], // 源字符串
const int Start, // 开始位置
const int End, // 结束位置
char *Return // 返回部分
)
{
// 返回指定字符串的中间某部分
// 返回部分清空
Return[0] = '\0';
// 源字符串长度
int Len = strlen(Source);
for (int i = Start; i <= End && i < Len; i++)
{
// 逐个字符添加返回字符串,直到到达结束位置,或者源字符串结束
strcat(Return, " ");
Return[strlen(Return) - 1] = Source[i];
}
}
VarType GetType(const char Name[MaxLenOfVar])
{
// 返回指定字符串的类型
// 从变量类型链表头开始查找
VarAllType *pAll = HeadAll;
while (pAll != NULL)
{
if (strcmp(pAll->Name, Name) == 0)
{
// 找到变量,返回类型
return pAll->Type;
}
// 继续查找
pAll = pAll->NextVar;
}
// 在系统函数中继续查找
for (int i = 0; i < TotSysFunc; i++)
{
if (strcmp(SysFuncName[i], Name) == 0)
{
// 找到变量,返回系统函数属性
return SystemFunc;
}
}
// 无此变量
return NoType;
}
int VarValue_Int(const char Name[MaxLenOfVar])
{
// 返回整型变量的值
// 在整型变量链表中查找
VarIntType *pInt = HeadInt;
while (pInt != NULL)
{
if (strcmp(pInt->Name, Name)==0)
{
// 找到变量,返回值
return pInt->Value;
}
// 继续查找
pInt = pInt->NextVar;
}
// 无此变量,返回零
return 0;
}
double VarValue_Real(const char Name[MaxLenOfVar])
{
// 返回实型变量的值
// 在实型变量链表中查找
VarRealType *pNewReal = HeadReal;
while (pNewReal != NULL)
{
if (strcmp(pNewReal->Name, Name) == 0)
{
// 找到变量,返回值
return pNewReal->Value;
}
// 继续查找
pNewReal = pNewReal->NextVar;
}
// 无此变量,返回零
return 0;
}
VarArrayHeadType *GetArrayDim(const char Name[MaxLenOfVar])
{
// 返回数组变量的头指针
// 在数组变量链表
VarArrayHeadType *Return = HeadArray;
while (Return != NULL)
{
if (strcmp(Return->Name, Name) == 0)
{
// 找到变量,返回指针
return Return;
}
// 继续查找
Return = Return->NextArray;
}
// 返回空
return NULL;
}
void ClearHeadBlank(char *str)
{
// 删除字符串的前部空格
// 字符串长度
int Len = strlen(str);
for (int i = 0; i < Len; i++)
{
if (str[i] != ' ')
{
// 找到非空格的头位置,以此前移
for (int j = 0; j < Len - i + 1; j++)
{
str[j] = str[j + i];
}
return;
}
}
// 全部都是空格,返回空
str[0] = '\0';
}
void ClearTailBlank(char *str)
{
// 删除尾部空格
// 指针,先指在字符串末尾
int j = strlen(str) - 1;
while (str[j] == ' ' && j >= 0)
{
// 当前位是空格,前移
j--;
}
// 后一位为字符串末尾
str[j + 1] = '\0';
}
void ClearBlankInside(char *str)
{
// 删除字符串中的空格
// 存储非空格字符的字符串
char *strBak;
strcpy(strBak, str);
int Len = strlen(strBak);
// 源字符串清空
str[0] = '\0';
for (int i = 0; i < Len; i++)
{
if (strBak[i] != ' ')
{
// 当前位非空格,添加入源字符串
strcat(str, " ");
str[strlen(str) - 1] = strBak[i];
}
}
}
void Para
(
const char SourcePara[MaxY], // 参数字符串
int *Return, // 参数数组的头指针
int &Tot // 参数的个数
)
{
// 从参数字符串中提取各参数值
// 参数个数
Tot = 0;
// 暂存参数字符串
char Source[MaxY];
strcpy(Source, SourcePara);
// 末尾添加逗号,方便分离
strcat(Source, ",");
// 存储一个参数字符串
char strExp[MaxY];
// 取第1个参数
GetPart(Source, 1, ',', strExp);
while (strExp[0] != '\0')
{
// 当前参数不为空
// 计算该参数的值
int Seat = 0;
int Exp = (int)Expression(strExp, Seat);
if (Error)
{
// 计算中有错误
ShowError(Error,RunLineNo, "");
return;
}
else
{
// 记下该参数的值
Return[Tot] = Exp;
Tot++;
}
// 得到下一个参数
GetPart(Source, Tot + 1, ',', strExp);
}
}
bool GetParaStr
(
const char Source[MaxY], // 源字符串
const int Start, // 左括号开始的位置
char *Return, // 返回的参数字符串
int &i // 对应右括号的位置的下一个位置
)
{
// 得到参数字符串,从字符串Source的Start位开始,该位必须是左括号
if (Source[Start] != '(')
{
Error = ErrorMissingParameter;
return false;
}
// 返回字符串清空
Return[0] = '\0';
// 括号匹配标志
int Mark = 0;
// 扫描指针
i = Start;
do
{
// 根据左右括号,修改匹配标志
switch (Source[i])
{
case '(':
Mark++;
break;
case ')':
Mark--;
break;
}
if (i > Start)
{
// 不是第一个左括号则添加
strcat(Return, " ");
Return[strlen(Return) - 1] = Source[i];
}
i++;
} while (i < strlen(Source) && Mark > 0);
// 去掉最后一个右括号
Return[strlen(Return) - 1] = '\0';
// 返回成功标志
if (Mark == 0)
{
return true;
}
else
{
Error = ErrorMissingBracketInArray;
return false;
}
}
int CalcPosition
(
const VarArrayHeadType *pThis, // 数组头指针
const int *Dim // 各维下标
)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -