C语言第十章习题答案

合集下载

第10章C语言程序设计习题答案

第10章C语言程序设计习题答案

C 语言程序设计( Visual C++6.0 环境)》习题答案习题十、思考题1.简述公有类型成员与私有类型成员的区别。

公有(public) 类型成员不但可以被类的成员函数访问,而且可以被外界访问,所以说公有类型定义了类的外部接口。

私有(private) 类型成员只能被类的成员函数访问,外界不能直接访问它。

类的数据成员一般都应该声明为私有成员。

2.简述构造函数与析构函数的作用。

构造函数的作用就是在对象在被创建时利用特定的值构造对象,将对象初始化。

析构函数的作用与构造函数正好相反,它是用来在对象被删除前进行一些清理工作。

析构函数调用之后,对象被撤消了,相应的内存空间也将被释放。

3.简述什么是友元函数。

友元函数是在类定义中由关键字friend 修饰的非成员函数。

友元函数可以是一个普通函数,也可以其它类中的一个成员函数,它不是本类的成员函数,但它可以访问本类的私有成员和保护成员。

4.简述公有继承、私有继承和保护继承三种继承方式的区别。

⑴、当类的继承方式为公有(public 继承)时,基类的公有(public )成员和保护( protected )成员仍然成为派生类的公有成员和保护成员,而基类的私有成员不能被派生类访问。

⑵、当类的继承方式为保护( protected )继承时,基类的公有(public )成员和保护( protected )成员将成为派生类的保护成员,而基类的私有成员不能被派生类访问。

⑶、当类的继承方式为私有(private )继承时,基类的公有(public )成员和保护(protected )成员将成为派生类的私有成员,而基类的私有成员不能被派生类访问。

5.定义一个圆柱体类,其属性为圆柱体的底面半径和高,能计算出圆柱体的体积。

#include<iostream.h>class cylinder{public:cylinder(float r,float h){radius=r;height=h;}float Volume();private:float radius;float height;};float cylinder::Volume(){return 3.14*radius*radius*height;}void main(){float r,h;cout<<" 请输入圆柱体的底面半径和高:"; cin>>r>>h;cylinder x(r,h);cout<<x.Volume()<<endl;}6.从第 5 题中定义的圆柱体类中派生出圆锥类,覆盖计算体积的成员函数。

《C语言程序设计》教材习题答案第10章

《C语言程序设计》教材习题答案第10章

一、选择题1. 设已定义“int a,* p”,下列赋值表达式中正确的是:C)p=&a2. 设已定义“int x,*p=&x;”,则下列表达式中错误的是:B)&*x3. 若已定义“int a=1 ,*b=&a;”,则“printf(“%d \n”,*b);”的输出结果为:A)a的值。

4. 设已定义“int x,*p,*pl=&x,*p2=&x;”,则下列表达式中错误的是:C)p=p1+p2.5. 设有函数定义“void p(int *x){printf(“%d\n”,*x);}”和变量定义“int a=3;”,则正确的函数调用是:C)p(&a)6. 函数“int fun(char * x){char * y=x; while(*y)y++;return(y-x); }”的功能是A)求字符串的长度。

7. 运行一下程序,输出结果为:B)5 6int fun (int a,int *b){a++;(*b)++;return a+*b;}void main(){int x=1,y=2;Printf(“%d”,fun(x,&y));Printf(“%d”,fun(x,&y));}8. 运行以下程序,输出结果为:C)58#include<stdio.h>Int * fun(int a ,int *b){a++;(*b)++;*b=a+*b;return b;}Void main(){Int x=1,y=2,*z;Z=fun(x,&y);Printf(“%d”,*z);Z=fun(x,&y);Printf(“%d”,*z);}9. 若已定义“int a[]={1,2 ,3,4},*p=a;”,则下面表达式中值不等于2的是C)*(++a)10. 若已定义“int a[]={1,2 ,3,4},*p=a+1;”,则p[2]的值为C)411. 设已定义“int x[4][10],*p=x[0];”,则下列表达式中的值为整形的是B)*(p+1)12. 设已定义“char s[]=”ABCD”;”,”printf(“%s”,s+1)”的值为C)BCD13. 设已定义“char str[]=”abcd”,*ptr=str;”,则*(prt+4)的值为B)014. 下面对字符串变量的初始化或赋值操作中,错误的是C)char a[10];a=”OK”;15. 设已定义“char *ps[2]={“abc”,”1234”};”,则以下叙述中错误的是A)ps为指针变量,它指向一个长度为2的字符串数组16. 设已定义“struct {int a,b;} s,*ps=&s;”,则错误的结构体成员引用是C)*ps.a17. 设已有以下定义,则表达式的值为2的是A)k=++p->datastruct st {int data;st *link;} a[3]={1,a+1,3,a+2,5,0},*p=a;二、编程题1. 输入3个字符串,输出其中最大的字符串(用字符指针)#include <stdio.h>#include <stdlib.h>#define str_count 3#define str_length 100int main(int argc, char *argv[]){char a[str_count][str_length],*p;printf("请输入3个字符串:");int i;for(i=0;i<str_count;i++){scanf("%s",a[i]);}p=a[0];for(i=1;i<str_count;i++){if(strcmp(p,a[i])<0){p=a[i];}}printf("最大的字符串为:%s",p);system("PAUSE");return 0;}2. 定义一个函数,函数的功能是求已知半径的圆的周长和面积。

c程序设计第四版(谭浩强)第十章答案

c程序设计第四版(谭浩强)第十章答案
}employee;
int main()
{
employee em[10];
FILE *p;
int i;
if((p=fopen("employee.dat","wb"))==NULL)
stu[n-1].ave=(stu[n-1].score[0]+stu[n-1].score[1]+stu[n-1].score[2])/3.0;
sort(stu);
if((p1=fopen("stud2.dat","wb"))==NULL)
{
printf("cannot open file stud\n");
}
for(i=0;i<n;i++)
if(fwrite(&stu[i],sizeof(struct student),1,p)!=1)
printf("file write error\n");
fclose(p);
}
int main()
{
int i;
printf("enter date of students:\n");
exit(0);
}
printf("new sort of students:\n");
for(i=0;i<n;i++)
{
fwrite(&stu[i],sizeof(struct student),1,p1);
printf("%ld %-6s %5.2f %5.2f %5.2f %5.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);

谭浩强版C语言的第十章《指针》答案

谭浩强版C语言的第十章《指针》答案

谭浩强版C语言的第十章《指针》答案第十章《指针》答案如下inc/testPtr.h#include <string.h>#include <ctype.h>#include <math.h>#include <assert.h>#define SIZE 1024int a2i(char *start, char *end){int size = 0, ret = 0;long base = 0;size = end - start + 1;base = (long)pow(10, size - 1);while(size-- > 0){ret += (*start++ - '0') * base;base /= 10;}return ret;}int extraNum(char *str, int arr[]){int ite = 0, counter = 0;char *start = NULL, *end = NULL;while(*str != '\0'){if(isdigit(*str)){start = end = str;while( isdigit(*end) && *end != '\0'){++end;}arr[ite++] = a2i(start, end-1);str = end;}else{str++;}}return ite;}int sortStr(char *arr[], int size){int ite1 = 0, ite2 = 0, minPos = 0;char *tmp;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ++ite2 ){if( strcmp(arr[ite2], arr[minPos]) < 0 ){minPos = ite2;}}if(minPos != ite1){tmp = arr[minPos];arr[minPos] = arr[ite1];arr[ite1] = tmp;}}return 0;}int sort(int arr[], int size){int minPos = 0, ite1 = 0, ite2 = 0, tmp = 0;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ite2++){if( arr[ite2] < arr[minPos] ){minPos = ite2;}}if(minPos != ite1){tmp = arr[ite1];arr[ite1] = arr[minPos];arr[minPos] = tmp;}}return 0;}int sortPtr(int arr[], int size){int minPos = 0, ite1 = 0, ite2 = 0, tmp = 0;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ite2++){if( *(arr + ite2) < *(arr + minPos) ){minPos = ite2;}}if(minPos != ite1){tmp = *(arr + ite1);*(arr + ite1) = *(arr + minPos);*(arr + minPos) = tmp;}}return 0;}int getMultiArr(int arr[][5], int n){int i = 0, j = 0, min = 0, tmp = 0, size = 5 * n;int *p = NULL, pos[size];/* copy */p = (int *)malloc(size * sizeof(int));assert(p != NULL);memcpy(p, arr, size * sizeof(int));/* sort */for(i = 0; i < size - 1; i++){min = i;for(j = i + 1; j < size; j++){if( *(p + j) < *(p + min)){min = j;}}if(min != i){tmp = *(p + min);*(p + min) = *(p + i);*(p + i) = tmp;}}/* move */for(i = 0; i < n; i++){for(j = 0; j < 5; j++){if( *p == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[0][0];arr[0][0] = tmp;continue;}if( *(p + 1) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[0][4];arr[0][4] = tmp;continue;}if( *(p + 2) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n - 1][0];arr[n - 1][0] = tmp;continue;}if( *(p + 3) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n - 1][4];arr[n - 1][4] = tmp;continue;}if( *(p + size - 1) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n/2][2];arr[n/2][2] = tmp;continue;}}}free(p);p = NULL;return 0;}int statStr(char *str){int upper = 0, lower = 0, space = 0, num = 0, other = 0;while(*str != '\0'){if(isdigit(*str)){num++;}else if (isupper(*str)){upper++;}else if (islower(*str)){lower++;}else if (isspace(*str)){space++;}else{other++;}str++;}assert(3 == upper);assert(5 == lower);assert(10 == num);assert(2 == space);assert(6 == other);return 0;}int average(int(*stu)[6], int classNum, int stuNum){int i = 0, ave = 0;for(i = 0; i < stuNum; i++){ave += (*(stu + i))[classNum];}ave /= stuNum;return ave;}int searchStu(int(*stu)[6], int stuNum){int counter = 0, i = 0, j = 1, stuCounter = 0;for (i = 0; i < stuNum; i++){counter =0;for(j = 1; j < 6; j++){if( (*(stu + i))[j] < 60 ){counter++;}}if(counter >= 2){stuCounter++;}}return stuCounter;}int moveInt(int arr[], int n, int m){int i = 0, *p = NULL;p = (int*)malloc(n * sizeof(int));assert(p != NULL);memcpy(p + m, arr, (n - m) * sizeof(int));memcpy(p, arr + n -m , m * sizeof(int));memcpy(arr, p, n * sizeof(int));free(p);p = NULL;return 0;}int myStrcmp(char *p1, char*p2){int ret = 0;while((*p1 != '\0') && (*p2 != '\0') && ( *p1 == *p2 ) ) {p1++;p2++;}if(*p1 == '\0'){ret = -1;}else if (*p2 == '\0'){ret = 1;}else{ret = (*p1 - *p2) > 0 ? 1 : -1;}return ret;}int revArr(int a[], int size){int tmp = 0, *start = NULL, *end = NULL;start = a;end = start + size - 1;size = size / 2;while(size >= 0){tmp = *(start + size);*(start + size) = *(end - size);*(end - size) = tmp;size--;}return 0;}char *getMonth(char *month[], int which) {assert(which <= 12);return ( *(month + which - 1));}int getStr(char *dest, char* src, int m) {int len = 0;len = strlen(src) + 1 - m;src = src + m - 1;memcpy(dest, src, len * sizeof(char));return 0;}int removePer3(int arr[], int size){int i = 0;for(i = 0; i < size; i++){if(((arr[i]) % 3) == 0){arr[i] = 0;}}return 0;}int getMinMax(int a[], int size){int i = 0, min = 0, max = 0, tmp = 0;min = max = 0;for(i = 0; i < size; i++){if (a[i] <= a[min]){min = i;}if(a[i] >= a[max]){max = i;}}tmp = a[0];a[0] = a[min];a[min] = tmp;tmp = a[size - 1];a[size - 1] = a[max];a[max] = tmp;}int test_10_1(){int ite = 0, iRet = 0, arr[5] = {121, 234, 456456, 543, 23};iRet = sortPtr(arr, 5);assert (23 == arr[0]);assert (121 == arr[1]);assert (234 == arr[2]);assert (543 == arr[3]);assert (456456 == arr[4]);printf("\r\nTest_10_1 Passed!");return 0;}int test_10_2(){int ite = 0, iRet = 0;char *arr[10] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable"};iRet = sortStr(arr, 3);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[2], "The efforts of your team can be coordinated into a definable") == 0);printf("\r\nTest_10_2 Passed!");return 0;}int test_10_3(){int ret = 0, a[10] = {7,2,3,9,1,0,7,6,5,0};ret = getMinMax(a, 10);assert(a[0] == 0);assert(a[9] == 9);printf("\r\nTest_10_3 Passed!");return 0;}int test_10_4(){int ret = 0, arr[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};ret = moveInt(arr, 9, 3);assert(6 == arr[0]);assert(7 == arr[1]);assert(8 == arr[2]);assert(0 == arr[3]);assert(1 == arr[4]);assert(2 == arr[5]);assert(3 == arr[6]);assert(4 == arr[7]);assert(5 == arr[8]);printf("\r\nTest_10_4 Passed!");return 0;}int test_10_5(){int ret = 0, i = 0, a[12] = {1,2,3,4,5,6,7,8,9,10,11,12};ret = removePer3(a, 12);assert(a[2] == 0);assert(a[5] == 0);assert(a[8] == 0);assert(a[11] == 0);printf("\r\nTest_10_5 Passed!");return 0;}int test_10_7(){int ret = 0;char s2[100] = {0}, *s1 = "hello world!";getStr(s2, s1, 7);assert( strcmp(s2, "world!") == 0);printf("\r\nTest_10_7 Passed!");return 0;}int test_10_8(){int ret = 0;char *str = "a123xABC ??#$%^ 302tab5876";ret = statStr(str);if(ret == 0){printf("\r\nTest_10_8 Passed!");}return 0;}int test_10_10(){int i = 0, j = 0, ret = 0;int arr[5][5] = { \{16, 17, 18, 19, 20}, \{11, 12, 13, 14, 15}, \{1, 2, 3, 4, 5}, \{21, 22, 23, 24, 25}, \{6, 7, 8, 9, 10}, \};ret = getMultiArr(arr, 5);assert(1 == arr[0][0] );assert(2 == arr[0][4] );assert(3 == arr[4][0] );assert(4 == arr[4][4] );assert(25 == arr[2][2] );printf("\r\nTest_10_10 Passed!");return 0;}int test_10_11(){int ite = 0, iRet = 0;char *arr[10] = { "In the", \"An aud", \"The ef", \"Proces", \"Sets o", \"Unifie", \"Out-of", \"Practi", \"Ration", \"Explor" \};iRet = sortStr(arr, 10);assert( strcmp(arr[0], "An aud") == 0 );assert( strcmp(arr[1], "Explor") == 0);assert( strcmp(arr[2], "In the") == 0);assert( strcmp(arr[3], "Out-of") == 0);assert( strcmp(arr[4], "Practi") == 0);assert( strcmp(arr[5], "Proces") == 0);assert( strcmp(arr[6], "Ration") == 0);assert( strcmp(arr[7], "Sets o") == 0);assert( strcmp(arr[8], "The ef") == 0);assert( strcmp(arr[9], "Unifie") == 0);printf("\r\nTest_10_11 Passed!");return 0;}int test_10_12(){int ite = 0, iRet = 0;char *arr[10] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable", \"Process by using one of the following", \"Sets of Rational ClearCase features", \"Unified Change Management (UCM),", \"Out-of-the-box process that supports best", \"Practices for change management as described in the IBM", \"Rational Unified Process. Project managers can configure", \"Explorer. For more information about Rational ClearCase"};iRet = sortStr(arr, 10);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "Explorer. For more information about Rational ClearCase") == 0);assert( strcmp(arr[2], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[3], "Out-of-the-box process that supports best") == 0);assert( strcmp(arr[4], "Practices for change management as described in the IBM") == 0);assert( strcmp(arr[5], "Process by using one of the following") == 0);assert( strcmp(arr[6], "Rational Unified Process. Project managers can configure") == 0);assert( strcmp(arr[7], "Sets of Rational ClearCase features") == 0);assert( strcmp(arr[8], "The efforts of your team can be coordinated into a definable") == 0);assert( strcmp(arr[9], "Unified Change Management (UCM),") == 0);printf("\r\nTest_10_12 Passed!");return 0;}int test_10_14(){int ret = 0, a[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};ret = revArr(a, 11);assert(10 == a[0]);assert(9 == a[1]);assert(8 == a[2]);assert(7 == a[3]);assert(6 == a[4]);assert(5 == a[5]);assert(4 == a[6]);assert(3 == a[7]);assert(2 == a[8]);assert(1 == a[9]);assert(0 == a[10]);printf("\r\nTest_10_14 Passed!");return 0;}int test_10_15(){int ave = 0, ret = 0;int student[4][6] = { \{1, 100, 90, 80, 70, 97}, \{2, 34, 45, 56, 78, 97}, \{3, 76, 34, 68, 84, 12}, \{4, 90, 90, 90, 75, 28} \};ave = average(student, 1, 4);ret = searchStu(student, 4);assert(75 == ave);assert(2 == ret);printf("\r\nTest_10_15 Passed!");return 0;}int test_10_16(){int iRet = 0, ite = 0, arr[SIZE] = {0};char *str = "a123x456 17960? 302tab5876";iRet = extraNum(str, arr);assert(123 == arr[0]);assert(456 == arr[1]);assert(17960 == arr[2]);assert(302 == arr[3]);assert(5876 == arr[4]);printf("\r\nTest_10_16 Passed!");return 0;}int test_10_17(){int ret = 0;char *s1 = "abcd", *s2 = "abCd";ret = myStrcmp(s1, s2);assert(ret == 1);char *s3 = "aBcd", *s4 = "abCd";ret = myStrcmp(s3, s4);assert(ret == -1);char *s5 = "abcde", *s6 = "abcd";ret = myStrcmp(s5, s6);assert(ret == 1);char *s7 = "abcd", *s8 = "abcde";ret = myStrcmp(s7, s8);assert(ret == -1);char *s9 = "abcd", *s10 = "abCde";ret = myStrcmp(s9, s10);assert(ret == 1);printf("\r\nTest_10_17 Passed!");return 0;}int test_10_18(){int which = 0;char *month[12] = { \"January", \"February", \"March", \"April", \"May", \"June", \"July", \"August", \"September", \"October", \"November", \"December" \};which = 11;assert( strcmp("November", getMonth(month, which)) == 0 );which = 7;assert( strcmp("July", getMonth(month, which)) == 0 );printf("\r\nTest_10_18 Passed!");return 0;}int test_10_20(){int ite = 0, iRet = 0;char *arr[5] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable", \"Process by using one of the following", \"Sets of Rational ClearCase features" \};iRet = sortStr(arr, 5);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[2], "Process by using one of the following") == 0);assert( strcmp(arr[3], "Sets of Rational ClearCase features") == 0);assert( strcmp(arr[4], "The efforts of your team can be coordinated into a definable") == 0);printf("\r\nTest_10_20 Passed!");return 0;}int test_10_21(){int ite = 0, iRet = 0, arr[5] = {121, 234, 456456, 543, 23};iRet = sort(arr, 5);assert (23 == arr[0]);assert (121 == arr[1]);assert (234 == arr[2]);assert (543 == arr[3]);assert (456456 == arr[4]);printf("\r\nTest_10_21 Passed!");return 0;}int testPtr(){int iRet = 0;#if 0#endifiRet += test_10_1();iRet += test_10_2();iRet += test_10_3();iRet += test_10_4();iRet += test_10_5();iRet += test_10_7();iRet += test_10_8();iRet += test_10_10();iRet += test_10_11();iRet += test_10_12();iRet += test_10_14();iRet += test_10_15();iRet += test_10_16();iRet += test_10_17();iRet += test_10_18();iRet += test_10_20();iRet += test_10_21();return iRet;}src/#include <stdio.h>#include <stdlib.h>#include <assert.h>#include "../inc/testFile.h" #include "../inc/testBits.h" #include "../inc/testPtr.h"int main(){int iRet = 0;#if 0iRet += testFile();assert(iRet == 0);iRet += testBits();assert(iRet == 0);#endifiRet += testPtr();assert(iRet == 0);return 0;}。

C语言习题结构体和杂类(答案)

C语言习题结构体和杂类(答案)

C语言习题结构体和杂类(答案)第十章结构体和杂类一.选择题1.如下说明语句,则下面叙述不正确的是(C)。

tructtu{inta;floatb;}tutype;A.truct是结构体类型的关键字B.tructtu是用户定义结构体类型C.tutype是用户定义的结构体类型名(变量名)D.a和b都是结构体成员名2.在16位PC机中,若有定义:tructdata{inti;charch;doublef;}b;则结构变量b占用内存的字节数是(D)。

A.1B.2C.8D.11A.1和2B.2和3C.7和2D.7和84.以下程序的输出结果是(D)。

unionmyun{truct{int某,y,z;}u;intk;}a;main(){a.u.某=4;a.u.y=5;a.u.z=6;a.k=0;printf(\A.4B.5C.6D.05.当定义一个共用体变量时,系统分配给它的内存是(C)。

A.各成员所需内存量的总和B.结构中第一个成员所需内存量C.成员中占内存量最大的容量D.结构中最后一个成员所需内存量6.若有以下程序段:uniondata{inti;charc;floatf;}a;intn;则以下语句正确的是(C)。

A.a=5;B.a={2,’a’,1.2}C.printf(“%d”,a);D.n=a;7.设truct{inta;charb;}Q,某p=&Q;错误的表达式是(d)。

A.Q.aB.(某p).bC.p->aD.某p.b9.以下对C语言中共用体类型数据的叙述正确的是(c)。

A.可以对共用体变量直接赋值B.一个共用体变量中可以同时存放其所有成员C.一个共用体变量中不能同时存放其所有成员D.共用体类型定义中不能出现结构体类型的成员10.下面对typedef的叙述中不正确的是(b)。

A.用typedef可以定义多种类型名,但不能用来定义变量B.用typedef可以增加新类型C.用typedef只是将已存在的类型用一个新的标识符来代表D.使用typedef有利于程序的通用和移植二.判断题1.共用体类型的变量的字节数等于各成员字节数之和。

C语言-谭浩强著-第10章练习题

C语言-谭浩强著-第10章练习题

第10章对文件的输入输出(2012年9月真题)(40)有以下程序#include <stdio.h>main(){ FILE *fp;int i,a[6]={1,2,3,4,5,6};fp=fopen("d2.dat","w+");for(i=0;i<6;i++) fprintf(fp,"%d\n",a[i]);rewind(fp);for(i=0;i<6;i++) fscanf(fp,"%d",&a[5-i]);fclose(fp);for(i=0;i<6;i++) printf("%d,",a[i]);}程序运行后的输出结果是A)4,5,6,1,2,3, B)1,2,3,3,2,1,C)1,2,3,4,5,6, D)6,5,4,3,2,1,答案:D(2012年3月真题)40、以下函数不能用于向文件写入数据的是A ftellB fwriteC fputcD fprintf答案:A(2011年9月真题)40.有以下程序#include<stdio.h>main(){FILE *fp; int k,n,i,a[6]={1,2,3,4,5,6};fp=fopen("d2.dat","w");for(i=0;i<6;i++)fprintf(fp,"%d\n",a[i]);fclose(fp); fp=fopen("d2.dat","r");for(i=0;i<3;i++)fscanf(fp,"%d%d",&k,&n);fclose(fp); printf("%d,%d\n",k,n); }程序运行后的输出结果是A.1,2B.3,4C.5,6D.123,456答案:C(2011年3月真题)(40)设fp已定义,执行语句fp=fopen("file","w");后,以下针对文本文件file操作叙述的选项中正确的是A)写操作结束后可以从头开始读 B)只能写不能读C)可以在原有内容后追加写 D)可以随意读和写(2010年3月真题)(40)有以下程序#include#include<stdio.h>main(){ FILE *fp;char str[10];fp=fopen("myfile.dat","w");fputs("abc",fp);fclose(fp);fopen("myfile.data","a+");fprintf(fp,"%d",28);rewind(fp);fscanf(fp,"%s",str); puts(str);fclose(fp); }程序运行后的输出结果是A)abc B) 28c C) abc28 D)因类型不一致而出错答案:B(2009年9月真题)(40)下列关于C语言文件的叙述中正确的是A)文件由一系列数据依次排列组成,只能构成二进制文件B)文件由结构序列组成,可以构成二进制文件或文本文件C)文件由数据序列组成,可以构成二进制文件或文本文件D)文件由字符序列组成,其类型只能是文本文件答案:C(2009年3月真题)40.有以下程序#include <stdio.h>main(){ FILE *f;f=fopen("filea.txt","w"); fprintf(f,"abc"); fclose(f); }若文本文件filea.txt中原有内容为:hello,则运行以上程序后,文件filea.txt的内容为A)helloabc B)abclo C)abc D)abchello答案:C(2008年9月真题)(40)有以下程序#include <stdio.h>main(){ FILE *pf;char *s1="China", *s2="Beijing";pf=fopen("abc.dat","wb+");fwrite(s2,7,1,pf);rewind(pf); /*文件位置指针回到文件开头*/fwrite(s1,5,1,pf);fclose(pf);}以上程序执行后abc.dat文件的内容是A) China B) Chinang C) ChinaBeijing D) Bei jingChina答案:B(2008年4月真题)30、下列叙述中错误的是( )。

C语言习题及答案(第十章)

C语言习题及答案(第十章)

10-2 定义指针变量p,q,r,让它们指向变量a,b,c,在指向d,e,f,最后指向变量x,y,z,然后输出p,q,r与*p,*q,*r.解:int a,b,c,d,e,f,x,y,z ;int *p,*q,*r ;p=&a;q=&b;r=&c ;p=&d;q=&e;r=&f ;p=&x;q=&y;r=&z ;printf(“%l,%l,%l”,p,q,r) ;printf(“%d,%d,%d”,*p,*q,*r) ;思考:指针也是一种数据类型吗?他可以不依赖其它数据类型而独立存在吗?10-3 应用指针,实现10个整数从打到小的排序输出。

解:#include "stdio.h"void main(){int i,j,a[10],*p,t;printf("请输入10个整数:\n");for(i=0;i<10;i++)scanf("%d",&a[i]);for(i=0;i<9;i++){p=&a[i] ;for(j=i+1;j<10;j++)if(a[j]>*p) p=&a[j] ;t=*p;*p=a[i];a[i]=t;}printf("排序后的数组为:\n");for(i=0;i<10;i++)printf("%d,",a[i]) ;}思考:指针变量的值可以由键盘输入确定吗?它与其所指类型的变量值之间的关系如何?10-4 应用指针,求n个数的最小值和最大值。

解:#include "stdio.h"#define L 100void main(){int i,n,a[L],*max,*min;printf("请确定欲输入数据的个数(<100):");scanf("%d",&n);printf("下面请依次输入%d的个数\n",n);for(i=0;i<n;i++){printf("第%d的个数:",i+1);scanf("%d",&a[i]);}max=min=&a[0] ;for(i=0;i<n;i++){if(a[i]>*max) max=&a[i];if(a[i]<*min) min=&a[i];}printf("最大值=%4d,最小值=%4d\n",*max,*min);}思考:指针也可以指向相同类型的数组元素,此题如用指针来表示来表示数组的各元素的值,程序应如何设计?10-5 应用指针,编写下列字符串处理函数:(1) 字符串的复制函数。

明解C语言入门篇第十章答案

明解C语言入门篇第十章答案

明解C语⾔⼊门篇第⼗章答案练习10-1#include <stdio.h>void adjust_point(int*n) {if (*n > 100)*n = 100;if (*n < 0)*n = 0;}int main() {int x;printf("请输⼊⼀个数:");scanf("%d", &x);adjust_point(&x);printf("修改后的值是%d",x);}练习10-2void decrement_date(int* y, int* m, int* d) {*d -= 1;if (*d == 0) {*m -= 1;if (*m == 1 || *m == 3 || *m == 5 || *m == 7 || *m == 10 || *m == 8 ||* m == 0) {*d = 31;}if (*m == 2) {*d = 28;if (*y % 4 == 0) {*d = 29;}}else*d = 30;if (*m == 0) {*m = 12;*y -= 1;}}}void increment_date(int* y, int* m, int* d) {*d += 1;if ((*m == 1 || *m == 3 || *m == 5 || *m == 7 || *m == 10 || *m == 8 || *m == 12) && (*d == 32)) {*d = 1;*m += 1;}if ((*m == 4 || *m == 6 || *m == 9 || *m == 11 ) && (*d == 31)) {*d = 1;*m += 1;}if (*m == 2) {if (*d == 29&& ((*y & 4) != 0)) {*d = 1;}if (*d == 30 && ((*y & 4) == 0)) {*d = 1;*m += 1;}}if (*m == 13) {*y += 1;*m = 1;}}练习10-3#include<stdio.h>void swap(int* px, int*py) {int temp = *px;*px = *py;*py = temp;}void sort3(int*n1, int*n2, int*n3) {if (*n1 > *n2) {swap(n1, n2);}if (*n1 > * n3) {swap(n1, n3);}if (*n2 > * n3) {swap(n2, n3);}}int main(void) {int n1, n2, n3;printf("n1=");scanf("%d", &n1);printf("n2=");scanf("%d", &n2);printf("n3=");scanf("%d", &n3);putchar('\n');sort3(&n1, &n2, &n3);printf("%d,%d,%d", n1, n2, n3); }练习10-4#include <stdio.h>#define number 5void set_idx(int*v, int n) {int i = 0;for (i = 0; i < n; i++) {*(v + i) = i;}}int main() {int i;set_idx(x, number);for (i = 0; i < number; i++) {printf("x[%d]=%d", i,x[i] );putchar('\n');}}练习10-5会报错数组中元素会溢出,因为直接从v【2】开始赋值。

C语言第十章复习题(含答案)

C语言第十章复习题(含答案)

1.若有说明:int i,j=7, *p=&i;,则与i=j;等价的语句是(B )。

(A)i= *p; (B)*p=*&j; (C)i=&j; (D)i=* *p;2.若有以下说明:int a[10]={1,2,3,4,5,6,7,8,9,10},*p=a;则数值为6的表达式是( C)。

(A)*p+6 (B)*(p+6) (C)*p+=5 (D)p+53.若有以下定义和语句:char *s1="12345",*s2="1234";printf("%d\n",strlen(strcpy(s1,s2)));则输出结果是( A)。

(A)4 (B)5 (C)9 (D)104.以下不能正确进行字符串赋初值的语句是(A )。

(A) char str[5]="good!"; (B) char str[]="good!";(C) char *str="good!"; (D) char str[5]={‘g',‘o',‘o',‘d'};5.若定义:int a=511,*b=&a;, 则printf("%d\n",*b);的输出结果为( D )。

(A) 无确定值 (B) a的地址 (C) 512 (D) 5116.下面程序的输出是( A )。

(A) 3 (B) 4 (C) 1 (D) 2main(){ int a[10]={ 1,2,3,4,5,6,7,8,9,10},*p=a;printf("%d\n",*(p+2));}7.若有说明:int n=2,*p=&n,*q=p;,则以下非法的赋值语句是(D )。

(A) p=q; (B) *p=*q; (C) n=*q; (D) p=n;8.有如下说明int a[10]={1,2,3,4,5,6,7,8,9,10},*p=a;则数值为9的表达式是( B )。

《C语言程序设计教程》(第三版)李凤霞 主编——第十章习题答案

《C语言程序设计教程》(第三版)李凤霞 主编——第十章习题答案

习题十一、单选题1~5 DDDCC6~10 AABDB11~14 CADC二、填空题1、34 122、ARRAY a,b,c;3、1 34、a c5、(*b).day=? b->day=?5、分析下列程序执行结果。

#include “stdio.h”main(){static struct s1{char c[4],*s;s1={“abc”,”def”};static struct s2{char *cp;struct s1 ss1;}s2={“ghi”,{“jkl”,”mno”}};printf(“%c%c\n”,s1.c[0],*s1.s); /*output ab */printf(“%s%s\n”,s1.c,s1.s); /*output abcdef */printf(“%s%s\n”,s2.cp,s2.ss1.s); /*output ghimno */printf(“%s%s\n”,++s2.cp,++s2.ss1.s); /* output hino */}6、以下程序的功能是:读入一行字符(如:a,...,y,z),按输入时的逆序建立一个链式的结点序列,即先输入的位于链表尾(如下图),然后再按输入的相反顺序输出,并释放全部结点。

#define getnode(type)_________malloc(sizeof(type)) ( (struct node *))main(){struct node{char info;struct node *link;}*top,*p;char c;top=NULL;while((c=getchar())______________) (!='\n'){p=getnode(struct node);p->info=c;p->link=top;top=p;}while(top){_________________; (p=top)top=top->link;putchar(p->info);free(p);}}7、下面的函数将指针p2所指向的线性链表链接到p1所指向的的链表的末端。

C语言程序设计答案(黄保和编)第10章

C语言程序设计答案(黄保和编)第10章

C语言程序设计答案(黄保和编)第10章厦门大学本科生公共课《C程序设计基础》教材习题答案第10章指针一、选择题1. 设已定义“int a, * p”,下列赋值表达式中正确的是:C)p=&a2. 设已定义“int x,*p=&x;”,则下列表达式中错误的是:B)&*x3. 若已定义“int a=1 ,*b=&a;”,则“printf;”的输出结果为:A) a的值。

4. 设已定义“int x,*p,*pl=&x,*p2=&x;”,则下列表达式中错误的是:C)p=p1+p2.5. 设有函数定义“void p(int *x){printf(“%d\\n”,*x);}”和变量定义“int a=3;”,则正确的函数调用是:C)p6. 函数“int fun{char * y=x; while(*y)y++;return(y-x); }”的功能是A)求字符串的长度。

7. 运行一下程序,输出结果为:B)5 6int fun (int a,int *b) {a++;++; return a+*b; }void main {int x=1,y=2;Printf(“%d”,fun(x,&y)); Printf(“%d”,fun(x,&y)); }8. 运行以下程序,输出结果为:C)58#include Int * fun(int a ,int *b) {a++;(*b)++; *b=a+*b; return b; }Void main {Int x=1,y=2,*z; Z=fun(x,&y); Printf(“%d”,*z); Z=fun(x,&y); Printf(“%d”,*z); }9. 若已定义“int a={1,2 ,3,4},*p=a;”,则下面表达式中值不等于2的是C)*(++a) 10. 若已定义“int a={1,2 ,3,4},*p=a+1;”,则p[2]的值为C)411. 设已定义“int x[4][10],*p=x[0];”,则下列表达式中的值为整形的是B)* 12. 设已定义“char s=”ABCD”;”,”printf(“%s”,s+1)”的值为C)BCD第1页/共7页厦门大学本科生公共课《C程序设计基础》教材习题答案第10章指针13. 设已定义“char str=”abcd”,*ptr=str;”,则*的值为B)014. 下面对字符串变量的初始化或赋值操作中,错误的是C)char a[10];a=”OK”; 15. 设已定义“char *ps[2]={“abc”,”1234”};”,则以下叙述中错误的是A)ps为指针变量,它指向一个长度为2的字符串数组16. 设已定义“struct {int a,b;} s,*ps=&s;”,则错误的结构体成员引用是C)* 17. 设已有以下定义,则表达式的值为2的是A)k=++p->datastruct st { int data; st *link;} a[3]={1,a+1,3,a+2,5,0},*p=a; 二、编程题1. 输入3个字符串,输出其中最大的字符串#include #include #define str_count 3 #define str_length 100int main(int argc, char *argv) {char a[str_count][str_length],*p; printf(\请输入3个字符串:\ int i;for(i=0;i #include #define pi第2页/共7页厦门大学本科生公共课《C程序设计基础》教材习题答案第10章指针int main(int argc, char *argv) {double r;printf(\请输入半径:\ scanf(\ double c,s;void circle(double r, double *,double *); circle(r,&c,&s);printf(\圆的周长和半径分别为:%lf,%lf\ system(\return 0; }void circle(double r,double *c,double *s) { *c=2*pi*r; *s=pi*r*r; }3. 定义函数max,函数参数为3个字符串,函数返回值最大的字符串。

《精通C程序设计教程》第十、十一章部分习题答案

《精通C程序设计教程》第十、十一章部分习题答案

《精通C程序设计教程》第十、十一章部分习题答案第十章10.2 read=0, green=1, yellow=5, white=6, black=710.6 42, 110.8#include "stdio.h"typedef struct student { long xh;char xm[21];int s1,s2,s3; } STU;#define N 5void inp_stu(STU a[N]){ int i;printf("Input %d students data\n",N);printf("xh xm s1 s2 s3\n");for(i=0;i<N;i++) scanf("%ld%s%d%d%d",&a[i].xh,a[i].xm,&a[i].s1,&a[i].s2,&a[i].s3); }void out_stu(STU a[N],int p[N]){ int i;for(i=0;i<N;i++) printf("%ld %s %d %d %d\n", \a[p[i]].xh,a[p[i]].xm,a[p[i]].s1,a[p[i]].s2,a[p[i]].s3);}#define SUM(a,i) (a[i].s1+a[i].s2+a[i].s3)void Sort(STU a[N],int p[N]) /* 索引冒泡排序*/{ int i,j,t;for(i=0;i<N;i++) p[i]=i;for(i=1;i<N;i++)for(j=0;j<N-i;j++)if(SUM(a,p[j])<SUM(a,p[j+1])) { t=p[j];p[j]=p[j+1];p[j+1]=t; }}void main(){ STU a[N];int p[N];inp_stu(a);Sort(a,p);out_stu(a,p);}10.10#include "stdio.h"#include "math.h"typedef struct { int y,m,d; } DA TE;long days(DA TE *p){ long m,n=(p->y-1)*365+(p->y-1)/4-(p->y-1)/100+(p->y-1)/400;for(m=1;m<p->m-1;m++)switch(m){ case 4:case 6:case 9:case 11:n+=30;break;case 2:n+=28+(p->y%4==0&&p->y%100!=0||p->y%400==0);break;default:n+=31;}return n;}void main(){ DA TE a1,a2;long n;while(1){ printf("Input date 1(y m d):");scanf("%d%d%d",&a1.y,&a1.m,&a1.d);if(a1.y<=0) break;printf("Input date 2(y m d):");scanf("%d%d%d",&a2.y,&a2.m,&a2.d);n=labs(days(&a1)-days(&a2));printf("The difference days=%ld\n",n);}}10.11#include "stdio.h"#include "conio.h"typedef struct { int h,m,s; } TIME;void inc_time(TIME *p){ int cy=1;p->s+=cy;cy=p->s/60;p->s%=60;p->m+=cy;cy=p->m/60;p->m%=60;p->h+=cy;}void dec_time(TIME *p){ int cy=1;p->s-=cy;cy=p->s<0;p->s=(p->s%60+60)%60;p->m-=cy;cy=p->m<0;p->m=(p->m%60+60)%60;p->h-=cy;}void main(){ char ch;TIME t;printf("Input a time(h m s):");scanf("%d%d%d",&t.h,&t.m,&t.s);while(1){ ch=getche();if(ch=='+') inc_time(&t);if(ch=='-') dec_time(&t);if(ch=='Q'||ch=='q') break;printf("\r%d:%d:%d\n",t.h,t.m,t.s);}10.14#include "stdio.h" /* 本例请用C++调制*/typedef struct node { int no,quantity;struct node *next; } NodeTp; void DscIns(NodeTp *h,NodeTp *s){ NodeTp *p=h->next,*pr=h;while(p&&p->quantity>s->quantity) { pr=p;p=p->next; }pr->next=s;s->next=p;}void Out(NodeTp *h){ NodeTp *p=h->next;while(p) { printf("%d,%d\n",p->no,p->quantity);p=p->next;}}void Ers(NodeTp *h){ NodeTp *p;while(h) { p=h;h=h->next;delete p;}}void main(){ NodeTp *h,*s,*ps;int no,quantity;h=new NodeTp;h->next=NULL;while(1){ printf("Input no and quantity:");scanf("%d%d",&no,&quantity);if(no==0) break;ps=h;s=h->next;while(s&&s->no!=no) { ps=s;s=s->next; }if(s==NULL) { s=new NodeTp;s->no=no;s->quantity=quantity; } else { s->quantity+=quantity;ps->next=s->next; }DscIns(h,s);}Out(h);Ers(h);}10.15#include "stdio.h" /* 本例请用C++调试*/#define N 10typedef struct node { int data;struct node *next; } NodeTp;NodeTp *Crt(int n){ int i;NodeTp *h,*p,*last;h=new NodeTp;h->data=1;last=h;for(i=2;i<=n;i++){ p=new NodeTp;p->data=i;last->next=p;last=p;}last->next=NULL;return h;void Out(NodeTp *h){ while(h) { printf("%6d",h->data);h=h->next; }}void Ers(NodeTp *h){ NodeTp *p;while(h) { p=h;h=h->next;delete p;}}NodeTp *Chg(NodeTp *h){ NodeTp *p1,*p2,*p,*last;p1=h;if(!p1) return h;p2=h->next;if(!p2) return h;h=NULL;while(1){ p=p2->next;if(h==NULL) { h=p2;p2->next=p1;p1->next=p; }else { last->next=p2;p2->next=p1;p1->next=p; }last=p1;p1=p;if(!p1) break;p2=p->next;if(!p2) break;}return h;}void main(){ NodeTp *h=Crt(N);h=Chg(h);Out(h);printf("\n");Ers(h);}10.16#include "stdio.h"typedef struct node { int data;struct node *next; }NodeTp;void AscIns(NodeTp *h,int x) /* 带附加头结点升序链表插入结点*/ { NodeTp *pr=h,*p=h->next,*s=new NodeTp;s->data=x;while(p&&p->data<x) { pr=p;p=p->next; }pr->next=s;s->next=p;}NodeTp *Crt(){ NodeTp *h=new NodeTp;int x;h->next=NULL;printf("Input integers until input positive integers or zero:\n");while(1){ scanf("%d",&x);if(x<=0) break;AscIns(h,x);}return h;}void Out(NodeTp *h){ h=h->next;while(h) { printf("%6d",h->data);h=h->next; } }void Ers(NodeTp *h){ NodeTp *p;while(h) { p=h;h=h->next;delete p;}}void DelRep(NodeTp *h){ NodeTp *p=h->next,*pn;while(1){ pn=p->next;if(pn==NULL) break;if(pn->data==p->data) { p->next=pn->next;delete pn; }else p=pn;}}void main(){ NodeTp *h=Crt();DelRep(h);Out(h);printf("\n");Ers(h);}10.17#include "stdio.h"typedef struct node { int data;struct node *next; }NodeTp; NodeTp *Crt(){ NodeTp *h=new NodeTp,*p;int x;h->next=NULL;printf("Input integers until input positive integers:\n"); while(1){ scanf("%d",&x);if(x<=0) break;p=new NodeTp;p->data=x;p->next=h->next;h->next=p;}return h;}void Out(NodeTp *h){ h=h->next;while(h) { printf("%6d",h->data);h=h->next; } }void Ers(NodeTp *h){ NodeTp *p;while(h) { p=h;h=h->next;delete p;}}void Del(NodeTp *h){ NodeTp *pr=h,*p=h->next;while(p)if(p->data>5&&p->data<20) { pr->next=p->next;delete p;p=pr->next; }else {pr=p;p=p->next;}}void main(){ NodeTp *h=Crt();Del(h);Out(h);printf("\n");Ers(h);}10.19NodeTp *GetPre(NodeTp *p){ NodeTp *pr=p;while(pr->next!=p) { pr=pr->next; }}10.20void link(NodeTp *p){ NodeTp *pr=p,*q=p;p=p->next;while(pr!=q) { p->previous=pr;pr=p;p=p->next; }}10.27 7 3 B最初为p2指针变量分配的空间丢失第十一章11.5#include "stdio.h"void main(){ FILE *fp;int i,j,c,n,w=4;printf("Input n=");scanf("%d",&n);if(n<=0) { printf("number of rows must be greater than 0.\n");return; }fp=fopen("a2.txt","w");for(i=0;i<n;i++){ printf("%*s",w*(n-i),"");fprintf(fp,"%*s",w*(n-i),"");c=1;printf("%*d",w,c);fprintf(fp,"%*d",w,c);for(j=1;j<=i;j++) { c=c*(i-j+1)/j;printf("%*d",2*w,c);fprintf(fp,"%*d",2*w,c); } printf("\n");fprintf(fp,"\n");}fclose(fp);}11.7#include "stdio.h"void main(){ FILE *fr;int ce,cd,c;char fname[81],ch;c=ce=cd=0;printf("Input a text file name:\n");scanf("%s",fname);fr=fopen(fname,"rb");if(!fr) { printf("File %s not found.\n",fname);return; }while(1){ ch=fgetc(fr);if(feof(fr)) break;c++;if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') ce++;if(ch>'0'&&ch<='9') cd++;}fclose(fr);printf("number of letters=%d,number of digits=%d,others=%d\n",ce,cd,c-ce-cd); }11.10#include "stdio.h"#include "math.h"void main(){ FILE *fw;int m,i,c,k;fw=fopen("prime.txt","w");if(!fw) { printf("Can't create the text file prime.txt\n");return; }fprintf(fw,"%6d",2);c=1;for(m=3;m<10000;m+=2){ k=(int)sqrt(m+1);for(i=3;i<=k;i+=2) if(m%i==0) break;if(i>k) { fprintf(fw,"%6d",m);c++;if(c%10==0) fprintf(fw,"\n"); }}fclose(fw);}11.12#include "stdio.h"void main(){ FILE *fp;char ch,fname[81];printf("Input a text file name:\n");scanf("%s",fname);fp=fopen(fname,"rb+");if(!fp) { printf("File %s not found.\n",fname);return; }ch=fgetc(fp);while(!feof(fp)){ if(ch>='A'&&ch<='Z') ch+=32;fseek(fp,-1L,SEEK_CUR);fputc(ch,fp);fseek(fp,0L,SEEK_CUR);ch=fgetc(fp);}fclose(fp);}11.22 char *argv[] 3 !feof(f1) ch, f2 11.23 0 “r”或”rb”feof(fp) ch==32 11.24 添加一行字符到字符文件b.txt11.25 A C D。

第10章练习及答案 谭浩强 著 C程序设计

第10章练习及答案 谭浩强 著 C程序设计

1、有以下程序main(){int a[10]={1,2,3,4,5,6,7,8,9,10}, *p=&a[3], *q=p+2;printf("%d\n", *p +*q);}程序运行后的输出结果是(B)A)16 B)10 C)8 D)62、有以下程序:main(){char s[]={"aeiou"},*ps;ps=s; printf("%c\n",*ps+4);}程序运行后的输出结果是(B)A)a B)eC)u D)元素s[4]的地址3、有以下程序main(){char s[]="159",*p;p=s;printf("%c",*p++);printf("%c",*p++);}程序运行后的输出结果是(A)A)15 B)16 C)12 D)594、有以下程序main(){int a[3][3],*p,i;p=&a[0][0];for(i=0;i<9;i++)p[i]=i;for(i=0;i<3;i++)printf("%d ",a[1][i]);}程序运行后的输出结果是(D)A)0 1 2 B)1 2 3C)2 3 4 D)3 4 55、以下程序运行后的输出结果是abcfg #include "string.h"void fun(char *s,int p,int k){ int i;for(i=p;i<k-1;i++)s[i]=s[i+2];}main(){ char s[]="abcdefg"; fun(s,3,strlen(s));puts(s);}6、有以下程序void swap(char *x,char *y){char t;t=*x; *x=*y; *y=t;}main(){char *s1="abc",*s2="123";swap(s1,s2);printf("%s,%s\n",s1,s2);}程序执行后的输出结果是(C)A)123,abc B)abc,123C)1bc,a23 D)321,cba7、有以下程序# include <string.h>void f(char *s, char *t){ char k;k=*s; *s=*t; *t=k;s++; t--;if (*s) f(s,t);}main(){ char str[10]="abcdefg", *p ;p=str+strlen(str)/2+1;f(p, p-2);printf("%s\n",str);}程序运行后的输出结果是(B)A)abcdefg B)gfedcbaC)gbcdefa D)abedcfg8、有以下程序#include<stdio.h>void fun(char *s){while(*s){if(*s%2==0)printf(“%c”,*s);s++;} }main(){ char a[]={“good”};fun(a);printf(“\n”);}注意:字母a的ASCⅡ码值为97,程序运行后的输出结果是(A)A)d B)goC)god D)good9、有以下程序#include <stdio.h>viod f(int *p,int *q); main(){ int m=1,n=2,*r=&m;f(r,&n);printf(“%d,%d”,m,n);}void f(int *p,int *q){p=p+1;*q=*q+1;}程序运行后输出的结果是(A)A)1,3 B)2,3C)1,4 D)1,210、有以下程序#include <stdio.h>main(){ char *a[ ]={“abcd”, “ef”, “gh”, “ijk”};int i;for(i=0;i<4;i++)printf(“%c”,*a[i]);}程序运行后输出的结果是(A)A)aegi B)dfhkC)abcd D)abcdefghijk (放至教案)1、设已有定义:float x;则以下对指针变量p进行定义且赋初值的语句中正确的是( D )A)float *p=1024;B)int *p=(float x);C)float p=&x;D)float *p=&x;2、设有定义:int n1=0,n2,*p=&n2,*q=&n1;,以下赋值语句中与n2=n1;语句等价的是( A ) A)*p=*q; B)p=q;C)*p=&n1; D)p=*q;3、若在定义语句:int a,b,c,*p=&c;之后,接着执行以下选项中的语句,则能正确执行的语句是( C )A)scanf("%d",a,b,c);B)scanf("%d%d%d",a,b,c);C)scanf("%d",p);D)scanf("%d",&p);4、若有以下定义int x[10],*pt=x;则对x数组元素的正确应用是( B )A)*&x[10]B)*(x+3)C)*(pt+10)D)pt+3。

C语言程序的设计课件源程序及习题的答案第10章文件及其应用

C语言程序的设计课件源程序及习题的答案第10章文件及其应用

再试一试
问题10.8 编一程序从问 题10.5写入的文件 stdScore.dat中读取 一条记录并显示在屏 幕上,要求从键盘输 入记录的序号。
调试问题 10.8 程序, 观察运行结

想一想
对问题10.7中找到的记录进行修改,修改完以后再写入原来的 文件中,应如何修改程序?请根据提示写出完整程序。
以mode 方式打开文件chFileName。其中,参数 chFileName 是将要读写文件的文件名,mode为文件 的操作方式。若文件打开成功,返回一个文件指针,若 打开失败,则返回空值NULL,NULL在stdio.h中被定 义为0。文件操作方式mode是一个整数,其取值及含 义如表10-1所示。
量chYesNo中; 若chYesNo= =’y’,则表示要修改记录,需要完成下面操作。
从键盘输入一条学生记录数据并存放在结构体变量stTemp中; 定位文件指针到第iRecordID条记录的开始处; 将结构体变量stTemp中写入文件; 若chYesNo= =’n’,则不对记录进行修改。
练一练
问题:编一程序在文件stdScore.dat (问题10.5已写入数据)的末尾添加一 条新记录,然后将文件中所有记录显示 出来。这条新记录为学生Lisa的成绩, 具体如下:
10.2 二进制文件的读写
对于二进制文件,以缓冲文件系统方式读写 文件的步骤同样为以下四个步骤: ①定义文件指针; ②打开文件; ③读写文件; ④关闭文件。
其中定义文件指针、打开文件和关闭文件的 操作在上一节中已介绍过,这里主要学习用 来读写二进制文件的两个函数fread和fwrite。
学一学
【流程图】与问题10.3的流程图相似,请参考图10-4 调试问题10.4程序, 观察运行结果

c语言 大学 第10章习题答案

c语言 大学 第10章习题答案
fclose(fp);
}
A.读文件中的字符B.统计文件中的字符数并输出C.打开文件D.关闭文件
B.用“R”方式也可以打开文件
C.用“w”方式打开的文件只能用于向文件写数据,且该文件可以不存在
D.用“a”方式可以打开不存在的文件
4.fscanf函数的正确调用形式是____D_____
A. fscanf(fp,格式字符串,输出表列)
B. fscanf(格式字符串,输出表列,fp);
C. fscanf(格式字符串,文件指针,输出表列);
D. fscanf(文件指针,格式字符串,输入表列);
5.函数调用语句:fseek(fp,-20L,2);的含义是____C____
A.将文件位置指针移到距离文件头20个字节处
B.将文件位置指针从当前位置向后移动20个字节
C.将文件位置指针从文件末尾处后退20个字节
D.将文件位置指针移到离当前位置20个字节处
第10章习题答案
1.系统的标准输入文件是指____A____
A.键盘B.显示器C.软盘D.硬盘
2.若执行fopen函数时发生错误,则函C. 1 D. EOF
3、在C中,下面对文件的叙述正确的是___C_____
A.用“r”方式打开的文件只能向文件写数据
fp1指向的文件中,正确的语句是____C____
A. for(i=0;i<80;i++) fputc(a[i],fp1);
B. for(i=0;i<10;i++) fputc(&a[i],fp1);
C.for(i=0;i<10;i++) fwrite(&a[i],8,1,fp1);

C语言程序设计(第3版)第10章补充习题及答案

C语言程序设计(第3版)第10章补充习题及答案

第10章补充习题及答案习题10.1 选择题(1)当已存在一个abc.txt文件时,执行函数fopen("abc.txt","r+")的功能是。

A.打开abc.txt文件,清除原有的内容B.打开abc.txt文件,只能写入新的内容C.打开abc.txt文件,只能读取原有内容D.打开abc.txt文件,可以读取和写入新的内容(2)若用fopen()函数打开一个已存在的文本文件,保留该文件原有内容,且可以读,可以写。

则文件打开模式是。

A."ab+" B."w+" C."a+" D."a"(3)以下不能将文件指针重新移到文件开头位置的函数是。

A.rewind(fp); B.fseek(fp,0,SEEK_SET);C.fseek(fp,-(long)ftell(fp),SEEK_CUR); D.fseek(fp,0,SEEK_END);(4)若用fopen()函数打开一个新二进制文件,该文件可以读也可以写,则文件的打开模式为 B 。

A."ab+" B."wb+" C."rb+" D."a+"(5)fread(buffer,64,2,fp)的功能是。

A.从fp所指的文件中读取64并存入buffer中B.从fp所指的文件中读取64和2并存入buffer中C.从fp所指的文件中读取64个字节的数据并存入buffer中D.从fp所指的文件中读取2个64字节的数据并存入buffer中(6)以下程序的功能是。

提示:PRN是打印设备。

#include <stdio.h>void main(){ FILE * fp;char chStr[]="HELLO";fp=fopen("PRN","w");fputs(chStr,fp);fclose(fp);}A.在屏幕显示HELLO B.把HELLO存入PRN文件中C.在打印机上打印出HELLO D.以上都不对10.2 填空题(1)使用fopen("abc","r+")打开文件时,若"abc"文件不存在,则返回。

C程序设计(第三版)习题答案(10章)谭浩强著

C程序设计(第三版)习题答案(10章)谭浩强著
*(p+i)=i+1; i=k=m=0;
while(m<n-1) {if(*(p+i)!=0)k++; if(k==3) {*(p+i)=0; k=0; m++; } i++; if(i==n)i=0; }
while(*p==0)p++; printf("%d",*p); } 10.6 main() {int len; char *str[20]; scanf("%s",str); len=length(str); printf("\nlen=%d\n",len); } length(p) char *p; {int n=0;
++sle; else if(*p>='A'&&*p<='Z')
++cle; else if(*p==' ')
++wsp; else if(*p>='0'&&*p<='9')
++di;
பைடு நூலகம்
else ++ot;
p++; } printf("sle=%d,cle=%d,wsp=%d,di=%d,ot=%d\n",sle,cle,wsp,di,ot); } 10.9 main() {int a[3][3],*p,i; for(i=0;i<3;i++) scanf("%d,%d,%d",a[i][0],a[i][1],a[i][2]); p=a; move(p); for(i=0;i<3;i++) printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]); } move(pointer) int *pointer; {int i,j,t; for(i=0;i<2;i++) for(j=i+1;j<3;j++) {t=*(pointer+3*i+j); *(pointer+3*i+j)=*(pointer+3*j+i); *(pointer+3*j+i)=t;

C++语言程序设计第十章、十一章答案

C++语言程序设计第十章、十一章答案

第十章和十一章习题作业10-5解:#include<stdio.h>#include<stdlib.h>typedef struct list{int data;struct list *next;}linklist;int main(){int n,m,k,i=1;linklist head,*p,*q,*l;printf("enter n:\n");scanf("%d",&n);printf("enter m:\n",&m);scanf("%d",&m);printf("enter k:\n",&k);scanf("%d",&k);l=&head;p=l;for(;i<=n;i++){q=(linklist *)malloc(sizeof(head));p->next=q;q->data=i;p=q;}p->next=l->next; /*创建循环表*/p=l->next;for(i=1;i<m;i++) /*找到m的位置*/p=p->next;while(p->data!=p->next->data){for(i=1;i<k-1;i++)p=p->next;q=p->next;printf("%3d",q->data);p->next=q->next;p=q->next; /*p指向第k+1个人的位置*/free(q); /*释放空间*/}printf("%3d\n",p->data);free(l);free(p);}10-12解:#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main(){int r[100],s[10],i;for (i=0;i<10;++i){s[i]=0;}srand(time(NULL));//初始化随机数种子,产生不同随机数序列 for (i=0;i<100;++i){r[i]=rand()%10;//产生一个随机数++s[r[i]];}for (i=0;i<10;++i){cout<<i<<' '<<s[i]<<endl;}}11—7解://1 保存现在的格式化参数设置,以便将来恢复这些设置;//2 把对齐方式由缺省的右对齐改为左对齐;//3 把输出域的宽度由缺省值0改为10;//4 消除对齐方式的设置;//5 更改浮点数的显示设置;//6 恢复原来的格式化参数设置。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C程序设计[谭浩强](第三版)课后答案第十章指针2010-06-25 14:04第十章指针10.1main(){int n1,n2,n3;int *p1,*p2,*p3;scanf("%d,%d,%d",&n1,&n2,&n3);p1=&n1;p2=&n2;p3=&n3;if(n1>n2)swap(p1,p2);if(n1>n3)swap(p1,p3);if(n2>n3)swap(p2,p3);printf("%d,%d,%d\n",n1,n2,n3);}swap(p1,p2)int *p1,*p2;{int p;p=*p1;*p1=*p2;*p2=p;}10.2main(){char *str1[20],*str2[20],*str3[20];char swap();scanf("%s",str1);scanf("%s",str2);scanf("%s",str3);if(strcmp(str1,str2)>0)swap(str1,str2);if(strcmp(str1,str3)>0)swap(str1,str3);if(strcmp(str2,str3)>0)swap(str2,str3);printf("%s\n%s\n%s\n",str1,str2,str3);}char swap(p1,p2)char *p1,*p2;{char *p[20];strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);}10.3main(){int number[10];input(number);max_min_value(number); output(number);}input(number)int number[10];{int i;for(i=0;i<10;i++)scanf("%d",&number);}max_min_value(number)int number[10];{int *max,*min;int *p,*end;end=number+10;max=min=number;for(p=number+1;p<end;p++) if(*p>*max)max=p;else if(*p<*min)min=p;*p=number[0];number[0]=*min;*min=*p;*p=number[9];number[9]=*max;*max=*p;return;}output(number)int number[10];{int *p;for(p=number;p<number+9;p++) printf("%d,",*p);printf("%d\n",*p);}10.4main(){int number[20],n,m,i;scanf("%d",&n);scanf("%d",&m);for(i=0;i<n:i++)scanf("%d",&number); move(number,n,m);for(i=0;i<n;i++)printf("%8d",number);}move(array,n,m)int array[20],n,m;{int *p,end;end=*(array+n-1);for(p=array+n-1;p>array;p--) *p=*(p-1);*array=end;m--;if(m>0)move(array,n,m);}10.5#define nmax 50main(){int i,k,m,n,num[nmax],*p; scanf("%d",&n);p=num;for(i=0;i<n;i++)*(p+i)=i+1;i=k=m=0;while(m<n-1){if(*(p+i)!=0)k++;if(k==3){*(p+i)=0;k=0;m++;}i++;if(i==n)i=0;}while(*p==0)p++;printf("%d",*p);}10.6main(){int len;char *str[20];scanf("%s",str);len=length(str);printf("\nlen=%d\n",len);}length(p)char *p;{int n=0;while(*p!='\0')return(n);}10.7main(){int m;char *str1[20],*str2[20];scanf("%s",str1);scanf("%d",&m);if(strlen(str1)<m)printf("error");else{copystr(str1,str2,m);printf("%s",str2);}}copystr(p1,p2,m)char *p1,*p2;int m;{int n=0;while(n<m-1){n++;p1++;}while(*p1!='\0'){*p2=*p1;p1++;p2++;}*p2='\0';}10.8#include"stdio.h"main(){int cle=0,sle=0,di=0,wsp=0,ot=0,i; char *p,s[20];for(i=0;i<20;i++)s=0;i=0;while((s=getchar())!='\n')i++;p=s;while(*p!='\n'){if(*p>='a'&&*p<='z')++sle;else if(*p>='A'&&*p<='Z')++cle;else if(*p==' ')else if(*p>='0'&&*p<='9')++di;else++ot;p++;}printf("sle=%d,cle=%d,wsp=%d,di=%d,ot=%d\n",sle,cle,wsp,di,ot); }10.9main(){int a[3][3],*p,i;for(i=0;i<3;i++)scanf("%d,%d,%d",a[0],a[1],a[2]);p=a;move(p);for(i=0;i<3;i++)printf("%d %d %d\n",a[0],a[1],a[2]);}move(pointer)int *pointer;{int i,j,t;for(i=0;i<2;i++)for(j=i+1;j<3;j++){t=*(pointer+3*i+j);*(pointer+3*i+j)=*(pointer+3*j+i);*(pointer+3*j+i)=t;}}10.10main(){int a[5][5],*p,i,j;for(i=0;i<5;i++)for(j=0;j<5;j++)scanf("%d",&a[j]);p=a;change(p);for(i=0;i<5;i++){printf("\n");for(j=0;j<5;j++)printf("%8d",a[j]);}}change(p)int *p;{int i,j,change;int *pmax,*pmin;pmax=p;pmin=p;for(i=0;i<5;i++)for(j=0;j<5;j++){if(*pmax<*(p+5*i+j))pmax=p+5*i+j;if(*pmin>*(p+5*i+j))pmin=p+5*i+j;}change=*(p+12);*(p+12)=*pmax;*pmax=change;change=*p;*p=*pmin;*pmin=change;pmin=p+1;for(i=0;i<5;i++)for(j=0;j<5;j++)if(((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j; change=*(p+4);*(p+4)=*pmin;*pmin=change;pmin=p+1;for(i=0;i<5;i++)for(j=0;j<5;j++)if(((p+5*i+j)!=(p+4))&&((p+5*i+j)!=p)&&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;change=*(p+20);*(p+20)=*pmin;*pmin=change;pmin=p+1;for(i=0;i<5;i++)for(j=0;j<5;j++)if(((p+5*i+j)!=p)&&((p+5*i+j)!=(p+4))&&((p+5*i+j)!=(p+20)) &&(*pmin>*(p+5*i+j)))pmin=p+5*i+j;change=*(p+24);*(p+24)=*pmin;*pmin=change;}10.11main(){int i;char *p,str[10][10];for(i=0;i<10;i++)scanf("%s",str);p=str;sort(p);for(i=0;i<10;i++)printf("%s\n",str);}sort(p)char *p;{int i,j;char s[10],*smax,*smin;for(i=0;i<10;i++){smax=p+10*i;for(j=i+1;j<10;j++){smin=p+10*j;if(strcmp(smax,smin)>0){strcpy(s,smin);strcpy(smin,smax);strcpy(smax,s);}}}}10.12#define MAX 20main(){int i;char *pstr[10],str[10][MAX];for(i=0;i<10;i++)pstr=str;for(i=0;i<10;i++)scanf("%s",pstr);sort(pstr);for(i=0;i<10;i++)printf("%s\n",pstr);}sort(pstr)char *pstr[10];{int i,j;char *p;for(i=0;i<10;i++){for(j=i+1;j<10;j++){if(strcmp(*(pstr+i),*(pstr+j))>0){p=*(pstr+i);*(pstr+i)=*(pstr+j);*(pstr+j)=p;}}}}10.13#include"math.h"main(){int n=20;float a,b,a1,b1,a2,b2,c,(*p)(),jiff(); scanf("%f,%f",&a,&b);scanf("%f,%f",&a1,&b1);scanf("%f,%f",&a2,&b2);p=sin;c=jiff(a,b,n,p);printf("sin=%f\n",c);p=cos;c=jiff(a1,b1,n,p);printf("cos=%f\n",c);p=exp;c=jiff(a2,b2,n,p);printf("exp=%f\n",c);}float jiff(a,b,n,p)float a,b,(*p)();int n;{int i;float x,f,h,area;h=(b-a)/n;x=a;area=0;for(i=1;i<=n;i++){x=x+h;area=area+(*p)(x)*h;}return(area);}10.14main(){int i,n,num[20];char *p;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&num);p=num;sort(p,n);for(i=0;i<n;i++)printf("%8d",num);}sort(p,m)char *p;int m;{int i;char change,*p1,*p2;for(i=0;i<m/2;i++){p1=p+i;p2=p+(m-1-i);change=*p1;*p1=*p2;*p2=change;}}10.15main(){int i,j,*pnum,num[4];float score[4][5],aver[4],*psco,*pave; char course[5][10],*pcou;pcou=course[0];for(i=0;i<5;i++)scanf("%s",pcou+10*i);printf("number");for(i=0;i<5;i++)printf(",%s",pcou+10*i);printf("\n");psco=score;pnum=num;for(i=0;i<4;i++){scanf("%d",pnum+i);for(j=0;j<5;j++)scanf(",%f",psco+5*i+j);}pave=aver;printf("\n");avsco(psco,pave);avcour1(pcou,psco);printf("\n");fali2(pcou,pnum,psco,pave);printf("\n");good(pcou,pnum,psco,pave);}avsco(psco,pave)float *psco,*pave;{int i,j;float sum,average;for(i=0;i<4;i++){sum=0;for(j=0;j<5;j+)sum+=(*(psco+5*i+j));average=sum/5;*(pave+i)=average;}}avcour1(pcou,psco)char *pcou;float *psco;{int i;float sum,average1;sum=0;for(i=0;i<4;i++)sum+=(*(psco+5*i))average1=sum/4;printf("%s %5.2f\n",pcou,average1); }fali2(pcou,pnum,psco,pave)char *pcou;int *pnum;float *psco,*pave;{int i,j,k,label;printf("\nnumber\n");for(i=0;i<5;i++)printf("%-8s",pcou+10*i);printf("\naverage\n");for(i=0;i<4;i++){label=0;for(j=0;j<5;j++)if(*(psco+5*i+j)<60.0)label++;if(label>=2){printf("%-8d",*(pnum+i));for(k=0;k<5;k++)printf("%-8.2f",*(psco+5*i+k));printf("%-8.2f",*(pave+i));}}}good(pcou,pnum,psco,pave)char *pcou;int *pnum;float *psco,*pave;{int i,j,k,label;printf("number");for(i=0;i<5;i++)printf("%-8s",pcou+10*i);printf("average");for(i=0;i<4;i++){label=0;for(j=0;j<5;j++)if(*(psco+5*i+j)>=85.0)label++;if((label>=5)||(*(pave+i)>=90)){printf("%-8d",*(pnum+i));for(k=0;k<5;k++)printf("%-8.2f",*(psco+5*i+k));printf("%-8.2f",*(pave+i));}}}10.16#include"stdio.h"main(){char str[50],*pstr;int i,j,k,m,e10,digit,ndigit,a[10],*pa;gets(str);pstr=str;pa=a;ndigit=0;i=j=0;while(*(pstr+i)!='\0'){if((*(pstr+i)>='0')&&(*(pstr+i)<='9')) j++;else{if(j>0){digit=*(pstr+i-1)-48;k=1;while(k<j){e10=1;for(m=1;m<=k;m++)e10=e10*10;digit+=(*(pstr+i-1-k)-48)*e10;k++;}*pa=digit;ndigit++;pa++;j=0;}}i++;}if(j>0){digit=*(pstr+i-1)-48;k=1;while(k<j){e10=1;for(m=1;m<=k;m++)e10=e10*10;digit+=(*(pstr+i-1-k)-48)*e10;k++;}*pa=digit;ndigit++;j=0;}printf("ndigit=%d\n",ndigit);j=0;pa=a;for(j=0;j<ndigit;j++)printf("%d",*(pa+j));}10.17main(){int m;char str1[20],str2[20],*p1,*p2;scanf("%s",str1);scanf("%s",str2);p1=str1;p2=str2;m=strcmp(p1,p2);printf("%d\n",m);}strcmp(p1,p2)char *p1,*p2;{int i=0;while(*(p1+i)==*(p2+i))if(*(p+i++)=='\0')return(0);return(*(p1+i)-*(p2+i));}10.18main(){static char *mname[13]={"illeagl","January","February","March", "April","May","June","July","August","September","October", "November","December"};int n;scanf("%d",&n);if((n>=1)&&(n<=12))printf("%s\n",*(mname+n));elseprintf("error");}10.20main(){int i;char **p,*pstr[5],str[5][10];for(i=0;i<5;i++)pstr=str;for(i=0;i<5;i++)scanf("%s",pstr);p=pstr;sort(p);for(i=0;i<5;i++)printf("%s\n",pstr);}sort(p)char **P;{int i,j;char *pchange;for(i=0;i<5;i++){for(j=i+1;j<5;j++){if(strcmp(*(p+i),*(p+j))>0){pchange=*(p+i);*(p+i)=*(p+j);*(p+j)=pchange;}}}}10.21main(){int i,n,digit[20],**p,*pstr[20];scanf("%d",&n);for(i=0;i<n;i++)pstr=&digit;for(i=0;i<n;i++)scanf("%d",pstr);p=pstr;sort(p,n);for(i=0;i<n;i++)printf("%d ",*pstr);}sort(p,n)int **p,n;{int i,j,*pchange;for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(**(p+i)>**(p+j)){pchange=*(p+i);*(p+i)=*(p+j);*(p+j)=pchange;}}}}第十一章结构体与共用体11.1struct{int year;int month;int day;}date;main(){int days;scanf("%d,%d,%d",&date.year,&date.month,&date.day); switch(date.month){case 1:days=date.day;break;case 2:days=date.day+31;break;case 3:days=date.day+59;break;case 4:days=date.day+90;break;case 5:days=date.day+120;break;case 6:days=date.day+151;break;case 7:days=date.day+181;break;case 8:days=date.day+212;break;case 9:days=date.day+243;break;case 10:days=date.day+273;break;case 11:days=date.day+304;break;case 12:days=date.day+334;break;}if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&date.month>=3)days+=1;printf("days=%d\n",days);}11.2struct dt{int year;int month;int day;}date;main(){scanf("%d,%d,%d",&date.year,&date.month,&date.day); printf("\n%d\n",days(date.year,date.month,date.day));}days(year,month,day)int year,month,day;{int daysum=0,i;static int daytab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31} for(i=1;i<month;i++)daysum+=daytab;daysum+=day;if((year%4==0&&year%100!=0||year%400==0)&&month>=3) daysum+=1;return(daysum);}11.311.4#define N 5struct student{char num[6];char name[8];int score[4];}stu[N];main(){input(stu);print(stu);}input(stu)struct student stu[];{int i,j;for(i=0;i<N;i++){printf("number");scanf("%s",stu.num);printf("name");scanf("%s",);for(j=0;j<3;j++){printf("\nscore\n");scanf("%d",&stu.score[j]);}printf("\n");}}print(stu)struct student stu[];{int i,j;printf("\nnumber name score1 score2 score3 \n"); for(i=0;i<N;i++){printf("%8s%10s",stu.num,);for(j=0;j<3;j++)printf("%7d",stu.score[j]);printf("\n");}}11.5struct student{char num[6];char name[8];int score[4];float avr;}stu[5];main(){int i,j,max,maxi,sum;float average;for(i=0;i<5;i++){printf("number");scanf("%s",stu.num);printf("name");scanf("%s",);for(j=0;j<3;j++){printf("\nscore\n");scanf("%d",&stu.score[j]);}}average=0;max=0;maxi=0;for(i=0;i<5;i++){sum=0;for(j=0;j<3;j++)sum+=stu.score[j];stu.avr=sum/3.0;average+=stu.avr;if(sum>max){max=sum;maxi=i;}}average/=5;printf("number name score1 score2 score3 average\n"); for(i=0;i<5;i++){printf("%8s%10s",stu.num,);for(j=0;j<3;j++)printf("%7d",stu.score[j]);printf("%6.2f\n",stu.avr);}printf("average=%5.2f\n",average);printf("The best student is %s,sum=%d\n",stu[maxi].name,max); }。

相关文档
最新文档