Tuesday, July 15, 2008

ConsoleIn of C

Files: JohnIn.h, JohnIn.c

1. JohnIn.h
---------------------------------------------

/* JohnIn.h
*
* abstract data type
*
* Operator: JohnIn_int , JohnIn_char
*/


/*
* JohnIn_int
*/
extern int int_console(void);

/*
* JohnIn_cdouble
*/
extern double dou_console(void);

/*
* JohnIn_char
*/
extern char char_console(void);



2. JohnIn.c
---------------------------------------------

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"JohnIn.h"

int int_console(void){
char temp[10];
int value1, test1;

scanf("%s", temp);
value1=atoi(temp);
sscanf(temp, "%d", &test1);
printf("%s", temp);

while(value1!=test1){
printf("\nInput is in wrong format.\n");
printf("Input number with at most 10 digits.\n");
printf("Minus sign is okay.\n");
printf("reenter an integer ---> ");
scanf("%s", temp);
value1=atoi(temp);
sscanf(temp, "%d", &test1);
}
return(value1);
}

double dou_console(void){
char temp[15];
double value2, test2;

scanf("%s", temp);
value2=atof(temp);
sscanf(temp, "%lf", &test2);

while(value2!=test2){
printf("\nInput is in wrong format.\n");
printf("Input number with at most 15 digits.\n");
printf("Minus sign is okay.\n");
printf("reenter an double ---> ");
scanf("%s", temp);
value2=atoi(temp);
sscanf(temp, "%lf", &test2);
}
return(value2);
}

char char_console(void){
char temp[2];

scanf("%s", temp);

while(strlen(temp)!=1){
printf("\nInput is in wrong format.\n");
printf("Input only one char.\n");
printf("reenter a char ---> ");
scanf("%s", temp);
}
return(temp[0]);
}

No comments: