C: input and output

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
# 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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
# 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;
        }
    }
 
}

  

调用:

1
displayInput();

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-11-12 CSharp: Adapter Pattern in donet 6
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示