Created
December 3, 2013 07:28
-
-
Save tdkn/7765315 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* clang-exam1203.cpp | |
*/ | |
#include "stdafx.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#include <limits.h> | |
#pragma warning(disable : 4996) | |
#define BUF 256 | |
#define MAX 21 | |
#define LENGTH 5 | |
/* Structure */ | |
typedef struct { | |
char b[MAX]; | |
int no; | |
} Student; | |
/* Function prototypes. */ | |
int input(Student *); | |
void display(Student *, int); | |
int isNumber(char *); | |
int isName(char *); | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
int i; | |
Student student[LENGTH]; | |
for (i = 0; i < LENGTH; i++) | |
{ | |
if (input(&student[i])) | |
{ | |
break; | |
} | |
} | |
display(student, i); | |
return 0; | |
} | |
/* Data input function. | |
* return 0 : success | |
* return 1 : failed | |
******************************/ | |
int input(Student *s) | |
{ | |
char buf[BUF]; | |
while (1) | |
{ | |
printf("Input Numeric\t: "); | |
gets(buf); | |
/* Detect "END" or "end" string */ | |
if (!strcmp(buf, "END") || !strcmp(buf, "end")) | |
{ | |
return 1; | |
} | |
if (isNumber(buf)) | |
{ | |
s->no = atoi(buf); | |
break; | |
} | |
} | |
while (1) | |
{ | |
printf("Input Name\t: "); | |
gets(buf); | |
/* Detect "END" or "end" string */ | |
if (!strcmp(buf, "END") || !strcmp(buf, "end")) | |
{ | |
return 1; | |
} | |
if (isName(buf)) | |
{ | |
strcpy(s->b, buf); | |
break; | |
} | |
} | |
return 0; | |
} | |
/* Data display function. | |
******************************/ | |
void display(Student *s, int count) | |
{ | |
int i; | |
for (i = 0; i < count; i++) | |
{ | |
printf("No:%03d\tNAME:%s\n", (s+i)->no, (s+i)->b); | |
} | |
} | |
/* Validate input number | |
* return 1 : is number | |
* return 0 : is NOT number | |
******************************/ | |
int isNumber(char *buf) | |
{ | |
int i = 0, no; | |
char tmp[MAX]; | |
if (!strcmp(buf, "")) | |
{ | |
printf("ERROR INPUT NUMERIC!\n"); | |
return 0; | |
} | |
while (*(buf+i) != '\0') | |
{ | |
if (!isdigit(*(buf+i))) | |
{ | |
printf("ERROR INPUT NUMERIC!\n"); | |
return 0; | |
} | |
i++; | |
} | |
no = atoi(buf); | |
itoa(no, tmp, 10); | |
if (strcmp(buf, tmp) || no < INT_MIN || INT_MAX < no) | |
{ | |
printf("ERROR LESS %d!\n", INT_MAX); | |
return 0; | |
} | |
return 1; | |
} | |
/* Validate input name | |
* return 1 : syntax OK | |
* return 0 : syntax NG | |
******************************/ | |
int isName(char *buf) | |
{ | |
int i = 0; | |
char c; | |
if (!strcmp(buf, "")) | |
{ | |
printf("ERROR INPUT ALPHABET!\n"); | |
return 0; | |
} | |
if (strlen(buf) > 20) | |
{ | |
printf("ERROR LESS %d!\n", MAX-1); | |
return 0; | |
} | |
while (*(buf+i) != '\0') | |
{ | |
c = *(buf+i); | |
if (isalpha(c) == 0 && c != ' ') | |
{ | |
printf("ERROR INPUT ALPHABET!\n"); | |
return 0; | |
} | |
i++; | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment