首页 > 其他分享 >实验五

实验五

时间:2024-12-08 19:11:26浏览次数:7  
标签:return int s1 char 实验 str printf

任务一
源代码1:
`#include<stdio.h>

define N 5

void input(int x[],int n);
void output(int x[],int n);
void find_min_max(int x[],int n,int *pmin,int *pmax);
int main()
{
int a[N];
int max,min;
printf("录入%d个数据:\n",N);
input(a,N);
printf("数据是:\n");
output(a,N);
printf("数据处理...\n");
find_min_max(a,N,&min,&max);
printf("输出结果:\n");
printf("min=%d,max=%d\n",min,max);
return 0;
}
void input(int x[],int n)
{
int i;
for(i=0;i<n;i++)
scanf("%d",&x[i]);
}
void output(int x[],int n)
{
int i;
for(i=0;i<n;++i)
printf("%d ",x[i]);
printf("\n");
}
void find_min_max(int x[],int n,int *pmin,int pmax)
{
int i;
pmin=pmax=x[0];
for(i=0;i<n;i++)
if(x[i]<
pmin)
pmin=x[i];
else if(x[i]>
pmax)
*pmax=x[i];
}运行结果1:![](/i/l/?n=24&i=blog/3527258/202412/3527258-20241208182855102-1692927596.png) 问题1:将 p_max和p_min改为最小值和最大值(对实参进行操作) 问题2:都指向数组元素x[0] 源代码2:#include<stdio.h>

define N 5

void input(int x[],int n);
void output(int x[],int n);
int *find_max(int x[],int n);
int main()
{
int a[N];
int pmax;
printf("录入%d个数据:\n",N);
input(a,N);
printf("数据处理...\n");
pmax=find_max(a,N);
printf("输出结果:\n");
printf("max=%d\n",
pmax);
return 0;
}
void input(int x[],int n)
{
int i;
for(i=0;i<n;i++)
scanf("%d",&x[i]);
}
void output(int x[],int n){
int i;
for(i=0;i<n;i++)
printf("%d ",x[i]);
printf("\n");
}
int *find_max(int x[],int n){
int i;
int max_index=0;
for(i=0;i<n;i++)
if(x[i]>x[max_index])
max_index=i;

return &x[max_index];

}`
运行结果2:
问题1:找到最大值的地址
问题2:可以

任务二
源代码1:
`#include<stdio.h>

include<string.h>

define N 80

int main()
{
char s1[N]="Learning makes me happy";
char s2[N]="Learning makes me sleepy";
char temp[N];
printf("sizeof(s1)vs strlrn(s1):\n");
printf("sizeof(s1)=%d",sizeof(s1));
printf("strlen(s1)=%d",strlen(s1));

printf("\nbefore swap:\n");
printf("s1:%s\n",s1);
printf("s2:%s\n",s2);

printf("\nswapping...\n");
strcpy(temp,s1);
strcpy(s1,s2);
strcpy(s2,temp);
printf("\nafter swap:\n");
printf("s1:%s\n",s1);
printf("s2:%s\n",s2);
return 0;

}`
运行结果1:
问题1:80  数组s1占多少字节  统计数组s1中的除去\0的具体字符数
问题2:不可以,字符数组除了初始化时无法直接进行赋值
问题3:交换的

源代码2:
`#include<stdio.h>

include<string.h>

define N 80

int main()
{
char *s1="Learning makes me happy";
char *s2="Learning makes me sleepy";
char *temp;
printf("sizeof(s1) vs strlen(s1):\n");
printf("sizeof(s1)=%d\n",sizeof(s1));
printf("strlen(s1)=%d\n",strlen(s1));

printf("\nbefore:\n");
printf("s1:%s\n",s1);
printf("s2:%s\n",s2);

printf("\nswapping...\n");
temp=s1;
s1=s2;
s2=temp;

printf("\nafter swap:\n");
printf("s1:%s\n",s1);
printf("s2:%s\n",s2);
return 0;

}`
运行结果2:
问题1:字符串"Learning makes me happy"的字符的首地址,计算的是指针变量s1所占的字节,统计的是从首地址开始一直到\0之前的字符数
问题2:可以,task2_1的line6是将字符串整个输送到字符数组中,task2_2是将字符串的首地址赋值给s1
问题3:交换的是指针变量的地址指向,没有交换

任务三
源代码:
`#include<stdio.h>
int main()
{
int x[2][4]={{1,9,8,4},{2,0,4,9}};
int i,j;
int ptr1;
int (
ptr2)[4];
printf("输出1:使用数组名、下标直接访问二维数组元素\n");
for(i=0;i<2;i++){
for(j+0;j<2;j++)
printf("%d",x[i][j]);
printf("\n");}
printf("\n输出2:使用指针变量ptr1(指向元素)间接访问\n");
for(ptr1=&x[0][0],i=0;ptr1<&x[0][0]+8;ptr1++,i++)
{
printf("%d",ptr1);
if((i+1)%4==0)
printf("\n");
}
printf("\n输出3:使用指针变量ptr2(指向一维数组)间接访问\n");
for(ptr2=x;ptr2<x+2;++ptr2)
{
for(j=0;j<4;j++)
{
printf("%d",
(*ptr2+j));
printf("\n");
}
}

return 0;
}`
运行结果:

任务四
源代码:
`#include<stdio.h>

define N 80

void replace(char str,char old_char,char new_char);
int main(){
char text[N]="Programing is difficult or not it is a question";
printf("原始文本:\n");
printf("%s\n",text);
replace(text,'i','
');
printf("处理后的文本:\n");
printf("%s\n",text);
return 0;
}
void replace(char str,char old_char,char new_char)
{
int i;
while(
str)
{
if(*str==old_char)
*str=new_char;
str++;
}
}`
运行结果:
问题1:将旧字符用新字符来代替
问题2:可以

任务五
源代码:
`#include<stdio.h>

define N 80

char *str_trunc(char *str,char x);
int main(){
char str[N];
char ch;
while(printf("输入字符串:"),gets(str)!=NULL)
{
printf("输入一个字符");
ch=getchar();

    printf("截断处理...\n");
    str_trunc(str,ch);
    
    printf("截断处理后的字符串:%s\n\n",str);
    getchar();
}
return 0;

}
char *str_trunc(char str,char c)
{
int i=0;
while(
(str+i)!=c)
{
i++;
}
*(str+i)='\0';
return str;
}`
运行结果:
问题:如果删去,后续就无法输入字符串,起到吞没回车的作用

任务六
源代码:
`#include <stdio.h>

include <string.h>

define N 5

int check_id(char *str); // 函数声明

int main()
{
char *pid[N] = {"31010120000721656X",
"3301061996X0203301",
"53010220051126571",
"510104199211197977",
"53010220051126133Y"};
int i;

for (i = 0; i < N; ++i)
    if (check_id(pid[i])) // 函数调用
        printf("%s\tTrue\n", pid[i]);
    else
        printf("%s\tFalse\n", pid[i]);

return 0;

}

// 函数定义
// 功能: 检查指针str指向的身份证号码串形式上是否合法
// 形式合法,返回1,否则,返回0
int check_id(char str) {
int i=0;
while(
(str+i))
{ i++; }
if(i!=18)
return 0;
for(i=0;i<17;i++)
{ if(!((str+i)>='0'&&(str+i)<='9'))
return 0;
}
if((str+i)>='0'&&(str+i)<='9'||*(str+i)=='X')
return 1;
else
return 0;
}`
运行结果:

任务七
源代码:
`# include<stdio.h>

define N 80

void encoder(char str,int n);
void decoder(char str,int n);
int main()
{
char words[N];
int n;
printf("输入英文文本:");
gets(words);
printf("输入n:");
scanf("%d",&n);
printf("编码后的英文文本:");
encoder(words,n);
printf("%s\n",words);
printf("对编码后的英文文本解码:");
decoder(words,n);
printf("%s\n",words);
return 0;
}
void encoder(char str,int n)
{
int i;
for(i=0;
(str+i)!=0;i++)
{
if(
(str+i)>='a'&&
(str+i)<='z'-n||(str+i)>='A'&&(str+i)<='Z'-n)
(str+i)+=n;
else if(
(str+i)>'z'-n&&(str+i)<='z'||(str+i)>'Z'-n&&(str+i)<='Z')
(str+i)=(str+i)+n-26;
}
}
void decoder(char str,int n)
{
int i;
for(i=0;
(str+i)!=0;i++)
{
if(
(str+i)>='a'+n&&(str+i)<='z'||(str+i)>='A'+n&&(str+i)<='Z')
(str+i)-=n;
else if(
(str+i)>='a'&&
(str+i)<'a'+n||(str+i)>='A'&&(str+i)<'A'+n)
(str+i)=(str+i)-n+26;
}
}`
运行结果:

任务八
源代码:
`#include<stdio.h>

include<string.h>

include<stdlib.h>

int main(int argc, char *argv[])
{
int i,j;
char temp=(char)malloc(10);
for(i=0;i<argc-1;i++)
for(j=i;j<argc-i-1;j++)
if(strcmp(argv[j],argv[j+1])<0)
{ strcpy(temp,argv[j]);
strcpy(argv[j],argv[j+1]);
strcpy(argv[j+1],temp);
}
for(i=1;i<argc;i++)
printf("Hellow,%s\n",argv[i]);
free(temp);
return 0;
}`
运行结果:

标签:return,int,s1,char,实验,str,printf
From: https://www.cnblogs.com/hcjinniuzuo/p/18593669

相关文章

  • 实验5 继承和多态
    task3pets.hpp点击查看代码#pragmaonce#include<iostream>#include<string>usingstd::cout;usingstd::endl;usingstd::string;classMachinePets{public:MachinePets(conststring&s):nickname(s){}stringget_nickname()......
  • 实验5 继承和多态
    实验任务3:pets.hpp代码:1#pragmaonce2#include<iostream>3#include<string>4usingstd::string;56classMachinePets{7public:8//带参数的构造函数9MachinePets(conststring&s):nickname(s){}10//纯虚函数,......
  • 实验五
    task_1.1#include<stdio.h>#defineN5voidinput(intx[],intn);voidoutput(intx[],intn);voidfind_min_max(intx[],intn,int*pmin,int*pmax);intmain(){inta[N];intmax,min;printf("¼Èë%d¸öÊý¾Ý:\n",N......
  • 实验5
    task1_1:1#include<stdio.h>2#defineN53voidinput(intx[],intn);4voidoutput(intx[],intn);5voidfind_min_max(intx[],intn,int*pmin,int*pmax);6intmain(){7inta[N];8intmin,max;910printf("录入%d个数据:\n"......
  • 实验五 继承和多态
    task3pets.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings);virtualstringtalk()const=0;stringget_nickname();private:stringnickname;......
  • 实验5
    任务1.11#include<stdio.h>2#defineN534voidinput(intx[],intn);5voidoutput(intx[],intn);6voidfind_min_max(intx[],intn,int*pmin,int*pmax);78intmain()9{10inta[N];11intmin,max;1213prin......
  • 交换综合实验
    实验拓扑实验要求 1.链路聚合配置:SW1和SW2分别通过GE0/0/3,GE0/0/4和GE0/0/5接口相互连接,把这三个接口捆绑成一个逻辑接口,使用的模式为static-lacp。lSW2为主劢端,两台设备之间最大可用的带宽为2G。2.VLAN配置:  在每台交换机创建VLAN,VLANID分别为1......
  • 实验五
    任务一源代码(1):#include<stdio.h>#defineN5voidinput(intx[],intn);voidoutput(intx[],intn);voidfind_min_max(intx[],intn,int*pmin,int*pmax);intmain(){inta[N];intmin,max;printf("录入%d个数据:\n",N);input(a,......
  • 实验5
    实验1#include<stdio.h>#defineN5voidinput(intx[],intn);voidoutput(intx[],intn);voidfind_min_max(intx[],intn,int*pmin,int*pmax);intmain(){inta[N];intmin,max;printf("录入%d个数据:\n",N);input(a......
  • 实验5 继承和多态
    任务1:task1.cpp1publisher.hpp:2#pragmaonce3#include<iostream>4#include<string>56usingstd::cout;7usingstd::endl;8usingstd::string;910//发行/出版物类:Publisher(抽象类)11classPublisher{12public:13......