c语言中,如果输入内容格式不符合规定,怎样判断并指出
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言中,如果输入内容格式不符合规定,怎样判断并指出相关的知识,希望对你有一定的参考价值。
怎样实现以下功能(括号中为输入的内容)
Enter a number:(one)
one is not a number.
#include <string.h>
int check_num( char *str )
while( *str )
if ( *str > '9' || *str<'0' )
return 1;
str++;
return 0;
int main()
char str[100];
printf("Enter a number:" );
gets(str);
if ( check_num( str ) )
printf("%s is not a number\n" , str );
return -1;
printf("you input a number:%s\n" , str );
return 0;
本回答被提问者采纳 参考技术B #include <stdio.h>
int main()
int j = scanf("%d", &a);
if (j == 1)
printf("输入的是数字\n");
else if (j == 0)
printf("输入的不是数字\n");
return 0 ;
追问
不管用啊……
追答#include
int main()
int a;
int j = scanf("%d", &a);
if (j == 1)
printf("输入的是数字\n");
else if (j == 0)
printf("输入的不是数字\n");
return 0 ;
详细点吧,我是新手……
追答查一下ASCII表,超出数字范围的自然不是数字了
追问只找到了各种符号和字母的……
挑战C语言
判断年月
先确定好该年是否闰年再写程序
#include<stdio.h>
void main(){
int year;
printf("Please enter year:");
scanf("%d",&year);
if(year%4==0&&year>=0){
if(year%100==0){
if(year%400==0)
printf("%d is leap year\n",year);
else
printf("%d is not leap year\n",year);
}
else
printf("%d is leap year\n",year);
}
else
printf("%d is not leap year\n",year);
}
void main()
{
int year,month,leap;//整数数据类型
printf("Please enter year,month:");//输出
scanf("%d,%d",&year,&month);//输入“year,month”前后数字中间加逗号
//leap year,leap=1代表闰年,leap=0代表平年(假设,1真,0假)
if(year%4==0&&year>=0)//判断数字是否被4整除以及大于0
{
if(year%100==0)//如果数字能被4整除并且大于0,再判断数字是否被100整除
{
if(year%400==0)//如果数字能被100整除,再判断是否被400整除
leap=1;//如果数字能被400整除(能被4整除,能被100整除),那数字1将赋值到变量leap
else
leap=0;//如果数字不能被400整除,能被4整除,能被100整除,那数字0将赋值到变量leap
}
else
leap=1;//如果数字不能被100整除并且能被4整除,那数字1将赋值到变量leap
}
else
leap=0;//如果数字不能被4整除,那数字0将赋值到变量leap
//是否闰年
if(leap==1)//判断
printf("%d is leap year\n",year);//如果leap等于1,那么输出是闰年
else//否则
printf("%d is not leap year\n",year); //如果leap不等于1,那么输出不是闰年
//spring:3,4,5;
//summer:6,7,8;
//autumn:9,10,11;
//winter:12,1,2;
switch(month)//判断选择,该月属于哪一季节
{
case 1: //month=1时,空的,下一句执行
case 2: printf("The season is winter\n");break; //month=2时输出“The season is spring”,break不可省略
//同理
case 3:
case 4:
case 5: printf("The season is spring\n");break;
case 6:
case 7:
case 8: printf("The season is summer\n");break;
case 9:
case 10:
case 11: printf("The season is autumn\n");break;
case 12:printf("The season is winter\n");break;
default:printf("The month is not exist\n");//default,不成立的时候输出“The month is not exist”
}
//31days:1,3,5,7,8,10,12;
//30days:4,6,9,11;
//leap year 29days:2;闰年中2月有29天
//not leap year 28days:2;平年中2月有28天
switch(month)//再判断选择,判断该月的天数
{
case 1:printf("The number of days of this month 31\n");break;//一月有31天
case 2:{ //2月比较特殊
if(leap==1)//判断
printf("The number of days of this month 29\n");//该年是闰年,输出2月有29天
else
printf("The number of days of this month 28\n");//该年是平年,输出是2月有28天
};break;
case 3:printf("The number of days of this month 31\n");break;
case 4:printf("The number of days of this month 30\n");break;
case 5:printf("The number of days of this month 31\n");break;
case 6:printf("The number of days of this month 30\n");break;
case 7:printf("The number of days of this month 31\n");break;
case 8:printf("The number of days of this month 31\n");break;
case 9:printf("The number of days of this month 30\n");break;
case 10:printf("The number of days of this month 31\n");break;
case 11:printf("The number of days of this month 30\n");break;
case 12:printf("The number of days of this month 31\n");break;
default:printf("The month is not exist\n");//不符合以上条件的时候就输出该季节不存在
}
}
以上是关于c语言中,如果输入内容格式不符合规定,怎样判断并指出的主要内容,如果未能解决你的问题,请参考以下文章