计算导论与C语言基础week10_1

本文介绍了使用C++处理字符串的几个实例,包括统计元音字母数量、比较字符串大小(忽略大小写)、寻找最长单词,以及交换矩阵中的指定行。通过这些示例,读者可以学习如何运用C++标准库函数解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.输入元音字母的个数
描述

在一个字符串中找出元音字母a,e,i,o,u出现的次数。
输入

输入一行字符串(字符串中可能有空格,请用cin.getline(s,counts)方法把一行字符串输入到字符数组s中,其中counts是s的最大长度,这道题里面可以直接写80。),字符串长度小于80个字符。
输出

输出一行,依次输出a,e,i,o,u在输入字符串中出现的次数,整数之间用空格分隔。

#include <iostream>
using namespace std;

int main() 
{
    char s[80], counts = 80;
    cin.getline(s, counts);//cin.getline可以接受空白符并输出
    int a[5] = { 0 };//开始没给赋0,出错
    for (int i = 0; i < counts; i++) {
        switch (s[i]) {
        case 'a':a[0]++; break;
        case 'e':a[1]++; break;
        case 'i':a[2]++; break;
        case 'o':a[3]++; break;
        case 'u':a[4]++; break;
        default:;//这里开始忘了打个; 其实default可以不要的
        }
    }
    cout << a[0] << ' ' << a[1] << ' ' << a[2] << ' ' << a[3] << ' ' << a[4] << endl;
    return 0;
}

2.忽略大小写比较字符串大小
描述

一般我们用strcmp可比较两个字符串的大小,比较方法为对两个字符串从前往后逐个字符相比较(按ASCII码值大小比较),直到出现不同的字符或遇到’\0’为止。如果全部字符都相同,则认为相同;如果出现不相同的字符,则以第一个不相同的字符的比较结果为准。但在有些时候,我们比较字符串的大小时,希望忽略字母的大小,例如”Hello”和”hello”在忽略字母大小写时是相等的。请写一个程序,实现对两个字符串进行忽略字母大小写的大小比较。
输入

输入为两行,每行一个字符串,共两个字符串。(请用cin.getline(s,80)录入每行字符串)(每个字符串长度都小于80)
输出

如果第一个字符串比第二个字符串小,输出一个字符”<”

如果第一个字符串比第二个字符串大,输出一个字符”>”

如果两个字符串相等,输出一个字符”=”

#include <iostream>
using namespace std;
#include <string.h>    //strcmp()在这个里面

int main() 
{
    char s1[80],s2[80],s3[80],s4[80];
//s3,s4存两个字符串化成全小写后的新字符串
    cin.getline(s1, 80);
    cin.getline(s2, 80);
    for (int i = 0; i < 80; i++) {
        if(s1[i] >= 'A'&&s1[i] <= 'Z') s3[i]=s1[i]+32;
        else s3[i] = s1[i];
        if(s2[i] >= 'A'&&s2[i] <= 'Z') s4[i]=s2[i]+32;
        else s4[i] = s2[i];
    }
    int signal = strcmp(s3, s4);
    if (signal > 0)cout << '>' << endl;
    else if (signal < 0)cout << '<' << endl;
    else cout << '=' << endl;
    return 0;
}

3.最长单词2(自己出了几次错最后参考blog)
问题描述
一个以’.’结尾的简单英文句子,单词之间用空格分隔,没有缩写形式和其它特殊形式
输入
一个以’.’结尾的简单英文句子(长度不超过500),单词之间用空格分隔,没有缩写形式和其它特殊形式
输出
该句子中最长的单词。如果多于一个,则输出第一个

#include <iostream>
using namespace std;

int main() 
{
    char s0[500] = { 0 }, word[50] = { 0 };
    cin.getline(s0, 500);
    int i = 0, j = 0, sizeWord = 0;
    while (i < 500 && s0[i] != '.') {
        if (s0[i] == ' ') {
            if (j > sizeWord) {
                sizeWord = j;//sizeWord存最大长度,出现更大长度时更新word[]
                for (int k = 0; k < j; k++) {
                    word[k] = s0[i - j + k];//i-j是改单词的起始位置
                }
            }
            j = 0;
        }
        else {
            j++;
        }
        i++;
    }
    if (s0[i] == '.') {
        if (j > sizeWord) {
            for (int k = 0; k < j; k++) {
                word[k] = s0[i - j + k];
            }
        }
    }

    cout << word << endl;
    return 0;
}

自己这里开始打算用二维数组,再设个len[500]存每个单词长度,但实现的时候出了问题,后来参考http://blog.youkuaiyun.com/nnnnnnnnnnnny/article/details/49049785

注意避免逻辑错最好开始就先画一下伪代码

4.矩阵交换行
描述

在main函数中, 生成一个5*5的矩阵,输入矩阵数据,并输入n,m的值。判断n,m是否在数组范围内,如果不在,则输出error;如果在范围内,则将n行和m行交换,输出交换n,m后的新矩阵。
输入

5*5矩阵的数据,以及n和m的值。
输出

如果不可交换,则输出error

如果可交换,则输出新矩阵

#include <iostream>
using namespace std;
#include <iomanip>
//setw()要用这个头文件,注意这个容易拼写错..

int main() 
{
    int i,j;
    char s[5][5];
    for (i = 0; i < 5; i++)
        for (j = 0; j < 5; j++)
            cin >> s[i][j];
    int n, m,temp_n;
    cin >> n >> m;
    if (n >= 0 && n < 5 && m >= 0 && m < 5) {
        for (i = 0; i < 5; i++) {
        //交换两行
            temp_n = s[n][i];
            s[n][i] = s[m][i];
            s[m][i] = temp_n;
        }

        for (i = 0; i < 5; i++) {
            for (j = 0; j < 5; j++) {
                cout << setw(4) << s[i][j];//setw()给后面的s[i][j]规定最小长度,这里右对齐最少占4个位
            }
            cout << "\n";
        }
    }
    else
        cout << "error" << endl;
    return 0;
}
For Instructors: Courses Based on the Book Instructors can use the CS:APP book to teach a number of different types of systems courses. Five categories of these courses are illustrated in Figure 2. The particular course depends on curriculum requirements, personal taste, and the backgrounds and abilities of the students. From left to right in the figure, the courses are characterized by an increasing emphasis on the programmer’s perspective of a system. Here is a brief description. ORG. A computer organization course with traditional topics covered in an untraditional style. Traditional topics such as logic design, processor architecture, assembly language, and memory systems are covered. However, there is more emphasis on the impact for the programmer. For example, data representations are related back to the data types and operations of C programs, and the presentation on assembly code is based on machine code generated by a C compiler rather than handwritten assembly code. ORG+. The ORG course with additional emphasis on the impact of hardware on the performance of application programs. Compared to ORG, students learn more about code optimization and about improving the memory performance of their C programs. ICS. The baseline ICS course, designed to produce enlightened programmers who understand the impact of the hardware, operating system, and compilation system on the performance and correctness of their application programs. A significant difference from ORG+ is that low-level processor architecture is not covered. Instead, programmers work with a higher-level model of a modern out-of-order processor. The ICS course fits nicely into a 10-week quarter, and can also be stretched to a 15-week semester if covered at a more leisurely pace. ICS+. The baseline ICS course with additional coverage of systems programming topics such as system-level I/O, network programming, and concurrent programming. This is the semester-long Carnegie Mellon course, which covers every chapter in CS:APP except low-level processor architecture.翻译以上英文为中文
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值