C语言实验题目(下).doc_第1页
C语言实验题目(下).doc_第2页
C语言实验题目(下).doc_第3页
C语言实验题目(下).doc_第4页
C语言实验题目(下).doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

定义两个字符数组s1、s2,并用赋初值的方法把两个字符串Computer和Language” 分别存放到s1、s2中,要求不用库函数strcat( ),把s2连接到s1的尾部,然后以%s格式输出连接后的字符串s1。#include void main() char s180,s240; int i=0,j=0; printf(input string1:); scanf(%s,&s1); printf(input string2:); scanf(%s,&s2); while(s1i!=0) i+; s1i= ; i+; while(s2j!=0) s1i+=s2j+; s1i = 0; printf(The new string is:%sn,s1);用赋初值的方法把字符串C is a general purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.存放到字符数组s中,编程统计其中的大写字母、小写字母、数字、空格、逗号的个数。#include void main() char s512 = C is a general purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.;int upper=0,lower=0,digit=0,space=0,comma=0;int i=0;while (si) if(si=A&si=a&si=0&si=9)digit+; if(si= )space+; if(si=,)comma+; i+; printf(这串字符串有大写字母%d个,小写字母%d个,数字%d个,空格%d个,逗号%d个n,upper,lower,digit,space,comma);试从主函数输入10个数据到数组中,编写对偶数项求和的子函数,它将计算结果返回给主函数,由主函数输出。#include int oqh(int a, int x) int i,s;s=0; for(i=1;ix;i+=2)s=s+ai; return(s); void main() int a10;int i,s;printf(请您在数组内输入10个数:);for(i=0;i10;i+) scanf(%d,&ai);s=oqh(a,10);printf(这个数组的偶数项的和是:%dn,s); 注意:oqh并无其他含义,是本人定义的一个函数名,偶数项求和的缩写。编写一个判断素数的程序,其中主函数用于完成输入一个整数并给出判断结果,单独编写一个函数用于判断其参数是否为素数,其返回值为1表示为素数,为0表示为非素数。#include #include int prime(int n)int m,i=2,t;t=(int)sqrt(n);for(;it)m=1;elsecontinue;return(m);void main()int n;int i;printf(请输入你要判断的数:n);scanf(%d,&n);while(n=1)printf(您输入了一个错误的数据,请重新输入:n);scanf(%d,&n);if(prime(n)printf(您输入的是一个素数n);elseprintf(您输入的不是一个素数n);输入三个整数,按由小到大的顺序输出。(要求使用指针来排序输出)#include void sort(int *a,int *b,int *c)int t=0;if(*a*b)t=*a;*a=*b;*b=t;if(*a*c)t=*a;*a=*c;*c=t;if(*b*c)t=*b;*b=*c;*c=t;void main()int a,b,c;printf(请您输入三个整数:);scanf(%d %d %d,&a,&b,&c);sort(&a,&b,&c);printf(它们由小到大的排列顺序是:%d %d %dn,a,b,c);或者是#include void main() void swap(int *p1,int *p2); int a,b,c; int *p1,*p2,*p3; printf(请您输入三个整数:); scanf(%d %d %d,&a,&b,&c); p1=&a; p2=&b; p3=&c; if(ab)swap(p1,p2); if(ac)swap(p1,p3); if(bc)swap(p2,p3); printf(它们由小到大的排列顺序是:%d %d %dn,a,b,c);void swap(int *p1,int *p2) int p; p=*p1; *p1=*p2; *p2=p;输入十个整数,放在数组list中,然后用指针法从后向前输出该数组中的整数。#include void main() int list10,i,*p=list; printf(请您输入10个整数:n); for(i=0;i=0;i-) printf(%-4d,*(p+i);如果输入的数字个数不定的情况,下面的代码可行#include void main()void sort(char *p,int m);int i,n;char *p,list30;printf(请您输入n的值:);scanf(%d,&n);printf(请您输入%d个整数:n,n);for(i=0;in;i+)scanf(%d,&listi);p=&list0;sort(p,n);printf(这%d个整数的逆序序列是:n,n);for(i=0;in;i+)printf(%-4d,listi);printf(n);void sort(char *p,int m)int i;char t,*p1,*p2;for(i=0;im/2;i+)p1=p+i;p2=p+(m-1-i);t=*p1;*p1=*p2;*p2=t;编写一个函数,它能对一个字符串(“I am a student”)测出长度,要求函数的形参是一个指针变量,函数返回值是字符串的长度。#include void main()int length(char *p);int len;char str20;printf(input string:);gets(str);len=length(str);printf(The length of string is %d.n,len);int length(char *p)int n;n=0;while(*p!=0)n+;p+;/*p=0;/n+;return(n);若要统计结果包含结束符,则启用*p=0;n+;两条语句编一个函数cstrcmp实现两个字符串的比较,具体为: int cstrcmp(char *p1, char *p2)p1,p2分别指向字符串s1,s2;若s1=s2则函数返回0;若s1s2,则函数返回1;若s1s2,则函数返回-1。声明字符串s1,s2时对其进行初始化。#include #include char s120;char s220;void input()printf(输入第1个字符串:n);scanf(%s,s1);printf(输入第2个字符串:n);scanf(%s,s2);int cstrcmp(char *p1,char *p2)p1=s1;p2=s2;if(strcmp(p1,p2)=0)return 0;if(strcmp(p1,p2)0)return 1;if(strcmp(p1,p2)0)return -1;void main()char *a;char *b;input();printf(这两个字符串比较的结果是:%dn,cstrcmp(a,b);如果要求返回的是不相同字母的ASCII码值:#include void main()int cstrcmp(char *p1,char *p2);int m;char str120,char str220,*p1,*p2;printf(请在下面输入两个字符串:n);gets(str1);gets(str2);p1=&str10;p2=&str20;m=cstrcmp(p1,p2);printf(这两个字符串比较的结果是:%dn,m);int cstrcmp(char *p1,char *p2)int i;i=0;while(*(p1+i)=*(p2+i)if(*(p1+i+)=0)return(0);return(*(p1+i)-*(p2+i);有5个学生,每个学生的数据包括学号、姓名、3门课的成绩,用赋初值的方法输入5个学生的数据到结构体数组中,输出每个学生3门课的平均成绩。#include struct student char num6; char name8;int score3;float avr;stu5=101,Zhou,93,89,87,102,Yang,85,80,78,103,Chen, 77,70,83,104,Qian,70,67,60,105,Li,72,70,69; void main() int i,j,sum;for(i=0;i5;i+) sum=0; for(j=0;j3;j+)sum+=stui.scorej; stui.avr=sum/3.0;printf(number name score1 score2 score3 averagen);for(i=0;i5;i+)printf(%3s%10s,stui.num,);for(j=0;j3;j+)printf(%7d,stui.scorej);printf(%10.2fn,stui.avr);如果按平均成绩由高到低排序后,输出每个学生的成绩#include struct student char num6; char name8; int score3;float avr; stu5=101,Zhou,93,89,87,102,Yang,85,80,78,103,Chen, 77,70,83,104,Qian,70,67,60,105,Li,72,70,69,temp; void main() int i,j,sum; for(i=0;i5;i+)sum=0; for(j=0;j3;j+)sum+=stui.scorej;stui.avr=sum/3.0;for(i=0;i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论