- 博客(43)
- 收藏
- 关注
原创 函数变量的作用域
#include<stdio.h>void a (void);void b (void);void c (void); int x=1; //全局变量x;int main(){ int x=5; printf("x in main is: %d\n",x); //5 { int x=7; printf("x in inner scope of main is: %d\n",x); //7 } //7到这里被释放 printf("x in main is
2020-06-28 22:57:32
345
原创 职工数据文件中有10条记录 将职工号为偶数的职工数据输出
#include<stdio.h>#include<stdlib.h>struct k{ int number; char name[10]; }; main() { int i; struct k a[10]={{100,"hz"},{101,"xx"}, {102,"ff"},{103,"pp"},{104,"az"},{105,"lbw"},{106,"lk"},{107,"yh"},{108,"mh"},{109,"lxw"}}; struct
2020-06-27 19:01:29
294
原创 简单字符加密存入文件
#include<stdio.h>#include<ctype.h> main(){ char ch; FILE *fp; if((fp=fopen("hh.txt","w"))==NULL)//判断文件是否创建成功 printf("error !"); else printf("请输入字符: "); ch=fgetc(stdin); //从键盘缓冲区得到一个字符 while(ch!=EOF) { if(isalpha(ch)) //判断所写数据
2020-06-26 16:42:10
314
原创 从文件A里读出数据写进文件B
#include<stdio.h>#include<stdlib.h>main(){ FILE *fpa,*fpb; fpa=fopen("a.dat","r"); fpb=fopen("b.dat","w"); if(fpa&&fpb==NULL) { printf("error!"); exit(0); } while(!feof(fpa)) //当fpa指针没到文件尾进行循环 fputc(fgetc(fpa),fpb); //
2020-06-19 23:45:37
561
原创 头插法使链表数据反序输出
#include<stdio.h>#include<stdlib.h> //头 插 法struct yes{ int data; struct yes *next; };int main(){ struct yes *p,*head; //定义两个自构造类型指针 int num,size=sizeof(struct yes); //size等于此类型长度 head=(struct ye
2020-06-15 17:06:38
338
原创 在单链表中插入节点
#include <stdio.h>#include <stdlib.h>struct yo { int data; struct yo *next;};int main() { struct yo *head,*p,*q,*s,*r; p=(struct yo*)malloc(sizeof(struct yo)); p->data=1; head=p; q=(struct yo*)malloc(sizeof(struct yo)); q->
2020-06-13 14:02:34
1669
原创 在链表删去头,中间,尾节点
#include <stdio.h>#include <stdlib.h>struct yo { int data; struct yo *next;};int main() { struct yo *head,*p,*q,*s; p=(struct yo*)malloc(sizeof(struct yo)); p->data=10; head=p; q=(struct yo*)malloc(sizeof(struct yo)); q->dat
2020-06-12 23:40:43
189
原创 建立单链表,遍历并求其平均数
#include <stdio.h>#include <stdlib.h>//尾插方建立单链表 struct yo{ int data; struct yo *next;};int main(){ struct yo *head,*p,*q,*s; p=(struct yo*)malloc(sizeof(struct yo)); p->data=10; head=p; q=(struct yo*)malloc(sizeof(struct yo));
2020-06-12 17:10:00
1071
原创 函数指针数组实现两数之间加减乘除运算
#include<stdio.h>int f(int a,int b){ return a+b; }int g(int a,int b){ return a-b;}int x(int a,int b){ return a*b;}int y(int a,int b){ return a/b;}int main(){ int i,z,a,b; printf("input number: "); scanf("%d %d",&a,
2020-06-06 18:17:57
2248
原创 输入字符,在字符串内查找有无此字符。若有输出此字符第一次出现的位置
include<stdio.h>char* search(char *str,char c) { char *p=str; while(*p!='\0') { if(*p==c) { return p; } p++; } return NULL; } int main() { char ch,*pc=NULL,*string="This a test of search string"; int position
2020-06-04 23:28:20
2106
1
原创 数组元素之和
#include<stdio.h>int sum(int *p,int); //函数声明int main(){ int q[]={10,20,30,40}; int s; s=sum(q,sizeof(q)/sizeof(q[0])); //将数组q和数组长度送入函数 printf("sum=%d\n",s); }int sum(int *p,int length) { int total=0; for(int i=
2020-06-02 18:37:40
1022
原创 输入三个字符串,输出最大的
#include<stdio.h>#include<string.h>int main(void){int i;char a[3][99]; char max[10]; for(i=0;i<3;i++){ gets(a[i]); } strcpy(max,a[0]); if(strcmp(max,a[1])<0) strcpy(max,a[1]); if(strcmp(max,a[2])<0)
2020-06-02 16:45:20
7346
1
原创 输入一个单词 将其逆序输出
#include <stdio.h> //int main(){int i,k, temp; char str[99]; printf("input a string: "); //输入字符串i=0;while ((str[i] =getchar() )!='\t') i++; str[i] ='\0'; //在字符串后一位+\0 表明这是个字符串 k=i-1; //k对应数组下标 0-n for(i=0;i<k;i++) //{ tem
2020-05-30 17:46:24
883
原创 递归实现斐波拉契数列第n项的值
#include <stdio.h> //int back(int); //函数原型声明int main( ){ int i,s; scanf("%d",&i); s=back(i); printf("%d",s); }int back(int b){if (b==0) return 0;if (b==1)return 1;return b=back(b-1)+back(b-2); //每一项等于它前1项+前2项之和}
2020-05-28 00:10:25
503
原创 二维数组实现5行杨辉三角
#include <stdio.h>int main(void){int i,j,k; //定义i遍历行,j遍历列,k控制空格int a[5][5]; //定义10行10列的二维数组printf("\n"); for(i=0;i<5;i++) //数组从第0行开始到第9行 {a[i][0]=1; //每行第一个元素都是1 a[i][i]=1;} // 每列的第一个元素都为1for(i=2;i<5;i++
2020-05-26 21:35:01
2092
原创 求二维数组里第一个出现的负数
#include<stdio.h>int main(){ int a[5][7]={1,2,3,4,5,-1,216,-3,-19,198,156,-36,-8,95,71}; for (int i=0;i<5;i++) { for (int j=0;j<7;j++) if (a[i][j]<0) printf("%d\n",a[i][j]); break; }
2020-05-26 17:10:41
1840
原创 指向数组的指针跟指向变量的指针的关系
#include<stdio.h> int main(){int a1=123,a2=234,a3=345,i;int *p1,*p2,*p3;int as[3]={1,2,3},*ps;p1=&a1; //p1指向了a1的地址 1000p2=p1+1; //p2指向了a1的下个地址 1004p3=p2+1; //同p2 1008 printf("p1=%p\np2=%p\np3=%p\n",p1,p2,p3); printf("a1=
2020-05-22 19:32:28
177
原创 用指针求出数组里最大最小值
#include<stdio.h> void daxiao(int a[],int length,int *min,int *max); //函数声明int main(){ int min,max; //定义两个变量 max min int a[]={19,38,50,3,11,25,69,79,53}; //定义一个数组给出初值 daxiao(a,sizeof(a)/sizeof(a[0]),&min,&am
2020-05-21 16:39:28
5206
原创 指针完成变量交换
#include<stdio.h> void swap(int *pa,int *pb) //*pa,*pb代表变量a,b的地址,{ int t; t=*pa; //让a的地址赋给t *pa=*pb; //把b的地址给a *pb=t; //最后把t里面原a的地址给b 完成交换 }int main(){ int a,b
2020-05-20 23:12:06
324
原创 函数实现选择排序
#include<stdio.h> int search(int a[] ,int length) //函数定义{ int max=0; //令max=a[0] for(int i=1;i<length;i++){ // i=a[1],i<10,i++ if (a[i]>a[max]) // max=i; //就让a[i]=max; }return max;
2020-05-19 18:04:11
2294
原创 二分法函数查找
#include<stdio.h> int search(int a[] ,int key,int length) //函数定义{ int ret=-1; //令ret=-1 int left=0; int right=length-1;//length-1把right放到数组最右边 while(right>left){ //正常情况下right的下标大于left; int mid=(left+
2020-05-18 16:56:33
593
原创 往数组输入5个数,用冒泡法将其正序输出
#include <stdio.h>#define N 5 //预定义一个N ,值为5int main() { int i,j,t,p; int a[5]; printf("请输入5个数字: "); for(p=0;p<5;p++) scanf("%d",&a[p]); //循环往数组赋值; for(i=0;i<N-1;i++) { //i代表轮 for(j=0;j<
2020-05-16 22:44:12
1092
原创 二维数组的赋值与输出
#include <stdio.h>int main () {int a[2][5]; //定义二维数组int i,j; //i用来遍历数组行 j用来遍历列printf("请给数组元素赋值: "); //给数组赋初值for (i=0;i<2;i++) { //循环遍历行 for(j=0;j<5;j++) //遍历列 scanf("%d",&a[i][j]); //循环一次赋一个值给
2020-05-16 16:32:08
3236
原创 编写搜索函数,在M数组找到由name数组输出
#include<stdio.h> int M[]={1,2,3,4,5}; //定义一个int数组char *name[]={"one","two","three","four","five"}; //一个char数组int search(int key ,int a[] ,int length) //定义搜索函数{ int ret=-1; for(int i=0;i<length;i++) { //i来遍历整个
2020-05-16 15:57:08
214
原创 冒泡排序实现一组数 正序和倒序排列
#include <stdio.h>#define N 10 //预定义一个N ,值为10int main() { int a[]={14,38,46,23,77,6,95,62,42,10}; //定义一维数组,将值给出 int i,j,t; //i代表轮,j代表次,t中间变量将前后值大小交换 for(i=0;i<N-1;i++) { //外循环从第0轮开始,到N-1轮结束。 for(j=0;j<
2020-05-15 17:45:52
560
原创 输入一行字符,把里面的t全部去掉
#include<stdio.h> int main() {char s[80],k[80]; //定义两个字符数组int i,j; gets(s); //从键盘键入字符for(i=j=0;s[i]!='\0';i++) if(s[i]!='t') printf("%c",s[i]); puts(k); return 0;}...
2020-05-14 19:03:04
255
原创 输入一行字符,统计其中单词个数,单词之间空格来分隔
#include<stdio.h>int main() {char s[80],c1,c2=' '; //定义一个有80个元素的字符数组int i=0,num=0; gets(s); //键入字符while(s[i]!='\0') { c1=s[i]; if(i==0) c2=' '; else c2=s[i-1]; if (c2==' ') num++; i++;}print
2020-05-14 17:16:30
1599
2
原创 实现简易计算器
#include<stdio.h>int main(){ int a,b; char S; printf("请输入数字 "); scanf("%d %c %d",&a,&S,&b); switch (S) { case '+': printf ("%d",a+b); break; case '-': printf ("%d",a-b); break; case '*': printf (
2020-05-13 23:24:55
429
原创 输出实现斐波那契数列
#include <stdio.h> int main(){ int a=1,b=1,c,i; //四个变量 printf("%d\n %d\n",a,b); //前两项a=1;b=1; for(i=3;;i++) { c=a+b; //第三项的值等于第一项加第二项 if (c>=1000) break; //第三项的值大于1000跳出; a=b; //把b的
2020-05-13 17:56:08
498
原创 父亲30岁,孩子6岁。多少年后父亲年龄是孩子两倍
#include <stdio.h> int main() { int i=30,j=6; //父亲 i=30,孩子 j=6 while(i!=j*2) { //当孩子年龄x2不等于父亲年龄时 进入循环 i++; //父亲年龄++ j++; //孩子年龄++ } i=i-30; //循环结束后减去父亲当时30岁,出结果 printf("%d年后父亲的年龄是儿子2倍",i); return 0;
2020-05-11 23:20:18
4269
原创 函数声明使用avg 函数
#include <stdio.h> float avg (int a,int b,int c); //函数声明 int main() { int o,m,l; o=avg(10,9,11); l=avg(60,40,80); m=avg(50,75,25); printf("%f %f %f\n",o,m,l); } float avg (int a,int b,int c) // 函数定义 {
2020-05-11 18:43:29
2872
原创 输出一个*号的平行四边形
#include <stdio.h> int main() { int i,j; //i=行 ,j=*数量int k=1; //空格数量for (i=1;i<=5;i++) { // 从第一行到第五行 for (j=1;j<=i;j++) //每行*号数量随着i的行号递加 printf ("*"); printf("\n"); //完成一次循环换行}//下层循环 for (i=4
2020-05-11 17:44:10
4607
原创 输出一个字符型的菱形图案
#include <stdio.h> int main() { int i,j,m; //i=行 ,j=空格,m=每一行字符数量 char a='A'; //定义字符 for (i=1;i<=4;i++) { //从第一行循环到第五行停止 for (j=1;j<=3-i;j++) // 每次循环空格的数量为4-循环的变量i; print
2020-05-11 16:41:17
1452
1
原创 输出直角三角形*号
#include <stdio.h> int main() { int i,j; //定义 i,j两个变量 for (i=1;i<=7;i++) { //外侧for ,代表行 for (j=1;j<=i;j++) //内层for,代表输出*号 printf("*"); //每次满足j<=i就输出一个*号 printf("\n"); //内循环完
2020-05-09 18:48:29
4545
原创 输出打印九九乘法表
#include <stdio.h> int main() {int i,j; //定义i,j两个变量 for (i=1;i<=9;i++) { // for (j=1;j<=i;j++) // printf("%d*%d=%d\t",i,j,i*j); //输入i*j的结果 printf("\n"); // }} ...
2020-05-08 23:30:32
840
原创 输入行列 输出*号矩形
#include <stdio.h> int main() {int m,n,i,j; //m,n为输入的行 列数printf("输入行 列 "); scanf("%d %d",&m,&n);if (m>0&&n>0) { //如果输入的m,n都大于0就进入循环 for (i=1;i<=m;i++) { //外层for代表行循环 for (j=1;j<=n;j++
2020-05-08 23:23:33
3321
原创 水仙花数
#include <stdio.h>int main(){ int num; int a,b,c; for(num=100;num<=999;num++) //因为是三位数 所以从100开始999结束; { a=num/100; //取出百位 给a; b=num/10%10; //取出十位 给...
2020-05-07 23:39:37
120
原创 用函数来判断数大小
int min(int a,int b) //函数返回类型 ,函数名,(参数){ int zxz; //{ if (a<b) { zxz=a; // 函数体 } else { zxz=b; } return zxz; // }}int main(...
2020-05-07 22:26:12
1154
原创 输入一些0-9之间的数,并统计每个数你输入了几次
#include <stdio.h>int main(){ int x,i; int a[10]={0,0,0,0,0,0,0,0,0,0}; //初始化数组元素的值 scanf("%d",&x); while (x>-1) { //当x>-1进入此循环 if (x>=0 &am...
2020-05-05 23:30:06
1804
原创 输入一些数 求这些数的平均数。并把你输入的数中大于它们平均数的数输出
#include <stdio.h>int main(){int x; //输入数的变量float sum=0; //数加起来的和int count =0; //记录输入数的个数int number[10]; //定义一个有10个元素的一维数组scanf("%d",&x);while ...
2020-05-05 19:45:48
872
2
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人