由键盘任意输入1个4位数整数,分别输出其中的个位、十位、百位、千位。
/* expe 3-10 */#include <stdio.h>#include <math.h>void main(void){int mun,a,b,c,d;scanf("%d",&mun);a=mun / 1000; /*千位*/b=(mun % 1000) / 100; /*百位*/c=(mun % 100) / 10; /*十位*/d=(mun % 10); /*个位*/printf("千位数字为=%d\n",a);printf("百位数字为=%d\n",b);printf("十位数字为=%d\n",c);printf("个位数字为=%d\n",d);}由键盘任意输入3个数据,找出其中的最大数和最小数。
#include<stdio.h>void main(void){float a,b,c,k,g;/* k 小g 大*/printf ("请输入三个数");scanf("%f%f%f",&a,&b,&c);if (a < b){k = a;g = b;}else{k = b;g = a;}if (k < c)/*最小*/{ printf ("\n最小的数=%5.2f",k);}else{printf ("\n最小的数=%5.2f",c);}if (g > c)/*最大*/{printf ("\n最大的数=%5.2f",g);}else{printf ("\n最大的数=%5.2f",c);} }判断三角形的形状#include<stdio.h>#include<math.h>void main(void){printf("请输入三角形三边");float a,b,c,p,s,k1,k2,k3;scanf("%f%f%f",&a,&b,&c);while (a + b < c || b + c < a || a + c < b ){printf("不能构成三角形,请重新输入");scanf("%f%f%f",&a,&b,&c);}k1 = a * a;k2 = b * b;k3 = c * c;if (a == b && b == c && c ==a){printf("此三角形为等边三角形");}else if (a == b || b == c || c ==a){printf("此三角形为等腰三角形"); }else if ((k1 == b * b + c * c) || (k2 == a * a + c *c) || (k3 = a * a + b * b)){printf("此三角形为直角三角形");}else{printf("此三角形为一般三角形");} }九九乘法表整体#include <stdio.h>void main(void){int i,j;for (i = 1;i < 10; i ++){for(j = 1;j < 10; j ++){printf("%5d",i * j);}printf("\n"); } }右半边#include <stdio.h>void main(void){int i,j;for (i = 1;i < 10; i ++){for(j = 1;j <= i; j ++){ printf("%5d",i * j);}printf("\n"); } }左半边#include <stdio.h>void main(void){int i,j;for (i = 1;i < 10;i ++)/*最外层循环*/ {for (j = 1;j < i; j++)/*打空格*/{ printf(" "); }for (j = 1;j <= 10 - i;j ++)/*计算*/{printf("%5d",j * i); }printf("\n"); } }打印菱形#include<stdio.h>void main(void){int i,j,k;for (i = 1;i <= 5;i ++){for (j = 0;j < 5 - i;j ++){printf(" ! "); }for (k = 1;k <= 2 * i - 1;k ++){printf(" * "); }printf("\n");}for (i = 4;i >= 0;i --){for (j = 4 - i;j > 0;j --){printf(" ! "); }for (k = 2 * i ;k >= 0;k --){ printf(" * ");}printf("\n"); } }找回文#include<stdio.h>#include<string.h>void main(void){char str[80];int i,j,iStrlenstr,iFlag;printf("请输入字符串\n");gets(str);iStrlenstr = strlen(str);for (i = 0 ,j = iStrlenstr - 1;i <= j ;i ++, j --){if (str[i] == str[j]){iFlag = 1; }else{iFlag = 0;break; }}if (iFlag == 1){printf("输入的是回文!\n"); }else{printf("输入的不是回文!\n"); }}找名字指针法/*不用查找函数,用指针的方法*/#include<stdio.h>void main(void){char str[10][80],a[80],*p[10],*q = NULL;int i, j,iFlag = 0;// int istrlenstr;printf("请输入名字\n");for ( i = 0 ; i < 10; i ++){gets(str[i]);}printf("请输入想查找的名字\n");gets(a);for (i = 0; i < 10; i ++){p[i] = str[i]; }q = a;for (i = 0; i < 10; i ++ ){iFlag = 0;j = 0;while(*(p[i] + j) && *(q + j)){if (*(p[i]+ j) != *(q + j)){break; }j++;}if (*(p[i] + j)== '\0' && *(q + j) == '\0'){ iFlag = 1;break; }}if (iFlag == 1)printf("FOUND ! 该名字的位置为%d",i);elseprintf("NOT FOUND !"); }/*利用指针给10个数排序*/#include<stdio.h>int main(void){int a[10],*p = NULL ,iTem;int i,j,iPos = 0;printf("请输入10个数\n");for (i = 0; i < 10; i ++){scanf("%d",&a[i]); }p = a;for(i = 0; i < 9; i ++){iPos = i;for (j = i + 1 ; j < 10 ;j ++){if (a[j]< a[iPos])iPos = j;}iTem = *(a + i);*(a + i) = *(a + iPos);*(a + iPos) = iTem;}printf("排序之后的为");for (i = 0; i < 10; i ++){printf("%4d",*(a + i)); }return 0; }/*名字排序,从大到小*/#include<stdio.h>#include<string.h>void main(void){char a[10][10],cTemp[10];int i,j,iMax;printf("输入十个学生姓名:\n");for(i = 0; i < 10; i ++){gets(a[i]);}for(i = 0;i < 10 ;i ++){iMax = i;for(j = i + 1;j < 10;j ++){if(strcmp(a[iMax],a[j]) < 0){iMax = j;}}if(iMax != i){strcpy(cTemp,a[i]);strcpy(a[i],a[iMax]);strcpy(a[iMax],cTemp);}}printf("按照字典降序排序为:\n");for(i = 0;i < 10;i ++){puts(a[i]);} }/*查找并删除某字符*/#include<stdio.h>int main(){char str[80],*p = NULL,*q;char t;printf("请输入字符串:\n");gets(str);printf("请输入想要删除的字符:\n");t = getchar();p = str;q = str;for (p = str; *p != '\0'; p ++){if (*p != t){*q++ = *p; }}*q = '\0';puts(str);}函数作业(全部)/*交换器,引用全局变量*/方法一int a,b;void main(void){scanf("%d%d",&a,&b);fun();printf("a=%d,b=%d\n",a,b);}void fun(void){int c;c = a;a = b;b = c;}方法二#include<stdio.h>#define swap(x,y) {int tmp=x;x=y;y=tmp;}int main(){int x,y;printf("请输入两个数字\n");scanf("%d%d",&x,&y);swap(x,y);printf("交换之后的数字是\n");printf("%d\t%d\t",x,y);return 0;}/*用函数找最大公约数*/#include<stdio.h>int maxd(int,int);int main(){int a,b,d;printf("请输入两个整数\n");scanf("%d%d",&a,&b);d = maxd(a,b);printf("这两个数的最大公约数为:%d",d);return 0;}int maxd(int a,int b){int m,n,k;if(a<b){m = b;n = a;}else{m = a;n = b;}do{k = m%n;m = n;n = k;}while(n != 0);return(m);}/*用函数统计*/#include<stdio.h>int icount(char* );int main(){char str[80];int m = 0;printf("请输入字符串\n");gets(str);m = icount(str);printf("字符串中数字的个数有:%d",m);return 0;}int icount(char *p){int k = 0;while( *p != '/0'){if (*p >= '0' && *p <= '9')k ++;}return k;}/*排序函数*/#include<stdio.h>#include<string.h>void iSort (int[],int);int main(){int a[10];int i;printf("请输入10个整数\n");for (i = 0; i < 10 ; i ++){scanf("%d",&a[i]); }iSort(a,10);printf("从小到大排序为\n");for (i = 0; i < 10; i ++){ printf("%4d",a[i]); } }void iSort(int x[],int n){int i,j,t,iPos;for (i = 0; i < n - 1; i ++){iPos = i;for (j = i + 1; j < n; j ++){if (x[j] < x[iPos])iPos = j ;}if (iPos != i){t = x[i];x[i] = x[iPos];x[iPos] = t; } } }/*用函数找素数*/#include<stdio.h>int iPrime(int);int main(){int i,k = 0;for (i = 1; i < 201; i += 2){k = iPrime(i);if (k == 1)printf("%d\n",i); } }int iPrime(int x){int a;for (a = 2; a <= x/2; a ++){if (x % a == 0)return 0;}return 1; }/*八进制转十进制*/#include<stdio.h>int iChange(char*);int main(){char s[10];int i,k;printf("请输入一个八进制的数");gets(s);k = iChange(s);printf("对应的十进制的数位=%d",k); }int iChange(char *x){char *p;int n = 0;p = x;for(;*p != '\0'; p ++){n = n * 8 + *p - '0'; } }/*用函数将矩阵转置规定是方阵*/#include<stdio.h>void change(int[][4]);int main(){int a[4][4],b[4][4]={0};int i,j;printf("请输入一个四阶矩阵\n");for (i = 0; i < 4; i ++){for (j = 0; j < 4 ;j ++){ scanf("%d",&a[i][j]); }}change(a);printf("转置过后的矩阵式\n");for (i = 0; i < 4; i ++){for (j = 0; j < 4 ;j ++){printf("%3d",a[i][j]);}printf("\n"); } }void change(int x[][4]){int i,j,t;for (i = 0;i < 4; i ++){for (j = 0; j < i; j ++){t = x[i][j];x[i][j] = x[j][i];x[j][i] = t; } } }/*判断是否是对称矩阵以4*4 的矩阵为例*/#include<stdio.h>int dui(int[][4]);int main(){int a[4][4],i,j,k = 0;printf("请输入一个四阶矩阵\n");for (i = 0; i < 4; i ++){for (j = 0; j < 4 ;j ++){scanf("%d",&a[i][j]);}}k = dui(a);if (k == 1){printf("该矩阵为对称矩阵"); }else{printf("该矩阵不是对称阵");} }int dui(int x[][4]){int i,j,t = 0;for (i = 0;i < 4; i ++){for (j = 0; j < i; j ++){if (x[i][j] == x[j][i])t = 1;} }return t;}/*用函数求cos与sin*/#include<stdio.h>#include<math.h>float icos(float);float isin(float);int main() {float a,cosx,sinx;printf("请输入x\n");scanf("%f",&a);cosx = icos(a);sinx = isin(a);printf("cos(x)=%7.2f\n",cosx);printf("sin(x)=%7.2f\n",sinx);}float icos(float a){int n = 2;float t,k,fcos;k = 0;t = 1;do{t *= (-1) * a * a / (n *( n - 1));k += t;n += 2;}while (fabs(t) >= 1e-6);fcos = 1 + k;return fcos;}float isin(float a){float fsin;fsin=sqrt(1 - (double)icos(a) * (double)icos(a));return fsin;}/*int n = 3;float t,k,fsin;k = 0;t = 1;do{t *= (-1) * a * a / (n * (n - 1));k += t;n += 2;}while (fabs(t) >= 1e-6);fsin =1 + k;return fsin;*//*用函数分别统计26个字母的个数*/#include<stdio.h>void jishu(char str[]);int iCount1[26] = {0};void main(void){char str[80];int i;printf("请输入字符串");gets(str);jishu(str);printf("26个大写字母的个数分别是\n");for (i = 0; i < 26; i ++){printf("%d\n",iCount1[i]);}}void jishu(char x[]){int i;for (i = 0; x[i] != '\0'; i ++){if (x[i] >= 'A' && x[i] <= 'Z'){iCount1[x[i] - 'A'] ++; } } }位运算/*地位字节按位取反*/#include<stdio.h>void main(void){unsigned int x;printf("请输入数据:");scanf("%d",&x);x = x ^ 0X00ff;printf("%x\n",x);} /*循环移位通过位或运算来实现循环的功能*/#include<stdio.h>void main(void){int a,n;printf("请输入一个整数:\n");scanf("%d",&a);printf("请输入移动的位数:\n");printf("注意:正数代表右移,负数代表左移");scanf("%d",&n);if(n > 0){a = ((a >> n) | (a << ((8 * sizeof(int)) - n)));//右移}if(n < 0){a = ((a << (- n)) | (a >> ((8 * sizeof(int)) - (-n))));//左移}printf("%u\n",a);}/*利用位异或运算加密*/#include<stdio.h>int main(){int stc,code,dst;dst = 0;//加密后code = 1234;//密码printf("请输入明文:\n");scanf("%d",&stc);dst = stc ^ code;printf("\n加密之后的文件是:%d\n",dst);return 0;}/*偶数为清零*/#include<stdio.h>void main(void){int a;printf("请输入");scanf("%d",&a);a = a & 0x5555;printf("%x\n",a);}/*十六进制转二进制*/#include<stdio.h>void main(void){int a,b[32],c = 1,i;scanf("%x",&a);for(i = 0; i < 32; i ++){b[i] = (a & c) >> i;c <<= 1;}for(i = 31; i >= 0; i --){ printf("%d",b[i]); } }员工工资#include<stdio.h>#define N 1struct man{long ch;char nam[10];int year;float fareJB;float fareYJ;float fareYF;float fareBX;float fareSD;float fareSF;};void fare(struct man *p) {struct man *q;for(q=p;q<p+N;q++){q->fareYF=p->fareJB+p->fareYJ;q->fareSF=q->fareYF-q->fareBX-q->fareSD;}for(q=p;q<p+N;q++){printf("应发工资\t实发工资\n");printf("%5.2f\t%5.2f\n",q->fareYF,q->fareSF);} }void PRINTF(struct man *p){struct man *q=p;printf("职工工号\t姓名\t年龄\t基本工资\t业绩工资\t应发工资\t保险费\t\t水电费\t实发工资\t\n");for(q=p;q<p+N;q++){printf("%8d%10s\t%d\t%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\t%8.2f\t",q->ch,q->nam,q->year,q->fareJB,q->fareYJ,q->fareYF,q->fareBX,q->fareSD,q->fareSF);} }void main(void){struct man person[N],*per;printf("输入员工信息");for(per=person;per<person+N;per++){gets(per->nam);scanf("%d%d%f%f%f%f",&per->ch,&per->year,&per->fareJB,&per->fareYJ,&per->fareBX,&per->fareSD);}fare(person);PRINTF(person);}部门销售情况管理程序#include<stdio.h>#define N 1struct bumen{long num;char name[20];float get[4];float ave;}a[N];void chazhao(struct bumen *p){long NUM;int i;printf("输入要查找的部门的代码:");scanf("%ld",&NUM);printf("\n");for(i=0;i<N;i++){if((p+i)->num==NUM){break;}}printf("部门代码\t部门名称\t春季销售额\t 夏季销售额\t秋季销售额\t冬季销售额\t平均值\n");printf("%ld\t %6s\t\t%8.2lf\t%8.2lf\t%8.2 lf\t%8.2lf\t%8.2lf\t\n",p->num,p->name,p->get[0],p->get[1],p->get[2],p->get[3],p->ave);}void qiuave(struct bumen *p){int i,j;float sum=0;for(i=0;i<N;i++){for(j=0;j<4;j++){sum=sum+p[i].get[j];}p[i].ave=sum/4;} } void order(struct bumen *p){struct bumen *min,P;int i,j,k;for(i=0;i<N-1;i++){min=&p[i];for(j=i+1;j<N;j++){if(p[j].ave<min->ave)min=&p[j];}P=p[i];p[i]=*min;*min=P;}printf("部门代码\t部门名称\t春季销售额\t夏季销售额\t秋季销售额\t冬季销售额\t平均值\n");for(k=0;k<N;k++){printf("%ld\t %6s\t\t%8.2lf\t%8.2lf\t%8.2lf\t%8.2lf\t%8.2lf\t\n",p[k].num,p[k].name,p[k].get[0],p[k].get[1],p[k].get[2],p[k].get[3],p[k].ave);}}void main(void){int i,j,n,x;float t;printf("输入部门数据");for(i=0;i<N;i++){scanf("%ld",&a[i].num);scanf("%s",&a[i].name);for(j=0;j<4;j++){scanf("%f",&t);a[i].get[j]=t;}}printf("1.输入部门代码或部门名称,查找此部门的销售情况\n2.计算各部门的平均销售额。