Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.4 kB
0
Indexable
Never
#include<iostream>
#include<cctype>
#include<cstring>
using namespace std;

const int MAX_LENGTH = 1500;

int main()
{
    char inputString[MAX_LENGTH] = {0};
    cin.getline(inputString, MAX_LENGTH);
    char outputString[MAX_LENGTH] = {0};
    char *checkPointers = inputString;//用來記錄函式中處理位置的指標
    char blank[3] = {' ', '('};


    for(int i = 0; i < MAX_LENGTH; i++)
    {
        if(isblank(inputString[i]) != 0)//分別用blank還有標點符號作為切單字的依據
        {
            strncat(outputString, checkPointers, &inputString[i] - checkPointers);
            if(isalnum(inputString[i+1]) != 0 || inputString[i+1] == '(')
            {
                for(int j = 0; j < MAX_LENGTH; j++)
                {
                    if(outputString[j] == '\0')
                    {
                        outputString[j] = ' ';
                        outputString[j+1] = '\0';
                        break;
                    }
                }
            }
            checkPointers = &inputString[i+1];
        }

        else if(ispunct(inputString[i]) != 0 && inputString[i] != '(')
        {
            strncat(outputString, checkPointers, &inputString[i+1] - checkPointers);//這裡有問題
            if(inputString[i] != ')' || ispunct(inputString[i+1]) == 0)
            {
                for(int j = 0; j < MAX_LENGTH; j++)
                {
                    if(outputString[j] == '\0')
                    {
                        outputString[j] = ' ';
                        outputString[j+1] = '\0';
                        break;
                    }
                }
            }
            checkPointers = &inputString[i+1];
        }

        else if(inputString[i] == '(')
        {
            strncat(outputString, checkPointers, &inputString[i] - checkPointers);
            strncat(outputString, blank, 3);
            checkPointers = &inputString[i+1];
        }
        if(inputString[i] == '\0')
        {
            break;
        }
    }

    for(int i = 0; i < MAX_LENGTH; i++)
    {
        if(outputString[i] == '\0')
        {
            outputString[i-1] = '\0';
            break;
        }
    }
    cout << outputString << endl;
    return 0;
}