heap storage에 메모리 할당함으로 char의 제한 없애기
하양이
질문 제목 :
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++;
}
}
}----------------------------------------------------------------------------------------
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |
2691483 | 파일 오픈시 에러 질문드립니다. (2) | 호습다 | 2025-04-14 |
2691450 | [visual c++ 툴]기초 질문 (3) | 해긴 | 2025-04-13 |
2691393 | UNIX 시스템을 사용하려면 어떤 프로그램이 좋을까요? (5) | 든솔 | 2025-04-13 |
2691334 | ㅠㅠ에러 (1) | Loseless | 2025-04-12 |
2691304 | 포인터배열에 대해 질문요 | 달님 | 2025-04-12 |
2691279 | float-정수변환-2진수변환 | 핫블루 | 2025-04-12 |