C语言 常用经典代码 小学生算数、
数组合并问题、
选择删除X元素、
数组元素排序
/*小学生算数 第二种方法*/ #include <stdio.h>
#include <stdlib.h>
#include <time.h>
int question_get1();
int question_get2();
int type;
void main( void )
{
int answer,Lever;
srand( (unsigned)time( NULL ) ); out:
printf("1.一位数运算!2.两位数运算!\n"); scanf("%d",&Lever);
printf( "请选择要测试的题目种类:" ); printf( "\n1.加法\n2.减法\n3.乘法\n4.除法\n" );
scanf( "%d", &type );
while( 1 )//实现系统循环
{
int temp;
char flag;
while(Lever==1) { } while(Lever==2) { } answer = question_get2();//调用二位数运算的函数 printf( "请给出你的答案:\n" ); break;//要跳出此次while循环 answer = question_get1();//调用一位数运算函数 printf( "请给出你的答案:\n" ); break;//要跳出此次while循环
fflush( stdin );
scanf( "%d", &temp );//储存测试者输入的结果
while( temp!=answer )//判断结果的对错 错误时 实现循环 运算 {
printf( "\n笨蛋,you are wrong! again!\n" );
fflush( stdin );//继续运算
scanf( "%d", &temp );
}
printf( "\n天才!you are rigth! Very good!\n" );
printf( "press 1 to go on,press 2 to exit,press 0 to end!\n" );//选择菜单 fflush( stdin );
scanf( "%c", &flag );
while( flag!='1' && flag!='2'&&flag!='0' )//选择菜单判断
{
printf( "press 1 to go on,press 2 to exit,press 0 to end!\nelse no working!\n" ); fflush( stdin );
scanf( "%c", &flag );
}
if ( flag=='2' )
goto out;//回到out实现整个系统的循环 else if(flag=='0') break;//结束本程序
}
}
int question_get1()//子函数 一位数运算
{
int a,b,c;
if( type==1 )
{
a=rand()%10;//在10以内产生随机数
b=rand()%10;//在10以内产生随机数
printf( "%d + %d = ?", a, b );
return(a+b);
}
else if( type==2 )
{
b=rand()%10;//在10以内产生随机数
c=rand()%10;//在10以内产生随机数
printf( "%d - %d = ?", b+c, b );
return(c);
}
else if( type==3 )
{
a=rand()%10;//在10以内产生随机数 b=rand()%10;//在10以内产生随机数 printf( "%d * %d = ?", a, b );
return(a*b);
}
else
{
b=rand()%10;
c=rand()%9+1;
printf( "%d / %d = ?", b*c, b );
return(c);
}
}
int question_get2()//子函数 二位数运算 {
int a,b,c;
if( type==1 )
{
a=rand()%100;//在100以内产生随机数 b=rand()%100;//在100以内产生随机数 printf( "%d + %d = ?", a, b );
return(a+b);
}
else if( type==2 )
{
b=rand()%100;//在100以内产生随机数
c=rand()%100;//在100以内产生随机数
printf( "%d - %d = ?", b+c, b );
return(c);
}
else if( type==3 )
{
a=rand()%100;//在100以内产生随机数
b=rand()%100;//在100以内产生随机数
printf( "%d * %d = ?", a, b );
return(a*b);
}
else
{
b=rand()%10;
c=rand()%10+1;
printf( "%d / %d = ? ", b*c , c );
return(b);
}
}
//数组合并问题
/*本程序可实现将随机产生的两个数m<10,n<100;\n将n插入随机产生的一个10元素一维数组第m个位置上*/
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
int main()
{
int A[11]; int i,m,n,c; srand( (unsigned)time( NULL ) ); printf("*本程序可实现将随机产生的两个数m<10,n<100;\n将n插入随机产生的一个10元素一维数组第m个位置上\n");
for(i=0;i<10;i++) { if(i==m-1) { m=rand()%10; n=rand()%100; printf("随机产生的m=%d\n",m); printf("随机产生的n=%d\n",n); while(1) { printf("\n*.按 1 运行本程序\n*.按其他任意键退出退出系统\n"); scanf("%d",&c); if(c==1) { } for(i=0;i<10;i++) { A[i]=rand()%100; printf("随机产生的数组为:\n"); for(i=0;i<10;i++) printf("%3d",A[i]); printf("\n");
}
} } for(i=11;i>=m;i--) { } A[i]=n; A[i]=A[i-1]; } printf("插入后的数组为:\n"); for(i=0;i<=10;i++) } else break; printf("%3d",A[i]); printf("\n"); return 0;
//选择删除X元素
#include <stdio.h> void main() {
int i,n,k,m; int a[10] = {4,21,13,34,13,6,7,10,9,10}; printf("删除前的数组为:\n"); for(i=0;i<10;i++) { printf("%d ",a[i]);
} } printf("\n"); printf("请输入要删除的数字:\n"); scanf("%d",&n); printf("删除后的数组为:\n"); m=10; for(i=0;i<10;i++) { if(a[i] == n) { m--; for(k=i;k<10;k++) { a[k] = a[k+1]; } } } for(i=0;i<m;i++) printf("%d ",a[i]); printf("\n");
//输入一个字符组,将其逆置输出 #include "stdio.h"
void main()
{
int i=0,k,temp;
} char str[100]; /* */ printf("输入一个字符串:\n"); while((str[i]=getchar())!='\n') i++; str[i]='\0'; k=i-1; for(i=0;i<k;i++) { } for(i=0;str[i]!='\0';i++) putchar(str[i]); temp=str[i]; str[i]=str[k]; str[k]=temp; k--; printf("\n");
//矩阵任意两行或两列交换 #include "stdio.h"
int main()
{
printf("交换前的矩阵为:\n"); for(i=0;i<4;i++) { int i,j,m,n,t; int A[4][5]={{1,2,3,4,5},{6,7,8,9,10,},{11,12,13,14,15},{16,17,18,19,20}}; int B[4][5]={0};
} for(j=0;j<5;j++) printf("%5d",A[i][j]); printf("\n"); while(1) { printf("请选择行交换还是列交换\n*.按 1 列交换\n*.按 2 行交换\n*.其他任意数退出系统!\n");
if(t==1) { } else if(t==2) { printf("请输入要交换的两行:"); scanf("%d",&m); scanf("%d",&n); for(i=0;i<4;i++) { } B[i][m-1]=A[i][m-1]; A[i][m-1]=A[i][n-1]; A[i][n-1]=B[i][m-1]; printf("请输入要交换的两列:\n"); scanf("%d",&m); scanf("%d",&n); scanf("%d",&t);
} } } for(j=0;j<5;j++) { } B[m-1][j]=A[m-1][j]; A[m-1][j]=A[n-1][j]; A[n-1][j]=B[m-1][j]; else break; printf("交换后的数组为:\n"); for(i=0;i<4;i++) { } for(j=0;j<5;j++) printf("%5d",A[i][j]); printf("\n"); return 0;
/*礼炮问题*/ #include"stdio.h" int main() {
} int t,count; for(t=0,count=0;t<=140;t++) if(t%3==0&&t<=60||t%5==0&&t<=100||t%7==0) count++; printf("所听到的炮声是:%d\n",count); return 0;
//数据结构
//增加或删除学生信息
#include "stdio.h"
struct student
{
};
void stud_out(struct student *a,int n); int stud_ins(struct student *a,int n);
void stud_del(struct student *a,int *n,int j);
int main()
{
struct student stu[50]={ {1001,"李天骄",1990,12,3,87.5,93.6,79.5,"北京联合大学信息学院97号"}, {1002,"张璍萸",1991,2,13,97.5,83.6,89.5,"北京联合大学信息学院97号"}, {1003,"陈自逸",1989,10,31,77.5,88.6,79.5,"北京联合大学信息学院97号"}}; int num=3; int n,k; stud_out(stu,num); while(1) { printf("请选择操作内容:1.增加学生;2.删除学生;0.退出系统!\n"); int ID; char name[20]; struct { int year,month,day; }birthday; float scores[3]; char address[100];
} scanf("%d",&k); if(k==1) { } else if(k==2) { } else { } } return 0; break; printf("请选择删除第几个学生\n"); scanf("%d",&n); num=stud_ins(stu,num); stud_out(stu,num); stud_del(stu,&num,n); printf("删除第%d个学生后\n",n); stud_out(stu,num);
int stud_ins(struct student *a,int n) {
int i; struct student tmp; printf("输入学号\n"); scanf("%d",&tmp.ID); printf("输入姓名\n"); fflush(stdin);
scanf("%s",tmp.name); printf("输入生日(年月日)\n"); scanf("%d",&tmp.birthday.year); scanf("%d",&tmp.birthday.month); scanf("%d",&tmp.birthday.day); printf("请输入C实验、C语言理论、C笔试成绩\n"); for(i=0;i<3;i++)
scanf("%f",&tmp.scores[i]);
printf("请输入地址\n");
fflush(stdin);
scanf("%s",&tmp.address);
a[n]=tmp;
return n+1;
}
void stud_del(struct student *a,int *n,int j)
{
int i;
for(i=j-1;i<*n-1;i++)
a[i]=a[i+1];
(*n)--;
}
void stud_out(struct student *a,int n)
{
int i,k;
printf(" 学号 姓名 生日 C实验 for(i=0;i<n;i++)
{
printf("%5d",a[i].ID);
printf("%7s",a[i].name);
printf("%5d",a[i].birthday.year); C理论 C笔试 地 址\n");
printf("%3d",a[i].birthday.month); printf("%3d",a[i].birthday.day); for(k=0;k<3;k++)
printf("%7.1f",a[i].scores[k]); printf("\t%s",a[i].address); printf("\n");
}
}
//数组元素排序
#include"iostream"
using namespace std;
void sort(int *p,int n);
int main()
{
int a[10]={21,3,45,6,76,9,10,23,22,34}; int *p,i;
p=a; sort(p,10); for(i=0,p=a;i<10;i++)
cout<<*(p++)<<" "; cout<<endl;
return 0;
}
void sort(int *p,int n)
{
int i,j,k,t;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++) //将数组a的首地址赋予指针p //调用排序函数对数组排序 //输出排列后数组
if(*(p+j)<*(p+k)) k=j; if(k!=i) { } } } t=*(p+i); *(p+i)=*(p+k); *(p+k)=t;