1.编写程序,输入一个字符ch,输出字符的类型,即字母( alpha)、数字( numerIc) 或其他字符( other)。 #indludestdio, h maino char ch ch=getchar; if(dh>=6588ch<=90|ch>=978&dh<=122) printf( \nch=%c ch) printf( nThis is an alpha!) else if(dh>=4888h<=57) printf( \nch=%C, ch); printf( nThis is a numeric else printf( \ nThis is other! " 2.编写程序,输入一个正整数,判断该数是奇数还是偶数,并输出结果。 maIn printf( \nplease input the integer(0): i scanf( %d, &m) printf \nThe integer is %d,m) if(m%2==0) printf \nThis is a even!) else printf( \nThis is a odd 3.有一函数: x< (-1≤x≤1) 3x+5 <x<10 5x+3bg0(2x2-1)-13(x>10) 编写一个程序,用 scanf函数输入x的值,输出y值。注意表达式的书写方法 运行程序,输入ⅹ的值(分别用上述4种情况),检査输出的y值是否正确
1. 编写程序,输入一个字符 ch,输出字符的类型,即字母(alpha)、数字(numeric) 或其他字符(other)。 #include “stdio.h” main() { char ch; ch=getchar(); if(ch>=65&&ch<=90||ch>=97&&ch<=122) { printf(“\nch=%c”,ch); printf(“\nThis is an alpha!”); } else if(ch>=48&&ch<=57) { printf(“\nch=%c”,ch); printf(“\nThis is a numeric!”); } else printf(“\nThis is other!”); } 2. 编写程序,输入一个正整数,判断该数是奇数还是偶数,并输出结果。 main() { int m; printf(“\nPlease input the integer(>0):”); scanf(“%d”,&m); printf(“\nThe integer is %d”,m); if(m%2==0) printf(“\nThis is a even!”); else printf(“\nThis is a odd!”); } 3. 有一函数: + − − + − + − = 5 3log (2 1) 13 3 5 1 1 2 10 2 3 x x x x x y ( ) ( ) ( ) ( 10) 1 10 1 1 1 − − x x x x 编写一个程序,用 scanf 函数输入 x 的值,输出 y 值。注意表达式的书写方法。 运行程序,输入 x 的值(分别用上述 4 种情况),检查输出的 y 值是否正确
#indude"math h ain float X,y; printf \nPlease input the x: ) scanf( %f,&x) y=pow(x, 3)-1 else +1 if(X<=10) y=3*x+5; else y=5*x+3*og(2*x*X-1)-13 printf( \ ny=%fy) 4.编写程序,输入年号,判断并输出该年是否闰年。 mal int printf \nPlease input the year scanf(%d", &year) f(year%4==0&&year%100!=0| lyear%400==0) printf( \ n%d is a leap year "year) printf( n%d is not a leap year! year) 6.从键盘输入三个数,代表三条线段的长度。请编写程序,判断这三条线段组 成的三角形是什么类型(不等边、等腰、等边或不能构成三角形) ma int a, b, cr printf \nPlease input three numbers: ) scanf(%d%d%d"r &a, &b, &c) if(a+b>c&&b+c>a&&a+c>b) if(a==b&&b==c) printf \nThis is an equilateral triangle if(a==blb==clla==c)
#include “math.h” main() { float x,y; printf(“\nPlease input the x:”); scanf(“%f”,&x); if(x<-1) y=pow(x,3)-1; else if(x<=1) y=-x*x+1; else if(x<=10) y=3*x+5; else y=5*x+3*log(2*x*x-1)-13; printf(“\ny=%f”,y); } 4. 编写程序,输入年号,判断并输出该年是否闰年。 main() { int year; printf(“\nPlease input the year:”); scanf(“%d”,&year); if(year%4==0&&year%100!=0||year%400==0) printf(“\n%d is a leap year!”,year); else printf(“\n%d is not a leap year!”,year); } 6. 从键盘输入三个数,代表三条线段的长度。请编写程序,判断这三条线段组 成的三角形是什么类型(不等边、等腰、等边或不能构成三角形)。 main() { int a,b,c; printf(“\nPlease input three numbers:”); scanf(“%d%d%d”,&a,&b,&c); if(a+b>c&&b+c>a&&a+c>b) { if(a==b&&b==c) printf(“\nThis is an equilateral triangle!”); else if(a==b||b==c||a==c)
printf nThis is an isosceles triangle else printf nThis is an unequilateral triangle!") else printf \ nThis is not a triangle 7.简单选择界面的编程。从键盘输入整数,输出不同的字符串: 输入1,输出 Good morning ●输入2,输出 Good afternoon; 输入3,输出 Good evening 输入4,输出 Good night; 输入其他数字,输出 Byebye maino int n: printf( " \nPlease input a number scanf("%d", &n) switch(n) case 1: printf( " Good morning ) break; case 2: printf(" Good afternoon ) break; case 3: printf(" Good evening"); break; case 4: printf("Good night ) break default: printf(" Byebye) 8.已知从银行贷款月利率为:期限一年,为0.90%;期限2年,为1%;期限3年,为1.11% 三年以上为1.2%。从键盘输入贷款金额和期限,计算到期后应归还银行本金和利息合计为 多少钱。 #indude < math h> ma float base rate sum descr: printf( \ eNter the base and the year scanf(%f%d", &base, &year) switch(year) case 1: rate=0. 009: break. case 2: rate=0.01: break
printf(“\nThis is an isosceles triangle!”); else printf(“\nThis is an unequilateral triangle!”); } else printf(“\nThis is not a triangle!”); } 7. 简单选择界面的编程。从键盘输入整数,输出不同的字符串: ⚫ 输入 1,输出 Good morning; ⚫ 输入 2,输出 Good afternoon; ⚫ 输入 3,输出 Good evening; ⚫ 输入 4,输出 Good night; ⚫ 输入其他数字,输出 Bye bye。 main() { int n; printf("\nPlease input a number:"); scanf("%d",&n); switch(n) { case 1:printf("Good morning");break; case 2:printf("Good afternoon");break; case 3:printf("Good evening");break; case 4:printf("Good night");break; default:printf("Bye bye"); } } 8.已知从银行贷款月利率为:期限一年,为 0.90%;期限 2 年,为 1%;期限 3 年,为 1.11%; 三年以上为 1.2%。从键盘输入贷款金额和期限,计算到期后应归还银行本金和利息合计为 多少钱。 #include <math.h> main() { int year; float base,rate,sum; clrscr(); printf("\nEnter the base and the year:"); scanf("%f%d",&base,&year); switch(year) { case 1: rate=0.009;break; case 2: rate=0.01;break;
case 3: rate=0.0111: break. default rate=0.012 sum=base* pow(1+rate, 12 year) printf("sum is %.2f n",sum) 9.输入一个不多于5位的正整数,要求:①求出它是几位数;②分别打印出 每一位数字;③按逆序打印出各位数字。 要准备以下测试数据: 1位正整数 2位正整数 3位正整数 4位正整数 5位正整数 除此之外,程序还应当对不合法的输入作必要的处理,如负数或超过5位的正整 mal long m; int a, b, c, d, e: printf( \nplease input a integer (m>0&&m<=99999): scanf(%ld ,&m) fm>=100008&m<=99999 printf( \ nThis is a 5 integer!") a=my/10000 b=m/1000-a*10 C=m/100-a*100-b*10 d=m/10-a*1000-b*100-c*10; e=m%10000; printf( inThe integer is %d %d %d %d%d! a, b, c,d, e); printf( \nThe inverse is %d %d %d %d %d!e, d, c, b, a) else if(m>=10008&m<=9999) printf( \ nThis is a 4 integer!) a=m/1000 b=m/100-a*10 m/10-a*100-b*10; d=m%1000 printf \nThe integer is %d %d %d %d! a, b, c, d); printf \nThe inverse is %d %d %d %d! d, c b, a);
case 3: rate=0.0111;break; default: rate=0.012; } sum=base*pow(1+rate,12*year); printf("sum is %.2f\n",sum); } 9. 输入一个不多于 5 位的正整数,要求:① 求出它是几位数;② 分别打印出 每一位数字;③ 按逆序打印出各位数字。 要准备以下测试数据: 1 位正整数 2 位正整数 3 位正整数 4 位正整数 5 位正整数 除此之外,程序还应当对不合法的输入作必要的处理,如负数或超过 5 位的正整 数。 main() { long m; int a,b,c,d,e; printf(“\nPlease input a integer(m>0&&m<=99999):”); scanf(“%ld”,&m); if(m>=10000&&m<=99999) { printf(“\nThis is a 5 integer!”); a=m/10000; b=m/1000-a*10; c=m/100-a*100-b*10; d=m/10-a*1000-b*100-c*10; e=m%10000; printf(“\nThe integer is %d %d %d %d %d!”,a,b,c,d,e); printf(“\nThe inverse is %d %d %d %d %d!”,e,d,c,b,a); } else if(m>=1000&&m<=9999) { printf(“\nThis is a 4 integer!”); a=m/1000; b=m/100-a*10; c=m/10-a*100-b*10; d=m%1000; printf(“\nThe integer is %d %d %d %d!”,a,b,c,d); printf(“\nThe inverse is %d %d %d %d!”,d,c,b,a);
else ifm>=10088m<=999) printf \ nThis is a 3 integer! ) b=m/10-a*10 C=m%100 printf \nThe integer is %d %d %d! ,a, b, c: printf \nThe inverse is %d %d %d! "c, b, a) else fm>=1088m<=99) printf \nThis is a 2 integer!) a=m/10 b=m%100; printf \nThe integer is %d %d! a, b); printf( \nThe inverse is %d %d! ",b, a) else fm>=1&&m<=9) printf \ nThis is a 1 integer! ) printf( nThe integer is %ld ,m) else printf \nNot the effect integer! ")
} else if(m>=100&&m<=999) { printf(“\nThis is a 3 integer!”); a=m/100; b=m/10-a*10; c=m%100; printf(“\nThe integer is %d %d %d!”,a,b,c); printf(“\nThe inverse is %d %d %d!”,c,b,a); } else if(m>=10&&m<=99) { printf(“\nThis is a 2 integer!”); a=m/10; b=m%100; printf(“\nThe integer is %d %d!”,a,b); printf(“\nThe inverse is %d %d!”,b,a); } else if(m>=1&&m<=9) { printf(“\nThis is a 1 integer!”); printf(“\nThe integer is %ld”,m); } else printf(“\nNot the effect integer!”); }