원장: 명령어 검증
Coddy의 Luau 개발자를 위한 C 코스 레슨. 48개 중 45번째.
챌린지
중급parser.c를 수정하여 ADD 또는 TAKE, 공백 하나, 1~15개의 소문자 ASCII 문자로 이루어진 이름, 공백 하나, 그리고 0~1000을 나타내는 한 개 이상의 숫자만 허용하세요. 부호, 뒤에 붙는 바이트, 추가 공백, 비어 있는 필드, 알 수 없는 작업은 거부하세요. 실패한 경우 출력 Command를 변경하지 마세요. 유효하지 않은 줄은 재고를 변경하지 않고 거부된 것으로 집계하세요. 이전 단계의 동작과 보고 형식을 유지하세요. 첫 번째 줄은 각 테스트에 표시된 단계 번호입니다. 이후 줄은 명령입니다. 네 단계 모두 같은 파일과 공개 헤더를 유지하세요. 잠긴 드라이버는 모든 단계에서 동일하며, 독립적인 원장을 직접 테스트합니다. 서로 다른 이름은 최대 16개이며, 각 이름은 소문자 ASCII 문자 1~15개로 이루어집니다. 명령 줄은 최대 64개이고 각각 128바이트보다 짧습니다. 누적 합계는 모두 int에 들어갑니다.
잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 공개된 잠금 상태의 main.c가 API를 직접 호출하므로, 선언된 공개 API와의 호환성을 유지하세요. main을 추가하지 마세요. 작업에서 범위를 명시적으로 확인하는 경우를 제외하면 모든 숫자 테스트 데이터는 오버플로가 발생하지 않을 만큼 작습니다. 지정된 출력만 출력하세요. 드라이버가 입력 구문 분석과 표시된 경우 출력을 처리합니다.
직접 해보기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){
Ledger probe,other;ledger_init(&probe);ledger_init(&other);Command sample={"ADD","probe",2};
if(!ledger_apply(&probe,&sample) || probe.count!=1 || probe.items[0].quantity!=2 || other.count!=0)return 3;
int stage;if(scanf("%d",&stage)!=1 || stage<1 || stage>4)return 1;
int ch;while((ch=getchar())!='\n' && ch!=EOF){}
Ledger ledger;ledger_init(&ledger);int accepted=0,rejected=0;char line[128];
while(fgets(line,sizeof line,stdin)){line[strcspn(line,"\n")]=0;Command command;
if(parse_command(line,&command) && ledger_apply(&ledger,&command))accepted++;else rejected++;
}
char output[1024];Ledger before=ledger;
if(!ledger_report(&ledger,stage,accepted,rejected,output,sizeof output))return 2;
if(ledger.count!=before.count)return 3;
for(size_t i=0;i<ledger.count;i++)if(strcmp(ledger.items[i].name,before.items[i].name)!=0 || ledger.items[i].quantity!=before.items[i].quantity)return 3;
puts(output);return 0;
}
Luau 개발자를 위한 C의 모든 레슨
직접 연습해 보세요: 온라인 C 컴파일러