playXP

서브 메뉴

Page. 1 / 12502 [내 메뉴에 추가]
글쓰기
작성자 아이콘 marine12
작성일 2018-10-09 20:47:46 KST 조회 423
제목
제 string 이용한 코드 한 번만 봐주십쇼 (_ _)

c언어로는 구현 성공했습니다. 감사합니다 공고 선생님들. 근데 문제가 생겨서 이제 string으로 시도하고 있습니다.

#include <iostream>

#include <string>

using namespace std;

int is_same_word(string arr[], string x, int y);

int main()
{
    string sen; //입력할 문자열

    string words[1000];
    int counts[1000];
    static int count = 0;
    cout << "문장을 입력하세요. (문장의 끝은 'Enter'입니다.) \n";

    getline(cin, sen, '\n'); //문자열 입력받음

    int start_index = 0;
    while (true)
    {
        int find_index = sen.find(' ', start_index); //띄워쓰기를 기준으로 find_index에 저장
        if (find_index == -1)                        //띄워쓰기가 없는경우
        {
            string temp = sen.substr(start_index);

            if (is_same_word(words, sen, count) == -1) //배열에 없는경우
            {
                words[count] = temp;
                counts[count] = 1;
                break;
            }
            else //배열에 있는 경우
                        {
                for (int a = 0; a < count; a++)
                {
                    if (temp == words[a])
                        counts[a] = counts[a] + 1;
                    break;
                }
                        }
            break;
        }
        else //띄워쓰기가 있는 경우
        {
            int temp_len = find_index - start_index;         //단어의 길이
            string temp = sen.substr(start_index, temp_len); //단어 복사하여 temp에 저장

            if (is_same_word(words, sen, count) == -1) //배열에 없는경우
            {
                words[count] = temp;
                counts[count] = 1;
                count++;
                start_index = find_index + 1;
            }
            else //배열에 있는경우
            {
                for (int b = 0; b < count; b++)
                {
                    if (temp == words)
                        counts = counts + 1;
                    start_index = find_index + 1;
                }
            }
        }
    }

    for (int c = 0; c <= count; c++)
    {
        cout << words[c] << " : " << counts[c] << endl;
    }
    return 0;
}

int is_same_word(string arr[void], string x, int y) //검색하는 함수
{
    int word;
    for (int i = 0; i <= y; i++)
    {
        word = x.find(arr[y]);
    }
    return word;

} 

이게 제 함수인디 작동이 안되고 있습니다. 한 번만 봐주시고 어떤 부분이 잘못 됐는 지 좀 알려주실 수 있으신가욤?

지속적인 허위 신고시 신고자가 제재를 받을 수 있습니다.
신고 사유를 입력하십시오:

아이콘 marine12 (2018-10-09 20:48:23 KST)
0↑ ↓0
센스 이미지를 등록해 주세요
띄워쓰기가 아니라 띄어쓰기입니다. 한글날에 죄송합니다용.
포더윈터 (2018-10-09 20:52:30 KST)
0↑ ↓0
센스 이미지
1. is_same_word니까 리턴형 bool로 하세요...

2. is_same_word의 파라메터 string 두 개 받게 하세요

3. is_same_word라는 말의 뜻이 검색이 아니잖아요
포더윈터 (2018-10-09 20:58:36 KST)
0↑ ↓0
센스 이미지
bool is_same_word(string a, string b)
{
  // a,b가 같으면 true, 틀리면 false를 리턴한다
}

이거부터 생각해보고 오세요 님은 아직 준비가 안됐음
아이콘 marine12 (2018-10-09 21:00:35 KST)
0↑ ↓0
센스 이미지를 등록해 주세요
ㄴ 감사합니다 ㅜㅜ
아이콘 기라졸 (2018-10-09 21:02:55 KST)
0↑ ↓0
센스 이미지
갓오브워 생각나네

아트레우스 넌 준비가 안됬다..!

You are not ready
포더윈터 (2018-10-09 21:03:57 KST)
0↑ ↓0
센스 이미지
저는 일리단의 you are not prepared 생각했는데
아이콘 헤르방 (2018-10-09 21:24:14 KST)
0↑ ↓0
센스 이미지를 등록해 주세요
1. string을 array of string으로 전환 (reference: https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c)
2. 전체 array of string을 돌며 word에 등록 혹은 += 1

코드 봐선 구조체나 클래스를 안 배운 거 같애서 int is_same_word() 보다 bool has_word() 란 이름으로 바꿔주면 조금 더 좋아보일 거 같네요
댓글을 등록하려면 로그인 하셔야 합니다. 로그인 하시려면 [여기]를 클릭하십시오.
롤토체스 TFT - 롤체지지 LoLCHESS.GG
소환사의 협곡부터 칼바람, 우르프까지 - 포로지지 PORO.GG
배그 전적검색은 닥지지(DAK.GG)에서 가능합니다
  • (주)플레이엑스피
  • 대표: 윤석재
  • 사업자등록번호: 406-86-00726

© PlayXP Inc. All Rights Reserved.