수다닷컴

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

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

허리달

2023.04.01


질문 제목 :
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

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

번호 제 목 글쓴이 날짜
2699555 c언어 다항식을 입력을 했는데 왜 출력이 안될까요? 피스케스 2025-06-27
2699528 C언어 포인터연산 질문입니다. (3) 안녕나야 2025-06-26
2699476 끌어올림;;달력 짜봤는데요 이 소스 줄일 수 있나요? - 스샷첨부 (2) 클라우드 2025-06-26
2699444 [좀 급함] system("explorer [주소] ") 문에 변수를 사용할 수 있나요? 알 2025-06-26
2699415 파일//read//와 배열 아란 2025-06-25
2699386 구조체 안에 일부분만 char 배열에 복사하려면 어떻게 해야하나요? (1) 미즈 2025-06-25
2699361 연결리스트 정렬하는 부분에 대해서 질문 드립니다 아이처럼 2025-06-25
2699304 [기초]아직 안주무시는분 계신가요..?포인터배열? 좀 도와주세요. 놀리기 2025-06-24
2699272 printf() 함수이용해서 프로그램 만들기 질문요! (5) 다가 2025-06-24
2699221 PUSH와 POP코드를 더 간단하게 어떻게 해야할까요? 파라미 2025-06-24
2699192 설치오류가 자꾸 나요 한번봐주세여~ (1) 소녀틳향기 2025-06-23
2699161 for loop안에 있는 if문 (9) Orange 2025-06-23
2699105 링크더리스트 이전 링크값 출력함수. 꼬꼬마 2025-06-23
2699078 정수를 한자리씩 배열에 담는 법은 어떻게 하나요.. (4) 귀염포텐 2025-06-22
2699024 C언어 공부하려는데 도와주세요!!! (2) 달님 2025-06-22
2698994 날짜 계산하는 C 코드 짜고 있는데 꽉 막혀서 질문드립니다.. (6) 별 2025-06-22
2698967 파일삭제 윈도우 폴더까지 접근하게하는 함수가 뭔가요 (2) 샤인 2025-06-21
2698938 c언어 메모리질문 (3) 나래 2025-06-21
2698909 서비스 요청 고객 관리 프로그램 짜는것좀 도와주세요ㅜㅜ (4) 궁수자리 2025-06-21
2698882 프로그래밍좀 짜주세요 (3) 황예 2025-06-21
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

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