c程序的设计实验指导与测试(第四版)所有示例代码
C语言程序设计 上机实验指导与习题 参考答案(第四版)
C 语言程序设计上机实验指导与习题参考答案(第四版)(学生改编)实验 1:C语言程序初步一、实验目的(1)了解所用的计算机系统的基本操作方法,学会独立使用该系统。
(2)了解在该系统上如何编辑、编译、连接和运行一个 C程序。
(3)通过运行简单的 C 程序,初步了解 C程序的特点。
(4)在教师的指导下,学会使用在线评判系统。
二、实验内容1.运行第一个 C 程序题目:ThefirstCProgram将下列程序输入visualc ,编译、连接和运行该程序。
includequotstdio.hquotmain printfquotThefirstCProgramnquot具体操作步骤(1)在编辑窗口中输入程序。
(2)保存程序,取名为a1.c。
(3)按照第一章中介绍的方法,编译、连接和运行程序。
,则该题完成。
4按照第三章介绍的方法,将代码提交到在线评判系统,系统返回“通过”2.在在线评判系统中提交实现了计算 ab 功能的程序题目 1001:计算ab由键盘输入两个整数,计算并输出两个整数的和。
实现该功能的程序如下,inclu dequotstdio.hquotmain int ab scanfquotddquotampaampbprintfquotdquotab(1)在程序编辑窗口中输入程序。
(2)保存程序,取名为a2.c。
(3)按照前二章中介绍的方法,编译、连接和运行程序。
(4)在程序运行过程中,输入 15 30↙↙表示输入回车符(5)如果看到如下输出结果,则表明1530 的结果正确,如果得不到如下结果,则需检查并更正程序。
45(6)按照第三章中介绍的方法进入在线评判系统。
(7)显示题目列表,点击题号为 1001,题名为“计算ab”的题目。
(8)查看完题目要求后,点击页面下端的“sumbit” ,参照第二章提交程序的方法提交程序a2.c。
(9)查看评判结果,如果得到“accepted”则该题通过,否则返回第一步检查程序是否正确。
C程序设计第四版实验报告材料实验1C程序地运行环境和运行C程序地方法
C程序设计第四版实验报告材料实验1C程序地运行环境和运行C程序地方法实用标准文案1.输入下面的程序:#includeint main{printf(This is a c program.\n);return 0;}编译连接无错误,检查得知输出结果无错误。
2.输入错误程序:#includeint main{int a,b,suma=123;b=456;sum=a+bprint (sum is d\n,sum);return 0;}错误提醒:12.cppe:\visual c\12.cpp(4) : error C2146: synta error : missing #;;#; before identifier #;a#;e:\visual c\12.cpp(6) : error C2146: synta error : missing #;;#; before identifier #;print#;e:\visual c\12.cpp(6) : error C2065: #;print#; : undeclared identifier执行 cl.ee 时出错.精彩文档.实用标准文案12.ee - 1 error(s), 0 warning(s)输入正确程序#includeint main{int a,b,sum;a=123;b=456;sum=a+b;printf(sum is d\n,sum);return 0;}输出结果3.输入下面的程序:#includeint main{int ma(int ,int y);int a,b,c;printf(input ab:);scanf(d,d,a,b);c=ma(a,b);printf(ma=d\\n,c);精彩文档.实用标准文案return 0;}int ma(int ,int y);{int z;if(>y) z=;else z=y;return(z);}输出结果将第ma函数中的第3,4两行合并写为一行#includeint main{int ma(int ,int y);int a,b,c;printf(input ab:);scanf(d,d,a,b);c=ma(a,b);printf(ma=d\\n,c);return 0;}int ma(int ,int y){int z;精彩文档.实用标准文案if(>y) z=;else z=y;return (z);}运行结果实验二1.输入如下程序:#includeint main{char c1,c2;c1=97;c2=98;printf(c,c\nc1,c2);printf(d,d\n,c1,c2);return 0;} 运行错误,因为c1前面没有字符将它隔开如果将程序第4,5行改成c1=197;c2=198;输出结果如果将程序第3行改成int c1,c2;精彩文档.实用标准文案)为整形数据(因为int运行时会输出函数输入数据:2.用下面的scanf#includeint main{int a,b;float ,y;char c1,c2;scanf(a=?=d,a,b);scanf(fe,,y);scanf(cc,c1,c2);printf (a=d,b=d,=5.1f,y=5.2f,c1=c,c2=c,a,b,,y,c1,c2);return 0;}a=3,b=7,=8.5,y=71.82,A,a输入a=3 b=7 =8.5 y=71.82 A a输入a=3 b=7 8.5 71.82 A a输入a=3 b=7 8.5 71.82Aa输入精彩文档.实用标准文案3 7 8.5 71.82Aa输入a=3 b=7输入8.5 71.82Aaa=3 b=7 输入8.5 71.82 Aaa=3 b=7 输入8.5 71.82Aa 3.输入以下程序:#includeint main{精彩文档.实用标准文案int i,j,m,n;i=8;j=10;m=++i;n=j++;printf(d,d,d,d\n,i,j,m,n); return 0;}输出结果:行改成,将第67m=i++;n=++j;输出结果:精彩文档.。
C语言实验指导书的程序编码
C语言实验指导书的程序编码C语言实验程序编码1.平面上两点间的距离#include#includeint main(){double a1,a2;double b1,b2;double distance;printf("please input value a1 and a2:\n"); scanf("%lf%lf",&a1,&a2);printf("please input value b1 and b2:\n"); scanf("%lf%lf",&b1,&b2);distance=sqrt((a1-a2)*(a1-a2)+(b1-b2)*(b1-b2));printf("%lf\n",distance);return 0;}2.#include#includeint main(){double a1,a2;double b1,b2;double distance;printf("please input value a1 and a2:\n");scanf("%lf%lf",&a1,&a2);printf("please input value b1 and b2:\n");scanf("%lf%lf",&b1,&b2);distance=sqrt((a1-a2)*(a1-a2)+(b1-b2)*(b1-b2));printf("%lf\n",distance);return 0;}3.#include//int 占用四字节的32位机#define LOW16 0xFFFFint main(){unsigned int A=0,B=0,C=0,t=0;char hex[33]={0};printf("Please input two unsigned int A and B: "); scanf("%u%u",&A,&B);C=A & LOW16;C<<=16;C |= (B>>16) & LOW16;printf("A=0x%08x\n",A);printf("B=0x%08x\n",B);printf("C=0x%08x\n",C);return 0;}4.#include#includemain(){double x1,y1;double x2,y2;double s;printf("please input a point a:\n");scanf("x1=%lf,y1=%lf\n",&x1,&y1);printf("please input the orther point b:\n");scanf("x2=%lf,y2=%lf\n",&x2,&y2);s=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));printf("平面上任意两点间的距离:%lf\n",s);return 0;}5.#includevoid main(){int i,head,foot,rabbit,chick;printf("Enter the 2 numbers :");scanf("%d%d",&head,&foot);printf("head=%d,foot=%d\n",head,foot);for(i=0;i<=head;i++){rabbit=i;chick=head-rabbit;if(rabbit*4+chick*2==foot)printf("rabbit=%d,chick=%d\n",rabbit,chick);}}6.#include//int 占用四字节的32位机#define LOW16 0xFFFFint main(){unsigned int A=0,B=0,C=0,t=0;char hex[33]={0};printf("Please input two unsigned int A and B: \n"); scanf("%u%u",&A,&B);C=A & LOW16;C<<=16;C |= (B>>16) & LOW16;printf("A=0x%08x\n",A);printf("B=0x%08x\n",B);printf("C=0x%08x\n",C);return 0;}7.#include "stdio.h"main()long int rmb,a,b,c,d,e,f,gw,sw,i;printf("Please enter a number:");scanf("%d",&rmb);if(rmb>0 && rmb<=2147483647){sw=rmb%100;gw=rmb%10;a=rmb/100;b=sw/50;c=(sw-b*50)/20;d=(sw-b*50-c*20)/10;e=gw/5;f=(gw-e*5)/1;printf("$100=%d\n$50=%d\n$20=%d\n$10=%d\n$5=%d\n $1=%d\n",a,b,c,d,e,f);}else printf("Error of money!");return 0;}8.#includevoid main(){float a;printf("请输入成绩a:\n");scanf("%f",&a);if (a>=0&&a<=100){switch((int)(a/10)){case 10:case 9:printf("优\n");break;case 8:printf("良\n");break;case 7:printf("中\n");break;case 6:printf("及格\n");break;default:printf("差\n");break;}}elseprintf("输入错误\n");printf("a=%f\n",a);}9.#include//int 占用四字节的32位机int main(){unsigned int A,B,C;printf("Please input two unsigned int A and B: \n"); scanf("%u%u",&A,&B);A=A<<16;B=B>>16;C=A|B;printf("A=0x%08x\n",A);printf("B=0x%08x\n",B);printf("C=0x%08x\n",C);return 0;}10.#includemain(){unsigned int a,b,c;a=65;b=66;c=67;printf( "a=%c,b=%c,c=%c\n",a,b,c); } 11.#includeint main(){int h,f,x,y;printf( "Input h and f:\n ");scanf( "h=%d,f=%d",&h,&f);y=(f-h-h)/2;x=h-y;printf( "x=%d,y=%d\n ",x,y); return 0;}12.#includevoid main(){int x,y;printf("please enter x,y:");scanf("%d%d",&x,&y);printf("\nx+y=%d\n",x+y);printf("x-y=%d\n",x-y);printf("x*y=%d\n",x*y);printf("x/y=%d\n",x/y);printf("x%y=%d\n",x%y);}13.#includemain(){int h,f,x,y;printf("please input values h and f:\n"); scanf("%d,%d",&h,&f);if (h>0 && f>0){x=(4*h-f)/2;y=(f-2*h)/2;printf("x=%d,y=%d\n",x,y);}elseprintf("input error!\n");return 0;}14.#includeint main(){int i,j,k;for(i=0;i<=100/3;i++){for(j=0;j<=100/2;j++){for(k=0;k<=100;k+=2){if(((i+j+k)==100)&&((3*i+2*j+k/2 )== 100)) printf("%d ,%d ,%d\n", i,j,k);}}}return 0;}15.#includeint main(){int i,j,k;for(i=100/3;i>0;i--){for(j=100/2;j>0;j--){for(k=100;k>0;k-=2){if(((i+j+k)==100)&&((3*i+2*j+k/2 )== 100)) printf("%d ,%d ,%d\n", i,j,k);}}}return 0;}16.#include#include#include//模拟一次投骰子的函数,返回值为0~5,表示骰子的六个面int get_number(void){int num = 0;num = rand() % 6;return num;}main(){int data[6];int i=0;srand(time(NULL));//设置随机数种子for( i=0 ; i < 6 ; i++)//数据初始化{data[i]=0;}for( i=0 ; i<1000; i++)//进行1000次试验{data[get_number()]++;}for( i=0 ; i < 6 ; i++)//统计结果输出{printf("点数为%d的次数为: %d\n",i+1,data[i]);}return 0;}16.1. #include#include#include#includevoid main(){srand(time(NULL));double p1=0,p2=0,p3=0,p4=0,p5=0,p6=0; floati1=0,i2=0,i3=0,i4=0,i5=0,i6=0;for(int i=0;i<1000;i++){if(rand()%6+1==1){i1++;}if(rand()%6+1==2){i2++;}if(rand()%6+1==3){i3++;}if(rand()%6+1==4){i4++;}if(rand()%6+1==5){i5++;}if(rand()%6+1==6){i6++;}}p1=i1/1000;p2=i2/1000;p3=i3/1000;p4=i4/1000;p5=i5/1000;p6=i6/1000;printf("出现1的概率%f:\n",p1); printf("出现2的概率%f:\n",p2); printf("出现3的概率%f:\n",p3);printf("出现4的概率%f:\n",p4);printf("出现5的概率%f:\n",p5);printf("出现6的概率%f:\n",p6);}17.#includemain( ){har string[81];int i,num=0,word=0;char c;gets(string);for(i=0;(c=string[i])!=′\0′;i++)if(c==' '||c==' ')word=0;else if(word= =0){word=1;num++;}printf(″There are %d words in the line.\n″,num);}18.#include#define N 5void main(){int i,j;float max,min,t,a[N];float pjf,sum=0;printf("please input value a[i]:\n");for(i=0;i<n;i++)< p="">scanf("%f",&a[i]);for (i=0;i<n;i++)< p="">{sum=sum+a[i];pjf=sum/N;}max=min=a[0];for(i=1;i<n;i++)< p="">{if(max<a[i])< p="">{max=a[i];}if(min>a[i]){min=a[i];}}printf("大于平均值的数是:\n"); for(i=0;i<n;i++)< p=""> {if(a[i]>pjf)printf("a[%d]=%5.2f\t",i,a[i]); }printf("\n");printf("排列后的数是:\n");for(i=0;i<n-1;i++)< p=""> {for(j=i;j<n;j++)< p=""> {</n;j++)<></n-1;i++)<></n;i++)<></a[i])<></n;i++)<></n;i++)<></n;i++)<>。
C语言程序设计实验及代码
C语言程序设计实验及代码随着计算机技术的快速发展,C语言作为一种高效且强大的编程语言,被广泛应用于软件开发、嵌入式系统以及科学计算等领域。
在学习C语言编程的过程中,进行实验并编写相应的代码是非常重要的一部分,可以帮助我们巩固所学的知识并提高编程能力。
本文将介绍几个常见的C语言程序设计实验,并给出相应的代码示例。
一、实验一:Hello, World!实验目的:熟悉C语言编程环境,掌握简单的编译和运行操作。
实验内容:编写一个简单的程序,在屏幕上输出"Hello, World!"。
代码示例:```c#include <stdio.h>int main() {printf("Hello, World!\n");return 0;}```二、实验二:求两个整数的和与差实验目的:学习C语言中的基本数据类型和算术运算符的使用。
实验内容:编写一个程序,输入两个整数并计算它们的和与差,然后输出结果。
代码示例:```c#include <stdio.h>int main() {int num1, num2, sum, diff;printf("请输入两个整数:");scanf("%d %d", &num1, &num2);sum = num1 + num2;diff = num1 - num2;printf("两数之和:%d\n", sum);printf("两数之差:%d\n", diff);return 0;}```三、实验三:判断一个数是奇数还是偶数实验目的:学习C语言中的分支语句的使用。
实验内容:编写一个程序,输入一个整数,并判断它是奇数还是偶数,然后输出结果。
代码示例:```c#include <stdio.h>int main() {int num;printf("请输入一个整数:");scanf("%d", &num);if (num % 2 == 0) {printf("%d是偶数。
大学生c语言程序设计(第四版谭洪强)实验5答案
for(j=0;j<4;j++)
if(min>a[i][j])//如果当前值a[i][j]比最小值还要小
{min=a[i][j];row=i;colum=j;}//用a[i][j]改写最小值,并记下他们的下标
printf("矩阵中最小元素是:%d,行下标是:%d,列下标是%d\n",min,row,colum);
例如:输入3输入5
则输出:则输出:
1 2 3 1 2 3 4 5
2 4 6 2 4 6 8 10
3 6 9 3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
算法分析:
分析:m的值是不定的,所以可以申明一个二维数组int a[10][10],当m=3时,只用前三行三列,当m=5时,就只用前五行五列;元素a[i][j]的值和他的行列下标可找到规律如下:每行的第0列元素a[i][0]=i+1;除第0列元素其它元素a[i][j]=a[i][0]*(j+1)。
a[i][0]=t; //给第一列赋值t,即以前行的最后一列
}
printf("矩阵变换后:\n");
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
printf("%5d",a[i][j]);
printf("\n");
}
return 0;
}
运行结果:
5、设计程序sy4-5.c,从键盘输入m的值(2<=m<=9),并用双重循环给数组赋值,在m行m列的二维数组中存放如下所示的数据,并将结果输出在显示上。
C++程序设计实验指导书(第四版)
目录实验一熟悉Visual C++开发环境及上机过程错误!未定义书签。
实验二类的定义和对象的使用............. 错误!未定义书签。
实验三多文件结构及静态成员和对象数组的使用错误!未定义书签。
实验四类的继承和派生................... 错误!未定义书签。
实验五多继承 .......................... 错误!未定义书签。
实验六虚函数的使用..................... 错误!未定义书签。
实验七抽象类的使用..................... 错误!未定义书签。
实验八运算符重载和友元................. 错误!未定义书签。
实验九 STL的使用....................... 错误!未定义书签。
实验十 STL的使用....................... 错误!未定义书签。
实验十一利用AppWizard向导生成Windows应用程序错误!未定义书签。
C++实验上机指导实验一熟悉Visual C++开发环境及上机过程一、实验目的1、了解和使用VC集成开发环境;2、熟悉VC环境的基本命令和功能键;3、熟悉常用的功能菜单命令;4、学习使用VC++环境的帮助;5、学习完整的C++程序开发过程;6、理解简单的C++程序结构。
二、实验内容使用Visual C++ 6.0集成环境来编辑、编译并运行程序1:#include <iostream>using namespace std;class TAdd{public:TAdd(int a,int b){x=a;y=b;cout<<"constructor."<<endl;cout<<x<<","<<y<<endl;}~TAdd(){cout<<"destructor."<<endl;cout<<x<<","<<y<<endl;}int add(){return x+y;}private:int x,y;};int main(){TAdd p1(3,4);cout<<"x+y="<<p1.add()<<endl;TAdd p2(30,40);cout<<"x+y="<<p2.add()<<endl;return 0;}三、实验步骤启动Visual C++ 6.0——选择菜单“开始/程序/Microsoft Visual Studio6.0/Microsoft Visual C++ 6.0”,启动后的用户界面如图1-1所示。
C++程序设计实验指导书(第四版)
目录实验一熟悉Visual C++开发环境及上机过程 (1)实验二类的定义和对象的使用 (6)实验三多文件结构及静态成员和对象数组的使用 (7)实验四类的继承和派生 (9)实验五多继承 (11)实验六虚函数的使用 (13)实验七抽象类的使用 (16)实验八运算符重载和友元 (18)实验九 STL的使用 (19)实验十利用AppWizard向导生成Windows应用程序 (21)实验一熟悉Visual C++开发环境及上机过程一、实验目的1、了解和使用VC集成开发环境;2、熟悉VC环境的基本命令和功能键;3、熟悉常用的功能菜单命令;4、学习使用VC++环境的帮助;5、学习完整的C++程序开发过程;6、理解简单的C++程序结构。
二、实验内容使用Visual C++ 6.0集成环境来编辑、编译并运行程序1:#include <iostream>using namespace std;class TAdd{public:TAdd(int a,int b){x=a;y=b;cout<<"constructor."<<endl;cout<<x<<","<<y<<endl;}~TAdd(){cout<<"destructor."<<endl;cout<<x<<","<<y<<endl;}int add(){return x+y;}private:int x,y;};int main(){TAdd p1(3,4);cout<<"x+y="<<p1.add()<<endl;TAdd p2(30,40);cout<<"x+y="<<p2.add()<<endl;return 0;}三、实验步骤启动Visual C++ 6.0——选择菜单“开始/程序/Microsoft Visual Studio6.0/Microsoft Visual C++ 6.0”,启动后的用户界面如图1-1所示。
《C语言实验指导书》第4版
C语言程序设计实验指导书目录实验1 运行简单的C程序................... 错误!未定义书签。
实验2 练习使用算术运算符 ............... 错误!未定义书签。
实验3 使用输入、输出函数 ............... 错误!未定义书签。
实验4 模拟ATM取款机界面............. 错误!未定义书签。
实验5 循环语句的使用——猜数字游戏错误!未定义书签。
实验6 一维数组的应用 ....................... 错误!未定义书签。
实验7 二维数组的应用 ....................... 错误!未定义书签。
实验8 函数参数的传递 ....................... 错误!未定义书签。
实验9 嵌套与递归调用的实现 ........... 错误!未定义书签。
实验10 结构体和共用体 ..................... 错误!未定义书签。
实验11 文件.......................................... 错误!未定义书签。
课程设计:单链表的操作 ...................... 错误!未定义书签。
实验1 运行简单的C程序一.实训目的:1、熟悉TC运行环境2、练习简单C程序的编写3、熟练掌握C程序的上机运行步骤二.实训内容:1.实验要求:编程求37+29的值,熟悉TC运行环境2.分析与设计:编写一个main()函数,将数据37和29分别用变量保存,然后用另一个变量存放两个变量的和,最后将和变量的值输出。
3.源代码:main(){int a,b,sum;a=37;b=29;sum=a+b;printf(“sum=%d”,sum);}在TC环境中输入上面的代码后,先编译、连接,再保存,然后再运行。
具体操作步骤如下所示:(1)编译(2)连接C语言程序设计实验指导书(3)保存F2(4)运行CTRL+F9(5)运行结果ALT+F5按任意键返回注意:1、第(1)步和第(2)步可以合起来操作,使用快捷键F92、程序比较大时,最好边输入代码,边保存,以防止意外情况的发生。
C语言程序设计 上机实验指导与习题 参考答案(第四版)
C 语言程序设计上机实验指导与习题参考答案(第四版)(学生改编)实验 1:C语言程序初步一、实验目的(1)了解所用的计算机系统的基本操作方法,学会独立使用该系统。
(2)了解在该系统上如何编辑、编译、连接和运行一个 C程序。
(3)通过运行简单的 C 程序,初步了解 C程序的特点。
(4)在教师的指导下,学会使用在线评判系统。
二、实验内容1.运行第一个 C 程序题目:ThefirstCProgram将下列程序输入visualc ,编译、连接和运行该程序。
includequotstdio.hquotmain printfquotThefirstCProgramnquot具体操作步骤(1)在编辑窗口中输入程序。
(2)保存程序,取名为a1.c。
(3)按照第一章中介绍的方法,编译、连接和运行程序。
,则该题完成。
4按照第三章介绍的方法,将代码提交到在线评判系统,系统返回“通过”2.在在线评判系统中提交实现了计算 ab 功能的程序题目 1001:计算ab由键盘输入两个整数,计算并输出两个整数的和。
实现该功能的程序如下,inclu dequotstdio.hquotmain int ab scanfquotddquotampaampbprintfquotdquotab(1)在程序编辑窗口中输入程序。
(2)保存程序,取名为a2.c。
(3)按照前二章中介绍的方法,编译、连接和运行程序。
(4)在程序运行过程中,输入 15 30↙↙表示输入回车符(5)如果看到如下输出结果,则表明1530 的结果正确,如果得不到如下结果,则需检查并更正程序。
45(6)按照第三章中介绍的方法进入在线评判系统。
(7)显示题目列表,点击题号为 1001,题名为“计算ab”的题目。
(8)查看完题目要求后,点击页面下端的“sumbit” ,参照第二章提交程序的方法提交程序a2.c。
(9)查看评判结果,如果得到“accepted”则该题通过,否则返回第一步检查程序是否正确。
C语言程序设计实验指导 部分源代码
C语言程序设计部分实验参考答案实验22-1-2#include <stdio.h>int main(){int c,f=150;c=f*5/9-32*5/9;printf("fahr=%d, celsius=%d\n",f, c);return 0;}思考:不一样结果变为65 因为取整过程不同造成误差2-1-3#include <stdio.h>int main(){int c=26,f;f=c*9/5+32;printf("celsius=%d, fahr=%d\n",c, f);return 0;}#include <stdio.h>int main(){//2-1-4int math=87,eng=72,comp=93;printf("math=%d, eng=%d, comp=%d, average=%d\n",math, eng, comp,(math+eng+comp)/3);return 0;}#include <stdio.h>int main(){//2-1/5int n=152,a,b,c;c=n%10;b=(n/10)%10;a=n/100;printf("整数%d 的个位数字是%d, 十位数字是%d, 百位数字是%d\n",n, c, b, a);return 0;}#include <stdio.h>int main(){//2-1-6int x=3,y;y=x*x;printf("%d=%d*%d\n",y,x,x);printf("%d*%d=%d\n",x,x,y);return 0;}#include <stdio.h>int main(){//2-2-1double x, y;printf("Enter x:");scanf_s("%lf",&x);if(x!=0){y=1/x;}else{y=0;}printf("f(%.2f)=%.1f\n",x,y);return 0;}#include <stdio.h>int main(){//2-2-2int celsius, fahr;fahr=100;celsius=5*(fahr-32)/9;printf("fahr=%d, celsius=%d\n",fahr,celsius);return 0;}#include <stdio.h>int main(){//2-2-3double money, rate,x=1.0;int i,year;printf("Enter money, year and rate:");scanf("%lf %d %lf",&money,&year,&rate);for(i=1;i<=year;i++)x=x*(1+rate);printf("intrest=%.2f\n",money*x-money);return 0;}#include <stdio.h>#include<math.h>int main(){//2-2-4double x, y;printf("Enter x");scanf_s("%lf",&x);if(x<0.0)y=pow(x+1,2)+2*x+1/x;elsey=sqrt(x);printf("f(%.2f)=%.2f\n",x,y);return 0;}#include <stdio.h>#include<math.h>int main(){//2-2-5double x, y;printf("Enter x:");scanf_s("%lf",&x);if(x==10)y=1/x;elsey=x;printf("f(%.2f)=%.1f\n",x,y);return 0;}#include <stdio.h>int main(){//2-2-6int x, y;printf("Enter num1:");scanf_s("%d",&x);printf("Enter num2:");scanf_s("%d",&y);printf("%d+%d=%d\n",x,y,x+y);printf("%d-%d=%d\n",x,y,x-y);printf("%d*%d=%d\n",x,y,x*y);printf("%d/%d=%d\n",x,y,x/y);printf("%d%%d=%d\n",x,y,x%y);return 0;}#include <stdio.h>int main(){//2-3-1int i,sum=0;for(i=1;i<=100;i++){sum=sum+i;}printf("sum=%d\n",sum);return 0;}#include <stdio.h>int main(){//2-3-2int i,m,s=0;printf("Enter m:");scanf("%d",&m);for(i=m;i<=100;i++)s=s+i;printf("sum=%d\n",s);return 0;}#include <stdio.h>int main(){//2-3-3int i,m,n;double s=0.0;printf("Enter m:");scanf("%d",&m);printf("Enter n:");scanf("%d",&n);for(i=m;i<=n;i++)s=s+1.0/i;printf("sum=%f\n",s);return 0;}#include <stdio.h>int main(){//2-3-4int i,n;double s=0.0;printf("Enter n:");scanf("%d",&n);for(i=1;i<=n;i++)s=s+1.0/(2*i-1);printf("sum=%f\n",s);return 0;}#include <stdio.h>int main(){//2-3-5int i,n;double s=1.0,t=1.0,sign=1.0;printf("Enter n:");scanf("%d",&n);for(i=1;i<n;i++){sign=-sign;t=(t+3);s=s+1/t*sign;}printf("sum=%.3f\n",s);return 0;}#include <stdio.h>int main(){//2-3-6int low, uper;double fahr,celsius;printf("Enter low:");scanf_s("%d",&low);printf("Enter uper:");scanf_s("%d",&uper);printf("fahr celsius\n");for(fahr=low;fahr<=uper;fahr+=2){celsius=5.0/9.0*(fahr-32.0);printf("%3.0f %6.1f\n",fahr,celsius);}return 0;}#include <stdio.h>int main(){//2-3-7int m, n, i;double p,sum=0.0;printf("Enter m:");scanf("%d",&m);printf("Enter n:");scanf("%d",&n);p=1.0;for(i=1;i<=m;i++)p=p*i;sum=sum+p;p=1.0;for(i=1;i<=n;i++)p=p*i;sum=sum+p;printf("sum=%.0f\n",sum);return 0;}#include <stdio.h>int main(){//2-3-8int x, n,i;double p=1.0;printf("Enter x:");scanf("%d",&x);printf("Enter n:");for(i=1;i<=n;i++)p=p*x;printf("%d的%d次方是%.0f\n",x, n,p);return 0;}#include <stdio.h>#include<math.h>int main(){//2-4-1int n,i;printf("Enter n:");scanf("%d",&n);for(i=0;i<=n;i++)printf("pow(3,%d)=%.0f\n",i,pow(3.0,i));return 0;}#include <stdio.h>#include<math.h>int main(){//2-4-4double sum=0.0,i;for(i=100;i<=1000;i++){sum+=sqrt(i);}printf("sum=%.2f\n",sum);return 0;}// return 0;}#include <stdio.h>#include<math.h>int main(){//2-2-5double x, y;printf("Enter x:");scanf_s("%lf",&x);if(x==10)y=1/x;elsey=x;printf("f(%.2f)=%.1f\n",x,y);return 0;}#include <stdio.h>int main(){//2-2-6int x, y;printf("Enter num1:");printf("Enter num2:");scanf_s("%d",&y);printf("%d+%d=%d\n",x,y,x+y);printf("%d-%d=%d\n",x,y,x-y);printf("%d*%d=%d\n",x,y,x*y);printf("%d/%d=%d\n",x,y,x/y);printf("%d%%d=%d\n",x,y,x%y);return 0;}#include <stdio.h>int main(){//2-3-1int i,sum=0;for(i=1;i<=100;i++){sum=sum+i;}printf("sum=%d\n",sum);return 0;}#include <stdio.h>int main(){//2-3-2int i,m,s=0;printf("Enter m:");scanf("%d",&m);for(i=m;i<=100;i++)s=s+i;printf("sum=%d\n",s);return 0;}#include <stdio.h>int main(){//2-3-3int i,m,n;double s=0.0;printf("Enter m:");scanf("%d",&m);printf("Enter n:");scanf("%d",&n);for(i=m;i<=n;i++)s=s+1.0/i;printf("sum=%f\n",s);return 0;}#include <stdio.h>int main(){//2-3-4int i,n;double s=0.0;printf("Enter n:");scanf("%d",&n);for(i=1;i<=n;i++)s=s+1.0/(2*i-1);printf("sum=%f\n",s);return 0;}#include <stdio.h>int main(){//2-3-5int i,n;double s=1.0,t=1.0,sign=1.0;printf("Enter n:");scanf("%d",&n);for(i=1;i<n;i++){sign=-sign;t=(t+3);s=s+1/t*sign;}printf("sum=%.3f\n",s);return 0;}#include <stdio.h>int main(){//2-3-6int low, uper;double fahr,celsius;printf("Enter low:");scanf_s("%d",&low);printf("Enter uper:");scanf_s("%d",&uper);printf("fahr celsius\n");for(fahr=low;fahr<=uper;fahr+=2){celsius=5.0/9.0*(fahr-32.0);printf("%3.0f %6.1f\n",fahr,celsius);}return 0;}#include <stdio.h>int main(){//2-3-7int m, n, i;double p,sum=0.0;printf("Enter m:");scanf("%d",&m);printf("Enter n:");scanf("%d",&n);p=1.0;for(i=1;i<=m;i++)p=p*i;sum=sum+p;p=1.0;for(i=1;i<=n;i++)p=p*i;sum=sum+p;printf("sum=%.0f\n",sum);return 0;}#include <stdio.h>int main(){//2-3-8int x, n,i;double p=1.0;printf("Enter x:");scanf("%d",&x);printf("Enter n:");scanf("%d",&n);for(i=1;i<=n;i++)p=p*x;printf("%d的%d次方是%.0f\n",x, n,p);return 0;}#include <stdio.h>#include<math.h>int main(){//2-4-1int n,i;printf("Enter n:");scanf("%d",&n);for(i=0;i<=n;i++)printf("pow(3,%d)=%.0f\n",i,pow(3.0,i));return 0;}#include <stdio.h>#include<math.h>int main(){//2-4-4double sum=0.0,i;for(i=100;i<=1000;i++){sum+=sqrt(i);}printf("sum=%.2f\n",sum);return 0;}实验3#include<stdio.h>#include<math.h>int main(){//3-1double a, b, c, d;printf("Enter a, b, c: ");scanf("%lf%lf%lf",&a,&b,&c);d=b*b-4*a*c;if(a==0)if(b==0)if(c==0)printf("系数均为,方程无意义!\n");elseprintf("a、b为,c不为,方程不成立!\n");elseprintf("x=%.2f\n",c/b);elseif(d>=0){printf("x1=%.2f\n",(-b+sqrt(d))/(2*a));printf("x1=%.2f\n",(-b-sqrt(d))/(2*a));}else{printf("x1=%.2f+%.2fi\n",-b/(2*a),sqrt(-d)/(2*a));printf("x1=%.2f-%.2fi\n",-b/(2*a),sqrt(-d)/(2*a));}return 0;}#include<stdio.h>int main(){//3-2int x,y;printf("Enter x: ");scanf("%d",&x);if(x<0)y=-1;else if(x==0)y=0;elsey=1;printf("sign(%d)=%d\n",x,y);return 0;}#include<stdio.h>int main(){//3-3int letter=0,blank=0,digit=0,other=0,i;char ch;printf("Input 10 characters: ");for(i=0;i<10;i++){ch=getchar();if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')letter++;else if(ch==' '||ch=='\n')blank++;else if(ch<='9'&&ch>='0')digit++;elseother++;}printf("letter=%d, blank=%d, digit=%d,other=%d\n",letter,blank,digit,other);return 0;}#include<stdio.h>int main(){//3-4char grade;printf("Input Grade: ");grade=getchar();switch(grade){case 'A':;case 'a':printf("%c对应的百分制成绩区间是~100\n",grade);break;case 'B':;case 'b':printf("%c对应的百分制成绩区间是~90\n",grade);break;case 'C':;case 'c':printf("%c对应的百分制成绩区间是~79\n",grade);break;case 'D':;case 'd':printf("%c对应的百分制成绩区间是~69\n",grade);break;case 'E':;case 'e':printf("%c对应的百分制成绩区间是~59\n",grade);break;default:printf("数据输入错误\n");}return 0;}#include<stdio.h>int main(){//3-5int choice,count;for(count=0;count<5;count++){printf("[1]apple\n");printf("[2]pear\n");printf("[3]orange\n");printf("[4]grape\n");printf("[0]exit\n");printf("Enter your choice:");scanf("%d",&choice);switch(choice){case 0: return 0;case 1:printf("price is %.2f\n",3.0);break;case 2:printf("price is %.2f\n",2.5);break;case 3:printf("price is %.2f\n",4.1);break;case 4:printf("price is %.2f\n",10.2);break;default:printf("price is %.2f\n",0.0);}}return 0;}#include<stdio.h>int main(){//3-6int i;double grade;for(i=0;i<5;i++){printf("Enter grade: ");scanf("%lf",&grade);if(grade<60.0)printf("Fail\n");elseprintf("Pass\n");}return 0;}#include<stdio.h>#include<math.h>int main(){//3-7double a,b,c,s;printf("Enter 3 sides of the triangle: ");scanf("%lf %lf %lf",&a,&b, &c);if(a+b>c&&a+c>b&&b+c>a){s=(a+b+c)/2;printf("area=%.2f; perimeter=%.2f\n",sqrt(s*(s-a)*(s-b)*(s-c)),a+b+c);}elseprintf("These sides do not correspond to a valid triangle\n");return 0;}#include<stdio.h>int main(){//3-8double salary,rate;printf("Enter the salary: ");scanf("%lf",&salary);if(salary<=850)rate=0.0;else if(salary<=1350.0)rate=0.05;else if(salary<=2850.0)rate=0.1;else if(salary<=5850.0)rate=0.15;elserate=0.2;printf("tax=%.2f\n",rate*(salary-850));return 0;}#include<stdio.h>int main(){//3-9int i,n,grade,a=0,b=0,c=0,d=0,e=0;printf("Enter n: ");scanf("%d",&n);for(i=0;i<n;i++){printf("Enter grade %d: ",i+1);scanf("%d",&grade);switch(grade/10){case 9:case 10:a++;break;case 8:b++;break;case 7:c++;break;case 6:d++;break;case 5:case 4:case 3:case 2:case 1:case 0:e++;break;}}printf("The number of A(90~100):%d\n",a);printf("The number of B(80~89):%d\n",b);printf("The number of C(70~79):%d\n",c);printf("The number of D(60~69):%d\n",d);printf("The number of E(0~59):%d\n",e);return 0;}#include<stdio.h>int main(){//3-10char sign;int x,y;printf("输入x运算符y:");scanf("%d%c%d",&x,&sign, &y);if(sign=='*')printf("%d * %d = %d\n",x,y,x*y);else if(sign=='/')printf("%d / %d = %d\n",x,y,x/y);else if(sign=='%')printf("%d Mod %d = %d\n",x,y,x%y);elseprintf("运算符输入错误!\n");return 0;}实验4#include<stdio.h>#include<cmath>int main(){//4-1-2int flag,denominator;double s,eps,item;printf("Input eps:");scanf("%lf",&eps);s=0.0;flag=1;denominator=1;item=1.0;while(fabs(item)>eps){item=flag*1.0/denominator;s=s+item;flag=-flag;denominator=denominator+3;}printf("s=%.6f\n",s);return 0;}#include<stdio.h>int main(){//4-1-3int x,sum;sum=0;printf("Input integers:");scanf("%d",&x);while(x>0){if(x%2!=0)sum=sum+x;scanf("%d",&x);}printf("The sum of the odd numbers is %d\n",sum);return 0;}#include<stdio.h>int main(){//4-1-4int num,count;printf("Input an integer:");scanf("%d",&num);count=0;do{num=num/10;count++;}while(num!=0);printf("count=%d\n",count);return 0;}#include<stdio.h>int main(){//4-1-5int n,x,min,i;printf("Input n:");scanf("%d",&n);printf("Input numbers:");scanf("%d",&x);min=x;for(i=1;i<n;i++){scanf("%d",&x);if(x<min)min=x;}printf("min=%d\n",min);return 0;}int main(){//4-1-6int n,s=0,c=0;printf("Input an integer:");scanf("%d",&n);do{s+=n%10;n/=10;c++;}while(n);printf("count=%d, sum=%d\n",c,s);return 0;}#include<stdio.h>int main(){//4-1-7double a,b,c,s=0.0;int i,n;printf("Input n:");scanf("%d",&n);for(a=1.0,b=1.0,i=0;i<n;i++,c=a+b,a=b,b=c) s+=(1.0+a/b);printf("s=%.2f\n",s);return 0;}#include<stdio.h>int main(){//4-1-8int a,n,i,s=0,t=0;printf("Input a, n:");scanf("%d%d",&a,&n);for(i=0;i<n;i++){t=10*t+a;s+=t;}printf("s=%d\n",s);return 0;}#include<stdio.h>int main(){//4-1-9int n,i;double h,sum=0.0;printf("Input height: ");scanf("%lf",&h);printf("Input n: ");scanf("%d",&n);sum+=h;for(i=1;i<=n;i++){sum+=h;h/=2.0;}printf("distance=%.1f\n",sum);printf("height=%.1f\n",h);return 0;}#include<stdio.h>#include<math.h>int main(){//4-2-2int m,n,i,j,st,c=0;printf("Input m: ");scanf("%d",&m);if(m==1)m++;printf("Input n: ");scanf("%d",&n);for(i=m;i<=n;i++){st=sqrt((double)i);for(j=2;j<=st;j++)if(!(i%j))break;if(j>st){printf("%d ",i);c++;if(!(c%6))printf("\n");}}return 0;}#include<stdio.h>int main(){//4-2-3int money,f1,f2,f5,count=0;printf("Input money: ");scanf("%d",&money);for(f1=1;f1<=money-7;f1++)for(f2=1;f2<=money-6;f2++)for(f5=1;f5<=money-3;f5++)if(f1+2*f2+5*f5==money)count++;printf("count=%d\n",count);for(f1=1;f1<=money-7;f1++)for(f2=1;f2<=money-6;f2++)for(f5=1;f5<=money-3;f5++)if(f1+2*f2+5*f5==money)printf("fen5: %d fen2: %d fen1: %d\n",f5,f2,f1);return 0;}#include<stdio.h>int main(){//4-2-4int m,n,k,a,b,c,tmp;printf("Input m: ");scanf("%d",&m);printf("Input n: ");scanf("%d",&n);for(k=m;k<=n;k++){tmp=k;a=tmp%10;tmp/=10;b=tmp%10;tmp/=10;c=tmp;if(a*a*a+b*b*b+c*c*c==k)printf("%d\t(%d*%d*%d+%d*%d*%d+%d*%d*%d=%d)\n",k,c,c,c,b,b,b,a,a,a,k);}return 0;}#include<stdio.h>int main(){//4-2-5int i,j,s;for(i=1;i<=200;i++){s=1;for(j=2;j<=i/2;j++)if(i%j==0)s=s+j;if(s==i){printf("%d=1",i);for(j=2;j<=i/2;j++)if(i%j==0)printf("+%d",j);printf("\n");}}return 0;}#include<stdio.h>//5-2int sign(int x){if(x>0)return 1;if(x==0)return 0;return -1;}int main(){int x;printf("Enter x:");scanf("%d",&x);printf("sign(%d)=%d\n",x,sign(x));return 0;}#include<stdio.h>//5-3int even(int x){return (x%2==0);}int main(){int x,s=0;printf("Input integers: ");scanf("%d",&x);while(x>0){if(!even(x))s+=x;scanf("%d",&x);}printf("The sum of the odd is %d\n",s);return 0;}#include<stdio.h>//5-4#include<math.h>double distance(double x1,double y1, double x2, double y2){ return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}int main(){double x1, y1, x2, y2;printf("Input (x1, y1):");scanf("%lf%lf",&x1,&y1);printf("Input (x2, y2):");scanf("%lf%lf",&x2,&y2);printf("distance=%.2f\n",distance(x1,y1,x2, y2));return 0;}#include<stdio.h>//5-5#include<math.h>int prime(int m){int k,sqrtm=sqrt((double)m);for(k=2;k<=sqrtm;k++)if(m%k==0)return 0;return 1;}int main(){int m,n,c=0,s=0,k;printf("Input m:");scanf("%d",&m);printf("Input n:");scanf("%d",&n);for(k=(m==1?2:m);k<=n;k++)if(prime(k)){c++;s+=k;}printf("count=%d, sum=%d\n",c,s);return 0;}#include<stdio.h>//5-6int countdigit(int number, int digit){int c=0;if(number<0)number=-number;do{if(number%10==digit)c++;number/=10;}while(number);return c;}int main(){int n;printf("Enter an integer:");scanf("%d",&n);printf("Number of digit 2: %d\n",countdigit(n,2));return 0;}#include<stdio.h>//5-7int is(int n){int s=0,x,m=n;do{x=m%10;s+=x*x*x;m/=10;}while(m);if(s==n)return 1;return 0;}int main(){int m,n,i;printf("Input m:");scanf("%d",&m);printf("Input n:");scanf("%d",&n);for(i=m;i<=n;i++)if(is(i))printf("%d\n",i);return 0;}#include<stdio.h>//5-8double fact(int n);int main(void){int i;double sum=0.0;/*需要初始化为*/for(i=1;i<=10;i++)/*上界是*/sum=sum+fact(i);printf("1!+2!+...+10!=%f\n",sum);return 0;}double fact(int n){int i;double result=1.0;for(i=1;i<=n;i++)result=result*i;/*result中累积n!*/ return result;}实验7#include<stdio.h>int main(){//7-2-4int a[6][6],n,i,j,flag=1;printf("Input n:");scanf("%d",&n);printf("Input array:\n");for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);for(j=0;j<n-1;j++){for(i=j+1;i<n;i++)if(a[i][j]){flag=0;break;}if(flag==0)break;}if(flag==0)printf("No\n");elseprintf("Yes\n");return 0;}#include<stdio.h>int main(){//7-2-6int a[6][6],n,i,j,row,col;printf("Input n:");scanf("%d",&n);printf("Input array:\n");for(i=0;i<n;i++)/*输入矩阵*/for(j=0;j<n;j++)scanf("%d",&a[i][j]);for(i=0;i<n;i++){/*对矩阵的每一行*/row=i;col=0;for(j=0;j<n;j++)/*找当前行的最大值元素a[row][col]*/if(a[i][j]>a[row][col]){col=j;}for(j=0;j<n;j++)/*检测a[row][col]是否为第col列的最小元素*/ if(a[j][col]<a[row][col])break;if(j>=n){//找到鞍点元素a[row][col]printf("a[%d][%d]=%d\n",row,col,a[row][col]);break;}}if(i>=n)/*所有行都检测过没有找到鞍点*/printf("NO\n");return 0;}#include<stdio.h>int main(){//7-3-2char c,s[80];int i,index=-1;printf("Input a character:");c=getchar();getchar();printf("Input a string:");gets(s);i=0;while(s[i]!='\0'){if(s[i]==c)index=i;i++;}if(index>-1)printf("index=%d\n",index);elseprintf("Not found\n");return 0;}#include<stdio.h>int main(){//7-3-3char s[80];int i,upcase=0,count=0;printf("Input a string:");gets(s);i=0;while(s[i]!='\0'){if(s[i]>='A'&&s[i]<='Z'){upcase++;switch(s[i]){case 'A':case 'E':case 'I':case 'O':case 'U':count++;}}i++;}if(upcase)count=upcase-count;printf("count=%d\n",count);return 0;}#include<stdio.h>int main(){//7-3-4char s[80];int i;printf("Input a string:");gets(s);i=0;while(s[i]!='\0'){if(s[i]>='A'&&s[i]<='Z')s[i]='Z'-(s[i]-'A');i++;}printf("After replaced: %s\n",s);return 0;}#include<stdio.h>int main(){//7-3-5char s[80];int i,n=0;printf("Input a string:");gets(s);i=0;while(s[i]!='\0'){if(s[i]>='0'&&s[i]<='9'){n=n*16+s[i]-'0';i++;continue;}switch(s[i]){case 'a':case 'A':n=n*16+10;break;case 'b':case 'B':n=n*16+11;break;case 'c':case 'C':n=n*16+12;break;case 'd':case 'D':n=n*16+13;break;case 'e':case 'E':n=n*16+14;break;case 'f':case 'F':n=n*16+15;break;}i++;}printf("Hex=%d\n",n);return 0;}实验8#include<stdio.h>//8-1-1void move(int *, int, int);int main(){int m, n, i, a[80], *p;printf("Input n, m:");scanf("%d%d",&n, &m);for(p=a,i=0;i<n;i++)scanf("%d",p+i);/*将&p++换成p+i*/ move(a,n,m);printf("After moved:");for(i=0;i<n;i++)printf("%5d",a[i]);return 0;}void move(int *x,int n, int m){int i,j,k;for(i=0;i<m;i++){k=x[n-1];/*暂存x[n-1],因为它将被覆盖*/for(j=n-1;j>0;j--)x[j]=x[j-1];/*设置断点*/x[0]=k;}}#include<stdio.h>//8-1-2#include<stdlib.h>int search(int *, int, int);int main(){int *a,n,i,x,index;printf("Input n:");scanf("%d",&n);if((a=(int*)malloc(n*sizeof(int)))!=NULL) exit(1);printf("Input %d integers:",n);for(i=0;i<n;i++)scanf("%d",a+i);printf("Input x:");scanf("%d",&x);index=search(a,n,x);if(index==-1)printf("Not found\n");elseprintf("index=%d\n",index+1);free(a);return 0;}int search(int *a,int n,int x){int i;for(i=0;i<n;i++)if(a[i]==x)return i;return -1;}#include<stdio.h>//8-1-3#include<stdlib.h>void sort(int*,int);int main(){int i,n,*a;printf("Input n:");scanf("%d",&n);if((a=(int*)malloc(n*sizeof(int)))!=NULL) exit(1);printf("Input %d integers:",n);for(i=0;i<n;i++)scanf("%d",a+i);sort(a,n);printf("After sorted the array is:");for(i=0;i<n;i++)printf("%d ",a[i]);printf("\n");return 0;}int max(int *a,int n){int i,index=0;for(i=1;i<n;i++)if(a[i]>a[index])index=i;return index;}void swap(int *x,int *y){int t=*x;*x=*y;*y=t;}void sort(int *a,int n){int i,j;for(i=n;i>0;i--){j=max(a,i);swap(a+i-1,a+j);}}#include<stdio.h>//8-1-4#include<stdlib.h>int main(){int n,i,total,token,*a;printf("Input n:");scanf("%d",&n);if((a=(int*)malloc(n*sizeof(int)))==NULL) exit(1);for(i=0;i<n;i++)a[i]=i+1;total=n;i=0;while(total>1){token=0;/*报数的当前值*/while(token<3){/*未出队的依次报数*/if(a[i]!=0)token++;if(token<3)/*未满m继续报数*/i=(i+1)%n;}a[i]=0;/*报到的出队*/i=(i+1)%n;total--;/*队中人数减*/}for(i=0;i<n;i++)if(a[i]!=0)break;printf("Last No. is: %d\n",a[i]);free(a);return 0;}#include<stdio.h>//8-2-2#include<string.h>int main(){char s[80],m[80];int length,max=0,i;printf("Input 5 strings: ");for(i=0;i<5;i++){scanf("%s",s);length=strlen(s);if(length>max){max=length;strcpy(m,s);}}printf("The longest is: %s\n",m);return 0;}#include<stdio.h>//8-2-3#include<string.h>void delchar(char *s,char c){int i=0;while(*s!='\0'){if(*s==c)/*遇到字符c*/strcpy(s,s+1);/*将该字符后的部分拷贝成从当前位置开始的串*/ s++;}}int main(){char s[80],c;printf("Input a string: ");gets(s);printf("Input a char: ");scanf("%c",&c);delchar(s,c);printf("After deleted, the string is: %s\n",s);return 0;}#include<stdio.h>//8-2-4#include<string.h>void strmcpy(char *s, char *t, int m){strcpy(s,t+m-1);}int main(){char s[80],t[80];int m;。
大学生C语言程序设计(第四版 谭洪强)实验4 答案
printf("Input insert number:");
scanf("%d",&x);
for(i=4;i>=0;i--) //从最后一个元素开始往前找插入位置
if(a[i]>x) //若元素值大于 x
a[i+1]=a[i]; //则该元素往后移
else
//否则
break;
//退出循环,找到插入位置在 i+1 位置
for(i=0;i< N;i++) printf("%4d",a[i]);
printf("\n");
printf("Enter m: "); scanf("%d",&m);
low=0;high=9; //设置初始查找范围
while(low<=high) //有查找范围,进入循环
{ mid=(low+high)/2; //求 mid,查找范围的中间元素下标
外循环:10 个数,需要 9 趟循环,第 i 趟从未排序数列(a[i],a[i+1],---a[9])里找出最小 数和 a[i]交换。内循环:(第 i 趟里找最小数的方法)1)首先把第 i 个数看成最小数记下它 的下标(k=i);2)然后从下标为 j=i+1 的元素开始一直到 j=9,逐一去比较,如果当前的元素 a[j]比最小数 a[k]还小,就用它的下标去改写 k;3)如果 k 的值发生了变化,就交换最小值 a[k]和未排序元素里的第一个元素 a[i]。 参考答案:
a[i]=a[i]*x; printf("数组每个元素乘以 x 后的值为:"); for(i=0;i<10;i++)
C程序设计(第四版)学习辅导实验
(1)检查所用的计算机系统是否已安装了c编译系统并确定它所在的子目录。
(2)进入所用的c编译集成环境。
(3)熟悉集成环境的界面和有关菜单的使用方法。
(4)输入并运行一个简单的正确的程序。
①输入下面的程序:#include <stdio.h>int main(){printf(“This is a c program.\n”);return 0;}②仔细观察屏幕上已输入的程序,检查有无错误。
实验结果:③对源程序进行编译,观察屏幕上的显示的编译信息。
如果出现“出错信息”,则应找出原因并改之,在进行编译,如果无错,则进行连接。
④如果编译连接无错误,运行程序,观察分析运行结果。
(5)输入并编辑一个有错误的c程序。
①输入以下程序。
#include <stdio.h>Int main(){int a,b,suma=123;b=456;sum=a+bprint(“sum is %d\n”,sum);return 0;}②进行编译,仔细分析编译信息窗口,可能显示有多个错误,逐个修改,直到不出现错误。
③使程序运行,分析运行结果。
Compiling...cai25412.cppF:\Program Files\Microsoft Visual Studio\MyProjects\cainian\cai25412.cpp(4) : error C2146: syntax error : missing ‘;’ before identifier ‘a’F:\Program Files\Microsoft Visual Studio\MyProjects\cainian\cai25412.cpp(6) : error C2146: syntax error : missing ‘;’ before identifier ‘print’F:\Program Files\Microsoft Visual Studio\MyProjects\cainian\cai25412.cpp(6) : error C2065: ‘print’ : undeclared identifier执行cl.exe时出错.cai25412.obj - 1 error(s), 0 warning(s)实验结果:(6)输入并运行一个需要在运行时输入数据的程序。
C语言程序设计实验指导(源代码)
《C语言程序设计》实验源程序代码目录实验二顺序结构程序设计(上) (2)实验三顺序结构程序设计(下) (2)实验四选择结构程序设计(上) (4)实验五选择结构程序设计(下) (4)实验六循环结构程序设计(上) (5)实验七循环结构程序设计(下) (6)实验八函数 (8)实验九数组(上) (9)实验十数组(下) (11)实验十一指针的应用 (12)实验十二结构体 (14)实验十三文件操作 (17)实验二顺序结构程序设计(上)//1-1void main(){ int a=1;printf("1:%d\n", sizeof(a));printf("2:%d\n", sizeof(float));printf("3:%d\n", sizeof(2.0*10));printf("4: %d\n", sizeof("Liu Dehua"));}//1-2-------------------------------------------------------- #include <stdio.h>void main(){ int a1=65,a2,a3;char ch='A'+1;printf("1:\101 \x42 C\n");printf("2:I say:\"How are you?\"\n");printf("3:\t\\\\Display Hello! I am Tom\b!\n");printf("4:65/10=%d,65%%10=%d\n",65/10, 65%10);printf("5:a1=%d,a1=\'%c\'\n",a1,a1);printf("6:ch=%c,ch=%d\n",ch,ch);a2=2,a3=(a1=3,a2+=a1,a1*a2);printf("7:a1=%d,a2=%d,a3=%d\n",a1,a2,a3);a3=a2=a1=2;printf("8:%d,%d,%d\n",(a1,a2+1),a3+2,a1+a2+a3);}//2------------------------------------------------------------#include <stdio.h>void main(){ float c,f;printf("请输入一个华氏温度:\n");scanf("%f",f);c==(5/9)*(f-32);printf("摄氏温度为:%5.2f\n"c);}实验三顺序结构程序设计(下)//1-1#include "stdio.h"void main(){ char ch=65;int a=1234;long g=12345678;float f=123.456789f;double d=9.876543E2;printf("1:%d,%c,%5d,%3c\n",ch,ch,ch,ch);printf("2:%8d,%-8d,%o,%x\n",a,a,a,a);printf("3:%10.3f,%f,%e\n",f,f,f);printf("4:%-10.2lf,%lf,%le\n",d,d,d);printf("5:%10s,%10.3s\n","Hello,world!","Hello,world!");}//1-2#include <stdio.h>main(){ int a,b,c;a=getchar();b=getchar();c=getchar();printf("a=%d b=%d c=%d\n",a,b,c);printf("a=%c b=%c c=%c\n",a,b,c);}//1-3---------------------------------------------------------------------------------------#include "stdio.h"void main(){int a=10,b;int *p; // p为指向整型变量的指针变量 printf("1:a的值=%d, a的内存地址值=%d\n",a, &a); //&a:获取变量a的内存地址值p=&a; //将变量a的内存地址值赋给指针变量p printf("2:p的值=%d, p的内存地址值=%d,p所指向的内存单元的值=%d\n",p,&p,*p);scanf("%d,%d" ,p,&b); //运行时输入100,200【enter】printf("3:a的值=%d,b的值=%d,p所指向的内存单元的值=%d\n",a,b,*p);}//3-3-----------------------------------------------------------#include <stdio.h>#include <math.h> //包含数学函数库void main(){ double a,b,c,p,s;printf("输入三角形三边a,b,c: ");scanf("%lf,%lf,%lf",&a,&b,&c);if(a+b>c && b+c>a && a+c>b) //判断该三边的值是否能构成一个三角形{ p= ;s=sqrt( ); //sqrt():求平方根函数printf("三角形面积=%lf\n",s);}elseprintf("三边(a=%lf,b=%lf,c=%lf)不能构成三角形! ",a,b,c);}实验四选择结构程序设计(上)//2#include <stdio.h>void main(){ char ch,c1,c2;printf("Enter a character:");ch=getchar();if((ch>='A')||(ch<='Z')&& (ch>='a')||(ch<='z'))c1=ch-1;c2=ch+1;if(ch='A'&&ch=’a’) c1=ch+25;else if(ch='Z'&&ch=’z’) c2=ch-25;putchar(c1);putchar(ch);putchar(c2);putchar('\n');}实验五选择结构程序设计(下)//1#include "stdio.h"void main(){ char m;printf(“input the month number:\n”);scanf (“%c”,&m);switch (m){case 1 : printf ("Jan");case 2 : printf ("Feb");case 3 : printf ("Mar");case 4 : printf ("Apr");else : printf ("error");}}//2----------------------------------------------------------------------- #include <stdio.h>void main(){ int a,b,d;char ch;printf("Please input a expression:");scanf("%d%c%d", );switch(ch){ case '+': printf("%d+%d=%d\n",a,b,a+b);break;case '-': printf("%d-%d=%d\n",a,b,a-b);break;case '*': printf("%d*%d=%d\n",a,b,a*b);break;case '/': if( )printf("Divisor is zero\n");elseprintf("%d/%d=%lf\n",a,b, a/b); /*强制类型转换*/;default: printf("Input Operator error!\n");}}实验六循环结构程序设计(上)//1-1#include <stdio.h>void main(){ int i=1,sum=0;while(i<100)sum=sum+i;i++;printf("Sum = %d\n",sum);}//1-2--------------------------------------------------------------------------------#include <stdio.h>void main(){ char x;x='Z';while(x!='A'){ printf("%3d",x);if(('Z'-x)%5= =0) printf("\n");x++;}}//2-1-----------------------------------------------------------------#include<stdio.h>void main(){ int n = 21,i;while ( ){ printf( "You take ... ");scanf("%d",&i);if ( )printf( "Don't try to cheat me, take 1, 2, 3, or 4, please! \n");else{ printf( "I take %d\n", 5-i);n = ; //剩下的火柴棍根数}}printf( " Now take the last one, please! \n Haha^-^, you loss!) \n "); }//2-2-------------------------------------------------------------------- #include <stdio.h>void main(){ int day,x1,x2;day=0;x1=1020;while( ){ x2= ;x1=x2;day++;}printf("day=%d\n",day);}//2-3---------------------------------------------------------------------- #include<stdio.h>void main(){ long x,m,y=0;int gw,count=0;printf("input x=");scanf("%ld",&x);;do{ gw=m%10;y= ;;count++;}while( );printf("x=%ld, count=%d, recycle=%ld\n",x,count,y);}}实验七循环结构程序设计(下)//1-1#include<stdio.h>void main(){ long sum(int);long s=0;int k;for(k=1;k<20;k+=2)s=s+sum(k);printf("s=%ld\n",s);}long sum(int x) //求1+2+...+x{ long result=0;int i;for(i=1;i<=x;i++)result+=i;return result;}//1-2------------------------------------------------------------------------#include<stdio.h>void main(){long s=0,result=0;int k,i;for(k=1;k<20;k+=2){for(i=1;i<=k;i++)result+=i;s=s+result;}printf("s=%ld\n",s);}//2-1--------------------------------------------------------------------------#include<stdio.h>void main(){ float score,sum=0,average;int persons=0;while(1){ scanf("%f",&score);if(score<0)continue;else if(score>100)break;else{ sum+=score;persons++;}}average=(float)(sum/persons);printf("人数=%d,平均分=%f\n",persons,average);}//2-2------------------------------------------------------#include <stdio.h>void main(){ int i,j;for (i=0;i<4;i++){ for (j=0;j<=20;j++)printf("\n");for (j=0;j<2*i+1;j++)printf("*");}}//2-3-----------------------------------------------------------#include "stdio.h"void main(){ int a,k,s=0;for (k=2; k<1000; k++){for (a=1; a<=k/2; a++)if(k%a==0) s+=a;if (s==k) //k是完数printf("%d is OK!\n",k);}}实验八函数//1-1#include "math.h"#include "stdio.h"void main(){ float a,b,c;printf("请输入三角形的边长值:\n");scanf("%f%f%f",a,b,c);if(a+b>c&&a+c>b&&b+c>a)printf("Area=%f\n",area(a,b,c));}area(float a,float b,float c){ float s,area_s;s=(a+b+c)/2.0;area_s=sqrt(s*(s-a)*(s-b)*(s-c));return(area_s);}//1-2--------------------------------------------------------------------------#include<stdio.h>int fun(int n){ int jc=1,i;for(i=2;i<=n;i++) jc*=n;return jc;}void main(){ int n;long sum=0;for(n=1;n<11;n++);if(n%3==0||n%5==0)sum+=fun(int n);printf("sum=%ld\n",sum);}//1-3---------------------------------------------------#include<stdio.h>swap(int *p1,int *p2){ int *p;p=p1;p1=p2;p2=p;}void main(){ int a,b;scanf("%d%d",&a,&b);printf("a=%d\tb=%d\n",a,b);swap(&a,&b);printf("a=%d\tb=%d\n",a,b);}//2-1-----------------------------------------------------------------#include<stdio.h>void printline(int n) //{ int i,k;for(i=1;i<=30-n;i++) printf("%c",' ');for(i=1;i<= ;i++) printf( );printf("\n");}void main(){ int i;for(i=1;i<=5;i++) ;for(i=4;i>=1;i--) ;}//2-2-------------------------------------------------------------------------------------------------------#include " stdio.h "int isprime(int m) //判别m是否为质数{ int i ;for(i=2;m%i!=0;i++); //注意:循环体是空语句return ( );}void main(){ int n,x,a,b;scanf("%d",&x);for(n=6;n<=x ;n+=2)for(a=3;a<=n/2;a+=2)if( ) //若a是素数{ b=n-a;if( ) //若b是素数{ printf(“%d=%d+%d”,n,a,b);; //退出a循环,判别下一个n的组合}}}实验九数组(上)//1-1#include<stdio.h>#define N 9;void main(){ int i,sum=0,a[N],max,min;for(i=0;i<N;i++)scanf("%d",a[i]);sum=max=min=a[0];for(i=0;i<N;i++){ sum+=a[i];if(a[i]>max)max=a[i];else if(a[i]<min)min=a[i];}for(i=0;i<N;i++){ printf("%8d",a[i]);if(i%3==2) printf("\n");}printf("max=%d min=%d average=%lf\n",max,min,sum*1.0/N);}//1-2--------------------------------------------------------------------------------------#include <stdio.h>void main(void){ const int n=3;int i,j,sum;int a[][n]={1,2,3,4,5,6,7,8,9};for(i=0;i<=n;i++)for(j=0;j<=n;j++)if(i==j||i+j==n)sum+=a[i][j];printf("sum=%d\n",sum);}//2-1--------------------------------------------------------------#include <stdio.h>#include <math.h>int Is_sushu(int x){ int i,re;re=x>1?1:0;for(i=2;i<=sqrt(x);i++)if( ){ re=0; break;}return(re);}void main(void){ const int n=10;int ss[n],hs[n],k,m,p1,p2;p1=p2=-1;for(k=1;k<=n;k++){ printf("NO %d:",k);scanf("%d",&m);if( ) //若m为素数{ p1++;ss[p1]=m;}else{ ;hs[p2]=m;}}for(k=0;k<=p1;k++) //输出素数数列printf("%4d ",ss[k]);printf(" count=%d\n",p1);for(k=0;k<=p2;k++) //输出合数数列printf("%4d ",hs[k]);printf("count=%d\n", );}//2-2-------------------------------------------------------#include<stdio.h>#include<stdlib.h>void main( ){ int a[10],i,j,t;for(i=0;i<10;i++){ a[i]= ; //生成随机整数printf("%d ",a[i]);}for(i=0;i<9;i++)for(j=0;j< ; j++)if(a[j]>a[j+1]){ t=a[j];a[j]=a[j+1]; ;}printf("\nthe sorted numbers: \n");for(i=0;i<10;i++)printf("%5d ",a[i]);}实验十数组(下)//1-1#include<stdio.h>void main( ){char a[]="wel",b[]="come";int i,n=0;while(!a[n]) n++;for(i=0;b[i]!='\0';i++)a[n+i]=b[i];printf("%s\n",a);}//1-2---------------------------------------------------------------#include<stdio.h>#define N 6int sum(int x,int n){ int i=0,s=0;while(i<=n)if(x[i]>0){s=s+x[i]; i++;}return s;}void main(){ int i=0,s,a[N];do{ printf("NO%d: ",i+1);scanf("%d",&a[i]);if((-100<=a[i])&&(a[i]<=100))i++;elseprintf("wrong! input again...\n");}while(i<N);s=sum(a[N],N);printf(" sum=%d\n",s);}//2-1------------------------------------------------------------#include "stdio.h"main(){ c har str[40];int alphabet=0,digit=0,space=0,other=0,i=0;printf("请输入一串字符:\n");gets(str);while( ){ if(('A'<=str[i])&&(str[i]<='Z')||('a'<=str[i])&&(str[i]<='z')) ++alphabet;else if(str[i]==' ') ++space;else if((str[i]<='9')&&(str[i]>='0')) ++digit;else ++other;i++;}printf("其中有%d个字母,%d个空格,%d个数字,%d个其他字符!\n",alphabet,space, digit,other);}//2-2-------------------------------------------------------------------------------------------------------#define N 50#define M 20#include <stdio.h>void main(){ char a[N][10]; //学号float score[N][M],max_score; //成绩int n,m,i,j,studid;/*输入学生的学号和各门课程的成绩*/printf("学生数n,课程数m:");scanf("%d,%d",&n,&m);for(i=1;i<=n;i++){ printf("输入第%d位学生的学号:",i);scanf("%s", );printf("按课程顺序输入成绩:");for(j=1;j<=m;j++)scanf("%f", );}/*找出每门课程最高的学生学号、课程代号和成绩并输出*/for(j=1;j<=m;j++){ studid=1;max_score=score[1][j];for(i=1;i<=n;i++)if(score[i][j]> max_score){ max_score=score[i][j];studid=i;}printf("第%d门课程最高分的学生学号是:%s,成绩为:%6.2f\n",j,a[studid],max_score);}}实验十一指针的应用//1-1#include <stdio.h>void main(){ int *p,i,a[5];p=a[1];printf("please enter 5 numbers:");for(i=0;i<5;i++)scanf("%d",p++);for(i=0;i<5;i++,p++)printf("%d ",*p);printf("\n");}//1-2-----------------------------------------------------------------------------------------#include<stdio.h>int digits(char *s){ int c=0;while(s){ if(*s >=0&&*s <=9)c++;s++;}return c;}void main(){ char s[80];printf("请输入一行字符\n");gets(s);printf("个数=%d\n",digits(s));}//2-1-------------------------------------------------------------#include<stdio.h>int findmax(int *s,int t ){ int i,k=0;for(i=0; ;i++)if( ) k=i;return ;}void main(){ int j,a[10]={12,23,34,45,56,67,78,89,11,22},k=0,*add;for(j=0;j<10;j++)printf("%4d%10xh\n",a[j],&a[j]);k=findmax(a,10);add=&a[k];printf("\n数组下标=%d,数组元素值=%d,元素地址=%xh\n",k,a[k], add);}//2-2------------------------------------------------------------------ #include <string.h>#include <stdio.h>#define N 81void fun(char *s){ int i=0,t,n= ;for(;s+i<s+n-1-i;i++){ t=*(s+i);*(s+i)= ;*(s+n-1-i)=t; }}void main(){ char a[N];gets(a);printf("The original string is:");puts(a);fun(a);printf("\nThe string after modified:"); puts(a);}//2-3-----------------------------------------------------------#include<stdio.h>#include "string.h"#define N 5void sort(char (*p)[20]) //利用行指针对N个字符串进行排序{ int i,j; char k[20];for (i=0; i<N-1; i++)for (j=i+1; j<N; j++)if (strcmp( ,*(p+j))>0) //*(p+i)表示第i个串{ strcpy(k,*(p+i));strcpy(*(p+i), );strcpy(*(p+j),k);}}void main(){ char ch[N][20]; int i,j;for (i=0; i<N; i++) gets(ch[i]); //输入N个串sort(ch); //利用行指针排序for(i=0; i<N; i++) puts(ch[i]); //输出}实验十二结构体//1-1#include<stdio.h>struct stud{ char name[20];int age;} ;fun(struct stud person[],int n){ int min,i;min=0;for(i=0;i<n;i++)if(person[i] <person[min] ) min=i;return (person );}void main(){ struct stud a[]={{"Zhao",21},{"Qian",20},{"Sun",19},{"LI",22}};int n=4;struct stud minpers;minpers=fun(a,n);printf("%s 是年龄小者,年龄是:%d\n",,minpers.age);}//2-1-----------------------------------------------------------#include<stdio.h>#include<string.h>struct bdata{ int id;char bname[20];float price;} ;int findbook(struct bdata st[],int n,char s[]){ int i;for(i=0;i<n;i++)if(________) return i;__________________;}void main(){ struct bdata book[100];char st[20];int index;printf("请输入要查找书名:";gets(st);index=findbook(_______________);if (index = = -1) printf("%s的书未找到\n",st);else printf("%s的书在%d位置\n",st,index);}//2-2------------------------------------------------------------------------------------------#include<stdio.h>#define M 3#define N 4struct stud{ int no;char name[16];float mark[M];float aver;};void input(struct stud st[]) //输入学生记录{ int i,j;float sum;for(i=0;i<N;i++){ printf("\n输入第%d个学生的学号,姓名:");scanf("%d,%s", );printf("\n输入第%d个学生的3门课程成绩:");for(j=0;j<M;j++)scanf("%f", );}}void output(struct stud st[]) //输出学生记录{ int i,j;printf("\n学号姓名课程1 课程2 课程3 平均分\n");for(i=0;i<N;i++){ printf("%5d %8s",st[i].no,st[i].name);for(j=0;j<M;j++)printf("%5.1f",st[i].mark[j]);printf("%5.2f",st[i].aver);}}void average(struct stud st[]) //求每个学生的平均成绩{ int i,j;float sum;for(i=0;i<N;i++){ sum= ;for(j=0;j<M;j++)sum= ;=sum/M;}}void main(){ struct stud preson[N];input( );average(preson);output(person);}//2-3---------------------------------------------------------------------------------------------------struct oneday{ int day,month,year; };void main(){ struct oneday day1;int alldays(struct oneday);printf("Enter year,month,day:");scanf("%d-%d-%d", );printf("%d\n",alldays(day1));}int runnian(int year){ if (year%400==0) return(1);if (year%4==0 && year%100!=0) return(1);return(0);}int alldays(struct oneday one){ int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int i,t=0;if (runnian(one.year)) day[2]=29;for (i=1; i< ;i++)t+=day[i];t=t+one.day;return(t);}实验十三文件操作//1-1#include <stdio.h>#include<string.h>void main(){ FILE *fp1;char ch[80];int i,j;fp1=fopen("C:\\data1.dat","b");for(i=1;i<=4;i++){ gets(ch);j=0;while(ch[j]!='\0'){ fputc(fp1, ch[j]); j++; }fputc(fp1,'\n');}fclose(fp1);}//1-2----------------------------------------------------------------------------------------#include <stdlib.h>#include <stdio.h>#include <time.h>void main ( ){ int x[10],i,k;FILE *fp2;srand( (unsigned)time( NULL ) );for (i=0;i<10;i++)x[i]=rand();fp2=fopen ("C:\\data2.dat","wb");if(fp2==NULL){ printf("Open error \n");exit(0);}for (int k=0 ; k<10 ; k++ )fwrite(x[k],sizeof(int), fp2);fclose (fp2) ;}//2-1----------------------------------------------------------------#include<stdio.h>#include<stdlib.h>void main(){ FILE *fp;int i=0;char *str[]={"visual C++","visual Basic","visual java","visual foxpro"};if((fp=fopen("C:\\data3.txt", ))==NULL){ printf("%s不能打开!\n","data3.txt");exit(1);}while(i<4){fprintf( );;}fclose(fp);//2-2-------------------------------------------------------------------------------------------------------------------#include<stdio.h>#include<stdlib.h>void main(){ char c;FILE *fp3; long num=0;if((fp3=fopen("c:\\ data3.txt", ))==NULL){ printf("Open error \n");exit(0); }c=fgetc(fp3);while ( ){;c=fgetc(fp3);}printf("\nnum=%ld\n", num);;}(注:可编辑下载,若有不当之处,请指正,谢谢!)。
第四版C语言实验报告实验3——顺序程序设计
第四版C语⾔实验报告实验3——顺序程序设计仲恺农业⼯程学院实验报告纸计算机科学与⼯程(院)计算机科学与技术专业计111班1 组C语⾔程序设计课实验3 最简单的C程序设计—顺序程序设计1.实验⽬的(1)掌握C语⾔中使⽤最多的⼀种语句—赋值语句的使⽤⽅法。
(2)掌握各种类型数据的愉⼈愉出的⽅法,能正确使⽤各种格式转换符。
2.实验内容和步骤(1)掌握各种格式转换符的正确使⽤⽅法①输⼈程序:#includevoid main(){int a, b;float d,e;char c1,c2;double f, g;long m, n;unsigned int p,q;a=61;b=62;c1='a';c2='b';d=3.56;e=-6.87;f=3157.890121;g=0.123456789;m=50000;n=-60000;p=32768;q=40000;printf("a=%d,b=%d\nc1=%c,c2=%c\nd=%6.2f,e=%6.2f\n",a,b,c1,c2,d,e);printf("f=%15.6f,g=%15.6f\nm=%ld,n=%ld\np=%u,q=%u\n",f,g,m,n,p,q);}②运⾏此程序并分析结果。
预计结果:a=61,b=62c1=a,c2=bd=``3.56,e=`-6.87f=````3157.890121,g=```````0.123456m=50000,n=-60000p=32768,q=40000(`代表空格)结果:g的输出看来是四舍五⼊了。
③在此基础上,将程序第9~14⾏改为:a=61;b=62;c1=a;c2=b;f=3157.890121;g=0.123456789;d=f;e=g;p=a=m=50000;q=b=n=-60000;运⾏程序,分析结果。
p=a=m=50000;q=b=n=-60000;这两条语句将p、a、m的值和q、b、n的值都重新赋了相应的新值。
《C程序设计基础》实验指导
《C程序设计基础》实验指导C程序设计基础实验指导实验一:变量和常量的使用实验目的:本实验旨在掌握C程序中变量和常量的使用方法,通过实际操作加深对C程序设计基础的理解。
实验内容:1. 声明一个整型变量a,并初始化为10。
2. 声明一个浮点型变量b,并初始化为3.14。
3. 输出变量a和b的值。
4. 将a的值加上100,b的值加上1.5。
5. 再次输出变量a和b的值。
实验步骤:1. 打开C程序设计工具,新建一个源文件,命名为demo.c。
2. 在源文件中添加以下代码:```c#include <stdio.h>int main(){int a = 10;float b = 3.14;printf("初始值:\n");printf("a = %d\n", a);printf("b = %.2f\n", b);a += 100;b += 1.5;printf("操作后的值:\n");printf("a = %d\n", a);printf("b = %.2f\n", b);return 0;}```3. 保存源文件,并点击编译运行按钮进行编译和运行。
4. 在控制台窗口中查看输出结果。
实验结果分析:实验中我们声明了一个整型变量a和一个浮点型变量b,并分别对其进行初始化。
然后我们使用printf函数将变量a和b的值输出到控制台窗口中。
接着,我们对a和b进行操作,a的值加上了100,b的值加上了1.5,并再次使用printf函数输出操作后的值。
最后的输出结果中,我们可以观察到变量a和b的值的变化。
实验二:运算符的使用实验目的:本实验旨在掌握C程序中运算符的使用方法,加深对C程序设计基础的理解。
实验内容:1. 声明两个整型变量a和b,并分别初始化为10和5。
2. 分别使用加法、减法、乘法和除法运算符对a和b进行运算,并输出结果。
【免费下载】C语言实验指导书第4版
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,系电,力根通保据过护生管高产线中工敷资艺设料高技试中术卷资,配料不置试仅技卷可术要以是求解指,决机对吊组电顶在气层进设配行备置继进不电行规保空范护载高与中带资负料荷试下卷高问总中题体资,配料而置试且时卷可,调保需控障要试各在验类最;管大对路限设习度备题内进到来行位确调。保整在机使管组其路高在敷中正设资常过料工程试况中卷下,安与要全过加,度强并工看且作护尽下关可都于能可管地以路缩正高小常中故工资障作料高;试中对卷资于连料继接试电管卷保口破护处坏进理范行高围整中,核资或对料者定试对值卷某,弯些审扁异核度常与固高校定中对盒资图位料纸置试,.卷保编工护写况层复进防杂行腐设自跨备动接与处地装理线置,弯高尤曲中其半资要径料避标试免高卷错等调误,试高要方中求案资技,料术编试交写5、卷底重电保。要气护管设设装线备备置敷4高、调动设中电试作技资气高,术料课中并3中试、件资且包卷管中料拒含试路调试绝线验敷试卷动槽方设技作、案技术,管以术来架及避等系免多统不项启必方动要式方高,案中为;资解对料决整试高套卷中启突语动然文过停电程机气中。课高因件中此中资,管料电壁试力薄卷高、电中接气资口设料不备试严进卷等行保问调护题试装,工置合作调理并试利且技用进术管行,线过要敷关求设运电技行力术高保。中护线资装缆料置敷试做设卷到原技准则术确:指灵在导活分。。线对对盒于于处调差,试动当过保不程护同中装电高置压中高回资中路料资交试料叉卷试时技卷,术调应问试采题技用,术金作是属为指隔调发板试电进人机行员一隔,变开需压处要器理在组;事在同前发一掌生线握内槽图部内纸故,资障强料时电、,回设需路备要须制进同造行时厂外切家部断出电习具源题高高电中中源资资,料料线试试缆卷卷敷试切设验除完报从毕告而,与采要相用进关高行技中检术资查资料和料试检,卷测并主处且要理了保。解护现装场置设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本科生实验报告实验课程算法语言与程序设计学院名称管理科学学院专业名称信息与计算科学学生学生学号0指导教师王玉兰实验地点数学应用与计算机仿真实验教学示中心实验成绩二〇一五年三月二〇一五年六月填写说明1、适用于本科生所有的实验报告(印制实验报告册除外);2、专业填写为专业全称,有专业方向的用小括号标明;3、格式要求:①用A4纸双面打印(封面双面打印)或在A4大小纸上用蓝黑色水笔书写。
②打印排版:正文用宋体小四号,1.5倍行距,页边距采取默认形式(上下2.54cm,左右2.54cm,页眉1.5cm,页脚1.75cm)。
字符间距为默认值(缩放100%,间距:标准);页码用小五号字底端居中。
③具体要求:题目(二号黑体居中);摘要(“摘要”二字用小二号黑体居中,隔行书写摘要的文字部分,小4号宋体);关键词(隔行顶格书写“关键词”三字,提炼3-5个关键词,用分号隔开,小4号黑体);正文部分采用三级标题;第1章××(小二号黑体居中,段前0.5行)1.1 ×××××小三号黑体×××××(段前、段后0.5行)1.1.1小四号黑体(段前、段后0.5行)参考文献(黑体小二号居中,段前0.5行),参考文献用五号宋体,参照《参考文献著录规则(GB/T 7714-2005)》。
目录目录目录 (3)第一章程序调试 (4)1、修改程序 (4)2、实验项目 (5)3、测试 (5)第二章 C语言基础 (6)1、修改程序 (6)2、实验项目 (6)3、测试 (8)第三章控制结构 (9)1、实验项目 (9)2、测试 (13)第四章数组 (15)1、实验项目 (15)2、测试 (20)第五章指针 (23)1、实验项目 (23)2、测试 (27)第六章函数 (29)1、实验项目 (29)2、测试 (36)第七章文件 (39)1、实验项目 (39)2、测试 (43)第八章结构体与共用体 (46)1、实验项目 (46)2、测试 (47)教师评阅 (53)第一章程序调试1、修改程序【实验1-1】源代码:#include <stdio.h>int main(void){int a, b, sum;a=40; b=50;sum=a+b;printf("Hello, C! \n");printf("Sum is %d \n", sum);return(0);}运行结果:【实验1-2】源代码:#include <stdio.h>int main(void){float aver;int n1=90,n2=85,n3=88,n4=79,n5=95;aver=(n1+n2+n3+n4+n5)/5;printf ("5aver = %0.2f \n", aver);return(0);}运行结果:2、实验项目无3、测试【ts1-1】源代码:#include <stdio.h>int main(void){int x, y, z;x=5, y=8;z=x+y;printf ("x=%d,y=%d,z=%d\n",x,y,z); return(0);}运行结果:【ts1-2】源代码:#include <stdio.h>int main(void){int a=2, b=3;float x=8.2, y=9.2, z;z=x/2 + y/b;printf ("z=%0.2f\n", z);return(0);}运行结果:第二章 C语言基础1、修改程序【实验2-1】源代码:#include <stdio.h>int main(void){int x, y, sum;printf("input X:");scanf("%d",&x);printf("input Y:");scanf("%d",&y);sum=x+y;printf ("sum=%d\n", sum);return(0);}运行结果:2、实验项目【实验2-2】源代码:#include<stdio.h>void main(){int s1,s2,s3,s4,s5;float a;printf("请输入五门课的成绩:");scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);a=(s1+s2+s3+s4+s5)/5.0;printf("平均分:%0.2f \n",a);}运行结果:【实验2-3】源代码:#include<stdio.h>void main(){printf("-----------------------------\n");printf("1. Enter data\n");printf("2. Data processing\n");printf("3. End\n");printf("-----------------------------\n");}运行结果:【实验2-4】源代码:#include<stdio.h>int main(void){int a,b,c,d,e,w=1,x=2,y=3,z=4;a=b=c=1;d=++a||++b&&++c;e=w>x&&y>z;printf("a=%d,b=%d,c=%d,d=%d,e=%d\n",a,b,c,d,e); return(0);}运行结果:3、测试【2-1】源代码:#include<stdio.h>int main(void){char c1,c2;printf("\nEnter a character:");scanf("%c",&c1);c1=c1-32;c2=c1+1;printf("\nResult is\n %c %c", c1,c2); printf("\n%4d%4d",c1,c2);return(0);}运行结果:【2-2】源代码:#include <stdio.h>int main(void){double t=1.0;t+=1/2.0+1/3.0+1/4.0+1/5.0;printf("\nThe result is %f\n",t);return(0);}运行结果:第三章控制结构1、实验项目【实验3-1】源代码:#include<stdio.h>int main (){ int a;printf("----------------------------");printf(" \n 1. Enter data \n 2. Data processing \n 3. End \n");printf("----------------------------\n");printf(" selet your number: \n");scanf("%d",&a);switch(a){case 1: printf("Enter data \n");break;case 2: printf("Data processing \n");break;case 3: printf("End \n");break;default : printf("error!");}}运行结果:【实验3-2】源代码:#include<stdio.h>int main (){int i=0,a[5],b=0; float c;printf("输入五个分数:");for(i=0;i<5;i++){ scanf("%d",&a[i]); }for(i=0;i<5;i++){ b+=a[i]; }c=b/5.0;printf("平均分:%3.2f\n 大于平均成绩的:",c);for(i=0;i<5;i++){if(a[i]>c) printf("%3d ",a[i]);}}运行结果:【实验3-3】源代码:#include<stdio.h>int main (){int a,g,s,b;for(b=1;b<=9;b++){for(s=0;s<=9;s++){for(g=0;g<=9;g++){a=g*g*g+s*s*s+b*b*b;if(a==b*100+s*10+g) printf("%d\t",a);}}}printf("\n");}运行结果:【实验3-4】源代码:#include<stdio.h>int main (){int a, i,j;printf("*\t");for(a=1;a<=9;a++) {printf("%d\t",a);}printf("---------------------------------------------------------------------------\n");for(i=1;i<=9;i++){printf("%d\t",i);for(j=1;j<=i;j++){printf("%d\t",j*i);}printf("\n");}}运行结果:【实验3-5】源代码:#include<stdio.h>int main (){char ch;int ch_num=0,dig_num=0,oth_num=0;do {scanf("%c",&ch);if(ch<='z'&&ch>='a') ch_num+=1;else if (ch<='Z'&&ch>='A') ch_num+=1;else if (ch<='9'&&ch>='0') dig_num+=1;else oth_num++ ;} while (ch!='\n');printf("字母:%d\n 数字:%d\n其他:%d\n",ch_num,dig_num,oth_num-1);return(0);}运行结果:【实验3-6】源代码:#include<stdio.h>#include<math.h>void main(){float x1,x2,x0,f0,f1,f2;printf("输入 X1 X2\n");scanf("%f%f",&x1,&x2);while(1){f1=2*x1*x1*x1-4*x1*x1+3*x1-6;f2=2*x2*x2*x2-4*x2*x2+3*x2-6;x0=(x1+x2)/2;f0=2*x0*x0*x0-4*x0*x0+3*x0-6;if(f0*f1<0)x2=x0;elsex1=x0;if(fabs(f0)<=1e-5)break;}printf("结果%f\n",x0);}运行结果:【实验3-7】源代码:#include<stdio.h>void main(){int i,j,k,n,m;scanf("%d",&n);m=0;for(i=1;i<n+1;i++){for(j=0;j<n-i;j++) printf(" ");for(k=1;k<=(2*i-1);k++){if(k<=i)printf(" %d",k);else{m++;printf(" %d",k-2*m);}}m=0;printf("\n");}}运行结果:2、测试【填空3-1】源代码:#include <stdio.h>#include <conio.h>int main(void){int num=0,g,s,b;printf("\nInput data :");scanf("%d",&num);printf("\nThe result is: ");g=num%10;s=num/10%10;b=num/100;if((g+b)==s)printf("YES\n");elseprintf("NO\n");return(0);}运行结果:【改错题3-2】源代码:#include<stdio.h>int main(void){int k,m=0,mc=0, j;printf("\nEnter data: ");scanf("%d",&k);while((k>=2)&&(mc<6)){if((k%7==0)||(k%11==0)){m+=k;mc++;}k--;}printf("\nresult is %d\n",m); return(0);}运行结果:【编程题3-3】源代码:#include<math.h>#include<stdio.h>main(){int n=1,count=0;float x;double sum ,term;printf("input x:");scanf("%f",&x);sum=x;term=x;do{term=-term*x*x/((n+1)*(n+2));sum=sum+term;n=n+2;count++;}while (fabs(term)>=1e-5);printf("sin(x)=%f,count=%d\n",sum,count);}运行结果:第四章数组1、实验项目【实验4-1】源代码:#include<stdio.h>int main(){int a[50],i=2,b;a[0]=1;a[1]=1;for(i=2;i<16;i++){a[i]=a[i-1]+a[i-2];}for(b=0;b<16;b++){printf("%6d",a[b]);if((b+1)%4==0) printf("\n");}}运行结果:【实验4-2】源代码:#include<stdio.h>int main(){int a[10] ,i,b=0;printf("输入五门课程成绩 :\n");for(i=0;i<5;i++) scanf("%d",&a[i]); //输入无门课程成绩for(i=0;i<5;i++) b+=a[i]; //计算总分b=b/5; //计算平均数for(i=0;i<5;i++) //找出分数高于平均数的{if(a[i]>b) printf("序号:%d 成绩: %d\n",i,a[i]);}}运行结果:【实验4-3】源代码:#include<stdio.h>int main(){int a[3][5],i,j;float b[3]={0,0,0};for(i=0;i<3;i++){ printf("输入第%d名学生的五门成绩:\n",i+1);for(j=0;j<5;j++){scanf("%d",&a[i][j]);}}for(i=0;i<3;i++) //计算平均分{for(j=0;j<5;j++){b[i]=b[i]+a[i][j];}}for(i=0;i<3;i++) b[i]/=5.0;for(i=0;i<3;i++) printf("第%d位同学的平均分%2.2f\n",i+1,b[i]);}运行结果:【实验4-4】源代码:#include<stdio.h>#include<string.h>int main(){char classes[6][15]={"Wang Wu","Zhang San","Li Si","Zhao Liu","Ding Yi","Qian er"};char name[15];int i,b;printf("输入名字:");gets(name);for(i=0;i<6;i++){if(strcmp(name,classes[i])==0){printf("%s是\n",name); b=1; break;}b=0;}if(b==0)printf("%s不是\n",name);}运行结果:【实验4-5】源代码:#include<stdio.h>void main(){char a[12]="abcdefklmnp",ch;int top,bot,mid;printf("Input a character :");scanf("%c",&ch);bot=0;top=10;for(;bot<top;){mid=(bot+top)/2;if(a[mid]==ch){printf("This charcreat in a\nSubscript%d\n",mid+1);break;}else if(ch>a[mid]) bot=mid+1;else top=mid-1;}if(bot>top)printf("This character not in a ");}运行结果:【实验4-6】源代码:#include<stdio.h>#define N 30void main(){int i=0;long n;char str[N];printf("Inputs a posivite integer:");scanf("%ld",&n);while(n>0){str[i]=n%10+'0';n=n/10;i++;}str[i]='\0';printf("%s\n",str);}运行结果:2、测试【填空题4-1】源代码:#include<stdio.h>#include<stdlib.h>#define N 20int main(void){int a[N][N];int n,i,j,k;double s=0,aver=0;printf("\nInput the dimension of array N: ");scanf("%d",&n);printf("***** The array *****\n");for(i=0;i<n;i++){for(j=0;j<n;j++){a[i][j]=rand()%10;printf("%4d",a[i][j]);if(i==0||j==0||i==n-1||j==n-1)s+=a[i][j];}printf("\n");}k=4*(n-1);aver =s/k;printf("***** THE RESULT *****\n");printf("The average is %2.3f\n",aver);return(0);}运行结果:【改错题4-2】源代码:#include <stdio.h>int main(void){int i, j;char s[80];printf("\nEnter a string: ");gets(s);printf("The original string: ");puts(s);for(i=j=0; s[i]!='\0';i++)if(s[i]!='S'){s[j]=s[i];j++;}s[j]='\0';printf("The string after deleted: "); puts(s);return(0);}运行结果:【编程题4-3】源代码:#include<stdio.h>void main(){int i=0,j;char str[100];int num[27]={0};printf("input str:");gets(str);while(str[i]!='\0'){j=str[i]-'a';num[j]=num[j]+1;i++;}for(j=0;j<26;j++) printf("%d ",num[j]);}运行结果:【完善程序4-4】源代码:#include<stdio.h>#include<math.h>#define N 10int main(void){int i;double x[N]={92.0,85.0,56.0,75.0,82.0,79.0,65.0,91.0,89.0,56.0}; double x1=0.0,s=0.0;for(i=0;i<10;i++)x1+=x[i];x1/=10;for(i=0;i<10;i++)s+=pow((x[i]-x1),2);s/=10;s=sqrt(s);printf("%f\n",s);return(0);}运行程序:第五章指针1、实验项目【实验5-1】源代码:#include<stdio.h>#define N 10int main(void){int x[N],*p1,*p2,i;for(i=0;i<N;i++)scanf("%d",x+i);for(p1=x,p2=x;p1-x<N;p1++)if(*p1>*p2) p2=p1;printf("MAX=%d,INDEX=%d\n",*p2,p2-x);return(0);}运行结果:【实验5-2】源代码:#include<stdio.h>#define M 20int main(void){int i,x[M],n, m, *p,*k,*j;printf("\nEnter n:");scanf("%d",&n);printf("\nEnter array x[i](i=0~%d):",n-1);for(i=0;i<n;i++)scanf("%d",x+i);printf("\n");m=n/2;k=x;j=x+n-1;p=x+m;for(;k<=p;k++,j--){int t=*k;*k=*j;*j=t;}printf("\nThe array inverted:\n");for(i=0;i<n;i++)printf("%d ",x[i]);return(0);}运行结果:【实验5-3】源代码:#include<stdio.h>#include<ctype.h>int main(void){char ch[80],*c;int d,k=0;printf("\nPlease input a string:"); gets(ch);c=ch;for(;*c;c++)if(k){if(*c==' '){k=0;*(c-1)=toupper(*(c-1)); }}elsek=1;printf("\nResult is: %s\n",ch);return(0);}运行结果:【实验5-4】源代码:#include<stdio.h>#define N 80int main(void){int i=0,j=0,n;char str[N],*p=str;printf("\nEnter a string:");scanf("%s",str);while(*(p+i)){if(*(p+i)<='Z'&&*(p+i)>='A'){str[j]=*(p+i);j++;}i++;}str[j]='\0';printf("%s \n",str);return(0);}运行结果:【实验5-5】源代码:#include<stdio.h>#include<stdlib.h>int main(){int *p,n;int i,j;float ave=0;printf("输入学生数n:");scanf("%d",&n);p=(int *)malloc(n*5*sizeof(int));if(p==NULL){printf("ERROR");exit(0);}for(i=0;i<n;i++){ printf("输入第%d名学生的五门成绩:\n",i+1);for(j=0;j<5;j++)scanf("%d",p+5*i+j);}for(i=0;i<n;i++) //计算平均分{for(j=0;j<5;j++)ave+=*(p+i*5+j);printf("第%d位同学的平均分%2.2f\n",i+1,ave/5);ave=0;}}运行结果:【实验5-6】源代码:#include<stdio.h>int main(void){int base=0;char str[80], *p=str;printf("Input the binary number\n"); gets(str);while(*p!='\0'){base=base*2+*p-48;p++;}printf("%d\n", base);return(0);}运行结果:2、测试【填空题5-1】源代码:#include<string.h>#include<stdio.h>int main(void){char str[50],*p1,*p2;int i, t=0;printf("Input:");scanf("%s",str);p1=str;p2=str+strlen(str)-1;for (i=0;i<=(strlen(str)-1)/2;i++) if (*p1!=*p2){t=1;break;}if(t!=1)printf("%s\n","yes!");elseprintf("%s\n","no!");return(0);}运行结果:【改错题5-2】源代码:运行结果:【填空题5-3】源代码:#include <stdio.h>#include <string.h>int main(void){char *p,str[100];long int n=0;int flag=1;p=str;printf("Enter a string:\n");gets(p);while(*p!='\0'){if(*p=='-')flag=-1;p++;}n=n*10+*p-48;p++;}printf("%ld\n",n*flag);return(0);}运行结果:第六章函数1、实验项目【实验6-1】源代码:#include<stdio.h>float aver (float b[],int n );int main(void){float ave,a[10];int i;for (i=0;i<10;i++)scanf ("%f",&a[i]);ave =aver(a,10) ;printf ("average =%6.2f\n", ave);return(0);}float aver (float b[],int n ){int i ;float max,min,sum ;max=min=sum=b[0] ;for (i=1;i<n;i++)if (b[i]>max )max=b[i] ;else if ( b[i]<min )min=b[i] ;sum=sum+b[i] ;}printf ("max=%6.2f\n min=%6.2f\n",max,min); return (sum/n) ;}运行结果:【实验6-2】源代码:#include <stdio.h>int main(void){int a,b,x;int gcd(int,int);printf("Enter a,b: ");scanf("%d%d",&a,&b);x=gcd(a,b);printf("%d",x);return(0);}int gcd(int u,int v){int t;while(u%v!=0){t=u%v;u=v;v=t;}return(v);}运行结果:【实验6-3】源代码:#include <stdio.h>int main(void){int a,b,x;int gcd(int,int);printf("Enter a,b: ");scanf("%d%d",&a,&b);x=gcd(a,b);printf("%d",x);return(0);}int gcd(int u,int v){if(u%v==0)return v;elsereturn (gcd(v,u%v));}运行结果:【实验6-4】源代码:#include <stdio.h>#include<stdlib.h>int main(int argc,char *argv[]) {int a,b,x;int gcd(int,int);a=atoi(argv[1]);b=atoi(argv[2]);printf("a=%d b=%d\n",a,b); x=gcd(a,b);printf("gcd=%d",x);return(0);}int gcd(int u,int v){int t;while(u%v!=0){t=u%v;u=v;v=t;}return(v);}运行结果:【实验6-5】源代码:#include <stdio.h>#include<stdlib.h>void invert(int a[],int n){int i,w,nn=n/2;for(i=0;i<nn;i++){ w=a[i-n];a[i-n]=a[-i-1];a[-i-1]=w;} }int main(void){int i,n,*a;while(1){printf("Enter n:");scanf("%d",&n);printf("\n");if(n>0)break;}a=(int *)malloc(n*sizeof(int));if(a==NULL){printf("allocation error_aborting"); exit(1);}printf("Enter a[0]...a[%d]:",n);for(i=0; i<n; i++)scanf("%d",a++);invert(a,n);printf ("the array has been invert:\n"); for(i=0;i<n;i++){if(i%5==0)printf("\n");printf("%5d",*(a-n+i));}return(0);}运行结果:【实验6-6】源代码:#include<stdio.h>#include<stdlib.h>float aver(int n,int m ,int *p);int main(){int *p,n,i,j,m;printf("输入学生数n:");scanf("%d",&n);printf("输入科目数m:");scanf("%d",&m);p=(int *)malloc(n*m*sizeof(int));if(p==NULL){printf("ERROR");exit(0);}for(i=0;i<n;i++){ printf("输入第%d名学生的%d门成绩:\n",i+1,m);for(j=0;j<m;j++)scanf("%d",p+m*i+j);}aver(n,m,p);}float aver(int n,int m ,int *p){float ave=0;int i,j;for(i=0;i<n;i++) //计算平均分{for(j=0;j<m;j++)ave+=*(p+i*m+j);printf("第%d位同学的平均分%2.2f\n",i+1,ave/m);ave=0;}}运行结果:【实验6-7】源代码:#include<stdio.h>#define N 10int score[N]={11,22,33,44,55,96,87,78,69,10};int *Getmax();int main(void){int *p;p=Getmax();printf("Max value in array is %d\n",*p);return(0);int *Getmax(){int t,i,position=0;t=score[0];i=0;while(i<N){if(score[i]>t){t=score[i];position=i;}i++;}return(&score[position]);}运行结果:【*实验6-8】源代码:#include<stdio.h>#include<stdlib.h>int fun(int n,int *p,int num);void main(){int i,n,*p,num,rut;printf("数字的个数n:");scanf("%d",&n);p=(int*)malloc(n*sizeof(int));if(p==NULL) exit(0);printf("数字:");for(i=0;i<n;i++)scanf("%d",p+i);printf("输入要查找的数num:");scanf("%d",&num);rut=fun(n,p,num);printf("地址:%p 数值:%d\n",p+rut,*(p+rut)); }int fun(int n,int *p,int num)int i,flag,*a;a=p;for(i=0;i<n;i++){if(*(a+i)==num)flag=i;}return(flag);}运行结果:2、测试【填空题6-1】源代码:#include<stdio.h>#define N 20int bb[N];int fun(char *p,char t) {int i=0, n=0;while(*p){if (*p==t)bb[n++]=i;p++;i++;}return (n);}int main(void){char str[N];char ch;int i, j,n;printf("\nInput the original string: ");gets(str);printf("\nInput character :");scanf("%c",&ch);n=fun(str,ch);printf("\nThe numbr of character is:%d\n",n);printf("\nThe suffix of character:");for(i=0;i<n;i++)printf("%d ",bb[i]);return(0);}运行结果:【调试6-2】源代码:#include<stdio.h>#include <conio.h>#include <string.h>char *fun(char *s,char *t){char *p,*r,*a=NULL;while(*s){ p=s;r=t;while(*r){if(*r==*p) {r++;p++;}else break;if(*r=='\0')a=s;}s++;}return a;}int main(void){char s[100],t[100],*p;printf("\nPlease enter string S: ");scanf("%s",s);printf("\nPlease enter substring t: ");scanf("%s",t);p=fun(s,t);if(p!=0) printf("\nThe result is:%s\n ",p);else printf("\nNot found!\n ");return(0);}运行结果:【完善程序6-3】源代码:#include <stdio.h>float fun (int m, int n){float m1,m2,m3;int i;m1=1;m2=1;m3=1;for(i=1;i<=m;i++)m1*=i;for(i=1;i<=n;i++)m2*=i;for(i=1;i<=(m-n);i++)m3*=i;return(m1/(m2*m3));}int main (void){int m,n;printf("\nEnter m,n: ");scanf("%d,%d",&m,&n);printf ("p=%f\n",fun(m,n) ) ;return(0);}运行结果:第七章文件1、实验项目【实验7-1】源代码:#include<stdio.h>#include<stdlib.h>int main (){int a, i,j;FILE *p;p=fopen("S7-1.out","w");if(p==NULL){printf("file can not open!");exit(0);}printf("*\t");fprintf(p,"*\t");for(a=1;a<=9;a++){printf("%d\t",a);fprintf(p,"%d\t",a);}printf("---------------------------------------------------------------------------\n");fprintf(p,"------------------------------------------------------------------------------\n");for(i=1;i<=9;i++){printf("%d\t",i);fprintf(p,"%d\t",i);for(j=1;j<=i;j++){printf("%d\t",j*i);fprintf(p,"%d\t",j*i);}printf("\n");fprintf(p,"\n");}fclose(p);}运行结果:【实验7-2】源代码:#include<stdio.h>#include<stdlib.h>#include<string.h>int main(void){FILE *fp;char str[100],filename[10];int i=0;if((fp=fopen("S7-2.out","w"))==NULL) {printf("Open file error\n");exit(0);}printf("please enter a string:\n"); gets(str);while(str[i]!='!'){if(str[i]>='a'&&str[i]<='z')str[i]=(str[i]-32);fputc(str[i],fp);i++;}fclose(fp);printf("\n");fp=fopen("S7-2.out","r");fgets(str,strlen(str)+1,fp);printf("%s\n",str);fclose(fp);return(0);}运行结果:【实验7-2】源代码:#include<stdio.h>#include<stdlib.h>float aver(int n,int m ,int *p);int main(){FILE *fp;int *p,n,i,j,m;fp=fopen("S7-2.dat","w");if(fp==NULL){printf("file can not open");exit(0);}printf("输入学生数n:");scanf("%d",&n);printf("输入科目数m:");scanf("%d",&m);p=(int *)malloc(n*m*sizeof(int));if(p==NULL){printf("ERROR");exit(0);}for(i=0;i<n;i++){ printf("输入第%d名学生的%d门成绩:\n",i+1,m);for(j=0;j<m;j++){scanf("%d",p+m*i+j);fprintf(fp,"%d ",*(p+m*i+j));}}fclose(fp);fp=fopen("S7-2.dat","r");if(fp==NULL){printf("file can not open");exit(0);}aver(n,m,fp);}float aver(int n,int m ,FILE *p){float ave=0;int i=0,j,a[20];while(!feof(p)){fscanf(p,"%d",&a[i]);i++;}for(i=0;i<n;i++) //计算平均分{for(j=0;j<m;j++)ave+=a[i*m+j];printf("第%d位同学的平均分%2.2f\n",i+1,ave/m);ave=0;}return 0;}运行结果:2、测试【改错7-1】源代码:#include <stdio.h>int main(void){FILE *in,*out;int i,j,a[3][3],b[3][3];in=fopen("CS7-1.dat","r");for(i=0; i<3; i++)for(j=0; j<3; j++)fscanf(in,"%d",&a[i][j]);rewind(in);for(i=0; i<3; i++)for(j=0; j<3; j++)fscanf(in,"%d",&b[i][j]);fun(a,b);out=fopen("CS7-1.out","w");for(i=0; i<3; i++){for(j=0; j<3; j++){printf ("%4d ", a[i][j]);fprintf (out,"%4d ", a[i][j]);}printf("\n");fprintf(out,"\n");}fclose(in);fclose(out);return(0);}fun(int a[3][3],int b[3][3]){int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++)a[i][j]+=b[i][j];}运行结果:【完善程序7-2】源代码:#include <conio.h>#include <stdio.h>int main (void){FILE *fp;int x,a[100], n, i ;void fun ();printf("\nPlease enter an integer number : \n ") ; scanf ("%d", &x) ;fun (x, a, &n) ;printf ("\nResult is : ") ;fp=fopen("CS7-2.out","w");if(fp==NULL){printf("file cam mot open");exit(0);}for(i=0;i<n;i++){printf("%d ",a[i]);fprintf(fp,"%d ",a[i]);}return(0);}void fun (int x, int p[], int *n) {int i,j=0;for(i=1;i<=x;i++){if(x%i==0)if(i%2==0){p[j]=i;j++;}}*n=j;}运行结果:第八章结构体与共用体1、实验项目【实验8-1】源代码:#include <stdio.h>#include <string.h>#define NUM 4struct student{int rank;char * name;float score;};struct student stu [ ]= { 3, "Tom", 89.5,4, "Mary", 76.5,1, "Jack",98.0,2, "Jim" ,92.0 };int main(void){char str[10];int i;do {printf ("Enter a name: ");gets(str);for(i=0;i<NUM;i++){if(strcmp(stu[i].name,str)==0)printf("%d %s %f\n",stu[i].rank,stu[i].name,stu[i].score);}}while(strcmp(str,"0" )!=0);return(0);}运行结果:【实验8-2】源代码:运行结果:2、测试【填空8-1】源代码:#include <stdio.h>#define N 8typedef struct{char num[10];double s[N];double ave;} STREC;int main(void){STREC s={ "GA005 ",85.5,76,69.5,85,91,72,64.5,87.5};int i;void fun();fun(&s);printf("The %s's student data:\n", s.num); /*输出学号*/for(i=0;i<N;i++)printf("%4.1f\n",s.s[i]); /*输出各科成绩*/ printf("\nave=%7.3f\n", s.ave); /*输出平均分*/return(0);}void fun(STREC *p){int i;p->ave=0.0;for(i=0;i<N;i++)p->ave=p->ave+p->s[i]; /*求各门成绩的总和*/ p->ave/=i;; /*求平均分*/}运行结果:【改错8-2】源代码:#include <string.h>#include <stdio.h>#include <stdlib.h>#define N 10typedef struct ss{char num[10];int s;} STU;STU *fun(STU a[], int m){STU b[N],*t;int i, j,k;/*******************found*****************/t=(STU *)malloc(m*sizeof(STU));for(i=0;i<N;i++)b[i]=a[i];for(k=0;k<m;k++){/*******************found*****************/for (i=j=0;i<N;i++)if(b[i].s<b[j].s)j=i;/*******************found*****************/t[k]=b[j];b[j].s=100;}return t;}outresult(STU a[],FILE *pf){int i;for(i=0;i<N;i++)fprintf(pf, "No=%s Mark=%d\n ",a[i].num, a[i].s);fprintf(pf, "\n\n ");}int main(void){STU a[N]={{"A01 ",77},{"A02 ",85},{"A03 ",96},{"A04 ",65},{"A05 ",75}, {"A06 ",96},{"A07 ",76},{"A08 ",63},{"A09 ",69},{"A10 ",78}};STU *pOrder;int i, m;printf("*****THE RESULT*****\n");outresult(a,stdout);printf("\nGive the number of the students who have lower score: ");scanf("%d",&m);while(m>10){printf("\nGive the number of the students who have lower score: "); scanf("%d",&m);}pOrder=fun(a,m);printf("\nThe Result is :");printf("\nThe low : \n");for(i=0;i<m;i++)printf("%s %d\n",pOrder[i].num, pOrder[i].s);free(pOrder);return(0);}。