c_cpp 这是一个包含30多个常用函数的包含文件,就像VB中的函数一样,使VB程序员能够更轻松地使用C / C ++
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 这是一个包含30多个常用函数的包含文件,就像VB中的函数一样,使VB程序员能够更轻松地使用C / C ++相关的知识,希望对你有一定的参考价值。
/*
this is a include file with over 30 common functions that act like the ones in VB
makeing it easyer for VB programmers to grasp C/C++ using farmilar functions from VB6
Code by Ben on PSC
http://planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=13864&lngWId=3
*/
#include <stdio.h>
#include <time.h>
#include <string>
//Strings
char* ToTitleCase(char* s);
char* ToString(char s[]);
char* Str(int Number);
bool StartsWidth(char* s, char* SubString);
bool EndsWith(char* s, char* SubString);
bool Equals(char* a, char* b);
bool Contains(char* s, char* Match, bool TextCompare);
int InStrRev(char* s, char* SubString, int Start, bool TextCompare);
int Instr(char* s, int start, char* substring, bool TextCompare);
int IndexOf(char* s, char* SubString);
int IndexOf(char* s, int Value);
int Asc(char c);
char Chr(int value);
int Len(char* s);
char* VbString(int Number, char Character);
char* Space(int Number);
char* StrReverse(char* s);
char* Mid(char* s, int start, int end);
char* Left(char* s, int Length);
char* Right(char* s, int Length);
char *Trim(char *s);
char* LTrim(char* s);
char* RTrim(char* s);
char* UCase(char* s);
char* LCase(char* s);
//Date Time
char* Now();
char* Year();
char* Day();
char* Month();
//Math
char* Hex(int Number);
int Fix(double Number);
int Oct(int Number);
int Val(char* Expression);
bool IsNumeric(char* Expression);
char* ToTitleCase(char* s)
{
int l = Len(s);
int i = 1;
char* Temp = NULL;
if (l == 0)
{
return "";
}
Temp = Mid(s, 0, Len(s));
// Uppercase first letter
Temp[0] = toupper(Temp[0]);
// Uppercase the start of each word
while (i < Len(Temp))
{
// Locate space
if (Temp[i - 1] == ' ')
{
Temp[i] = toupper(Temp[i]);
}
i++;
}
return Temp;
}
char* ToString(char s[])
{
int l = Len(s);
char* Str = (char*)malloc(l * sizeof(char*));
strcpy(Str, s);
return Str;
}
bool StartsWidth(char* s, char* SubString)
{
if (strcmp(SubString, Left(s, Len(SubString))) == 0)
{
return true;
}
return false;
}
bool EndsWith(char* s, char* SubString)
{
if (strcmp(SubString, Right(s, Len(SubString))) == 0)
{
return true;
}
return false;
}
char* Str(int Number)
{
char sNum[20];
/*
char sNum[20];
sprintf(sNum,"%d",Number);
//Resize array
char *pStr = (char*)malloc(Len(sNum) * sizeof(char*));
//Copy contents of sNum into pStr
strcpy(pStr,sNum);
//Return
return pStr;
*/
return itoa(Number, sNum, 10);
}
char* Hex(int Number)
{
char sHex[10];
int i = 0;
// Convert to hex number
sprintf(sHex, "%x", Number);
// Resize char* to fix sHex
char* Str = (char*)malloc(Len(sHex) * sizeof(char*));
// Copy sHex into Str
strcpy(Str, sHex);
// Return
return Str;
}
int Fix(double Number)
{
return (int)Number;
}
int Oct(int Number)
{
int LeftOver = 0;
int iOct = 0;
int i = 1;
// Convert number to oct
do
{
LeftOver = Number % 8;
Number = (Number / 8);
iOct = (iOct + LeftOver * i);
i = (i = 10);
} while (Number != 0);
// Return Oct number
return iOct;
}
int Val(char* Expression)
{
return atoi(Expression);
}
bool IsNumeric(char* Expression)
{
bool IsGood = true;
while (*Expression)
{
if (!isdigit(*Expression))
{
IsGood = false;
}
*Expression++;
}
return IsGood;
}
int IndexOf(char* s, char* SubString)
{
return Instr(s, 0, SubString, false);
}
int IndexOf(char* s, int Value)
{
int i = 0;
int j = -1;
while (*s)
{
if (*s == Value)
{
j = i;
break;
}
*s++;
i++;
}
return j;
}
bool Equals(char* a, char* b)
{
return strcmp(a,b) == 0;
}
bool Contains(char* s, char* Match, bool TextCompare = false)
{
if (Instr(s, 0, Match, TextCompare) != -1)
{
return true;
}
return false;
}
int InStrRev(char* s, char* SubString, int Start, bool TextCompare = true)
{
int i = 0;
int idx = -1;
char* sSub = NULL;
for (i = Start; i > 0; i--)
{
sSub = Mid(s, i, Len(SubString));
//
if (TextCompare == true)
{
if (strcmp(LCase(SubString), LCase(sSub)) == 0)
{
idx = i;
break;
}
}
else
{
// Check for string
if (strcmp(sSub, SubString) == 0)
{
idx = i;
break;
}
}
}
return idx;
}
int Instr(char* s, int start, char* substring, bool TextCompare = true)
{
int i = 0;
char* check;
int idx = -1;
for (i = start; i < Len(s); i++)
{
check = Mid(s, i, Len(substring));
if (TextCompare == true)
{
if (strcmp(LCase(substring), LCase(check)) == 0)
{
idx = i;
break;
}
}
else
{
// Check for string
if (strcmp(check, substring) == 0)
{
idx = i;
break;
}
}
}
return idx;
}
int Asc(char c)
{
return (int)c;
}
char Chr(int value)
{
return (char)value;
}
char* VbString(int Number, char Character)
{
char* Str = (char*)malloc(Number * sizeof(char*));
int i = 0;
while (i < Number)
{
Str[i] = Character;
i++;
}
Str[i] = '\0';
return Str;
}
char* Space(int Number)
{
return VbString(Number, '\ ');
}
char* StrReverse(char* s)
{
int l = Len(s);
int i = 0;
int j = 0;
char* Str = (char*)malloc(l + 1 * sizeof(char*));
for (i = l; i > 0; i--)
{
Str[j] = s[i - 1];
// INC Counter
j++;
}
// Set last char
Str[j] = '\0';
// Return
return Str;
}
char* Mid(char* s, int start, int end)
{
// Resize char *
char* sPtr = (char*)malloc(end + 1);
strncpy(sPtr, s + start, end);
// Add ending char
sPtr[end] = '\0';
// Return
return sPtr;
}
char* Left(char* s, int Length)
{
return Mid(s, 0, Length);
}
char* Right(char* s, int Length)
{
return Mid(s, Len(s) - Length, Length);
}
int Len(char* s)
{
int i = 0;
while (*s)
{
*s++;
i++;
}
return i;
}
char *Trim(char *s){
//Trim left and right of string,
return LTrim(RTrim(s));
}
char* LTrim(char* s)
{
while (*s == '\ ')
*s++;
return s;
}
char* RTrim(char* s)
{
char* StrCopy = NULL;
int i = strlen(s) - 1;
int idx = 0;
while (i >= 0)
{
if (s[i] != ' ')
{
idx = i;
break;
}
i--;
}
StrCopy = (char*)malloc(idx * sizeof(char*));
StrCopy[idx + 1] = '\0';
strncpy(StrCopy, s, idx + 1);
// Return
return StrCopy;
}
char* UCase(char* s)
{
int l = Len(s);
char* Str = (char*)malloc(l * sizeof(char*));
int i = 0;
while (i < l)
{
Str[i] = toupper(s[i]);
i++;
}
Str[i] = '\0';
return Str;
}
char* LCase(char* s)
{
int l = Len(s);
char* Str = (char*)malloc(l * sizeof(char*));
int i = 0;
while (i < l)
{
Str[i] = tolower(s[i]);
i++;
}
Str[i] = '\0';
return Str;
}
char* Now()
{
time_t ttime = time(0);
struct tm* timeinfo;
char t[100];
int l = 0;
int idx = -1;
// Get time
time(&ttime);
// Get local time
timeinfo = localtime(&ttime);
// Copy time to t array
strcpy(t, asctime(timeinfo));
// Locate \n
idx = InStrRev(t, "\n", Len(t), false);
if (idx != -1)
{
// Copy time
return Mid(t, 0, idx);
}
// Return nothing
return "";
}
char* Year()
{
char* sDate = Now();
int l = Len(sDate);
int idx = InStrRev(sDate, " ", l);
if (idx == -1)
{
return "";
}
// Extract year
return Mid(sDate, idx + 1, l);
}
char* Day()
{
char* sDate = Now();
int idx = Instr(sDate, 0, " ", false);
if (idx == -1)
{
return "";
}
// Extract day
return Left(sDate, idx);
}
char* Month()
{
char* sDate = Now();
int spos = Instr(sDate, 0, " ", false);
int epos = Instr(sDate, spos + 1, " ", false);
if (spos == -1)
{
return "";
}
// Extract month
return Mid(sDate, (spos + 1), (epos - spos) - 1);
}
以上是关于c_cpp 这是一个包含30多个常用函数的包含文件,就像VB中的函数一样,使VB程序员能够更轻松地使用C / C ++的主要内容,如果未能解决你的问题,请参考以下文章