取输入中的下一个整数getint(), 处理+ -号

《C程序设计语言》

5-1 在上面的例子中,如果符号+或- 后面紧跟的不是数字,getint函数将把符号视为数字0的有效表达方式。修改该函数,将这种形式的+或- 符号重新写回到输入流中。

#include <stdio.h>
#include <ctype.h>
#define SIZE 100
#define BUFSIZE 100
int bufp = 0;//缓存区指针
int buf[BUFSIZE];//缓存区
int getint(int *);
int getch();
void ungetch(char );
/*取输入中的下一个整数*/
int main()
{
    int n, array[SIZE];
    for(n = 0; n < SIZE && getint(&array[n]) != EOF; n++)
        printf("%d  ", array[n]);
    return 0;
}
int getint(int *ch)
{
    char c;
    int sign;
    while(isspace(c = getch()))
        ;
    if(c != EOF && c != '+' && c != '-' && !isdigit(c)){
        ungetch(c);
        return 0;
    }
    sign = ((c == '-')? -1: 1);
    if(c == '-' || c =='+'){
        char sign2 = c;
        c = getch();
        if(!isdigit(c)){
            *ch = 0;
            if(c != EOF)
                ungetch(c);
            ungetch(sign2);
            return 0;

        }
    }

    for(*ch = 0; isdigit(c);c = getch()){
        *ch = *ch * 10 + (c - '0');
    }
    *ch *= sign;
    if(c != EOF)
        ungetch(c);
    return c;
}
int getch()
{
    if(bufp > 0)
        return buf[--bufp];
    else
        return getchar();
}
void ungetch(char c)
{
    if(bufp >= BUFSIZE)
        printf("the buf is full");
    else
        buf[bufp++] = c;
}
 

以下是使用BasicDataSource类创建数据源对象和查询数据的Java代码示例: ```java import java.sql.*; import org.apache.commons.dbcp2.BasicDataSource; public class DBCPUtils { private static final String URL = "jdbc:mysql://localhost:3306/test"; private static final String USERNAME = "root"; private static final String PASSWORD = "password"; private static BasicDataSource dataSource = null; static { dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl(URL); dataSource.setUsername(USERNAME); dataSource.setPassword(PASSWORD); dataSource.setInitialSize(5); dataSource.setMaxTotal(10); } public static QueryState queryUserById(int userId) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; QueryState state = null; try { conn = dataSource.getConnection(); String sql = "SELECT * FROM users WHERE id = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, userId); rs = stmt.executeQuery(); if (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String password = rs.getString("password"); String email = rs.getString("email"); Date birthday = rs.getDate("birthday"); state = new QueryState(id, name, password, email, birthday); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return state; } public static void main(String[] args) { QueryState state = queryUserById(1); if (state != null) { System.out.println(state.toString()); } else { System.out.println("No user found."); } } } class QueryState { private int id; private String name; private String password; private String email; private Date birthday; public QueryState(int id, String name, String password, String email, Date birthday) { this.id = id; this.name = name; this.password = password; this.email = email; this.birthday = birthday; } public int getId() { return id; } public String getName() { return name; } public String getPassword() { return password; } public String getEmail() { return email; } public Date getBirthday() { return birthday; } public String toString() { return "id=" + id + ", name=" + name + ", password=" + password + ", email=" + email + ", birthday=" + birthday.toString(); } } ``` 该示例,我们使用了Apache Commons DBCP数据库连接池库的BasicDataSource类创建了一个数据源对象,然后在queryUserById方法使用该数据源对象获数据库连接,执行SQL查询语句,并返回查询结果封装成的QueryState对象。最后在main方法调用该方法进行查询,并输出查询结果。 需要注意的是,我们在代码硬编码了数据库连接信息,实际应用应该将其配置在properties文件等更安全的方式进行管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值