
C语言
zmyyyyu
这个作者很懒,什么都没留下…
展开
-
C语言非常道 c0609.c 练习6.8
结构类型处理复杂的数据结构声明位于源文件的开头,相当于结构类型的全局声明上述:& emp 得到一个指向结构类型的指针,或者说指向 struct employee 类型的指针在程序相应的位置可以达成替换的作用练习6.8编写程序,将写入文件中的内容读到一个结构类型的变量中,并在调试器里观察读取的内容是否与前面写入的一致& 符号 变成指针的形式 sizeof 计算变量的字节数p );三种等价的表达形式练习6.9。原创 2023-07-25 16:03:21 · 401 阅读 · 0 评论 -
C语言非常道 6.7
使用结构类型,计算结构类型的大小,在上面这个程序中包含一个输入输出的头文件即可。原创 2023-07-25 09:41:35 · 212 阅读 · 0 评论 -
C语言非常道 6.6课后题
如果多次调用函数 write , 则每次写入的内容都位于上一次写入的内容之后,如果多次读取文件,则每次都从上一次读取的内容之后开始。每当读取或是写入文件时,将自动调整文件位置,使其位于下一次读写的开始处。写入文件内容后:发现写入的内容有图形符号,是因为所有文本编辑软件都试图将文件的内容解释为图形字符, 但前面写入的是整数, 只有当他偶尔与某个图形字符的编码是同一个数字时,才会显示为奇怪的字符。缓冲区解释:指连续的内存空间,典型的应用是在数据的发送方和接收方之间起到一个调节和缓冲的作用,比如说是蓄水池。原创 2023-07-24 19:30:35 · 184 阅读 · 0 评论 -
C语言非常道 6.6.3 逐位 & | ^ 三种运算符
一般来说程序中都会判断所对应的二进制的每个位,每个宏都只负责一个标志位,O_RDWR,仅对应于文件打开方式的第二个bit,意思是创建或者打开文件的目的是既读又写;操作系统会对每个打开的文件记录他们的属性和各种状态,,根据这些信息对文件进行操作,操作系统使用文件描述符定位每个文件的属性和状态信息的线索与把柄。函数, 是在<fcntl.h>,一般来说是给一个字符串,上面chua的程序给了字面串,将创建一个隐藏的数组,进而转化为指针指向数组中第一个元素,,用于哪些人能够打开这个文件,参数的类型是int。原创 2023-07-22 12:22:46 · 453 阅读 · 0 评论 -
C语言非常道 6.4习题解答
感觉在这个程序里面这样去定义就是多此一举,但是如果在一个这种类型很多的函数里面,就会非常方便。原创 2023-07-21 17:27:11 · 771 阅读 · 0 评论 -
编写一个函数比较两个字符串是否相同
【代码】编写一个函数比较两个字符串是否相同。原创 2023-07-20 15:53:58 · 482 阅读 · 0 评论 -
八位二进制数转化为十进制数输出
/* 八位二进制数转化为十进制数输出 分析:将二进制转化为十进制,只要将二进制数的每一位乘以该位的权然后相加*/#include "iostream"using namespace std;//计算x的n次方double power(double x,int n);int main(){ int value=0; cout<<"Enter an 8 bit binary number:"; for(int i=7;i>=0;i原创 2021-10-04 21:04:35 · 2272 阅读 · 0 评论 -
C语言设计题概述
/// 1. 大写转小写 小写转大写 其他字符不变/*char fun(char ch){ if ((ch>='a')&&(ch<='z')) return ch -'a' + 'A'; if ( isupper(ch) ) #include <ctype.h> return ch +'a'-'A'; return ch;}*//// 2.原创 2021-09-17 23:54:37 · 352 阅读 · 2 评论 -
C语言设计题题型概览
26题:#define N 5double fun ( int w[][N] ){ int i,j,k=0; double av=0.0; for(i=0;i<N;i++) for(j=0;j<N;j++) if(i==0||i==N-1||j==0||j==N-1) { av=av+w[i][j]; k++; } return av/k;}27题:和上面的题差不多一样,就是最.原创 2021-09-17 23:51:34 · 173 阅读 · 0 评论 -
结构体变量的使用
/* 结构体变量的使用*/#include "stdio.h"#include "string.h"struct date{ int year; int month; int day;};struct student{ char name[12]; int age; float score; struct date birthday; ///注意这个位置的结构体嵌套};fl原创 2021-09-12 16:28:50 · 596 阅读 · 1 评论 -
结构体应用1
/* 设某班班级班长竞选,有三个候选人,10个人进行投票,编写一程序模拟投票过程,给出各候选人 的票结果,并给出班长的最终人选。 分析:对于每个候选人,我们抽象出两个属性,一个是姓名,一个是得票数。进行投票时根据姓名 来对应提高得票数*/#include "stdio.h"#include "string.h"struct candidate //结构体 班长候选人 成员变量就是名字和他们所得的原创 2021-09-12 15:57:38 · 153 阅读 · 0 评论 -
求链表数据域的平均值
#include <stdio.h>#include <stdlib.h>#define N 8struct slist{ double s; struct slist *next;};typedef struct slist STREC;double s[N]={85,76,69,85,91,72,64,87};double fun( STREC *h ){ double ave=0.0; STREC *p=h->nex原创 2021-09-11 16:11:26 · 1632 阅读 · 1 评论 -
利用结构体和数组函数的结合实现查找学生的学号
#include <stdio.h>#include <string.h>#define N 16typedef struct{ char num[10]; int s;} STREC;STREC fun( STREC *a, char *b ) //函数返回指定学号的学生数据{ int i; STREC t={'\0', -1}; STREC str={'\0',-1}; for(i=0;i<N;i++) i原创 2021-09-10 17:03:08 · 1131 阅读 · 1 评论 -
八进制数字字符转化为十进制详解
#include <stdio.h>#include <stdlib.h>#include <string.h>int fun( char *p ){ int n; n=*p-'0'; p++; ///还有就是要注意指针值的变化 while( *p!=0 ) { n=n*8+*p-'0'; p++; } return n;}main(){ char原创 2021-09-09 23:50:13 · 4413 阅读 · 2 评论 -
eg 7-13 A secondary pointer variable outputs all the strings that the pointer array points to
/* 使用二级指针变量输出指针数组指向的所有字符串*/#include <stdio.h>int main(){ char *name[]={"January","February","March","April","May","June"}; char **p; int i; for(i=0;i<6;i++) { p=name+i; printf("%s\n",*p); } ret原创 2021-09-05 15:10:51 · 61 阅读 · 0 评论 -
eg 7-12
/* 设一个函数,在调用它的时候,每次实现不同的功能,输入两个整数,第一次调用时 求出这两个整数的和,第二次求出这两个整数的差。*/#include <stdio.h>int sum(int,int);int difference(int,int);int process(int,int,int(*pfun)(int,int));int main(){ int a,b; printf("Enter a and b: "); scanf(原创 2021-09-05 12:25:33 · 100 阅读 · 0 评论 -
eg 7-11 A function pointer calls a function to find the sum of two integers
/* 使用函数指针调用函数求得两个整数的和*/#include <stdio.h>int fsum(int,int);int main(){ int a=20,b=30; int sum; int (*pfsum)(int,int); pfsum=fsum; sum=pfsum(a,b); printf("sum is:%d\n",sum); return 0;}int fsum(int m,int n){原创 2021-09-05 12:07:34 · 72 阅读 · 0 评论 -
eg 7-10 Returns a pointer to the sum of two integers
/* 编写一函数,求两个整数的和,要求返回类型为指针*/#include <stdio.h>int *sump(int,int);int main(){ int a=20,b=30; int *psum; psum=sump(a,b); printf("sum is:%d\n",*psum); return 0;}int *sump(int m,int n){ int s=m+n,*ps=&s; re原创 2021-09-05 11:38:20 · 65 阅读 · 0 评论 -
eg 7-9 A custom function returns the sum and difference of two integers
/* 编写一个函数,计算两个整数的和与差,要求使用自定义函数返回和与差 分析:注意主函数中调用时传递的实参必须是同基类型的指针或者变量地 址*/#include <stdio.h>void compute(int m,int n,int *,int *);int main(){ int a=20,b=10; int sum,difference; compute(a,b,&sum,&difference);原创 2021-09-05 10:54:29 · 69 阅读 · 0 评论 -
eg 7-8 Custom functions to count odd and even numbers
/* 编写一个程序,随机生成1-100之间的内的50个整数,统计其中的奇数和偶数的个数, 要求统计功能由自定义函数 分析:自定义函数一般只有一个返回值,但这个问题中需要两个返回值,所以我们可 以考虑返回值从形参处返回,需要形参类型为指针类型,并且需要两个,分别带回奇 数和偶数个数*/#include <stdio.h>#include "stdlib.h"#include "time.h"void odd_even(int x,int *ce原创 2021-09-05 10:43:22 · 438 阅读 · 0 评论 -
eg 7-5 Output the elements of a two-dimensional array using a row pointer
/* 使用行指针输出二维数组中的元素*//*///方法一:#include <stdio.h>int main(){ int a[2][3]={1,2,3,4,5,6}; int (*p)[3]; int i,j; p=a; for(i=0;i<2;i++) { for(j=0;j<3;j++) printf("%2d ",p[i][j]); printf("\n")原创 2021-09-04 16:41:37 · 81 阅读 · 0 评论 -
eg 7-4 Print the values of all elements of a two-dimensional array using pointer arrays
/* 用指针变量输出二维数组所有元素的值*/#include <stdio.h>int main(){ int a[2][3]={1,2,3,4,5,6}; int *pa; for(pa=a[0];pa<a[0]+6;pa++) //等价于for(pa=&a[0][0];pa<&a[0][0]+6;pa++) printf("%d\t",*pa); printf("\n");原创 2021-09-04 15:53:25 · 70 阅读 · 0 评论 -
eg 7-2 A custom function can exchange the values of two variables
/* 编写一自定义函数,使其完成两个变量值交换的功能,并在主函数中调用 分析:在自定义函数中,,我们了解到主函数调用自定义函数时分配内存 空间,调用结束后,内存空间收回,在这样的情况下对形参值的改变并不 能实现实参值的交换 但是,可以利用指针作为形参,实参是变量的地址,这样即使内存空间收 回,由于指针获得了变量的内存地址,可以间接的通过指针直接把实参值 改变,这时内存收回的仅仅是存放指针变量的空间而已*/#include <stdio.h&原创 2021-09-04 14:49:06 · 68 阅读 · 0 评论 -
eg 7-1 Use an indirect accessor to access the object to which the pointer points
/* 使用间接访问符*访问指针指向的对象*/#include <stdio.h>int main(){ int a,*ap=&a,b,*bp=&b; a=10;b=20; printf("%d\t%d\n",a,b); printf("%d\t%d\n",*ap,*bp); /// * 不能少 return 0;}原创 2021-09-04 10:32:14 · 67 阅读 · 0 评论 -
eg 6-23 Write a function to invert all the valid elements of a string
/* 编写函数,将一个字符串的全部有效元素逆置。例如字符串为abcde,逆置后 为edcba 分析:函数原型可以声明为:void reverseStr(char str[]);参数str为要 逆置的字符串。先求得字符串的长度:n=strlen(str);定义两个整型变量i和j 开始时让他们分别为字符串的第一个元素和最后一个元素的下标,即i=0;j=n-1; 我们交换下标为i和j的两个元素的值,这样str[0]就和str[n-1]交换过来了。然 后让i+原创 2021-09-03 23:14:19 · 96 阅读 · 0 评论 -
eg 6-22 Base conversion function
/* 编写一进制转换的函数,将十进制整数N转换得到其r(设r为2,8或16)进制数 分析:将十进制N转换为r进制的数,其转换方法利用辗转相除法,以N=1348, r=8为例转换方法如下: 除8取余数,余数从下往上依次是从高位到低位 转换过程可以用函数conversion实现,其算法 可以描述如下: 当N>0时重复(1)(2) (1)若N!=0,则将N%r(N除以r的余数)存入数组a中,执行(2);若N=0,将数组a中 所有元素逆序输出,算原创 2021-09-03 22:49:50 · 69 阅读 · 0 评论 -
eg 6-21 Find elements in the middle
/* 假设一个长度为n的一维数组a中的元素是从小到大有序排列的,编写一个函数 利用折半法在数组中查找一个值为k的元素,如果找到,返回该元素在数组中的 下标,否则返回-1 分析:折半查找法的基本思想: 设数组的下标下界为low(开始low=0),上界为high(开始high=n-1),如果low<=high 则进入下面的循环: (1)取中间位置mid=(low+high)/2 (2)将下标为k和mid的元素相比较,有三种情况: 若k原创 2021-09-03 21:51:45 · 99 阅读 · 0 评论 -
eg 6-20 A recursive function finds the greatest commen divisor of two integer
/* 编写一个递归函数gcd,求解两个整数m和n的最大公约数*/int gcd(int m,int n) //gcd函数的定义,功能是递归求解m和n的最大公约数{ if(n==0) return m; else return gcd(n,m%n);}#include <stdio.h>int main(){ int m,n,x; printf("请输入两个整数:"); scanf("%d%d",&m原创 2021-09-03 20:06:58 · 75 阅读 · 0 评论 -
eg 6-18 Macros with arguments
/* 用带参数的宏求圆的面积*/#include <stdio.h>#define PI 3.1415926#define S(r) PI*r*rint main(){ double a=3.0,s; s=S(a); printf("半径为%f的圆的面积为%lf\n",a,s); return 0;}原创 2021-09-03 17:46:30 · 76 阅读 · 0 评论 -
eg 6-15 Extern examples
/* extern变量举例*/#include <stdio.h>void num(){ extern int x,y; //扩展x,y的作用域为从此处到程序结束 int a,b; a=x+y; b=x-y; printf("a=%d,b=%d\n",a,b);}int x,y;int main(){ int a=7,b=5; x=a+b; y=a-b; pri原创 2021-09-03 16:51:14 · 80 阅读 · 0 评论 -
eg 6-14 static 99 multiplication table
/* 在函数中使用static存储类别实现输出九九乘法表*/#include <stdio.h>void Mul(void) //函数定义{ static int a=1; //a为具有static存储类别的静态局部变量,只赋一次初值 int i; for(i=1;i<=a;i++) printf("%d*%d=%2d ",a,i,a*i); ///a保留上次调用结果 printf原创 2021-09-03 16:17:49 · 101 阅读 · 0 评论 -
eg 6-13 Static examples of local variables
/* static局部变量举例*/#include <stdio.h>int fun(){ static int a=1; int b=1,c; a++;b++; c=a+b; printf("a=%d,b=%d,c=%d\n",a,b,c); return c;}int main(){ int i; for(i=0;i<2;i++) printf("fun=%d\n",fun());原创 2021-09-03 16:00:44 · 60 阅读 · 0 评论 -
eg 6-9 Multi-dimensional array names as function arguments
/* 求3*4矩阵的转置矩阵*/#include <stdio.h>#define M 3#define N 4void reverse(int x[][N],int y[][M]) //求x矩阵的转置矩阵y,形参数组的第一维大小说明可以省略,第二维不能省{ int i,j; for(i=0;i<N;i++) //矩阵转置 for(j=0;j<M;j++) y[i][j原创 2021-09-02 18:32:18 · 84 阅读 · 0 评论 -
eg 6-8 Array names are function arguments
/* 输入三十个学生的百分制成绩,按照五分制统计其成绩分布情况*/#include <stdio.h>#define M 30#define N 5void scorecount(int a[],int b[],int n) //对长度为n的数组a按照五分制进行统计,统计数组存入b数组{ int i,m; for(i=0;i<N;i++) b[i]=0; //将b数组所有元素置为零,各分数段人数从0开始统计累加 fo原创 2021-09-02 17:45:07 · 72 阅读 · 0 评论 -
eg 6-7 The sum of all primer numbers in an integer array
/* 求整型数组中所有的素数之和*/#include <stdio.h>#include "math.h"int main(){ int a[20],i,sum=0; //sum保存素数累加的结果 int prime(int x); //函数prime的声明 printf("请输入二十个整数:\n"); for(i=0;i<20;i++) { printf("输入第%d个数:",i原创 2021-09-02 09:30:11 · 80 阅读 · 0 评论 -
eg 6-6 Hanoi problem
/* Hanoi(汉诺塔)问题,这是一个典型的只有哟个递归方法(不能用 其他方法解决的问题。问题是这样的:有三个塔座A,B,C.A 塔座上 有n个大小不等的圆盘,大的在下,小的在上,要把这n个圆盘从A 移到C上,每次只能移动一个圆盘,移动可以借助B进行,但在任何 时候,任何塔座上的圆盘都必须保持大盘在下,小盘在上,要求编 程打印出移动的步骤 算法的三个步骤可以分成两类操作: (1)将n-1个圆盘从一个塔座移到另外一个塔座上(n>1),原创 2021-09-01 23:52:55 · 133 阅读 · 0 评论 -
eg 6-5 Factorial is a recursive method
/* 求s=1!+2!+3!+...+n! 要求:n的值在主函数中用键盘输入,结果s也在主函数中输出 其他功能用函数来完成*/#include <stdio.h>#include <stdlib.h>long sum(int n); //函数sum的声明,功能是求s=1!+2!+3!+...+n!long fac(int n); //函数fac的声明,功能是求n的阶乘int main()原创 2021-09-01 22:50:48 · 79 阅读 · 0 评论 -
eg 6-4 The function implements factorial
/* 求s=1!+2!+3!+...+n! 要求:n的值在主函数中用键盘输入,结果s也在主函数中输出 其他功能用函数来完成*/#include <stdio.h>#include <stdlib.h>long sum(int n); //函数sum的声明,功能是求s=1!+2!+3!+...+n!long fac(int n); //函数fac的声明,功能是求n的阶乘int main()原创 2021-09-01 22:15:48 · 60 阅读 · 0 评论 -
eg 6-3 Incorrect integer exchange
/* 以下程序是否可以实现两个整数的交换,如不可以,为什么?*/#include <stdio.h>int main(){ int x=7,y=11; //主函数中定义的变量 printf("调用函数前x=%d,y=%d\n",x,y); swap(x,y); printf("调用函数前x=%d,y=%d\n",x,y); //调用后 return 0;}void swap(i原创 2021-09-01 21:34:03 · 58 阅读 · 0 评论 -
eg 6-2 Write a function that implements some four operations on two integers
/* 编写函数operate,实现两个整数的某种四则运算 分析:将四则运算设计成一个独立的函数operate(int a,char oper,int b),其参数有三个 字符型用于接收运算符号,主函数完成变量的初始化及调用operate函数,在调用函数前要 对函数加以声明*/#include <stdio.h>#include "stdlib.h" //此头文件中由exit函数int operate(int原创 2021-09-01 20:12:05 · 76 阅读 · 0 评论