关于处理scanf输入bug的一次更新

3 篇文章 0 订阅
2 篇文章 0 订阅

处理scanf的bug

写这些函数的具体原因大家可以看我之前的那一篇博客

这次更新是因为 上次的只写了处理 整形的浮点型那些的输入bug,并且上次存在敲击空格导致bug的问题 目前已经解决

啊 当时第二天写的一直忘了更新 今天突然想起来了

这次更新中加入了大量的和字符串相关的输入bug的处理

如果这些大家发现这些功能存在bug 欢迎大家私信或者评论区反馈

毕竟博主还在学习 接受大家的指点并更改

下面直接上代码

#pragma once
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <malloc.h>
#define FALSE 0
#define RIGHT 1

-----------------------------------------------------------------
int Input_Int(int min, int max, int size);//输入指定范围内的整数
int Input_Int_SingleDigits(int min, int max);//输入0-9
double Input_Double(double min, double max, int size);//输入指定范围的浮点数
char Input_YorN(); //输入Y y N n

//----------------小于等于长度即可-----------------------------

char* Input_String(int size,int mode);//输入指定大小的字符串
char* Input_Numeric_String(int size);//输入指定长度的纯数字的字符串 
char* Input_Alphabetic_String(int size);//输入指定长度的纯字母的字符串 
char* Input_Alpha_and_Num_String(int size);///输入指定长度的纯数字的字符串 
char* Input_Chinese_String(int size);//输入指定长度的中文的字符串
char* Input_AlphabeticAndChinese_String(int size);//输入指定长度的中文或英文字符串(这个要适当给大size,因为中文英文占的不一样)
//--------------------------------------------------------------

//--------------等于才可以--------------------
char* Input_Numeric_String_force(int size);//输入指定长度的纯数字的字符串 
char* Input_Alphabetic_String_force(int size);;//输入指定长度的纯字母的字符串  
char* Input_Alpha_and_Num_String_force(int size);//输入指定长度的纯数字的字符串 
char* Input_Chinese_String_force(int size); //输入指定长度的中文的字符串

//--------------------------------------------
#include "tool.h"
//void main()
//{
//	Input_AlphabeticAndChinese_String(10);
//}
/*
* function: you can input a int number in a specified range
* param: int min is the min number you can input ; int max is the max number you can input ;
*        int size is the digits of the number
* return: the number
* author:陈梓烨
* date:2021.6.8
*/


int Input_Int(int min, int max, int size)
{
	int flag = RIGHT;
	int flag_negative = FALSE;
	char arr[50];
	while (1)
	{
		scanf_s("%[^\n]", arr, 50);
		getchar();
		int length = strlen(arr);//获取字符串长度
		if (arr[0] == '-')
		{
			int i = 1,j = 0;
			flag_negative = RIGHT;
			for (j ,i; i <= length ; i++, j++)
			{
				arr[j] = arr[i];
			}
			arr[i ] = '\0';
			length = length - 1;
		}

		if (length > size)//如果你的输入大于规定应该的长度
		{
			flag = FALSE;
		}
		if (length < size)//这行很重要 解决的问题是:假设范围是0-150的话   12,1这种数就不合规了 因为遍历的问题
		{
			size = length;
		}
		for (int i = 0; i < size; i++)
		{
		
			if (          !(arr[i] >= 48 && arr[i] <= 57)    )//如果有不在0-9之间的数字
			{
				flag = FALSE;
			}
		
		}
		//字符串没有错误后进行转换
		int num = atoi(arr);
		if (flag_negative)
		{
			num = -num;
		}
		if (!(num <= max && num >= min ))
		{
			flag = FALSE;
		}

		if (flag)
		{
			return num;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
			flag_negative = FALSE;
		}
	}
}

/*
* function: you can input a positive int number in a SingleDigits like 2 - 8 , 1-5
* param: int min is the min number you can input ; int max is the max number you can input ;
* return: the number
* author:陈梓烨
* date:2021.6.8
*/
int Input_Int_SingleDigits(int min,int max)
{
	int number;
	return number = Input_Int(min, max, 1);//位数设置为1位
}

/*
* function: you can input a positive double number in a specified range
* param: double min is the min number you can input ; double max is the max number you can input ;
*		 int size is the digits of the number for examble 12.1 ,its size is 2        123.1 its size is 3
* return: the number
* author:陈梓烨
* date:2021.6.8
*/
double Input_Double(double min,double max,int size )
{
	
	int flag = RIGHT;
	char arr[50];
	int Index_dot = 0; //小数点的索引
	int count_dot = 0; //小数点个数 超过1个就不行
	int length;    //位数
	int flag_negative = FALSE;

	while (1)
	{

		scanf_s("%[^\n]", arr, 50);
		getchar();
		int arrLength = strlen(arr);//总长度
		
		if (arr[0] == '-')
		{
			int i = 1, j = 0;
			flag_negative = RIGHT;
			for (j, i; i <= arrLength; i++, j++)
			{
				arr[j] = arr[i];
			}
			arr[i] = '\0';
			arrLength = arrLength - 1;
		}
		for (int i = 0; i < arrLength; i++)
		{
			if (        !(arr[i] >= 48 && arr[i] <= 57 || arr[i] == 46 )        )//不在0-9 和不是 . 小数点
			{
				flag = FALSE;
			}
			if (arr[i] == 46)
			{
				Index_dot = i;//记录小数点位置
				count_dot++;
			}
			if (count_dot > 1) //小数点超过1个
			{
				flag = FALSE;
			}
		}
	
		length = Index_dot - 1;
		//printf("%d\n", length)
		
		if (length > size)
		{
			flag = FALSE;
		}
		double num = atof(arr);
		//printf("%f\n", num);
		if (flag_negative)
		{
			num = -num;
		}
		if (!(num <= max && num >= min))
		{
			flag = FALSE;
		}
		if (flag)
		{
			return num;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
			flag_negative = FALSE;
			count_dot = 0;
		}
	}
}

/*
* function: you can only input Y y N n
* param:
* return: you enter in Y, y ,N,n
* author:陈梓烨
* date:2021.6.8
*/
char Input_YorN()
{
	int flag = RIGHT;
	char arr[20];
	char input;
	while (1)
	{
		scanf_s("%[^\n]", arr, 20);
		int size = strlen(arr);
		if (size > 1)
		{
			flag = FALSE;
		}
		if (  !(arr[0] == 'Y' || arr[0] == 'N' || arr[0] == 'y' || arr[0] == 'n')  )
		{
			flag = FALSE;
		}
		if (flag)
		{
			input = toupper(arr[0]);
			
			return input;
		}
		else
		{
			printf("输入格式错误请重新输入:");
			flag = RIGHT;
		}
		
	}
}

/*
* function:you can input any character in the string in a specified range
* param:length of the string
* return:the string you input 
* author:陈梓烨
* date:2021.6.9
* 
* An important thing is that you need char *h to receive the return;   like char *h = InputString(5);  
**/
char* Input_String(int size,int mode)
{
	//size = size;
	int flag = RIGHT;
	char arr[100];
	char* dst = (char*)calloc(size, sizeof(char*));
	int length = 0;
	while (1)
	{
		scanf_s("%[^\n]", arr, 100);
		getchar();
		length = strlen(arr);
		if (mode == 1) {
			if (length > size)
			{
				flag = FALSE;
			}
		}
		else
		{
			if (length != size)
			{
				flag = FALSE;
			}
		}
		if (flag)
		{
			strcpy_s(dst, size+1, arr);//注意这个 +1 特别重要否则出错 
			return dst;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
		}
	}
}

/*
* function:you can input numeric string in a specified range
* param:the length of your string
* return:the string you input
* author:陈梓烨
* date:2021.6.9
**/
char* Input_Numeric_String(int size)
{
	
	int flag = RIGHT;
	while (1)
	{
		char* h = Input_String(size,1);//保证长度没有出错
		int length = strlen(h);
		if (length < size)
		{
			size = length;
		}
		for (int i = 0; i < size; i++)
		{
			if (!(h[i] >= 48 && h[i] <= 57))//如果有不在0-9之间的数字
			{
				flag = FALSE;
			}
		}
		if (flag)
		{
			return h;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
		}
	}
}

/*
* function:you can input alphabetic string 
* param:the length of your string
* return:the string you input
* author:陈梓烨
* date:2021.6.9
**/
char* Input_Alphabetic_String(int size)
{

	int flag = RIGHT;
	while (1)
	{
		char* h = Input_String(size,1);//保证长度没有出错
		int length = strlen(h);
		if (length < size)
		{
			size = length;
		}
		for (int i = 0; i < size; i++)
		{
			char temp = toupper(h[i]);
			if(  !(temp>='A' && temp<='Z')  )
			{
				flag = FALSE;
			}
			
		}
		if (flag)
		{
			return h;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
		}
	}
}

/*
* function:you can input alphabetic and numeric in a string
* param:the length of your string
* return:the string you input
* author:陈梓烨
* date:2021.6.9
**/
char* Input_Alpha_and_Num_String(int size)
{

	int flag = RIGHT;
	while (1)
	{
		char* h = Input_String(size,1);//保证长度没有出错
		int length = strlen(h);
		if (length < size)
		{
			size = length;
		}
		for (int i = 0; i < size; i++)
		{
			char temp = toupper(h[i]);
			if ((temp >= 'A' && temp <= 'Z')|| (temp >= '0' && temp <= '9'))
			{
			} 
		
			else
			{
				flag = FALSE;
			}

		}
		if (flag)
		{
			return h;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
		}
	}
}

/*
* function:you can input Chinese in a string
* param:the length of your string
* return:the string you input
* author:陈梓烨
* date:2021.6.9
**/
char* Input_Chinese_String(int size)
{
	size = 2 * size;
	int flag = RIGHT;
	while (1)
	{
		char* h = Input_String(size,1);//保证长度没有出错
		for (int i = 0; i < size; i++)
		{
			if (!(h[i] <= 0))
			{
				flag = FALSE;
			}
		}
		if (flag)
		{
			return h;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
		}
	}
}
/*
* function:you can input Chinese or Alphabetic in a string
* param:the length of your string
* return:the string you input
* author:陈梓烨
* date:2021.7.18
**/
char* Input_AlphabeticAndChinese_String(int size)
{

	int flag = RIGHT;
	while (1)
	{
		char* h = Input_String(size, 1);//保证长度没有出错
		int length = strlen(h);
		if (length < size)
		{
			size = length;
		}
		for (int i = 0; i < size; i++)
		{
			char temp = toupper(h[i]);
			if (!(temp >= 'A' && temp <= 'Z' || temp < 0))
			{
				flag = FALSE;
			}

		}
		if (flag)
		{
			return h;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
			getchar();
		}
	}
}

/*
* function:you can input alphabetic string need and equailty to the size
* param:the length of your string
* return:the string you input
* author:陈梓烨
* date:2021.6.9
**/
char* Input_Numeric_String_force(int size)
{

	int flag = RIGHT;
	while (1)
	{
		char* h = Input_String(size, 0);//保证长度没有出错
		for (int i = 0; i < size; i++)
		{
			if (!(h[i] >= 48 && h[i] <= 57))//如果有不在0-9之间的数字
			{
				flag = FALSE;
			}
		}
		if (flag)
		{
			return h;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
		}
	}
}

/*
* function:you can input alphabetic string and  need equailty to the size
* param:the length of your string
* return:the string you input
* author:陈梓烨
* date:2021.6.9
**/
char* Input_Alphabetic_String_force(int size)
{

	int flag = RIGHT;
	while (1)
	{
		char* h = Input_String(size, 0);//保证长度没有出错
		int length = strlen(h);
		if (length < size)
		{
			size = length;
		}
		for (int i = 0; i < size; i++)
		{
			char temp = toupper(h[i]);
			if (!(temp >= 'A' && temp <= 'Z'))
			{
				flag = FALSE;
			}

		}
		if (flag)
		{
			return h;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
		}
	}
}

/*
* function:you can input alphabetic and numeric in a string and need equailty to the size
* param:the length of your string
* return:the string you input
* author:陈梓烨
* date:2021.6.9
**/
char* Input_Alpha_and_Num_String_force(int size)
{

	int flag = RIGHT;
	while (1)
	{
		char* h = Input_String(size, 0);//保证长度没有出错
		int length = strlen(h);
		if (length < size)
		{
			size = length;
		}
		for (int i = 0; i < size; i++)
		{
			char temp = toupper(h[i]);
			if ((temp >= 'A' && temp <= 'Z') || (temp >= '0' && temp <= '9'))
			{
			}

			else
			{
				flag = FALSE;
			}

		}
		if (flag)
		{
			return h;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
		}
	}
}

/*
* function:you can input Chinese in a string and need equailty to the size
* param:the length of your string
* return:the string you input
* author:陈梓烨
* date:2021.6.9
**/
char* Input_Chinese_String_force(int size)
{
	size = 2 * size;
	int flag = RIGHT;
	while (1)
	{
		char* h = Input_String(size, 0);//保证长度没有出错
		for (int i = 0; i < size; i++)
		{
			if (!(h[i] <= 0))
			{
				flag = FALSE;
			}
		}
		if (flag)
		{
			return h;
		}
		else
		{
			printf("输入格式有误,检查并重新输入:");
			flag = RIGHT;
			
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这里煤球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值