当前位置:首页 > 大学大一c语言程序设计实验室上机题全部代码答案(实验报告)
#include
void search(char *s1, char *s2, char *s3) {
char *p;
int word,i,m,n; char a[20]; p=s1; s3[0]='\\0';
while (*p!='\\0') {
word=0;
for (i=0;p[i]!='\\0';i++)//记录单词的长度i {
if (p[i]!=' '&&word==0) { word=1; m=i; }
else if (p[i]==' '&&word==1) { n=i; break; } }
if (p[i]=='\\0'&&word==1) n=i;
if (word==1) {//将单词复制到atemp中 strncpy(a,p+m,n-m); a[n-m]='\\0'; //puts(s3);
char *pfind;// strstr:找出atemp字符串在s2字符串中第一次出现的位置。
if ( (pfind=strstr(s2,a))!=NULL ) { // finded
// 只是找到包含,如果完全一致,应该将找到对应的单词取出来再比较
if ( *(pfind+strlen(a))==' ' || *(pfind+strlen(a))=='\\0' ) if (strlen(a)>strlen(s3)) strcpy(s3,a); }
p=p+n;
}
else break; } }
main() {
char *s1=\char *s2=\ char s3[20]; search(s1,s2,s3); printf(\}
实验8-3:
编写一个主函数,测试上述两个函数的正确性。 结果如上两题所示。
实验8-4:
输入一个字符串,内有数字和非数字字符,例如: A123cdf 456.78cpc876.9er 849.1 将其中连续的数字作为一个实数,依次存放到一数组a中。例如123存放在a[0],456.78存放在a[2],依次类推,统计共有多少个数,并输出这些数。
源代码:
#include
void f(char *sp,float b[]) { char *p; float value; int power,i=0,j=0; p=sp; while(*p) { for(;!(*p>='0'&&*p<='9');p++) value=0; for(value=0;(*p>='0'&&*p<='9');p++,i++) value=value*10+*p-'0'; if(*p=='.') p++; for(power=1;(*p>='0'&&*p<='9');p++,i++) { value=value*10+*p-'0'; power=power*10; }
value=value/power; b[j++]=value; } printf(\数字个数为:%d\\n\}
void main() { int j; float b[4]={0}; char *a=\ f(a,b); for(j=0;j<4;j++) printf(\}
实验9-1:
编写一个建立单链表的函数,设链表的表元素信息包含学号、姓名、一门课的成绩;写一个按照学号查学生成绩的函数;最后写一个主函数,它先调用建立函数,再调用查询函数,显示查到学生的姓名和成绩。
源代码:
#include
}stu[4]={{1,\main() { int i,j; printf(\请输入学号:\\n\ scanf(\ for(j=0;j<4;j++) if(stu[j].num==i) printf(\}
实验9-2:
编一程序,能把从终端输入的一个字符串中的小写字母全部转换成大写字母,要求输入的字符的同时指定该字符在字符串中的序号(即字符在字符串中的顺序号,例如第1个字符的序号为1),字符和序号存入结构体中,字符串存入结构体数组中,然后显示结构体数组的结
果(用字符!表示输入字符串的结束)。
源代码:
#include
实验9-3:
在本实验的实验内容1的基础上,实现在链表元素index之前插入元素的操作Insert( struct node *head, int index)和删除指定位置元素的操作Delete( struct node * head, int index),并编写主函数测试。
源代码:
#include
char name[15]; char num[15]; char risk[15];
struct student *next; };
共分享92篇相关文档