#include<stdio.h> int main(void) { int x,y; printf("Enter x:"); scanf("%d",&x); if(x>0){ y=1; } else if(x==0){ y=0; } else if(x<0) { y=-1; } printf("y=%d",y); return 0; }
#include<stdio.h> int main(void) { int count,i,n; double grade,sum; printf("Enter n:"); scanf("%d",&n); sum=0; count=0; for(i=1;i<=n;i++){ printf("Enter grade#%d:",i); scanf("%Lf",&grade); sum=sum+grade; if(grade>=60) count++; } printf("平均成绩=%.3f ",sum/n); printf("及格人数=%d ",count); return 0; }
#include<stdio.h> int main(void) { int letter,blank,digit,other; char ch; int i; letter=blank=digit=other=0; printf("Enter 15 characters:"); for(i=1;i<=15;i++){ ch=getchar(); if((ch>='a'&& ch<='z')||(ch>='A'&& ch<='Z')) letter++; else if(ch>='0'&&ch<='9') digit ++; else if((ch>=' '&&ch<=' ')||(ch>=' '&&ch<=' ')) blank++; else other++; } printf("letter=%d,digit=%d,blank=%d,other=%d ",letter,digit,blank,other); return 0; }
#include<stdio.h> int main(void) { int year,n,i; 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("这一年是闰年 "); } else printf("这一年不是闰年 "); } return 0; }
#include<stdio.h> int main() { char ch; printf("输入成绩等级:"); ch=getchar(); switch(ch){ case 'A': printf("A对应90-100 "); break; case 'B': printf("B对应80-89 "); break; case 'C': printf("C对应70-79 "); break; case 'D': printf("D对应60-69 "); break; case 'E': printf("E对应0-59 "); break; default: printf("结束 "); break; } return 0; }
#include<stdio.h> int main(void) { int choice,i; double price; for(i=1;i<=5;i++){ printf("[1]apples "); printf("[2]pears "); printf("[3]oranges "); printf("[4]grapes "); printf("[0]exit "); printf("Enter choice:"); scanf("%d",&choice); if(choice==0) break; switch(choice){ case 1:price=3.0;break; case 2:price=2.5;break; case 3:price=4.10;break; case 4:price=10.20;break; default:price=0.0;break; } printf("price=%.2f ",price); } printf("Thanks "); return 0; }