实验三-简单的分支与循环结构
#include<stdio.h> int main(void) { int i,n; double x,y; printf("Enter n:"); scanf("%d",&n); for(i=1,i<=1,i++){ printf("Enter x:"); scanf("%if",&x); if(x<=0){ printf("Error!"); } else if(x<=50){ y=0.53*x; } else(x>50){ y=0.58*x; } printf("y=f(%f)=%.1.0f\n,x,y); } return 0; }
#include<stdio.h> int main(void) { int x,y; printf("Enter x:\n"); scanf_s("%d",&x); if(x<0){ y = -1; } else if(x==0){ y=0; } else{ y=1; } printf("y=%d\n",y); return 0; }
/* 输入一个正整数 n,再输入 n 个学生的成绩,计算平均成绩,并统计所有及格学生的人数 */ #include<stdio.h> int main(void) { int count,i,num; double grade,total; num=0; total=0; count=0; printf("Enter grade:\n"); scanf_s("%Lf",&grade); while(grade>=0){ total=total+grade; num++; if(grade>=60) count++; scanf_s("%Lf",&grade); } if(num!=0){ printf("Grade average is %.2f\n",total/num); printf("The number of 及格的人 is %d\n",count); } else printf("Grade average is 0\n"); return 0; }
#include<stdio.h> int main(void) { int i,year,n; printf("请输入n"); scanf_s("%d",&n); printf("Input -1 to quit:"); scanf_s("%d", &year); if( year==-1 ) return -1; for(i=1;i<=n;i++){ printf("请输入年份:"); scanf_s("%d",&year); if((year%4==0&&year%100!=0)||(year%400==0)){ printf("这一年是闰年!\n"); } else{ printf("这一年不是闰年!\n"); } } return 0; }
#include <stdio.h> int main(void) { char ch; printf("输入成绩:"); ch=getchar(); switch(ch){ case 'A': printf("90-100\n"); break; 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("Invalid input\n"); break; } return 0; }