/*计算分段函数*/
#include<stdio.h> int main(void) { double x,y; printf("Enter x:"); scanf("%lf",&x); if(x<0){ y=-1; } else if(x=0){ y=0; } else{ y=1; } printf("y=%.0f\n",y); return 0; }
/*输入一个正整数 n,再输入 n 个学生的成绩,计算平均成绩,并统计所有及格学生的人数*/ #include<stdio.h> int main(void) { int count,i,n; /*count记录及格人数*/
double grade,total; /*分别存放成绩,成绩之和*/
printf("Enter n:"); scanf("%d",&n); total=0; count=0; for(i=1;i<=n;i++){ printf("Enter grade#%d:",i); scanf("%lf",&grade); total=total+grade; /*累加成绩*/
if(grade>=60){ count++; } } printf("grade average=%.2f\n",total/n); printf("Number of not failures=%d\n",count); return 0; }
/*统计英文字母、空格或回车、数字字符和其他字符*/
#include<stdio.h> int main(void) { int blank,letter,digit,other; char ch; /*定义一个字符变量ch*/
int i;
blank=letter=digit=other=0; /*置存放统计结果的三个变量的初值为零*/
printf("Enter 15 characters:"); for(i=1;i<=15;i++){ /*循环执行10次*/
ch=getchar(); /*从键盘输入一个字符,赋值给变量ch*/
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) letter++; else if(ch>='0'&&ch<='9') digit++; else if(ch>=' '&&ch<=' ') blank++; else other++; } printf("blank=%d,letter=%d,digit=%d,other=%d\n",blank,letter,digit,other); return 0; }
/*判断该年是否为闰年*/
#include<stdio.h> int main(void) { int year,i,n; printf("enter n:"); scanf("%d",&n); for(i=1;i<=n;i++){ printf("enter year:"); scanf("%d",&year); if(year=-1) break; if((year%4==0&&year%100!=0)||(year%400==0)){ printf("YES,YES!!!!\N"); } else{ printf("NO,,NO,,NO\n"); } } return 0; }
#include<stdio.h> int main(void) { char ch; /*定义一个字符变量ch*/
printf("chengjishi:"); ch=getchar(); /*从键盘输入一个字符,赋值给变量ch*/
switch(ch){ case 'A': printf("90-100\n"); break; /*跳出switch语句*/
case 'B': printf("80-89\n"); break; case 'C': printf("70-79\n"); break; case 'D': printf("60-69\n"); break; case 'E': printf("0-59\n"); break; default: printf("cuowu\n"); break; } return 0; }
/*查询自动售货机中商品的价格*/
#include<stdio.h> int main(void) { int choice,i; double price; for(i=1;i<=5;i++){ printf("[1]select apples\n"); /*查询苹果的价格*/
printf("[2]select pears\n"); printf("[3]select oranges\n"); printf("[4]select grapes\n"); printf("[0]select Exit\n"); /*退出查询*/
printf("Enter choice:"); scanf("%d",&choice); if(choice==0) break; /*此处用break跳出for循环*/
switch(choice){ case 1:price=3.00;break; case 2:price=2.50;break; case 3:price=4.10;break; case 4:price=10.20;break; default:price=0.00;break; } printf("price=%0.2f\n",price); } return 0; }