Skip to content

Instantly share code, notes, and snippets.

@castaneai
Last active August 29, 2015 14:02
Show Gist options
  • Save castaneai/e31fd39e600a54414373 to your computer and use it in GitHub Desktop.
Save castaneai/e31fd39e600a54414373 to your computer and use it in GitHub Desktop.
add_schedule.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct schedule{
int year;
int month;
int day;
int hour;
int minute;
char title[1024];
char content[1024];
};
typedef struct schedule SCHEDULE;
char** split(char *originalString, int stringNum, char* separator){
char **stringArray;
int i;
char *tp;
stringArray = (char **) malloc(stringNum*sizeof(char *));
for (i = 0; i < stringNum; i++){
stringArray[i] = (char *) malloc(1024);
}
tp = strtok(originalString, separator);
stringArray[0] = tp;
// 1個目の区切りが成功したのでi = 1
i = 1;
while (tp != NULL){
tp = strtok(NULL, separator);
if (tp != NULL){
stringArray[i] = tp;
// 区切りが1つ成功したので iを1増やす
i++;
}
}
// 区切った数が引数で要求された区切り個数stringNumと一致していない場合は
// フォーマットが間違ってるのでエラーとしてNULLを返す
if (i != stringNum) {
return NULL;
}
return stringArray;
}
void printSchedule(SCHEDULE* s)
{
printf("予定日時:%d/%02d/%02d %02d:%02d\n", s->year, s->month, s->day, s->hour, s->minute);
printf("タイトル:%s\n", s->title);
printf("内容:%s\n", s->content);
}
int main(void)
{
//char line[1024] = "a 2012 10 31 12 6 待ち合わせ 駅の改札口前";
char line[1024] = "a 2012 10 31 12 6 待ち合わせ";
char** result;
char command;
SCHEDULE schedule;
printf("schedule>%s\n", line);
command = line[0];
printf("コマンドは%cです\n", command);
if (command == 'a') {
result = split(line, 8, " ");
// フォーマットが違ったらエラー
if (result == NULL) {
printf("入力フォーマットが正しくありません\n");
return -1;
}
schedule.year = atoi(result[1]);
schedule.month = atoi(result[2]);
schedule.day = atoi(result[3]);
schedule.hour = atoi(result[4]);
schedule.minute = atoi(result[5]);
strcpy(schedule.title, result[6]);
strcpy(schedule.content, result[7]);
}
printSchedule(&schedule);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment