华中科技大学实用标准C语言程序设计及指导应用习题问题详解

合集下载

标准C语言程序设计和应用——华中科技大学出版社第三章课后题

标准C语言程序设计和应用——华中科技大学出版社第三章课后题

1.比大小#include<stdio.h>main(){int a,b;int max;printf("Please enter 2 numbers:\n");scanf("%d%d",&a,&b);max=a>b?a:b;printf("The bigger one is %d",max);}2.三个整数比大小#include<stdio.h>main(){int a,b,c;int max,mid;printf("Please enter 3 numbers:\n");scanf("%d%d%d",&a,&b,&c);mid=a>b?a:b;max=mid>c?mid:c;printf("The biggest one is %d",max); }3.累加#include<stdio.h>main(){int i;int sum=0;for(i=1;i<101;i++){sum+=i;}printf("The answer is %d",sum);}4.计算通项公式中的每一项#include<stdio.h>main(){int an_2=10,an_1=-3;int an;int i,n;printf("请输入需要的项的个数:\n");scanf("%d",&n);printf("第1项:10\n");printf("第2项:-3\n");if(n<=2){printf("Error!");}else{for(i=3;i<=n;i++){an=3*an_1+an_2;an_2=an_1;an_1=an;printf("第%d项:\t%d\n",i,an);}}}5.判断奇偶数:#include<stdio.h>main(){int a;printf("Please enter a number:\n");scanf("%d",&a);if(a%2==0){printf("%d is an Even number!",a);}else{printf("%d is an Odd number!",a);}}6.求前n项和#include<stdio.h>main(){int an=8,bn=1,sum=0;int n;int i;printf("请输入你需要多少项的和:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("第%d项是%d\n",i,an);sum+=an;bn+=3;an+=bn;}printf("\n前%d项的和为%d",n,sum);}7.求分段函数的解:#include<stdio.h>main(){float x,y;printf("Please enter a number:\n");scanf("%f",&x);if(x<1.0){y=x;}else if((x>=1)&&(x<10)){y=2*x-1;}else{y=3*x-11;}printf("y=%.2f",y);}8.给出一个不多于5位的数,求出其位数,并逆序打印:#include<stdio.h>main(){int in,out,n;int wei=1;int a,b,c,d,e;int i;printf("Please enter a number:\n");scanf("%d",&in);n=in;if(a<99999){for(i=0;i<5;i++){n/=10;if(n>0){wei++;。

华科2013年C语言程序设计试卷-A卷及参考答案(正式用卷)

华科2013年C语言程序设计试卷-A卷及参考答案(正式用卷)

计算机学院2012-2013学年第2学期C语言程序设计考试试卷A卷(或B卷): A卷闭卷(或开卷):闭卷考试时间:2013年 7 月 5 日专业班级学号学生姓名题号一二三四五六七总分核对人题分10 10 10 10 20 20 20 100得分注意:试卷后面附有运算符的优先级和结合性表。

一、单项选择题(在下面每小题的A、B、C、D四个选项中,只有一个选项得分评卷人是正确的,请选择正确的选项并填写到括号内,选择正确得分。

本大题共10小题,每小题1分,共10分。

)1、以下为正确的标识符是【】A、forB、u.3C、3cD、DO2、以下为合法的整型常量是【】A、-1UB、01111001BC、ffffD、01383、下列数据中属于字符串常量的是【】A、'a'B、"a"b"c"C、'123'D、"a\"b\"c"4、表达式-a+++b中记号(token)的个数是【】A、3个B、4个C、5个D、6个5、 -3的16位补码是【】A、0xfffdB、0x1011C、0x7ffdD、0x00036、以下为正确的转义字符是【】A、'\'B、'\\'C、'\0x12'D、'\0f1'7、设有声明:enum {U,V,W=0,X,Y=0,Z} a; 则值为1的枚举常量的个数是【】A、1个B、2个C、3个D、4个8、设有声明: 【】union U{ long a; short b; char c; char s[20];} v={0x01020304},*p=&v;则下列选项正确的是A、printf("%d\n",sizeof(v))输出4B、printf("%d\n",p->s[0])输出4C、printf("%d\n",v.c)输出1D、printf("%x\n",v.b)输出1029、设有声明和语句:FILE *fp; fp=fopen("c:\\abc.dat","r+"); 【】则下列选项正确的是A、打开文件的文件名为\abc.datB、打开的是二进制文件C、打开的是文本文件D、打开的文件只能进行读操作10、设有如下代码片段:【】int a[]={1,2,3,4,5},i;char *p=(char *)a;for(i=0;i<3;i++)printf("%d ",*((p+=sizeof(int))-sizeof(int)));printf("\n");则该代码片段的输出结果是A、1 2 3 4 5B、1 2 3C、1 1 1D、2 3 4二、 多项选择题(下面每小题的A 、B 、C 、D 备选项中,有两个或两个以上的选项是正确的,请选择正确的选项并填写到括号内。

华中科技大学-C语言程序设计_上机2019

华中科技大学-C语言程序设计_上机2019
• 课后习题 • 上机指导书
华中科技大学信息学院平台课—C语言程序设计
8
第五次上机
• 程序编写 ➢ 输入 n 个整数( n<10),排序后输出。排序的原则由函数 的一个参数决定,参数值为 1,按递减顺序排序,否则按递 增顺序排序。 ➢ 课后习题15
➢ 插入排序
• 课后习题 • 上机指导书
华中科技大学信息学院平台课—C语言程序设计
7
第四次上机
• 1. 已知五位数a2b3c能被23整除,编程求此五位数。 • 2. 编写函数GetMaxMin,求3个整形数据的最大值和最小值,
main函数完成输入3个整数值,调用所编函数计算最大值和最小 值,在main函数中输出最大和最小值。 • 3. 掌握程序调试方法,包括断点、单步、观察变量。(验证第四 章一(1-3))
➢ 输入一个华氏温度,要求输出摄氏温度。公式为c=5(F-32)/9
。取2位小数。
• 课后习题
➢ 五、程序3
➢ 表达式的值
• 上机指导书
华中科技大学信息学院平台课—C语言程序设计
6
第三次上机
• 程序编写 ➢ 第3章 二 4、6、8、12、14
• 课后习题ቤተ መጻሕፍቲ ባይዱ
• 上机指导书
华中科技大学信息学院平台课—C语言程序设计
华中科技大学信息学院平台课—C语言程序设计
3
上机要求
• 教学方式上机需要实验报告(上机前准备好
),给出测试内容(包括定义程序目标、设
计程序),写出代码
• 上机调试,记录出现问题,给出解释
• 用不同颜色笔修改上机报告中的程序
• 交上机报告
华中科技大学信息学院平台课—C语言程序设计
4
第一次上机

华中科技大学C++程序题集合1

华中科技大学C++程序题集合1

HUST C++程序设计题答案集1 致华科的小学弟小学妹,这篇文档是学长我辛辛苦苦刷到一百分的程序题(各种单枪匹马深夜作战),留着吧,将来写到不会的时候拿出来查查(具体方法:Ctrl + H,输入关键字查询,会有你想要的答案。

)当然,这里面的程序仅作参考,如果你有更好的想法,呃,就用你自己的吧。

祝学业有成。

你们的苦逼的学长XX/*------------------------------------------------------- 【程序设计】--------------------------------------------------------- 题目:定义一个日期类Date,包括年、月、日三个数据成员,以及一个判断闰年的成员函数输出结果见图:样张.JPG-------------------------------------------------------*/ #include <iostream>using namespace std;class Data {public:Data(){};Data(int,int,int);boolLeapYear();void Print();private:int year;int month;int day;};/**********Program**********/bool Data::LeapYear(){if((year%4==0&&year%100!=0)||(year%400==0))return 1;else return 0;}Data::Data(inty,intm,int d){year=y;month=m;day=d;}/********** End **********/void Data::Print(){cout<<year<<"/"<<month<<"/"<<day;if(LeapYear())cout<<" is a leap year"<<endl;elsecout<<" is not a leap year"<<endl;}void main(){Data d1(2012,8,9),d2(2009,12,8);d1.Print();d2.Print();}/*------------------------------------------------------- 【程序设计】---------------------------------------------------------题目:设计一个点类Point,再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形的对角顶点,并可以输出4个坐标值和面积。

华中科技大学C语言实验报告

华中科技大学C语言实验报告

课程实验报告课程名称: C语言程序设计专业班级: CS1409学号: U201414813姓名:唐礼威指导教师:吴海报告日期: 2015年6月24日计算机科学与技术学院目录1 第一次实验 (1)1.1 实验目的 (1)1.2 实验内容 (1)1.3 实验小结 (10)2第二次实验 (11)2.1 实验目的 (11)2.2 实验内容 (11)2.3 实验小结 (16)3第三次实验 (16)3.1 实验目的 (16)3.2 实验内容 (16)3.3 实验小结 (25)4第四次实验 (25)4.1 实验目的 (25)4.2 实验内容 (25)4.3 实验小结 (30)5第五次实验 (31)5.1 实验目的 (31)5.2 实验内容 (31)5.3 实验小结 (37)6 第六次实验 (37)6.1 实验目的 (37)6.2 实验内容 (37)6.3 实验小结 (44)7第七次实验 (44)7.1 实验目的 (44)7.2 实验内容 (44)7.3 实验小结 (55)8第八次实验 (55)8.1 实验目的 (55)8.2 实验内容 (55)8.3 实验小结 (63)9 实验总结 (63)1 第一次实验1.1实验目的(1)熟练掌握各种运算符的运算功能,操作数的类型,运算结果的类型及运算过程中类型转换,重点是C语言特有的运算符,例如位运算符,问号运算符,逗号运算符等;熟记运算符的优先级和结合性。

(2)掌握if,while,for循环语句的用法与含义。

(3)掌握简单C程序(顺序结构程序)的编写方法。

(4)掌握getchar, putchar, scanf 和printf 函数的用法(5)学会简单使用函数调用1.2 实验内容(一)使用两个变量实现两数交换【部分程序代码:】#include <stdio.h> //预处理void main() //主函数{int a,b; //定义两变量a=a+b;b=a-b;a=a-b; //精华部分,实现交换}要点说明:要先用一个数储存两个数的值,于是将a+b的值赋给a,从中减去b即获得a的值,赋给b。

C语言程序设计实用教程 参考答案

C语言程序设计实用教程 参考答案

C语言程序设计实用教程参考答案C语言程序设计是计算机科学中最基础和重要的一门课程,对于计算机专业的学生来说至关重要。

本教程将为初学者提供一份实用的参考答案,帮助他们学习和理解C语言的程序设计。

1. 概述C语言是一种通用的高级编程语言,最初由贝尔实验室的Dennis Ritchie开发。

它在计算机科学领域具有广泛的应用,特别在系统开发和嵌入式系统中非常常见。

2. 程序结构C语言的程序结构主要包含预处理指令、函数、语句和注释。

预处理指令通过“#”符号开头,用于引入头文件和宏定义。

函数是C语言中的基本模块,用于封装和组织代码。

语句是具体的执行指令,用于实现特定的功能。

3. 常用数据类型C语言提供了多种数据类型,如整数、浮点数、字符和字符串等。

其中整数可以分为有符号整数和无符号整数。

浮点数用于处理带有小数部分的数值。

字符类型用于表示单个字符,字符串类型用于表示多个字符的序列。

4. 控制结构控制结构是C语言中的重要概念,用于控制程序的流程和执行顺序。

常见的控制结构包括条件语句(if-else语句和switch语句)和循环语句(while循环、for循环和do-while循环)。

5. 数组和指针数组是一种用于存储相同类型数据的集合。

C语言中的数组可以一维或多维。

指针是一种特殊的变量类型,用于存储内存地址。

通过指针可以实现对其他变量的间接操作。

6. 函数函数是C语言中的重要概念,用于将代码封装成可复用的模块。

C语言提供了多种函数类型和参数传递方式。

函数的定义包括函数名、返回类型、参数列表和函数体。

7. 文件操作C语言提供了对文件的操作功能,包括文件的打开、读取、写入和关闭等。

文件操作需要使用标准库函数和文件指针来实现。

8. 动态内存分配C语言中可以通过动态内存分配函数(如malloc和free)来动态分配和释放内存。

动态内存分配可以提高程序的灵活性和效率。

9. 结构体和联合体结构体是一种自定义数据类型,用于封装不同类型的数据成员。

华中科技大学标准C语言程序设计及应用习题答案

华中科技大学标准C语言程序设计及应用习题答案

第二章一.选择题1.C2.B D3.A4.A5. D6.C7.D8.C9.A 10.D11.B 12.D 13.C 14.D 15.A16.B 17.A 18.B 100011 001111二.判断题1.错2.错3.错4.错三.填空题1. B 662. n1=%d\nn2=%d\n3. 0四.计算1(1)x|y = 0x002f(2)x^y = 0x0026(3)x&y = 0x0009;(4)~x+~y = 0xffc6(5)x<<=3 0x0068(6)y>>=4 0x00022(1)6(2)50(3)1(4)–16(5)1(6)203(1)0(2)1(3)1(4)4(5)8(6)14(1)12(2)0(3)1(4)27(5)1(6)6(7)24(8)27(9)–295(1)0(2)1(3)1(4)–3(5)2五.程序分析题程序1b=20 a=3600程序2第三章一.填空题1.s=62.963.(1) scanf("%c",&c);(2) c-32 更好的答案:c-('a'-'A')2.1main(){int a,b;printf("please input a & b:");scanf("%d%d",&a,&b);printf("\nmax is %d\n",(a>b)?a:b);}2.2int max(int x,int y);main(){int a,b,c,max;printf("please input a & b & c:");scanf("%d%d%d",&a,&b,&c);max=a>b?a:b;max=c>max?c:max;printf("\nmax is %d\n",max);2.3main(){int i=0,sum=0;while(i<=100){sum+=i;i++;}printf("1+2+3+......+100=%d\n",sum);}2.4main(){int i;int a=10,b=-3;int c;printf("%6d%6d",a,b);for(i=2;i<10;i++){c=3*b+a;printf("%6d",c);a=b;b=c;}printf("\n");}2.5main(){int i;while(1){printf("please input a data(0:exit):");scanf("%d",&i);if(i==0)break;if(i%2==0)printf("the data %d is a even number.\n",i);elseprintf("the data %d is a odd number.\n",i); }}#include <stdio.h>main(){int i;int a=8,b=1;int sum=0;for(i=0;i<10;i++){b+=3;sum += a;a+=b;printf("a%8d b:%8d\n",a,b);}printf("The Sum Of Is:%d\n",sum);}2.7#include <stdio.h>main(){float x,y;printf("please input x:");scanf("%f",&x);if(x<1.0)y=x;else if(x<10)y=2*x-1;elsey=3*x-11;printf("y=%f\n",y);}2.8#include <stdio.h>main(){long a,i,b,a1;while(1){printf("please input data(1-99999):");scanf("%ld",&a);printf("a:%ld\n",a);if(a<=0||a>=100000)break;i=0;a1=0;while(a!=0){b=a%10;printf("%8d",b);a/=10;i++;a1=a1*10+b;}printf("\n i:%ld a1:%ld\n",i,a1);}}2.9#include <stdio.h>#include <time.h>#include <stdlib.h>main(){int a,b,i,k=0;randomize();a=random(1001);/*create a random data(0-1000)*/ for(i=0;i<20;i++){printf("please guess a number:");scanf("%d",&b);if(a>b){k++;printf("\n%d:Smaller,guess again!\n",k);}else if(a<b){k++;printf("\n%d:Bigger,guess again!\n",k);}else{printf("\nYou guess right,congraturation!") ;printf("\nYou have guessed %d times",k);break;}}if(k==20)printf("\nsorry,you failed!");}2.10#include <stdio.h>main(){int a,b,c;int num;for(a=0;a<10;a++){for(b=0;b<10;b++){for(c=1;c<10;c++){num=a*100+b*10+c;if((num%3==0)&&(a==5||b==5||c==5))printf("%8d",num);}}}printf("\n\n\n");}2.11#include <stdio.h>main(){int i;int a,b;printf("please input a,b:");scanf("%d%d",&a,&b);for(i=a<b?a:b;i>0;i--){if(a%i==0&&b%i==0){printf("The max=%d",i);break;}}for(i=a>b?a:b;i>0;i++){if(i%a==0&&i%b==0){printf("\nThe min=%d",i);break;}}2.12#include <stdio.h>main(){int a,k=0;printf("please input data:");scanf("%d",&a);while(a%2==0){a=a/2;k++;}printf("\nthe number of factor(2) is %d",k); }2.13main(){long i,t=1;long sum=0;for(i=1;i<=10;i++){t*=i;sum+=t;printf("%ld!=%ld\n",i,t);}printf("sum:%ld\n",sum);}2.14#include <stdio.h>void main(){int i,x=0;for(i=9;i>=1;i--){x=2*(x+1);}printf("The first day:%d",x);}2.15#include <stdio.h>#define PI 3.141593main(){float r,h;float v;printf("please input r,h:");scanf("%f%f",&r,&h);v=1.0/3*PI*r*r*h;printf("V=%.2f",v);}2.16#include<stdio.h>#include<math.h>main(){long int sn=0;long int m=0,t=0;int a,n,i;printf("please input a n:");scanf("%d%d",&a,&n);for(i=0;i<n;i++){m=m*10+a;sn+=m;}printf("a+aa+aaa+...+aa...a=%ld\n\n",sn); }2.17#include <stdio.h>main(){int k;printf("please input k:");do{scanf("%d",&k);if(k>=0&&k<=6)break;}while(1);switch(k){case 0:printf("Sunday.\n");break;case 1:printf("Monday.\n");break;case 2:printf("Tuesday.\n");break;case 3:printf("Wednesday.\n");break;case 4:printf("Thursday.\n");break;case 5:printf("Friday.\n");break;case 6:printf("Saturday.\n");break;}}2.18#include <stdio.h>main(){int i;double x,a,b=1,sum=1;printf("please input x:");scanf("%lf",&x);a=x;for(i=1;a/b>=1e-6;i++){sum=sum+a/b;a=a*x;b=(i+1)*b;}printf("\nsum=%lf",sum);}2.19#include <stdio.h>#include <math.h>main(){float a,x1,x2;printf("input a:");scanf("%f",&a);x1=1.0;while(1){x2=1.0/2*(x1+a/x1);if(fabs(x2-x1)<1e-5)break;x1=x2;}printf("sqrt(a)=%f",x2);}第四章写出下列程序输出结果1.no 1 a=1no 1 a=0no 1 a=1no 1 a=02.a=0 b=0 c=0a=1 b=0 c=0a=2 b=0 c=03.main:x=5,y=1,n=1func:x=6,y=21,n=11main:x=5,y=1,n=11func:x=8,y=31,n=21第五章1.D2.D3.C4.65.CDABC6.(1)j+=2(2)a[i]<a[j]7.(1)s[i++]!='\0'(2)s[i-1]8.D9.B10.6(同题4)11.s[i]>='0'&&s[i]<='9'12. (1)'\0' (2)str1[i]-str2[i]5.1#include<stdio.h>int fun();int fun(){int a[3][3],sum;int i,j;sum=0;/*error*/for(i=0;i<3;i++){for(j=0;j<3;j++)scanf("%d",&a[i][j]);/*error*/}for(i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%d\n",sum);}void main(){fun();}5.2#include <stdio.h>void main( void ){float Num[10];float Input,GetNum[11];int i,j;float a=6;for(i=0; i<10; i++){Num[i]=a;a=a+7.5;}Loop1: printf("Please input a Number(0-80) Input=");scanf("%f",&Input);if((Input<0)||(Input>70)==1)goto Loop1;for(i=0; i<10; i++ ){if(Input<Num[i])goto Loop2;}Loop2: for(j=0; j<i; j++ )GetNum[j]=Num[j];GetNum[j]=Input;for(j=i; j<=10; j++,i++)GetNum[j+1]=Num[i];for(j=0; j<11; j++ )printf("%3.3f ",GetNum[j]);}5.3#include "stdio.h"#include "stdlib.h"main(){int a1,a2,a3,a4,a5,a6,a7,a8,a9;int a[3];int i;for(;;){for (i=0;i<3;i++){a[i]=rand()%3;}while((a[0]!=a[1]) && (a[0]!=a[2]) && (a[1]!=a[2]))/*get three different numbers 0,1,2*/{a1=a[0]+1;/*divide 1~9 into three groups,a1~a3,a4~a6,a7~a9*/a2=a[1]+1;a3=a[2]+1;a4=a1+3;a5=a2+3;a6=a3+3;a7=a1+6;a8=a2+6;a9=a3+6;/* make sure that each line and each row is made up with three members in different group.*//* such as: a9 a1 a5a2 a6 a7a4 a8 a3 */if(((a1+a5+a9) == (a2+a6+a7)) && ((a1+a5+a9) == (a3+a4+a8))&& ((a1+a6+a8) == (a5+a7+a3)) && ((a1+a5+a9) == (a2+a4+a9))){printf("%d %d %d\n%d %d %d\n%d %d %d\n",a9,a1,a5,a2,a6,a7,a4,a8,a3);return;}}}}5.4#include <stdio.h>void main(void){char input1[100],input2[100],input3[100];int i,Eng=0,eng=0,num=0,blank=0,other=0;printf("Input 3 rows of character,each row don't exceed 80 characters:\n");gets(input1);printf("The second row:\n");gets(input2);printf("The third row:\n");gets(input3);/*test the first row*/for(i=0; i<100; i++){if(input1[i]=='\0')goto Loop1;else if(('A'<=input1[i])&&(input1[i]<='Z')==1)Eng++;else if(('a'<=input1[i])&&(input1[i]<='z')==1)eng++;else if(('0'<=input1[i])&&(input1[i]<='9')==1)num++;else if(input1[i]==32)blank++;else other++;}/*test the second row*/Loop1: for(i=0; i<100; i++){if(input2[i]=='\0')goto Loop2;else if(('A'<=input2[i])&&(input2[i]<='Z')==1)Eng++;else if(('a'<=input2[i])&&(input2[i]<='z')==1)eng++;else if(('0'<=input2[i])&&(input2[i]<='9')==1)num++;else if(input2[i]==32)blank++;else other++;}/*test the third row*/Loop2: for(i=0; i<100; i++){if(input3[i]=='\0')goto Loop3;else if(('A'<=input3[i])&&(input3[i]<='Z')==1)Eng++;else if(('a'<=input3[i])&&(input3[i]<='z')==1)eng++;else if(('0'<=input3[i])&&(input3[i]<='9')==1)num++;else if(input3[i]==32)blank++;else other++;}Loop3: printf("Upper english character:%d\nLower english character:%d\nNumber:%d\nBlank:%d\nOther characters:%d\n",Eng,eng,num,blank,other);}5.5#include <stdio.h>void main(void){char str1[80],str2[40];int i,j,k;/*Input two string*/printf("Please input the first string\n str1=");gets(str1);printf("Please input the second string\n str2=");gets(str2);/*Get the end of str1*/for(i=0; i<80; i++){if(str1[i]=='\0')break;}/*Copy str2 to str1*/for(j=i,k=0; str2[k]!='\0';k++,j++)str1[j]=str2[k];str1[i+k]='\0';puts(str1);}5.6#include <stdio.h>struct student{char name[20];int score;}stu[5],stu1;void main(void){ int i,j;printf("Input student's score and name(5),seperate using the character of ',' :\n");for(i=0; i<5; i++)scanf("%d,%s",&stu[i].score,&stu[i].name);/*sorting*/for(i=0; i<5; i++)for(j=0; j<4-i; j++)if(stu[j].score>stu[j+1].score){stu1=stu[j];stu[j]=stu[j+1];stu[j+1]=stu1;}printf("After sotred,score and name:\n");for(i=0; i<5; i++)printf("%d,%s\n",stu[i].score,stu[i].name);}5.7#include<stdio.h>main(){ int a[3][3],i,j,m,n,o,p;printf("Please input a 3*3 shuzu:\n");for (i=0;i<=2;i++)for (j=0;j<=2;j++)scanf("%d",&a[i][j]);for (i=0;i<=2;i++){ m=(a[i][0]>a[i][1])? a[i][0]:a[i][1];n=(m>a[i][2])? m: a[i][2];}for (j=0;j<=2;j++){ o=(a[0][j]<a[1][j])? a[0][j]:a[1][j];p=(o<a[2][j])? o: a[2][j];}if (n==p)printf("Andian is %d\\n",p);elseprintf("There is no andian.\\n"); }5.8#include <stdio.h>void main(void){ int a[4][3],b[3][4],i,j;for(i=0; i<4; i++){ for(j=0; j<3; j++)scanf("%d",&a[i][j]);}for(i=0; i<4; i++){printf("\n");for(j=0; j<3; j++)printf("%5d ",a[i][j]);}for(i=0; i<4; i++){for(j=0; j<3; j++)b[j][i]=a[i][j];}printf("\n");for(i=0; i<3; i++){printf("\n");for(j=0; j<4; j++)printf("%5d ",b[i][j]);}}5.9#include <stdio.h>#include<math.h>void main(void){ char str[6];int i,j=0,m=0,n=0;gets(str);while(str[j]!='\0') j++;for(i=j-1;i>=0;i--){m=(str[i]-'0')*(pow(8,j-i-1));n+=m;}printf("shijinzhi %d",n);}5.10#include <stdio.h>void main(void){char input1[100];int i,num=0,blank=0;printf("Please input a row character:\n");gets(input1);/*test the first row*/for(i=0; i<100; i++){if(input1[i]=='\0')goto Loop;else if(('A'<=input1[i])&&(input1[i]<='Z')==1)num++;else if(('a'<=input1[i])&&(input1[i]<='z')==1)num++;else if (input1[i]==32)blank++;}Loop: printf("Character:%d\nBlank:%d\n",num,blank);}第六章一、选择题1.D :p中记录的是a的地址,*p访问地址a的值2.D :指针数组3.D:指针可以进行是否相等判断,空指针可以进行指针变量初始化,指针可以偏移;4.C:5.C、D:a[5]不存在,若*&a[4]则正确;C,D正确;6.D:7.B:8.B:二、填空题1.*(p+3) 2 222.cdefgbcdefgabcdefg73.6385三、程序分析题1.p = s[0] ;错误,应改为:p = s;或p = &s[0];2.int x , *p;错误,应改为:double x, *p;3.*p = x ;错误,应改为:p = &x ;4.声明顺序错误;应该改为:void main(void){int a;int *p = &a;a = 10;printf(“%d\n”,*p);}2.3 #include <stdio.h>#include <string.h>main(){char ch[2][5] = {"6937","8254"},*p[2]; int i,j,s=0;for(i=0;i<2;i++)p[i] = ch[i];for(i=0;i<2;i++)for(j=0;p[i][j]>'\0';j+=2)s = 10*s + p[i][j] - '0';printf("%d\n",s);}4.1#include <stdio.h>#include <string.h>main(){int count = 0;char str1[100],str2[100];char *p1,*p2;printf("please enter string 1:");gets(str1);printf("please enter string 2:");gets(str2);printf("str1:%s\n",str1);printf("str2:%s\n",str2);p1 = str1;p2 = str2;while(*p1!='\0'&&*p2!='\0'){if(*p1++==*p2++)count++;}printf("count:%d\n",count);}4.2#include <stdio.h>#include <string.h>main(){int i,j,k;int a[3];int temp;printf("please enter array a[3]:"); scanf("%d%d%d",&a[0],&a[1],&a[2]);for(i=0;i<2;i++){k = i;for(j= i+1;j<3;j++){if(a[j]<a[i])k = j;}if(k!=i){temp = a[i];a[i] = a[k];a[k] = temp;}}for(i=0;i<3;i++)printf("%d\t",a[i]);printf("\n");}4.3#include <stdio.h>#include <string.h>#define N 100main(){char s1[N], s2[N],*p;int m;printf("please enter string1:");gets(s1);p = s1;printf("enter m:");scanf("%d",&m);strcpy(s2,p+m);printf("string2:%s\n",s2);}4.4#include <stdio.h>#include <string.h>main(){char s[100]="iuiui012asdd90k890y098kkkk1234",*p; char data[100][100]={{0}};int count=0;int i;p = s;/*printf("please enter a string:");gets(p);*/printf("sssss:%s\n",s);while(*p!='\0'){i=0;while(*p>='0'&&*p<='9'){data[count][i] = *p;i++;p++;}if(i!=0)count++;p++;}printf("count:%d\n",count);for(i=0;i<count;i++)printf("%s\n",data[i]);}4.5#include <stdio.h>#define SIZE 4main(){int data[SIZE][SIZE],i,j,d;int max,m=0,n=0;for(i=0;i<SIZE;i++){for(j=0;j<SIZE;j++){scanf("%d",&data[i][j]);}}for(i=0;i<SIZE;i++){for(j=i+1;j<SIZE;j++){d = data[i][j];data[i][j] = data[j][i];data[j][i] = d;}}max = data[0][0];for(i=0;i<SIZE;i++){printf("\n");for(j=0;j<SIZE;j++){printf("%4d",data[i][j]);if(data[i][j]>=max){max = data[i][j];m = i;n = j;}}}printf("\nmax:%4d m:%d n:%d\n",max,m,n);}4.6#include <stdio.h>#include <string.h>main(){char *p[] = {"test","capital","index","large","small"}; char **pstr = p;int a,b,n = 5;char *temp;for(a=0;a<n-1;a++){for(b=a+1;b<n;b++){if(strcmp(pstr[a],pstr[b])>0){temp = pstr[a];pstr[a] = pstr[b];pstr[b] = temp;}}}for(a=0;a<n;a++)printf("%s\n",pstr[a]);}4.7#include <stdio.h>#include <string.h>main(){char s[100],temp;char *p = s;int i,length;printf("please enter a string: ");gets(s);while(*p++!='\0');length = p - s -1;printf("string length:%d\n",length);for(i=0;i<length/2;i++){temp = s[i];s[i] = s[length-i-1];s[length-i-1] = temp;}printf("%s\n",s);}第七章一,选择题1.A2.D3.B4.D5.C6.A7.B二.程序填空题1.(1) a[k](2) a[k](3) a[k]2.(1) a[i](2) j(3) i+1或6三.改错题1.#include<stdio.h>#include<string.h>void swap(char *,char *); /*此处有错误,函数声明时要加分号*/main(){char a[80],b[80],c[80];scanf("%s%s%s",a,b,c); /*此处有错误,a,b,c分别为数组a[80],b[8],c[80]的首地址*/ if(strcmp(a,b)>0) swap(a,b); /*此处有错误,字符串比较应该使用strcmp函数*/if(strcmp(b,c)>0) swap(b,c); /*此处有错误,原因同上*/if(strcmp(a,b)>0) swap(a,b); /*原算法不能实现排序,需要添加此语句*/printf("%s\n%s\n%s\n",a,b,c);}7.3.1#include <stdio.h>#include <string.h>void swap(char *pstr1,char *pstr2){char p[80];strcpy(p,pstr1);strcpy(pstr1,pstr2);strcpy(pstr2,p);}main(){char a[80]="ccc",b[80]="bbb",c[80]="aaa";/* scanf("%s%s%s",a,b,c); */if(strcmp(a,b)>0)swap(a,b);if(strcmp(b,c)>0)swap(b,c);if(strcmp(a,b)>0)swap(a,b);printf("%s\n%s\n%s\n\n\n",a,b,c);}7.4.1#include <stdio.h>int SquSum(int,int);main(){int a,b;int c;printf("enter 2 integer:");scanf("%d%d",&a,&b);c = SquSum(a,b);printf("a=%d b:%d a2+b2=%d\n",a,b,c); }int SquSum(int x,int y){int z;z = x*x+y*y;return z;}7.4.2#include <stdio.h>int GongYueShu(int,int);int GongBeiShu(int,int);main(){int a,b;int gbs,gys;printf("enter 2 integer:");scanf("%d%d",&a,&b);gys = GongYueShu(a,b);gbs = GongBeiShu(a,b);if(gys<0)printf("There has not gong yue shu!!!!!!\n"); elseprintf("max gong yue shu:%d\n",gys); printf("min gong bei shu:%d\n",gbs);}int GongYueShu(int x,int y){int i,gys=-1,min;min = x;if(x>y)min = y;for(i=2;i<=min;i++){if((x%i==0)&&(y%i==0))gys = i;}return gys;}int GongBeiShu(int x,int y){int i,gbs,max;max = x;if(y>x)max = y;for(i=max;i<=x*y;i++){if((i%x==0)&&(i%y==0)){gbs = i;break;}}return gbs;}7.4.4#include <stdio.h>#include <string.h>void Delete_Ch(char *,char);main(){char str[80],ch;printf("please a string:");gets(str);printf("please a char:");scanf("%c",&ch);Delete_Ch(str,ch);printf("%s\n\n",str);}void Delete_Ch(char *p,char ch){while(*p){if(*p==ch){strcpy(p,p+1);}elsep++;}}7.4.5#include <stdio.h>int Is_ShuShu(int); /* return 0:shushureturn -1:no shushu*/ main(){int a,ret;printf("pls enter a integer:");scanf("%d",&a);ret = Is_ShuShu(a);if(ret==0)printf("%d is a shushu!!!\n",a);elseprintf("%d is not a shushu\n",a); }int Is_ShuShu(int x){int i;for(i=2;i<x/2;i++){if(x%i==0)return -1;}if(i>=x/2)return 0;}7.4.6#include <stdio.h>#include <string.h>void StrCat(char *s1,char *s2){while(*s1!='\0')s1++;while(*s2!='\0'){*s1 = *s2;s1++;s2++;}*s1='\0';}main(){char a[200]="123456789",b[100]="ABCDEFG";printf("pls enter string 1:");gets(a);printf("pls enter string 2:");gets(b);StrCat(a,b);printf("%s\n\n",a);}7.4.7#include <stdio.h>#include <string.h>void Stat(char *s,int *ch,int *dig,int *spa,int *oth){while(*s!='\0'){if(((*s>='a')&&(*s<='z'))||((*s>='A')&&(*s<='Z')))(*ch)++;else if((*s>='0')&&(*s<='9'))(*dig)++;else if(*s == ' ')(*spa)++;else(*oth)++;s++;}}main(){char a[200];int ch=0,dig=0,spa=0,oth=0;printf("pls enter a string :");gets(a);Stat(a,&ch,&dig,&spa,&oth);printf("ch:%d dig:%d spa:%d oth:%d",ch,dig,spa,oth); }7.4.8#include <stdio.h>#include <string.h>void reverse(char *s) {if(*s){}main(){char s[] = "abcde"; reverse(s);puts(s);}7.4.9#include <stdio.h>#include <string.h>paixu(int *p,int n,int flag) {int i,j,m;int temp;for(i=0;i<n-1;i++) {m = i;for(j=i+1;j<n;j++){if(flag == 1){if(p[j]>p[m])m = j;}else{if(p[j]<p[m])m = j;}}if(m!=i){temp = p[i];p[i] = p[m];p[m] = temp;}}}main(){int a[20];int n,i;int flag;printf("please enter n(<20):"); scanf("%d",&n);getchar();printf("please enter array a:");for(i=0;i<n;i++)scanf("%d",&a[i]);getchar();printf("please enter paixu flag:\n"); printf("\t1:degression\n");printf("\t0:increasion\t:");scanf("%d",&flag);paixu(a,n,flag);printf("array a is:");for(i=0;i<n;i++)printf("%4d",a[i]);printf("\n\n");}。

C语言实验指导书(第2版)-华中科技大学

C语言实验指导书(第2版)-华中科技大学

C语言程序设计实验指导书(第二版)编著曹计昌卢萍李开张茂元华中科技大学计算机学院2008年3月目录编写说明 (4)实验1 Turbo C 2.0集成开发环境及简单程序调试 (5)一、实验目的 (5)二、实验任务 (5)三.操作指导(结合第1题) (7)实验2 表达式和标准输入与输出 (17)一、实验目的 (17)二、实验题目及要求 (17)1、源程序改错题 (17)2、源程序修改替换题 (18)3、编程设计题 (18)实验3 流程控制实验 (19)一、实验目的 (19)二、实验题目及要求 (19)1.源程序改错题 (19)2.源程序修改替换题 (19)3.编程设计题 (19)4. 选做题 (20)实验4 函数与程序结构实验 (21)一、实验目的 (21)二、实验题目及要求 (21)1.源程序改错题 (21)2.源程序修改替换题 (21)3.跟踪调试题 (22)4.编程设计题 (22)5.选做题 (23)实验5 编译预处理实验 (24)一、实验目的 (24)二、实验题目及要求 (24)1.源程序改错题 (24)2.源程序修改替换题 (24)3.跟踪调试题 (25)4.编程设计题 (26)实验6 标准库及用户自定义库实验 (27)一、实验目的 (27)二、实验题目及要求 (27)1.编程设计题 (27)2.选做题 (28)实验7 数组实验 (29)一、实验目的 (29)二、实验题目及要求 (29)1.源程序改错题 (29)2.源程序完善、修改、替换题 (29)3.跟踪调试题 (30)4.编程设计题 (31)5.选做题 (31)实验8 指针实验 (32)一、实验目的 (32)二、实验题目及要求 (32)1.源程序改错题 (32)2.源程序完善、修改、替换题 (32)3.跟踪调试题 (33)4.编程设计题 (34)5.选做题 (34)实验9 结构与联合实验 (35)一、实验目的 (35)二、实验题目及要求 (35)1.表达式求值的程序验证题 (35)2.源程序修改替换题 (35)3.编程设计题 (37)4.选做题 (37)实验10 文件实验 (38)一、实验目的 (38)二、实验题目及要求 (38)1.文件类型的程序验证题 (38)2.源程序修改替换题 (39)3.编程设计题 (39)编写说明本实验指导书是根据2008年出版发行的《C语言程序设计》教材编写。

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

第二章一.选择题1.C2.B D3.A4.A5. D6.C7.D8.C9.A 10.D 11.B 12.D 13.C 14.D 15.A16.B 17.A 18.B 100011 001111二.判断题1.错2.错3.错4.错三.填空题1. B 662. n1=%d\nn2=%d\n3. 0四.计算1(1)x|y = 0x002f(2)x^y = 0x0026(3)x&y = 0x0009;(4)~x+~y = 0xffc6(5)x<<=3 0x0068(6)y>>=4 0x00022(1) 6(2)50(3) 1(4)–16(5) 1(6)203(1)0(2) 1(3) 1(4) 4(5)8(6) 14(1)12(2)0(3) 1(4)27(6) 6(7)24(8)27(9)–295(1)0(2) 1(3) 1(4)–3(5) 2五.程序分析题程序1b=20 a=3600程序2第三章一.填空题1.s=62.963.(1) scanf("%c",&c);(2) c-32 更好的答案:c-('a'-'A')2.1main(){int a,b;printf("please input a & b:");scanf("%d%d",&a,&b);printf("\nmax is %d\n",(a>b)?a:b);}2.2int max(int x,int y);main(){int a,b,c,max;printf("please input a & b & c:");scanf("%d%d%d",&a,&b,&c);max=a>b?a:b;max=c>max?c:max;printf("\nmax is %d\n",max);}2.3{int i=0,sum=0;while(i<=100){sum+=i;i++;}printf("1+2+3+......+100=%d\n",sum);}2.4main(){int i;int a=10,b=-3;int c;printf("%6d%6d",a,b);for(i=2;i<10;i++){c=3*b+a;printf("%6d",c);a=b;b=c;}printf("\n");}2.5main(){int i;while(1){printf("please input a data(0:exit):");scanf("%d",&i);if(i==0)break;if(i%2==0)printf("the data %d is a even number.\n",i);elseprintf("the data %d is a odd number.\n",i); }}2.6#include <stdio.h>main(){int i;int a=8,b=1;int sum=0;for(i=0;i<10;i++){b+=3;sum += a;a+=b;printf("a%8d b:%8d\n",a,b);}printf("The Sum Of Is:%d\n",sum);}2.7#include <stdio.h>main(){float x,y;printf("please input x:");scanf("%f",&x);if(x<1.0)y=x;else if(x<10)y=2*x-1;elsey=3*x-11;printf("y=%f\n",y);}2.8#include <stdio.h>main(){long a,i,b,a1;while(1){printf("please input data(1-99999):");scanf("%ld",&a);printf("a:%ld\n",a);if(a<=0||a>=100000)break;i=0;a1=0;while(a!=0){b=a%10;printf("%8d",b);a/=10;i++;a1=a1*10+b;}printf("\n i:%ld a1:%ld\n",i,a1);}}2.9#include <stdio.h>#include <time.h>#include <stdlib.h>main(){int a,b,i,k=0;randomize();a=random(1001);/*create a random data(0-1000)*/ for(i=0;i<20;i++){printf("please guess a number:");scanf("%d",&b);if(a>b){k++;printf("\n%d:Smaller,guess again!\n",k);}else if(a<b){k++;printf("\n%d:Bigger,guess again!\n",k);}else{printf("\nYou guess right,congraturation!") ;printf("\nYou have guessed %d times",k);break;}}if(k==20)printf("\nsorry,you failed!");}2.10#include <stdio.h>main(){int a,b,c;int num;for(a=0;a<10;a++){for(b=0;b<10;b++){for(c=1;c<10;c++){num=a*100+b*10+c;if((num%3==0)&&(a==5||b==5||c==5))printf("%8d",num);}}}printf("\n\n\n");}2.11#include <stdio.h>main(){int i;int a,b;printf("please input a,b:");scanf("%d%d",&a,&b);for(i=a<b?a:b;i>0;i--){if(a%i==0&&b%i==0){printf("The max=%d",i);break;}}for(i=a>b?a:b;i>0;i++){if(i%a==0&&i%b==0){printf("\nThe min=%d",i);break;}}2.12#include <stdio.h>main(){int a,k=0;printf("please input data:");scanf("%d",&a);while(a%2==0){a=a/2;k++;}printf("\nthe number of factor(2) is %d",k); }2.13main(){long i,t=1;long sum=0;for(i=1;i<=10;i++){t*=i;sum+=t;printf("%ld!=%ld\n",i,t);}printf("sum:%ld\n",sum);}2.14#include <stdio.h>void main(){int i,x=0;for(i=9;i>=1;i--){x=2*(x+1);}printf("The first day:%d",x);}2.15#include <stdio.h>#define PI 3.141593main(){float r,h;float v;printf("please input r,h:");scanf("%f%f",&r,&h);v=1.0/3*PI*r*r*h;printf("V=%.2f",v);}2.16#include<stdio.h>#include<math.h>main(){long int sn=0;long int m=0,t=0;int a,n,i;printf("please input a n:");scanf("%d%d",&a,&n);for(i=0;i<n;i++){m=m*10+a;sn+=m;}printf("a+aa+aaa+...+aa...a=%ld\n\n",sn); }2.17#include <stdio.h>main(){int k;printf("please input k:");do{scanf("%d",&k);if(k>=0&&k<=6)break;}while(1);switch(k){case 0:printf("Sunday.\n");break;case 1:printf("Monday.\n");break;case 2:printf("Tuesday.\n");break;case 3:printf("Wednesday.\n");break;case 4:printf("Thursday.\n");break;case 5:printf("Friday.\n");break;case 6:printf("Saturday.\n");break;}}2.18#include <stdio.h>main(){int i;double x,a,b=1,sum=1;printf("please input x:");scanf("%lf",&x);a=x;for(i=1;a/b>=1e-6;i++){sum=sum+a/b;a=a*x;b=(i+1)*b;}printf("\nsum=%lf",sum);}2.19#include <stdio.h>#include <math.h>main(){float a,x1,x2;printf("input a:");scanf("%f",&a);x1=1.0;while(1){x2=1.0/2*(x1+a/x1);if(fabs(x2-x1)<1e-5)break;x1=x2;}printf("sqrt(a)=%f",x2);}第四章写出下列程序输出结果1.no 1 a=1no 1 a=0no 1 a=1no 1 a=02.a=0 b=0 c=0a=1 b=0 c=0a=2 b=0 c=03.main:x=5,y=1,n=1func:x=6,y=21,n=11main:x=5,y=1,n=11func:x=8,y=31,n=21第五章1.D2.D3.C4.65.CDABC6.(1)j+=2(2)a[i]<a[j]7.(1)s[i++]!='\0'(2)s[i-1]8.D9.B10.6(同题4)11.s[i]>='0'&&s[i]<='9'12. (1)'\0' (2)str1[i]-str2[i]5.1#include<stdio.h>int fun();int fun(){int a[3][3],sum;int i,j;sum=0;/*error*/for(i=0;i<3;i++){for(j=0;j<3;j++)scanf("%d",&a[i][j]);/*error*/}for(i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%d\n",sum);}void main(){fun();}5.2#include <stdio.h>void main( void ){float Num[10];float Input,GetNum[11];int i,j;float a=6;for(i=0; i<10; i++){Num[i]=a;a=a+7.5;}Loop1: printf("Please input a Number(0-80) Input=");scanf("%f",&Input);if((Input<0)||(Input>70)==1)goto Loop1;for(i=0; i<10; i++ ){if(Input<Num[i])goto Loop2;}Loop2: for(j=0; j<i; j++ )GetNum[j]=Num[j];GetNum[j]=Input;for(j=i; j<=10; j++,i++)GetNum[j+1]=Num[i];for(j=0; j<11; j++ )printf("%3.3f ",GetNum[j]);}5.3#include "stdio.h"#include "stdlib.h"main(){int a1,a2,a3,a4,a5,a6,a7,a8,a9;int a[3];int i;for(;;){for (i=0;i<3;i++){a[i]=rand()%3;}while((a[0]!=a[1]) && (a[0]!=a[2]) && (a[1]!=a[2]))/*get three different numbers 0,1,2*/{a1=a[0]+1;/*divide 1~9 into three groups,a1~a3,a4~a6,a7~a9*/a2=a[1]+1;a3=a[2]+1;a4=a1+3;a5=a2+3;a6=a3+3;a7=a1+6;a8=a2+6;a9=a3+6;/* make sure that each line and each row is made up with three members in different group.*//* such as: a9 a1 a5a2 a6 a7a4 a8 a3 */if(((a1+a5+a9) == (a2+a6+a7)) && ((a1+a5+a9) == (a3+a4+a8))&& ((a1+a6+a8) == (a5+a7+a3)) && ((a1+a5+a9) == (a2+a4+a9))) {printf("%d %d %d\n%d %d %d\n%d %d %d\n",a9,a1,a5,a2,a6,a7,a4,a8,a3);return;}}}}5.4#include <stdio.h>void main(void){char input1[100],input2[100],input3[100];int i,Eng=0,eng=0,num=0,blank=0,other=0;printf("Input 3 rows of character,each row don't exceed 80 characters:\n");gets(input1);printf("The second row:\n");gets(input2);printf("The third row:\n");gets(input3);/*test the first row*/for(i=0; i<100; i++){if(input1[i]=='\0')goto Loop1;else if(('A'<=input1[i])&&(input1[i]<='Z')==1)Eng++;else if(('a'<=input1[i])&&(input1[i]<='z')==1)eng++;else if(('0'<=input1[i])&&(input1[i]<='9')==1)num++;else if(input1[i]==32)blank++;else other++;}/*test the second row*/Loop1: for(i=0; i<100; i++){if(input2[i]=='\0')goto Loop2;else if(('A'<=input2[i])&&(input2[i]<='Z')==1)Eng++;else if(('a'<=input2[i])&&(input2[i]<='z')==1)eng++;else if(('0'<=input2[i])&&(input2[i]<='9')==1)num++;else if(input2[i]==32)blank++;else other++;}/*test the third row*/Loop2: for(i=0; i<100; i++){if(input3[i]=='\0')goto Loop3;else if(('A'<=input3[i])&&(input3[i]<='Z')==1)Eng++;else if(('a'<=input3[i])&&(input3[i]<='z')==1)eng++;else if(('0'<=input3[i])&&(input3[i]<='9')==1)num++;else if(input3[i]==32)blank++;else other++;}Loop3: printf("Upper english character:%d\nLower english character:%d\nNumber:%d\nBlank:%d\nOther characters:%d\n",Eng,eng,num,blank,other);}5.5#include <stdio.h>void main(void)char str1[80],str2[40];int i,j,k;/*Input two string*/printf("Please input the first string\n str1=");gets(str1);printf("Please input the second string\n str2=");gets(str2);/*Get the end of str1*/for(i=0; i<80; i++){if(str1[i]=='\0')break;}/*Copy str2 to str1*/for(j=i,k=0; str2[k]!='\0';k++,j++)str1[j]=str2[k];str1[i+k]='\0';puts(str1);}5.6#include <stdio.h>struct student{char name[20];int score;}stu[5],stu1;void main(void){ int i,j;printf("Input student's score and name(5),seperate using the character of ',' :\n");for(i=0; i<5; i++)scanf("%d,%s",&stu[i].score,&stu[i].name);/*sorting*/for(i=0; i<5; i++)for(j=0; j<4-i; j++)if(stu[j].score>stu[j+1].score){stu1=stu[j];stu[j]=stu[j+1];stu[j+1]=stu1;}printf("After sotred,score and name:\n");for(i=0; i<5; i++)printf("%d,%s\n",stu[i].score,stu[i].name); }5.7#include<stdio.h>main(){ int a[3][3],i,j,m,n,o,p;printf("Please input a 3*3 shuzu:\n");for (i=0;i<=2;i++)for (j=0;j<=2;j++)scanf("%d",&a[i][j]);for (i=0;i<=2;i++){ m=(a[i][0]>a[i][1])? a[i][0]:a[i][1];n=(m>a[i][2])? m: a[i][2];}for (j=0;j<=2;j++){ o=(a[0][j]<a[1][j])? a[0][j]:a[1][j];p=(o<a[2][j])? o: a[2][j];}if (n==p)printf("Andian is %d\\n",p);elseprintf("There is no andian.\\n"); }5.8#include <stdio.h>void main(void){ int a[4][3],b[3][4],i,j;for(i=0; i<4; i++){ for(j=0; j<3; j++)scanf("%d",&a[i][j]);}for(i=0; i<4; i++){printf("\n");for(j=0; j<3; j++)printf("%5d ",a[i][j]);}for(i=0; i<4; i++){for(j=0; j<3; j++)b[j][i]=a[i][j];}printf("\n");for(i=0; i<3; i++){printf("\n");for(j=0; j<4; j++)printf("%5d ",b[i][j]);}5.9#include <stdio.h>#include<math.h>void main(void){ char str[6];int i,j=0,m=0,n=0;gets(str);while(str[j]!='\0') j++;for(i=j-1;i>=0;i--){m=(str[i]-'0')*(pow(8,j-i-1));n+=m;}printf("shijinzhi %d",n);}5.10#include <stdio.h>void main(void){char input1[100];int i,num=0,blank=0;printf("Please input a row character:\n");gets(input1);/*test the first row*/for(i=0; i<100; i++){if(input1[i]=='\0')goto Loop;else if(('A'<=input1[i])&&(input1[i]<='Z')==1)num++;else if(('a'<=input1[i])&&(input1[i]<='z')==1)num++;else if (input1[i]==32)blank++;}Loop: printf("Character:%d\nBlank:%d\n",num,blank);}第六章一、选择题1.D :p中记录的是a的地址,*p访问地址a的值2.D :指针数组3.D:指针可以进行是否相等判断,空指针可以进行指针变量初始化,指针可以偏移;4.C:5.C、D:a[5]不存在,若*&a[4]则正确;C,D正确;6.D:7.B:8.B:二、填空题1.*(p+3) 2 222.cdefgbcdefgabcdefg73.6385三、程序分析题1.p = s[0] ;错误,应改为:p = s;或p = &s[0];2.int x , *p;错误,应改为:double x, *p;3.*p = x ;错误,应改为:p = &x ;4.声明顺序错误;应该改为:void main(void){int a;int *p = &a;a = 10;printf(“%d\n”,*p);}2.3 #include <stdio.h>#include <string.h>main(){char ch[2][5] = {"6937","8254"},*p[2]; int i,j,s=0;for(i=0;i<2;i++)p[i] = ch[i];for(i=0;i<2;i++)for(j=0;p[i][j]>'\0';j+=2)s = 10*s + p[i][j] - '0';printf("%d\n",s);}4.1#include <stdio.h>#include <string.h>main(){int count = 0;char str1[100],str2[100];char *p1,*p2;printf("please enter string 1:");gets(str1);printf("please enter string 2:");gets(str2);printf("str1:%s\n",str1);printf("str2:%s\n",str2);p1 = str1;p2 = str2;while(*p1!='\0'&&*p2!='\0'){if(*p1++==*p2++)count++;}printf("count:%d\n",count);}4.2#include <stdio.h>#include <string.h>main()int i,j,k;int a[3];int temp;printf("please enter array a[3]:"); scanf("%d%d%d",&a[0],&a[1],&a[2]);for(i=0;i<2;i++){k = i;for(j= i+1;j<3;j++){if(a[j]<a[i])k = j;}if(k!=i){temp = a[i];a[i] = a[k];a[k] = temp;}}for(i=0;i<3;i++)printf("%d\t",a[i]);printf("\n");}4.3#include <stdio.h>#include <string.h>#define N 100main(){char s1[N], s2[N],*p;int m;printf("please enter string1:");gets(s1);p = s1;printf("enter m:");scanf("%d",&m);strcpy(s2,p+m);printf("string2:%s\n",s2);}4.4#include <stdio.h>#include <string.h>main(){char s[100]="iuiui012asdd90k890y098kkkk1234",*p; char data[100][100]={{0}};int count=0;int i;p = s;/*printf("please enter a string:");gets(p);*/printf("sssss:%s\n",s);while(*p!='\0'){i=0;while(*p>='0'&&*p<='9'){data[count][i] = *p;i++;p++;}if(i!=0)count++;p++;}printf("count:%d\n",count);for(i=0;i<count;i++)printf("%s\n",data[i]);}4.5#include <stdio.h>#define SIZE 4main()int data[SIZE][SIZE],i,j,d;int max,m=0,n=0;for(i=0;i<SIZE;i++){for(j=0;j<SIZE;j++){scanf("%d",&data[i][j]);}}for(i=0;i<SIZE;i++){for(j=i+1;j<SIZE;j++){d = data[i][j];data[i][j] = data[j][i];data[j][i] = d;}}max = data[0][0];for(i=0;i<SIZE;i++){printf("\n");for(j=0;j<SIZE;j++){printf("%4d",data[i][j]);if(data[i][j]>=max){max = data[i][j];m = i;n = j;}}}printf("\nmax:%4d m:%d n:%d\n",max,m,n);}4.6#include <stdio.h>#include <string.h>main(){char *p[] = {"test","capital","index","large","small"}; char **pstr = p;int a,b,n = 5;char *temp;for(a=0;a<n-1;a++){for(b=a+1;b<n;b++){if(strcmp(pstr[a],pstr[b])>0){temp = pstr[a];pstr[a] = pstr[b];pstr[b] = temp;}}}for(a=0;a<n;a++)printf("%s\n",pstr[a]);}4.7#include <stdio.h>#include <string.h>main(){char s[100],temp;char *p = s;int i,length;printf("please enter a string: ");gets(s);while(*p++!='\0');length = p - s -1;printf("string length:%d\n",length);for(i=0;i<length/2;i++){temp = s[i];s[i] = s[length-i-1];s[length-i-1] = temp;}printf("%s\n",s);}第七章一,选择题1.A2.D3.B4.D5.C6.A7.B二.程序填空题1.(1) a[k](2) a[k](3) a[k]2.(1) a[i](2) j(3) i+1或6三.改错题1.#include<stdio.h>#include<string.h>void swap(char *,char *); /*此处有错误,函数声明时要加分号*/main(){char a[80],b[80],c[80];scanf("%s%s%s",a,b,c); /*此处有错误,a,b,c分别为数组a[80],b[8],c[80]的首地址*/ if(strcmp(a,b)>0) swap(a,b); /*此处有错误,字符串比较应该使用strcmp函数*/if(strcmp(b,c)>0) swap(b,c); /*此处有错误,原因同上*/if(strcmp(a,b)>0) swap(a,b); /*原算法不能实现排序,需要添加此语句*/printf("%s\n%s\n%s\n",a,b,c);}7.3.1#include <stdio.h>#include <string.h>void swap(char *pstr1,char *pstr2){char p[80];strcpy(p,pstr1);strcpy(pstr1,pstr2);strcpy(pstr2,p);}main(){char a[80]="ccc",b[80]="bbb",c[80]="aaa";/* scanf("%s%s%s",a,b,c); */if(strcmp(a,b)>0)swap(a,b);if(strcmp(b,c)>0)swap(b,c);if(strcmp(a,b)>0)swap(a,b);printf("%s\n%s\n%s\n\n\n",a,b,c);}7.4.1#include <stdio.h>int SquSum(int,int);main(){int a,b;int c;printf("enter 2 integer:");scanf("%d%d",&a,&b);c = SquSum(a,b);printf("a=%d b:%d a2+b2=%d\n",a,b,c); }int SquSum(int x,int y){int z;z = x*x+y*y;return z;}7.4.2#include <stdio.h>int GongYueShu(int,int);int GongBeiShu(int,int);{int a,b;int gbs,gys;printf("enter 2 integer:");scanf("%d%d",&a,&b);gys = GongYueShu(a,b);gbs = GongBeiShu(a,b);if(gys<0)printf("There has not gong yue shu!!!!!!\n"); elseprintf("max gong yue shu:%d\n",gys); printf("min gong bei shu:%d\n",gbs);}int GongYueShu(int x,int y){int i,gys=-1,min;min = x;if(x>y)min = y;for(i=2;i<=min;i++){if((x%i==0)&&(y%i==0))gys = i;}return gys;}int GongBeiShu(int x,int y){int i,gbs,max;max = x;if(y>x)max = y;for(i=max;i<=x*y;i++){if((i%x==0)&&(i%y==0)){gbs = i;break;}return gbs;}7.4.4#include <stdio.h>#include <string.h>void Delete_Ch(char *,char);main(){char str[80],ch;printf("please a string:");gets(str);printf("please a char:");scanf("%c",&ch);Delete_Ch(str,ch);printf("%s\n\n",str);}void Delete_Ch(char *p,char ch){while(*p){if(*p==ch){strcpy(p,p+1);}elsep++;}}7.4.5#include <stdio.h>int Is_ShuShu(int); /* return 0:shushureturn -1:no shushu*/main(){int a,ret;scanf("%d",&a);ret = Is_ShuShu(a);if(ret==0)printf("%d is a shushu!!!\n",a);elseprintf("%d is not a shushu\n",a);}int Is_ShuShu(int x){int i;for(i=2;i<x/2;i++){if(x%i==0)return -1;}if(i>=x/2)return 0;}7.4.6#include <stdio.h>#include <string.h>void StrCat(char *s1,char *s2){while(*s1!='\0')s1++;while(*s2!='\0'){*s1 = *s2;s1++;s2++;}*s1='\0';}main(){char a[200]="123456789",b[100]="ABCDEFG"; printf("pls enter string 1:");gets(a);gets(b);StrCat(a,b);printf("%s\n\n",a);}7.4.7#include <stdio.h>#include <string.h>void Stat(char *s,int *ch,int *dig,int *spa,int *oth){while(*s!='\0'){if(((*s>='a')&&(*s<='z'))||((*s>='A')&&(*s<='Z')))(*ch)++;else if((*s>='0')&&(*s<='9'))(*dig)++;else if(*s == ' ')(*spa)++;else(*oth)++;s++;}}main(){char a[200];int ch=0,dig=0,spa=0,oth=0;printf("pls enter a string :");gets(a);Stat(a,&ch,&dig,&spa,&oth);printf("ch:%d dig:%d spa:%d oth:%d",ch,dig,spa,oth); }7.4.8#include <stdio.h>#include <string.h>void reverse(char *s)if(*s){}main(){char s[] = "abcde"; reverse(s);puts(s);}7.4.9#include <stdio.h>#include <string.h>paixu(int *p,int n,int flag) {int i,j,m;int temp;for(i=0;i<n-1;i++) {m = i;for(j=i+1;j<n;j++){if(flag == 1){if(p[j]>p[m])m = j;}else{if(p[j]<p[m])m = j;}}if(m!=i){temp = p[i];p[i] = p[m];p[m] = temp;}}}main(){int a[20];int n,i;int flag;printf("please enter n(<20):"); scanf("%d",&n);getchar();printf("please enter array a:");for(i=0;i<n;i++)scanf("%d",&a[i]);getchar();printf("please enter paixu flag:\n"); printf("\t1:degression\n");printf("\t0:increasion\t:");scanf("%d",&flag);paixu(a,n,flag);printf("array a is:");for(i=0;i<n;i++)printf("%4d",a[i]);printf("\n\n");}。

相关文档
最新文档