大整数乘法
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
const char* bigIntergerMulti(const char* str1, const char* str2);
int main()
{
char *str1 = "99";
char *str2 = "99";
const char *res = bigIntergerMulti(str1, str2);
std::cout << "result1 : " << res << std::endl;
std::cout << "result2 : " << 99 * 99 << std::endl;
system("pause");
return 0;
}
const char* bigIntergerMulti(const char* str1, const char* str2)
{
if (!str1 || !str2)
return NULL;
int len1 = ::strlen(str1);
int len2 = ::strlen(str2);
std::vector<int> cpm(len1 + len2, 0);
for (int i = 0; i < len1; ++i)
{
for (int j = 0; j < len2; ++j)
{
cpm[i + j + 1] += (str1[i] - '0')*(str2[j] - '0');
}
}
for (int i = len1 + len2 - 1; i > 0; --i)
{
if (cpm[i] >= 10)
{
cpm[i - 1] += cpm[i] / 10;
cpm[i] %= 10;
}
}
int indx = 0;
while (cpm[indx] == 0)
{
++indx;
}
char *strres = new char[len1+len2-indx+1];
int k = 0;
while (indx < len1 + len2)
{
strres[k++] = cpm[indx++] + '0';
}
strres[k] = '\0';
return strres;
}
字符串分离
#include <iostream>
#include <cstring>
#include <cstdlib>
void DivideString(const char* pInputStr, long lInputLen, char *pOutputStr);
int main()
{
const char *inStr = "abc def gh i d";
long inLen = strlen(inStr);
char *outStr = new char[inLen];
DivideString(inStr, inLen, outStr);
std::cout << "outStr is : " << outStr << std::endl;
system("pause");
return 0;
}
void DivideString(const char* pInputStr, long lInputLen, char *pOutputStr)
{
if (!pInputStr || !pOutputStr)
return;
int i, j = 0;
bool first = true;
for (i = 0; i < lInputLen; ++i)
{
if (pInputStr[i] != ' ')
{
pOutputStr[j++] = pInputStr[i];
first = true;
}
else
{
if (first)
{
pOutputStr[j++] = ',';
first = false;
}
}
}
pOutputStr[j++] = ',';
pOutputStr[j++] = '\0';
}
单链表逆序
#include <stack>
#include <iostream>
#include <cstdlib>
typedef struct tagListNode
{
int value;
struct tagListNode *next;
}ListNode;
void convert(ListNode **head);
int main()
{
ListNode* curr = new ListNode;
curr->value = 0;
curr->next = NULL;
ListNode* head = curr;
for (int i = 1; i < 10; ++i)
{
curr->next = new ListNode;
curr = curr->next;
curr->next = NULL;
curr->value = i;
}
convert(&head);
while (head)
{
std::cout << "value is : " << head->value << std::endl;
head = head->next;
}
system("pause");
return 0;
}
void convert(ListNode **head)
{
std::stack<ListNode*> stack_;
while (*head)
{
stack_.push(*head);
*head = (*head)->next;
}
ListNode* curr = stack_.top();
curr->next = NULL;
*head = curr;
stack_.pop();
while (!stack_.empty())
{
curr->next = stack_.top();
curr = curr->next;
curr->next = NULL;
stack_.pop();
}
}