首页 > 其他分享 >实验5

实验5

时间:2024-12-08 22:10:04浏览次数:4  
标签:int s1 char ++ 实验 str printf

#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 min, max;
 
    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)函数find_min_max实现的功能是找出录入数据中的最大值和最小值

(2)45行时,pmin和pmax都指向x[0]的地址,即第一个录入的数据的地址

实验1.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");
    output(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 max_index = 0;
    int i;
 
    for(i = 0; i < n; ++i)
        if(x[i] > x[max_index])
            max_index = i;
    
    return &x[max_index];
}

 

(1)函数find_max实现的功能是找出数据中的最大值并返回其地址

(2)可以

实验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 tmp[N];
 
    printf("sizeof(s1) vs. strlen(s1): \n");
    printf("sizeof(s1) = %d\n", sizeof(s1));
    printf("strlen(s1) = %d\n", strlen(s1));
 
    printf("\nbefore swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);
 
    printf("\nswapping...\n");
    strcpy(tmp, s1);
    strcpy(s1, s2);
    strcpy(s2, tmp);
 
    printf("\nafter swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);
 
    return 0;
}

(1)数组1的大小是80个字节;sizof(s1)计算的是数组1所占的字节数;strlen(s1)计算的是字符的个数

(2)不可以,数组名是地址,不能直接用等于,而应该用strcpy

(3)交换了

2.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 *tmp;

    printf("sizeof(s1) vs. strlen(s1): \n");
    printf("sizeof(s1) = %d\n", sizeof(s1));
    printf("strlen(s1) = %d\n", strlen(s1));

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

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

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

    return 0;
}

 

(1)存放的是字符串“Learning makes me happy”的首地址;sizeof(s1)计算的是指针变量所占的字节数;strlen(s1)统计的是字符数

(2)可以。char *s1定义了一个字符串数组用来存储字符串,然后再把字符串的首地址赋值给s1

(3)交换的是指针所指的地址,并没有交换两个字符串常量。

实验3

#include <stdio.h>

int main() {
    int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
    int i, j;
    int *ptr1;     // 指针变量,存放int类型数据的地址
    int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组

    printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
    for (i = 0; i < 2; ++i) {
        for (j = 0; j < 4; ++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;
}

实验4

#include <stdio.h>
#define N 80

void replace(char *str, char old_char, char new_char); // 函数声明

int main() {
    char text[N] = "Programming 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)检索指定字符i,并把它替换成*

(2)可以

实验5

#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 x)
{
    while(*str)
    {
        if(*str!=x)
        {
            str++;
        }
        else
        {
            *str='\0';
            break;
        }
    }
    return str;
}

getchar()可防止在输入字符串后读入空串

实验6

#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;
}
int check_id(char *str)
{
    int i;
    if (strlen(str) != 18)
        return 0;
    for (i = 0; i < 17; i++)
        if (str[i] < '0'||str[i]>'9')
            return 0;
    if ((str[17] <= '9' && str[17] >= '0') || str[17] == 'X')
        return 1;
    else
        return 0;
}

实验7

#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) 
{
    while (*str)
    {
        if (*str >= 'a' && *str <= 'z')
            *str = (*str - 'a' + n ) % 26 + 'a';
        if (*str >= 'A' && *str <= 'Z')
            *str = (*str - 'A' + n ) % 26 + 'A';
        str++;
    }
}
void decoder(char *str, int n) {
    while (*str)
    {
        if (*str >= 'a' && *str <= 'z')
            *str = (*str - 'a' - n + 26) % 26 + 'a';
        if (*str >= 'A' && *str <= 'Z')
            *str = (*str - 'A' - n + 26) % 26 + 'A';
        str++;
    }
}

实验8

#include <stdio.h>
int main(int argc, char *argv[]) {
    int i,j;
    for(i=1;i<argc;i++)
    {
        for(j=1;j<argc-i;j++)
        {
            char *tmp;
            if(strcmp(argv[j],argv[j+1])>0)
            {
                tmp=argv[j];
                argv[j]=argv[j+1];
                argv[j+1]=tmp;
            }
        }
    }
    for(i = 1; i < argc; ++i)
        printf("hello, %s\n", argv[i]);
    return 0;
}

 

 

标签:int,s1,char,++,实验,str,printf
From: https://www.cnblogs.com/hexu7/p/18581005

相关文章

  • 实验五
    任务3:源码:1#include<iostream>2#include<vector>3#include"pets.hpp"45voidtest(){6usingnamespacestd;78vector<MachinePets*>pets;910pets.push_back(newPetCats("miku"));11......
  • 实验五
    任务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,N);......
  • 实验五
    TASK11#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;1213printf(&......
  • 实验5
    实验1源代码11#include<stdio.h>2#defineN53voidinput(intx[],intn);4voidoutput(intx[],intn);5voidfind_min_max(intx[],intn,int*pmin,int*pmax);6intmain(){7inta[N];8intmin,max;9printf("录入%d个数据:\n"......
  • 实验五
    任务一: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);in......
  • 实验五
    任务一代码1#pragmaonce23#include<iostream>4#include<string>56usingstd::cout;7usingstd::endl;8usingstd::string;910//发行/出版物类:Publisher(抽象类)11classPublisher{12public:13Publisher(constst......
  • 实验5 继承和多态
    任务1:task1.cpp1#include"publisher.hpp"2#include<vector>3#include<typeinfo>45usingstd::vector;67voidtest(){8vector<Publisher*>v;910v.push_back(newBook("HarryPotter","......
  • 实验5
    任务一publish.hpp#pragmaonce#include<iostream>#include<string>usingstd::cout;usingstd::endl;usingstd::string;//发行/出版物类:Publisher(抽象类)classPublisher{public:Publisher(conststring&s="");//构......
  • 操作系统实验报告-实验三—页式虚拟存储管理程序设计
    一. 实验目的和要求目的存储管理的主要功能之一是合理地分配主存空间。请求页式管理是一种常用的虚拟存储管理技术。本实验的目的是通过请求页式存储管理中页面置换算法的模拟设计来了解虚拟存储技术的特点,掌握请求页式存储管理的页面置换算法。要求模拟页式虚拟存储管......
  • 实验5
    #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,N);......