从0开始<十三>:getint()函数(类似于scanf函数)以及库函数strncpy、strncat及strncmp函数的实现
来源:互联网 发布:linux内核调试方法 编辑:程序博客网 时间:2024/05/01 02:57
程序一:编写getint(int *pn)函数
#include <stdio.h>#include <ctype.h>#define BUFSIZE 100char buf[BUFSIZE]; /*用于ungetch函数的缓冲区*/int bufp = 0; /*buf中下一个空闲位置*/int getch(void); /*取一个字符*/void ungetch(int); /*把字符压回到输入中*/int getint(int *pn);int main(void){int i,arr[3];for (i=0; i<3 && getint(&arr[i])!=EOF; ++i);for (i=0; i<3; ++i)printf("%d\n",arr[i]);return 0;}int getch(void){return (bufp>0)?buf[--bufp]:getchar();}void ungetch(int c) /*把字符压回到输入中*/{if (bufp >= BUFSIZE)printf("ungetch: too many characters\n");elsebuf[bufp++] = c;}/*将输入中的下一个整型数值赋值给*pn*/int getint(int *pn){int c,sign;while ( isspace(c = getch())); /*跳过空白符*/if (!isdigit(c) && c!=EOF && c!='+' && c!= '-') {ungetch(c);return 0;}sign = (c == '-')?-1:1;if (c == '+' || c== '-')c = getch();for (*pn=0; isdigit(c); c=getch())*pn = 10 * *pn + (c - '0');*pn *= sign;if (c != EOF)ungetch(c);return c;}程序二:实现库函数strncpy、strncat 、strncmp,它们最多对参数字符串中的前n个字符进行操作。例如,函数strncpy(s,t,n)将t中的最多n个字符复制到s中
#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_BUF 16char *mystrncpy(char *s, const char *ct, size_t n) {char *p;p = s;for (; n > 0 && *ct != '\0'; --n)*p++ = *ct++;for (; n > 0; --n)*p++ = '\0';return s;}char *mystrncat(char *s, const char *ct, size_t n) {char *p;p = s;while (*p != '\0')++p;for (; n > 0 && *ct != '\0'; --n)*p++ = *ct++;*p = '\0';return s;}int mystrncmp(const char *cs, const char *ct, size_t n) {while (n > 0 && *cs == *ct && *cs != '\0') {++cs;++ct;--n;}if (n == 0 || *cs == *ct)return 0;if (*(unsigned char *) cs < *(unsigned char *) ct)return -1;return 1;}void test_ncpy(const char *str) {char std_buf[MAX_BUF];char liw_buf[MAX_BUF];memset(std_buf, 0x42, sizeof(std_buf));strncpy(std_buf, str, sizeof(std_buf));memset(liw_buf, 0x42, sizeof(liw_buf));mystrncpy(liw_buf, str, sizeof(liw_buf));if (memcmp(std_buf, liw_buf, sizeof(std_buf)) != 0) {fprintf(stderr, "liw_strncpy failed for <%s>\n", str);exit(EXIT_FAILURE);}}void test_ncat(const char *first, const char *second) {char std_buf[MAX_BUF];char liw_buf[MAX_BUF];memset(std_buf, 0x69, sizeof(std_buf));strcpy(std_buf, first);strncat(std_buf, second, sizeof(std_buf) - strlen(std_buf) - 1);memset(liw_buf, 0x69, sizeof(liw_buf));strcpy(liw_buf, first);mystrncat(liw_buf, second, sizeof(liw_buf) - strlen(liw_buf) - 1);if (memcmp(std_buf, liw_buf, sizeof(std_buf)) != 0) {fprintf(stderr, "liw_strncat failed, <%s> and <%s>\n",first, second);exit(EXIT_FAILURE);}}void test_ncmp(const char *first, const char *second) {size_t len;int std_ret, liw_ret;if (strlen(first) < strlen(second))len = strlen(second);elselen = strlen(first);std_ret = strncmp(first, second, len);liw_ret = mystrncmp(first, second, len);if ((std_ret < 0 && liw_ret >= 0) || (std_ret > 0 && liw_ret <= 0) || (std_ret == 0 && liw_ret != 0)) {fprintf(stderr, "liw_strncmp failed, <%s> and <%s>\n",first, second);exit(EXIT_FAILURE);}}int main(void) {test_ncpy("");test_ncpy("a");test_ncpy("ab");test_ncpy("abcdefghijklmnopqrstuvwxyz"); /* longer than MAX_BUF */test_ncat("", "a");test_ncat("a", "bc");test_ncat("ab", "cde");test_ncat("ab", "cdefghijklmnopqrstuvwxyz"); /* longer than MAX_BUF */test_ncmp("", "");test_ncmp("", "a");test_ncmp("a", "a");test_ncmp("a", "ab");test_ncmp("abc", "ab");printf("All tests pass.\n");return 0;}
0 0
- 从0开始<十三>:getint()函数(类似于scanf函数)以及库函数strncpy、strncat及strncmp函数的实现
- strncmp, strncat, strncpy函数的定义与实现
- strncmp、strncpy、strncat、strlen库函数的实现
- 模拟实现C语言strncpy.strncat.strncmp函数
- 【C语言】模拟实现strncpy、strncat、strncmp函数
- 【C语言】模拟实现strncpy strncat strncmp strchr strrchr函数
- 模拟实现 strncpy strncat strncmp strchr strrchr 函数
- C语言~模拟实现strncpy、strncat、strncmp函数
- 模拟输出受限制的字符串函数-strncat,strncmp,strncpy
- strlen(),strcpy(),strncpy(),strcat(),strncat(),strcmp(),strncmp()等函数的实现
- 关于字符串函数的一些代码实现(strncpy strncat strncmp strchr strrchr)
- c语言中部分限制字符函数的实现strncmp,strncat,strncpy,strstr,strrstr
- 模拟实现长度受限制的字符串函数(strncpy、strncat、strncmp)
- 用指针实现库函数strncpy strncmp strncat
- 模拟实现C库函数strncpy、strncat、strncmp
- strcpy、strcat函数源码,strncpy,strncat,strncmp,strtok
- 模拟实现strcmp,strncmp,strstr,strcat,strncat,strcpy,strncpy,strlen ,memcpy,memmove等函数
- 字符串函数模拟实现之strcpy、strncpy、strcmp、strncmp、strcat、strncat
- hadoop datanode部分启动不了
- 动态规划算法学习
- 对QT namespace UI的理解
- leetcode 110 Balanced Binary Tree
- freescale:i.MX6ul平台—ext4文件系统挂载错误总结
- 从0开始<十三>:getint()函数(类似于scanf函数)以及库函数strncpy、strncat及strncmp函数的实现
- QT学习资料总结
- jquery操作checkbox,实现全选。
- Gson 源码分析 总结
- IOS网络笔记--(三方库)上拉加载下拉刷新
- SNN 未被启动异常
- python模块之codecs
- 偶数个3【递推公式】
- 栈中最小元素