/** # encoding: utf-8 # 版权所有 2023 涂聚文有限公司 # 许可信息查看: # 描述:输入输入出格式 # Author : geovindu,Geovin Du 涂聚文. # IDE : CLion 2023.1.1 c17 windows 10 # Datetime : 2023/11/12 6:43 # User : geovindu # Product : CLion # Project : ctest # File : DuInput.c # explain : 学习 */ // // Created by geovindu on 2023/11/12. // #ifndef CTEST_DUINPUT_H #define CTEST_DUINPUT_H #include <stdio.h> #include <ctype.h> #include <stdbool.h> #include <string.h> /** * @brief Name buffer size */ #define LENGTH 50 /** * @brief Function prototypes */ void eatspaces(void); /** * @brief * @param n * @return */ bool getinteger(int *n); /** * @brief * @param name * @param length * @return */ char *getname(char *name, size_t length); /** * @brief * @return */ bool isnewline(void); /** * @brief 显示 * * * */ void displayInput(); #endif //CTEST_DUINPUT_H
/** # encoding: utf-8 # 版权所有 2023 涂聚文有限公司 # 许可信息查看: # 描述: 输入输入出格式 # Author : geovindu,Geovin Du 涂聚文. # IDE : CLion 2023.1.1 c17 windows 10 # Datetime : 2023/11/12 6:43 # User : geovindu # Product : CLion # Project : ctest # File : DuInput.c # explain : 学习 */ // // Created by geovindu on 2023/11/12. // #include "../includes/DuInput.h" /** * @brief Function to ignore spaces from standard input 空格字符串 * */ void eatspaces(void) { char ch = 0; while(isspace(ch = (char)getchar())); ungetc(ch, stdin); } /** * @brief Function to read an integer from standard input * @param n * @return */ bool getinteger(int *n) { eatspaces(); int value = 0; int sign = 1; char ch = 0; // Check first character if((ch = (char)getchar()) == '-') // should be minus sign = -1; else if(isdigit(ch)) // ...or a digit value = ch - '0'; else if(ch != '+') // ...or plus { ungetc(ch, stdin); return false; // Not an integer } // Find more digits while(isdigit(ch = (char)getchar())) value = 10*value + (ch - '0'); ungetc(ch,stdin); // Push back non-digit character *n = value*sign; // Set the sign return true; } /** * @brief Function to read an alphabetic name from input * @param name * @param length * @return */ char* getname(char *name, size_t length) { eatspaces(); // Remove leading spaces size_t count = 0; char ch = 0; while(isalpha(ch = (char)getchar())) // As long as there are letters... { name[count++] = ch; // ...store them in name. if(count == length - 1) // Check for name full break; } name[count] = '\0'; // Append string terminator if(count < length - 1) // If we didn't end for name full... ungetc(ch, stdin); // ...return non-letter to stream return name; } /** * @brief Function to check for newline * * * */ bool isnewline(void) { char ch = 0; if((ch = (char)getchar()) == '\n') return true; ungetc(ch, stdin); // Not newline so put it back return false; } /** * @brief * * * */ void displayInput() { int number; char name[LENGTH] = {'\0'}; printf("Enter a sequence of integers and alphabetic names in a single line:\n"); while(!isnewline()) { if(getinteger(&number)) printf("Integer value:%8d\n", number); else if(strnlen_s(getname(name, LENGTH), LENGTH) > 0) printf("Name: %s\n", name); else { printf("Invalid input.\n"); return 1; } } }
调用:
displayInput();
输出: