프로그래밍

 3198, 1/160 회원가입  로그인  
   jjong1986
   c,c++ 질문입니다. 동적할당 관련...

http://www.hackerschool.org/HS_Boards/zboard.php?AllArticle=true&no=2916 [복사]


문제가


      단어의 갯수입력 : 3
      단어1:television
      단어2:hi
      단어3:welcome

      (입력단어)television-hi-welcome

이렇게 출력되도록 짜는 것인데요
제가 동적할당을 잘몰라서요 제가  코드가 있는데 좀봐주세요 ㅠㅠ
/*  
//이경우에는 문자하나씩 밖에 입력이안되요 ㅠㅠ
//문자열 입력방법만 알면될것같은데 ㅠㅠ..
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
int main()
{
        int n,i;
        char *ch;
        scanf("%d",&n);
        ch = (char*)malloc(sizeof(char*)*n);
        for(i=0;i<n;i++)
        {
                printf("단어%d:",i+1);
                cin>>ch[i];
        }
        for(i=0;i<n;i++)
        {
                cout<<ch[i]<<endl;
        }
        free(ch);
        return 0;
}
*/

  Hit : 4223     Date : 2011/03/17 02:41



    
별빛을담아 일단 확실히 하고 싶은게요...
이게 C언어인가요 C++인가요? ㅇㅅㅇ
cin이나 cout이 있는걸 보면 C++인데.... malloc은 C언어서 쓰는 거거든요
C++을 공부하신다면 malloc보다는 new와 delete를 사용하시는게 더 편하고 쉽게 할 수 있습니다.
2011/03/17  
jjong1986 아아 둘다 공부해서 좀 섞어서 해보았어요 ㅠㅠ 제가 실력이 별로 없어서 효율보다는 지금 프로그램이 가동되는걸 중요시하고 있어서요 ㅠㅠ 2011/03/17  
rainroad87 C언어
------------------------------------------------------------------------
#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define MAX_WORD_LEN 80
#define MAX_WORD_COUNT 10

int main()
{
int nWordCount = 0;
int i=0;
char szTotalWord[MAX_WORD_LEN*MAX_WORD_COUNT] = {0,};

printf("단어의 갯수 입력 : ");
scanf("%d",&nWordCount);

if(nWordCount > MAX_WORD_COUNT)
{
return -1;
}

for(i=0;i<nWordCount;i++)
{
char *szWord = (char*)malloc(sizeof(char)*MAX_WORD_LEN);

printf("단어%d : ",i+1);
scanf("%s",szWord);

if(i != nWordCount-1)
{
sprintf(szWord,"%s-",szWord);
}

strcat(szTotalWord,szWord);

free(szWord);
}
printf("입력단어 : %s\n",szTotalWord);
}
2011/03/17  
rainroad87 C++언어
------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
using namespace std;

#define MAX_WORD_LEN 80
#define MAX_WORD_COUNT 10

class MergeWord
{
public:
int m_WordCout;
char m_szTotalWord[MAX_WORD_LEN*MAX_WORD_COUNT];
bool m_bErrorCount;

public:
MergeWord()
{
m_WordCout = 0;
memset(m_szTotalWord,0,sizeof(m_szTotalWord));
m_bErrorCount = true;
}
bool Run()
{
if(!m_bErrorCount)
{
return false;
}
for(int i=0;i<GetWordCount();i++)
{
char Word[MAX_WORD_LEN] = {0,};
cout << "단어" << i+1 << " : ";
cin >> Word;
SetWord(i,Word);
}
return true;
}

void SetWordCount(int WordCount)
{
if(WordCount > MAX_WORD_COUNT)
{
m_bErrorCount = false;
}
m_WordCout = WordCount;
}
void ShowWords()
{
if(m_bErrorCount)
{
cout << "입력단어 : " << m_szTotalWord << endl;
}
}

private:
void SetWord(int nCount,char* Word)
{
char *szWord = new char[MAX_WORD_LEN];
strcpy(szWord,Word);

if(nCount != GetWordCount()-1)
{
sprintf(szWord,"%s-",szWord);
}

strcat(m_szTotalWord,szWord);

delete[] szWord;
}

int GetWordCount()
{
return m_WordCout;
}
};

int main()
{
MergeWord Word;
int nWordCount = 0;

cout << "단어의 갯수입력 : ";
cin >> nWordCount;
Word.SetWordCount(nWordCount);
if(!Word.Run())
{
return -1;
}
Word.ShowWords();
return 0;
}
2011/03/17  
rainroad87 제일 쉬운 방법
-------------------------------------------------------
#include "stdafx.h"
#include <stdio.h>

void main()
{
printf("단어의 갯수입력 : 3\n");
printf("단어1:television\n");
printf("단어2:hi\n");
printf("단어3:welcome");
printf("\n");
printf("(입력단어)television-hi-welcome\n");
}
2011/03/17