원장: 명령 검증
Coddy의 GDScript 개발자를 위한 C 코스 레슨. 48개 중 45번째.
챌린지
중급parser.c를 수정하여 ADD 또는 TAKE, 공백 하나, 1~15개의 소문자 ASCII 문자로 된 이름, 공백 하나, 그리고 0~1000을 나타내는 한 자리 이상의 숫자만 허용하세요. 부호, 뒤에 붙는 바이트, 추가 공백, 빈 필드 및 알 수 없는 연산은 거부하세요. 실패한 경우 출력 Command를 변경하지 마세요. 유효하지 않은 줄은 재고를 변경하지 않고 거부된 것으로 계산합니다. 이전 단계의 동작과 보고서 형식을 유지하세요. 첫 번째 줄은 각 테스트에 표시된 단계 번호입니다. 이후 줄은 명령입니다. 네 단계 모두에서 동일한 파일과 공개 헤더를 유지하세요. 잠금된 드라이버는 모든 단계에서 동일하며 독립된 원장을 직접 테스트합니다. 서로 다른 이름은 최대 16개이고, 각 이름은 소문자 ASCII 문자 1~15개로 구성되며, 길이가 128바이트 미만인 명령 줄은 최대 64개입니다. 누적 합계는 모두 int 범위에 들어갑니다.
잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 공개된 잠금 상태의 main.c에서 직접 호출하는 공개 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;
}
GDScript 개발자를 위한 C의 모든 레슨
직접 연습해 보세요: 온라인 C 컴파일러