一、找出最长的字符串
#include "stdio.h" #include "string.h" int main() { int i; char sx[5],smax[5]; printf("输入五个字符串:"); for(i=0;i<5;i++){ scanf("%s",sx); if(strlen(sx)>strlen(smax)){ strcpy(smax,sx); } } printf("max is %s\n",smax); return 0; }
二、分类统计字符个数
#include <stdio.h> #include <string.h> int main() { char sc[80]; int LETTER,letter,blank,digit,other,i; LETTER=letter=0; digit=blank=other=0; printf("输入一行文字:"); gets(sc); i=0; while(sc[i]!='\0'){ if('A'<=sc[i]&&'Z'>=sc[i]) LETTER++; else if(sc[i]>='a'&&sc[i]<='z') letter++; else if(sc[i]>='0'&&sc[i]<='9') digit++; else if(sc[i]==' ') blank++; else other++; i++; } printf("LETTER=%d,letter=%d,digit=%d,blank=%d,other=%d\n",LETTER,letter,digit,blank,other); return 0; }
三、学生信息管理系统
#include "stdio.h" #include "string.h" struct students_list{ int xuehao; char name[10]; float chengji1,chengji2,chengji3; float ave; }; int Count=0; void new_student(struct students_list students[],int n); void print_student(struct students_list students[]); void search_student(struct students_list students[],char *name); void average_student(struct students_list students[],char *name); int main() { int choice,n; char name[10]; struct students_list students[50]; do{ printf("******学生信息管理系统******\n"); printf("\t1:输入学生基本信息\n"); printf("\t2:输出学生基本信息\n"); printf("\t3:计算学生平均成绩\n"); printf("\t4:查找学生基本信息\n"); printf("\t0:退出\n"); printf("请选择功能:"); scanf("%d",&choice); switch(choice){ case 1: printf("请输入学生个数:"); scanf("%d",&n); new_student(students,n); break; case 2: print_student(students); break; case 3: printf("请输入要查找的学生姓名:"); scanf("%s",name); average_student(students,name); break; case 4: printf("请输入要查找的学生姓名:"); scanf("%s",name); search_student(students,name); break; case 0: break; } }while(choice!=0); printf("谢谢使用学生信息管理系统!\n"); return 0; } void new_student(struct students_list students[],int n) { int i; if(Count>50){ printf("系统已满!\n"); return; } printf("请输入%d个学生的学号、姓名、成绩1、成绩2、成绩3\n",n); for(i=Count;i<Count+n;i++){ scanf("%d%s%f%f%f",&students[i].xuehao,students[i].name,&students[i].chengji1,&students[i].chengji2,&students[i].chengji3); } Count=Count+n; } void print_student(struct students_list students[]) { printf("学生信息系统中的学生为:\n"); int i; for(i=0;i<Count;i++){ printf("%d %s %.2f %.2f %.2f\n",students[i].xuehao,students[i].name,students[i].chengji1,students[i].chengji2,students[i].chengji3); } } void average_student(struct students_list students[],char *name) { int i; if(Count==0){ printf("该系统无学生!\n"); return; } printf("学生的平均成绩为:"); for(i=0;i<Count;i++){ students[i].ave=(students[i].chengji1+students[i].chengji2+students[i].chengji3)/3.0; printf("第%d个学生的平均成绩为%.2f\n",i+1,students[i].ave); } } void search_student(struct students_list students[],char *name) { int i,flag=0; if(Count==0){ printf("查无此人!\n"); return; } for(i=0;i<Count;i++){ if(strcmp(name,students[i].name)==0){ flag=1; break; } } if(flag){ printf("姓名:%s\t",students[i].name); printf("学号:%d\t",students[i].xuehao); printf("成绩:%.2f\t",students[i].chengji1); printf("成绩:%.2f\t",students[i].chengji2); printf("成绩:%.2f\t",students[i].chengji3); } else printf("查无此人!\n"); }
四、冒泡排序法
#include <stdio.h> void sort(int a[80],int n) { int i,j,t; for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(a[i]>a[j]){ t=a[i]; a[i]=a[j]; a[j]=t; } } } } int main() { int a[80],n,i; printf("输入一个整数n:"); scanf("%d",&n); printf("输入%d个数:",n); for(i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,n); for(i=0;i<n;i++){ printf("%d\t",a[i]); } return 0; }
五、报数游戏
#include<stdio.h> void CountOff(int n,int m,int out[]) { int i,a[80],j=0,k=0; for(i=0;i<n;i++){ a[i]=i+1; } while(k<n){ for(i=0;i<n;i++){ if(a[i]!=0){ j++; if(j==m){ out[k]=a[i]; j=0; k++; } } } } } int main() { int i,x,y,Out[80]; printf("输入总人数n:"); scanf("%d",&x); printf("输入退出次位m(m<n):"); scanf("%d",&y); CountOff(x,y,Out); printf("退出的顺序编号:"); for(i=0;i<x;i++){ printf("%3d",Out[i]); } printf("\n"); return 0; }