C++编程 学习笔记(五)语句

1、条件语句
if(condition)
{statement}
elseif(condition)
{statement}
else{statement}
2、switch语句
switch(变量)
{case 常量表达式1:statement;break;
case 常量表达式2:statement;break;
…;default:statement;break;}
3、迭代语句
(1)while&do…while
while(condition)
statement;
先判断条件再进行循环
do statement
while(condition)
先进行循环再判断条件
都是condition为假时,终止循环
(2)for两个形式
a判断循环:for(initializer ; condition ; expression)
statement
initializer初始化一个值,可以定义多个变量,但只能有一个声明语句,即变量基本类型要相同;condition循环进行的条件; expression负责修改initializer变量
b遍历范围:for(declaration:expression)
statement
expression是要遍历的序列,可以是初始化列表、数组、vector或者string;declaration定义变量,最简单用auto类型说明符。如果要对序列进行读写操作,循环变量要声明为引用类型。
4、跳转语句
break:终止离它最近的while、do while、for或者switch语句
continue:只能在while、do while、for循环内部使用,继续进入下个循环
goto:无条件跳转到同一函数另一个语句,少用 goto label; label:+语句
try:异常处理,不知道怎么用

**Exercise:**1读入一个txt文件,用switch统计元音字母,包括大小写,统计空格、制表符、换行符的数量,统计含有下面序列的数量(ff、fl、fi),写在txt文件的后面。
C++对txt的读取和写入,参考网站:http://blog.csdn.net/lh3325251325/article/details/4761575

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>

using namespace std;

int count(string str);

static int acount = 0, ecount = 0, icount = 0, ocount = 0, ucount = 0, \
black = 0, changeline = 0, tab = 0, ff = 0, fl = 0, fi = 0;//为了实现累加,这里采用了静态变量

void countletter()
{
    ifstream myfile("F://MyC//MyC//english.txt");//注意在C++中路径的形式,不能直接copy
    ofstream outfile;   //创建一个新的文件
    outfile.open("myfile2.txt");  //把文件命名并定义为txt 
    string s;
    char str[100];
    if (!myfile)     //检查是否存在文件,没有警告
    {
        cerr << "cannot find the txt" << endl;
    }
    else
    {
    //while (myfile >> s)
    //{
    //  cout << s << endl;
    //}这个操作是逐个单词读入,每个单词分行
    while (getline(myfile, s))   //读取txt文档每一行,以换行符为分界线
    {
        count(s);
        //cout << s << endl;   //输出文档的字段
        tab++;   //因为换行只能这样直接加

        if (outfile.is_open())      //如果要输入的文件打开了就写进每一句
        {
            outfile << s << endl;
        }

    }

    outfile <<"\n"<<"the result of counting is"<<"\n"    //结果输出,写到新建的文档里面
        << "acount="<<acount << "\n"
        << "ecount=" <<ecount << "\n"
        << "icount="<<icount << "\n"
        << "ocount="<<ocount << "\n"
        << "ucount="<<ucount << "\n"
        << "black="<<black << "\n"
        << "changeline="<<changeline << "\n"
        << "tab="<<tab << "\n"
        << "ff ="<<ff << "\n"
        << "fl="<<fl << "\n"
        << "fi="<<fi << endl;
    }
    outfile.close();
}

int count (const string str)
{

    if (str.empty())   //判断输入的是不是空的指针,是的话警告
    {
        cerr << "no string?!" << endl;
    }

    for (auto &c:str)     //取str中的地址,这里表示c是str的别名
    {
        auto *a = &c + 1;  //指针指向下一个地址,也就是下一个字符
        auto b = *a;       //*表示解引用,得到的是下一个字符
        switch (c)          //switch判断字符,并累加
        {
        case 'a':case 'A':
            acount++;
            break;
        case 'e':case 'E':
            ecount++;
            break;
        case 'i':case 'I':
            icount++;
            break;
        case 'o':case 'O':
            ocount++;
            break;
        case 'u':case 'U':
            ucount++;
            break;
        case ' ':
            black++;
            break;
        case '\r':
            changeline++;
            break;
        case 'f':

            if (b == 'f')
                ff++;
            else if(b == 'l')
                fl++;
            else if (b == 'i')
                fi++;
            break;
        default:
            break;
        }
    }
    return 0;    //C++的函数只能有一个返回值,所以下面的形式是错的
    //解决的办法就是(1)采用static静态变量(2)用结构体或者数组,就可以包含多个变量
    /*return acount,ecount,icount,ocount,ucount,\
        black,changeline,tab,ff, fl,fi;*/
}

2编写一段程序,从标准输入中读取若干string对象并查找重复出现的单词,即一个单词紧跟在另一个单词后面,记录重复出现次数最大的单词,如果不存在,说明单词没有重复出现过。

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>

using namespace std;

void repeatconut()
{
    string word;
    int num,count=1;
    vector<string> wordset ;
    cout << "input string" << endl;
    while (cin >> word)
    {
        if (word ==".")  //可以采用逗点结束或者ctrl+z结束
            break;
        else
        wordset.push_back(word);//往string中读取单词      
    }
    num=wordset.size(); //返回wordset中元素的个数;
    cout <<  "the num of word is " << num << endl;
    for (int i = 0; i <=num-2; i++)//遍历容器中的元素,遇到相同计数加1,不同重新计数
    {
        if (wordset[i] == wordset[i + 1])
            count++;
        else
        {
            if (i != num - 2)
            {
                cout << wordset[i] << ": " << count << endl;
                count = 1;
            }
            else
            {
                cout << wordset[i] << ": " << count << endl;
                cout << wordset[i+1] << ": 1"<< endl;
            }
        }
    }

}

3输入两个包含整数的vector对象,检验其中一个vector是否为另一个的前缀。
对于cin的读取有几个形式:
(1)cin可以连续从键盘读取想要的数据,以空格、tab或换行作为分隔符。当cin>>从缓冲区中读取数据时,若缓冲区中第一个字符是空格、tab或换行这些分隔符时,cin>>会将其忽略并清除,继续读取下一个字符
(2)不想略过空白字符,那就使用 noskipws 流控制。比如cin>>noskipws>>input;或者用cin.get()可以读取到换行和空格,如果只要读取的到具体数字,但是要判断换行和空格可以参考上面的例子
可以参考下面网址:http://www.cnblogs.com/A-Song/archive/2012/01/29/2331204.html

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>

using namespace std;
int prefixtion(vector<int> a, vector<int> b);
void prefix()
{
    vector<int> numset1, numset2;
    int i = 0;
    cout << "input two int vector" << "\n"
        <<"the first one:"<<endl;
    do
    {
        cin >> i;
        cout << i << endl;
        numset1.push_back(i);
    } while (cin.get()!='\n');   //通过这个形式可以实现输入到换行符就停止,同时不会读入空格

    cout << "the second one" << endl;
    i = 0;
    do
    {
        cin >> i;
        cout << i << endl;
        numset2.push_back(i);
    } while (cin.get() != '\n');
    cout << "the size of two strings is "<< numset1.size()<<" and "<< numset2.size() << endl;
    if (numset1.size()>numset2.size())
    {
        if (!prefixtion(numset2, numset1))
            cout << "the numset2 is the prefixtion of numset1" << endl;
        else
            cout << "not prefixtion" << endl;
    }
    else
    {
        if (!prefixtion(numset1, numset2))
            cout << "the numset1 is the prefixtion of numset2" << endl;
        else
            cout << "not prefixtion" << endl;
    }
}

int prefixtion(vector<int> a, vector<int> b)
{
    int flag = 0;
    for  (int i = 0;  i < (a.size()-1); i++)
    {
        if (a[i] != b[i])
        {
            flag = 1;
            break;
        }
    }
    return flag;
}

4读入一个txt文件,用while检查,当一个单词连续出现两次或者文本读完终止

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>

using namespace std;
void txtending()
{
    ifstream myfile("F://MyC//MyC//english.txt");//读入文件的数据流
    ofstream outfile;   //创建一个新的文件
    outfile.open("myfile5_4.txt");  //把文件命名并定义为txt
    string s,temp;

    if (!myfile)     //检查是否存在文件,没有警告

        cerr << "cannot find the txt" << endl;
    else
    {
        while (myfile>>s)   //每次只读入一个单词,以空格符作为分隔的标志
        {
            if (s==temp)
                break;
            else temp = s;

            if (outfile.is_open())      //如果要输入的文件打开了就写进每一句
                outfile << s << endl;
        }
    }
    outfile.close();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值