수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

heap storage에 메모리 할당함으로 char의 제한 없애기

하양이

2023.07.19


질문 제목 :
heap storage에 메모리 할당함으로 char의 제한 없애기

현재 코드는 아래의 밑줄그어져있는 부분처럼 프로그램 종료 조건과 인풋 글자수가 제한되어있습니다.
heap storage도 쓰지 말라고해서 전혀 쓰지 않았구요.
여기서 heap storage를 사용하는 코드로 이러한 제한들을 없애고 싶은데 자꾸 오류가 나네요..
제가 정말 약한 부분이라... ㅠㅠ 도움좀 부탁드립니다
(문제 원문: modify your program so that there is no fixed limit on the size of a name in the input. the only limit should be the amount of available memory. you may use heap storage.)

질문 내용 :
기존의 프로그램 설명------------------------------------------------------------------------------scoreboard.c: 스탠다드 아웃풋을 통해 커맨드를 읽고 다음과 같은 커맨드를 수행한다.void score(char *string int number);
플레이어의 이름 string과 점수 number를 기록한다.
output은 없고, number는 -999999999와 999999999사이의 정수이다.void best(char *string);
한줄에 best string number 식으로 output을 한다.
여기서 number는 이름 string의 지금까지의 최고점수를 뜻한다.
만약 그 플레이어의 점수가 기록되지 않았다면 number대신에 ?를 output한다.void highscore(void);
한줄에 highscore number 식으로 output을 한다.
여기서 number는 지금까지의 최고점수를 뜻한다.
만약 어떤 점수도 기록되지 않았다면 highscore ?를 output한다.void disqualify(char *symbol);
이름이 symbol 인 모든 점수들을 다 제거한다.
highscore 커맨드는 이 플레이어의 의한 점수를 다 제외할것이다.
disqualify 이후에도 score 커맨드를 통해 다시 게임에 참가할수있다.void leaders(void);
한줄에 leaders 최고득점자이름들을 output 한다.
한 사람의 이름전에는 single space를 output 한다.
이름은 ascii에서의 사전순서로 나열한다.
만약 leader가 없다면 leaders ? 를 output 한다.프로그램은 3000개의 유효 커맨드 이후나 eof 중 먼저 발생시 종료된다.
만약 input name이 20자를 초과한다면, 전체의 이름을 다 읽되 첫20자까지만 고려한다.
heap storage를 써서는 안된다.readstring.h, readstring.c: int readstring(char *s, int n);정수 n까지의 string c를 읽습니다.예제 input
score fredflintstonefrombedrock 10
score wilma 20
score fredflintstonefrombe 20
highscore
score betty 30
highscore
best fredflintstonefrombedrock
score fredflintstonefrombedrock 25
best fredflintstonefrombeyond
best barney
score a 250
score b 200
score c 300
score d 250
disqualify c
highscore
best c
leaders예제 output
highscore 20
highscore 30
best fredflintstonefrombe 20
best fredflintstonefrombe 25
best barney ?
highscore 250
best c ?
leaders a d
코드-------------------------------------------------------------------------------------------
#include stdio.h
#include string.h
#include stdlib.h
#include stdbool.h
#include readstring.h// 이름과 점수를 저장할 structure
typedef struct ascore{
char name[21];
int score;
} score;score scorelist[3000];
int scorecount=0;void score(char *string, int number){
strcpy(scorelist[scorecount].name,string); // scorelist에 이름을 기록
scorelist[scorecount].score=number; // scorelist에 점수를 기록
scorecount++; // 몇번째 score 펑션 call 인가
}void best(char *string){
int bestscore=-1000000000; // becuase the number is -999999999 at least
int counter=0;
int i,j; for(i=0;iscorecount;i++){
if(!strcmp(string,scorelist[i].name)){
if(scorelist[i].scorebestscore){
bestscore = scorelist[i].score;
}
}
} printf(best %s ,string);
if(bestscore==-1000000000){
printf(?\n);
}else{
printf(%d\n,bestscore);
}
}void highscore(){
int highscore=-1000000000;
int i; if (scorecount==0){
printf(highscore ?\n);
} else {
for(i=0;iscorecount;i++){
if(scorelist[i].scorehighscore){
highscore=scorelist[i].score;
}
}
printf(highscore %d\n,highscore);
}
}void disqualify (char *symbol){
int i;
for(i=0;iscorecount;i++){
if(!strcmp(symbol,scorelist[i].name)){
scorelist[i].score=-1000000000;
}
}
}void leaders(){
int i,j;
int leadingscore=-1000000000;
char leaders[3000][21];
char temp[21];
int leaderscount=0; // find the real leadingscore
if(scorecount==0){
printf(leaders ?\n);
return;
}else{
for(i=0;i=scorecount;i++){
if(scorelist[i].scoreleadingscore){
leadingscore=scorelist[i].score;
}
}
} // find the leaders
for(i=0;iscorecount;i++){
if(scorelist[i].score==leadingscore){
// find the duplications
for(j=0;jleaderscount;j++){
if(!strcmp(leaders[j],scorelist[i].name)) break;
}
if (j==leaderscount){ // the end of the array: no duplication
strcpy(leaders[leaderscount++],scorelist[i].name);
}
}
} // order leaders array in ascending order
for(i=0;ileaderscount-1;i++){
for(j=i;jleaderscount;j++){
if(strcmp(leaders[i],leaders[j])0){
&nbbsp; strcpy(temp,leaders[i]);
strcpy(leaders[i],leaders[j]);
strcpy(leaders[j],temp);
}
}
} // printing
printf(leaders );
for(i=0;ileaderscount;i++){
printf(%s ,leaders[i]);
}
printf(\n);
}int main(void){
int n=0, n_command=0;
while(n_command3000){ // program processes 3000 valid commands only
char str[21];
if (scanf(%s,str)!=1) break;
if (!strcmp(str,score)){
readstring(str,20);
scanf(%d,&n);
score(str,n);
n_command++;
} else if (!strcmp(str,best)){
readstring(str,20);
best(str);
n_command++;
} else if (!strcmp(str,highscore)){
highscore();
n_command++;
} else if (!strcmp(str,disqualify)){
readstring(str,20);
disqualify(str);
n_command++;
} else if (!strcmp(str,leaders)){
leaders();
n_command++;
}
}
}----------------------------------------------------------------------------------------

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

번호 제 목 글쓴이 날짜
2694731 리눅스 커널의 시작점 질문 미르 2025-05-13
2694702 이거 뭐가문제인가요 코드수정좀 (3) 맑은 2025-05-13
2694675 C언어 후위표기를 중위표기로 앨런 2025-05-13
2694646 안녕하세요 파일 합치기 함수! (1) 연블루 2025-05-13
2694618 잘몰라서 설명부탁드립니다. scanf 관련 (3) 파라 2025-05-12
2694590 이 코드가 뭐하는 코드일까요? #2 빵순 2025-05-12
2694559 동적할당으로 배열(2차원열)을 만드는데 있어 그걸 함수화시키는데... (1) 늘솔길 2025-05-12
2694532 네트워크에 관하여... (4) 황소자리 2025-05-12
2694503 프로그램 연산 후 바로 종료되는 현상 (6) Judicious 2025-05-11
2694450 while문질문입니다. (1) 허리품 2025-05-11
2694420 C언어 질문할게요(유니코드,자료형,버퍼,캐스트연산자) 은새 2025-05-11
2694370 내일까진데 함수호출 제발 도와주세요!!!!!!!!!11 들찬 2025-05-10
2694339 putchar()의 괄호 안에 int c=10;로 전에 선언된 c를 넣으면 안되는 이유에서 제가 생각한 것이 그 이유가 되는지 확인하고 싶습니다. (3) 미르 2025-05-10
2694316 이 코드 어디가 잘못되었는지 고수분들 ㅠㅠ (2) 나빛 2025-05-10
2694285 언어 공부하는 과정 좀 추천해주세요! (1) 아빠몬 2025-05-09
2694258 카운터.. 질문입니다. (4) 하늘빛눈망울 2025-05-09
2694229 단순한 질문이요 (8) 여름 2025-05-09
2694202 용돈을 가지고 할 수 있는 일을 여러가지로 출력하는 방법 좀 알려주세요! (2) 미나 2025-05-09
2694145 화면깜빡임을 없애고 싶은데요... (1) 어서와 2025-05-08
2694069 unsigned 질문입니다. 힘차 2025-05-07
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com