大整数乘法,字符串分离和单链表逆转

大整数乘法

// 大整数乘法
#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;
}

字符串分离

/*****************************************************************************
 *                                                                           *
 *  通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。 *
 *  请编写一个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补  *
 *  充一个’,’并将子串存储。 如果输入“abc def gh i     d”,               * 
 *  结果将是abc,def,gh,i,d,                                                  *
 *                                                                           *
 ****************************************************************************/

#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';
}

// 解题思路 ,先给一个bool变量first,并置为真。遍历输入字符串,如果字符不等于space,
// 则直接写入输出字符串,将first设置为真。如果遇到了space,并且first为true,则将‘,’写入输出字符串,
// 并将first设置为false。如果遇到space,但first为false,直接跳过即可。全部遍历完后,需要将倒数第一
// 个字符设置为',',最后一个设置为'\0',避免出现乱码。

单链表逆序

/*****************************************************************
  将输入的一个单向链表,逆序后输出链表中的值。链表定义如下: 
  typedef struct tagListNode
  {
      int value;
      struct tagListNode *next;
  }ListNode;
  要求实现函数:
  void converse(ListNode **head);
 【输入】head:    链表头节点,空间已经开辟好
 【输出】head:    逆序后的链表头节点
 【返回】无
 【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出 
******************************************************************/

#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) // if( *head != NULL )
    {
        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();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值