C++笔记之零碎点

1,测试cin.ignore()的作用,用于去除缓冲区的残留数据 

 1 #include <iostream>
 2 
 3 int main()
 4 {
 5     char buff[20];
 6     char buff1[20];
 7 
 8     std::cin >> buff1;
 9     std::cin.ignore(7, '\n');       // 通常把获取前n个字符写的很大,去掉前一次输入的‘\n’
10     std::cin.getline(buff, 10);     // 只能获取9个,存最末尾储'\0'
11 
12     std::cout << buff << std::endl;
13 
14     return 0;
15 }

 

2.整形的占用位数

 1 #include <iostream>
 2 #include <climits>
 3 
 4 int main()
 5 {
 6     using namespace std;
 7 
 8     int n_int = INT_MAX;
 9     short n_short = SHRT_MAX;
10     long n_long = LONG_MAX;
11     long long n_llong = LLONG_MAX;
12 
13     cout << "int      : " << sizeof n_int << "bytes." << endl;
14     cout << "short    : " << sizeof n_short << "bytes." << endl;
15     cout << "long     : " << sizeof n_long << "bytes." << endl;
16     cout << "long long: " << sizeof n_llong << "bytes." << endl;
17     cout << endl;
18 
19     cout << "Maximum values:" << endl;
20     cout << "int      : " << n_int << endl;
21     cout << "short    : " << n_short << endl;
22     cout << "long     : " << n_long << endl;
23     cout << "long long: " << n_llong << endl;
24     
25 }

 

运行结果: 

int      : 4bytes.
short    : 2bytes.
long     : 8bytes.
long long: 8bytes.

Maximum values:
int      : 2147483647
short    : 32767
long     : 9223372036854775807
long long: 9223372036854775807

3,整形溢出 

 

4,cin的相关方法 - ignore,get,getline,peek,gcount,read 

 1 #include <iostream>
 2 #include <climits>
 3 using namespace std;
 4 int main()
 5 {
 6     const int SIZE = 50;
 7     char buff[SIZE];
 8 
 9     cout << "请输入一段文本:";
10     cin.read(buff, 10);         //读取输入,阻塞计数,不忽略回车
11 
12     cout << "输入的文本字符数为:" << cin.gcount() << endl;  //统计
13 
14     cout << "输入的文本信息是:";
15     cout.write(buff, 19);       //读取缓冲区数据,打印
16     cout << endl;
17 
18     return 0;
19 
20 }

 

运行结果: 

请输入一段文本:yfu
67t
87y
输入的文本字符数为:10
输入的文本信息是:yfu
67t
8

5,文件复制

此处换行符会被过滤掉,所以逐行读取,手动添加ENDL。

 1 #include <iostream>
 2 #include <climits>
 3 #include <fstream>
 4 
 5 using namespace std;
 6 int main()
 7 {
 8     ifstream in; //文件输入流类对象
 9     in.open("../test.txt", ios::binary); //用两种方法来打开文件
10     ofstream out("../test1.txt", ios::binary | ios::app); //文件输出流类对象
11 
12     if(!in){
13         cerr << "打开文件失败" << endl;
14         return 0;
15     }
16 
17 //    char x;
18 //    while( in >> x){
19 //        if(x != '\n'){
20 //            cout << x;
21 //            out << x;
22 //        }
23 //        else{
24 //            cout << "遇到换行";
25 //            out << "-------********\n";
26 //        }
27 
28     in.seekg(ios::beg); //指针指向文件头
29 
30     for(string s; getline(in, s);){
31         cout << s << endl;
32         out << s << endl;
33     }
34 
35     cout << endl;
36 
37     in.close();
38     out.close();
39 
40     return 0;
41 }

 

6,输入输出,切换循环

 1 #include <iostream>
 2 
 3 int main()
 4 {
 5     char answer;
 6 
 7     std::cout << "请问你毕业了吗?[Y/N]" << std::endl;
 8     std::cin >> answer;
 9 
10     switch(answer){
11         case 'Y':
12         case 'y':
13             std::cout << "你毕业了!" << std::endl;
14             break;
15 
16         case 'N':
17         case 'n':
18             std::cout << "你怎么还没有毕业!" << std::endl;
19             break;
20 
21         default:
22             std::cout << "输入不符合要求!!!" << std::endl;
23             break;
24     }
25 
26     std::cin.ignore(100, '\n');
27 
28     std::cout << "输入任何字符结束" << std::endl;
29     std::cin.get();
30 
31 }

 

如图7所示,温度转换(我写的第一个C ++程序)

 1 #include <iostream>
 2 
 3 int main()
 4 {
 5     // 输入选择,确定公式
 6     char answer;
 7     std::cout << "请问待转换的温度单位序号:\n"
 8               << "1. 华氏度(℉) \n"
 9               << "2. 摄氏度(℃) " << std::endl;
10     std::cin >> answer;
11 
12     // 输入温度值
13     float temperature;
14     std::cout << "请输入当前温度:" << std::endl;
15     std::cin.ignore(100, '\n');
16     std::cin  >> temperature;
17 
18     // 进行判断,输出结果
19     float f2c(float num);
20     float c2f(float num);
21     float result = -1000;
22 
23     switch(answer){
24         case '1':
25             result = f2c(temperature);
26             break;
27         case '2':
28             result = c2f(temperature);
29             break;
30         default:
31                         std::cout << "输入不符合要求!!!" << std::endl;
32             break;
33     }
34 
35     if(result != -1000){
36         std::cout << result << std::endl;
37     }
38     else{
39         std::cout << "输入错误" << std::endl;
40     }
41 
42 }
43 
44 float f2c(float num){
45     // 摄氏度 = 【(华氏度 - 32) ÷ 1.8】℃
46     float result;
47 
48     result = (num-32) ;
49     result /= 1.8;
50 
51     return result;
52 }
53 
54 float c2f(float num){
55     // 华氏度 = (32 + 摄氏度 × 1.8)℉
56     float result;
57 
58     num *= 1.8 ;
59     result = num + 32;
60 
61     return result;
62 }

 

手痒写一个Python的版本:

 1 temper = input('请输入温度,形如【23.2 C】或者【23.2 F】')
 2 temp_value, temp_type = temper.split(' ')
 3 
 4 temp_value = float(temp_value)
 5 temp_type = temp_type.upper()
 6 
 7 print(temp_type, temp_value)
 8 if (temp_type == 'C') :
 9     result = str(round(temp_value*1.8 + 32, 2)) + ''
10 elif(temp_type == 'F') :
11     result = str(round((temp_value-32)/1.8, 2)) + ''
12 else:
13     result = None
14     print('输入有误')
15 
16 print(result)

 

8.地址运算符和间接值运算符的使用

 
 

运行结果如下: 

Values: updates = 6, *p_updates = 6
Addresses: &updates = 0x7ffd2f82ce0c, &p_updates = 0x7ffd2f82ce0c

可见,对变量名对应地址取间接运算符,相当与获得变量名对应值

9.数组的指针地址

 1 #include<iostream>
 2 #include<string>
 3 #include <limits>
 4 
 5 int main()
 6 {
 7     const unsigned short ITEMS = 5;
 8 
 9     int intArray[ITEMS] = {1, 2, 3, 4, 5};
10     char charArray[ITEMS] = {'f', 'I', 'j', 'K', ';'};
11 
12     int *intPtr = intArray;
13     char *charPtr = charArray;
14     
15     //显示整形数组的内存地址
16     std::cout << "整型数组输出:" << '\n';
17     for (int i = 0; i < ITEMS; i++) {
18         std::cout << *intPtr << " at " << reinterpret_cast<unsigned long>(intPtr) << std::endl;
19         intPtr++;
20     }
21     
22     //显示字节型数组的内存地址和指针对打印的影响
23     std::cout << "字符型数组输出:" << '\n';
24     for (int i = 0; i < ITEMS; i++) {
25         std::cout << *charPtr << " at " << reinterpret_cast<unsigned long>(charPtr) << " - " << charPtr << std::endl;
26         charPtr++;
27     }
28     
29     //测试运算
30     int *temp = intArray;
31     std::cout << *temp+1<< std::endl;
32     std::cout << *(temp+1) << std::endl;
33     return 0;
34 };

 

9.字符串指针,动态创建

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     const int num = 5;
 9     
10     string temp;
11     auto *plist  = new string [num];
12 
13     for (int i = 0; i<num; i++) {
14         cout << "请输入第" << i+1 << "个字符" << endl;
15         cin >> temp;
16 
17         plist[i] = temp;
18     }
19 
20     cout << plist->length() << endl; //4个字节,64位系统
21 
22     for (int i = 0; i<num; i++) {
23         cout << plist << " --- " <<  plist[0] << endl; //不是plist[i]
24         plist ++;   //指针加法,指向下一个字符
25     }
26     
27 }

 

10.输出阶乘

#include <iostream>
#include <string>

using namespace std;

struct student{
    char age[20];
    char gender[20];
    string name;
};

const int Arsize = 16;

int main()
{
    long long factor[Arsize];
    factor[0] = factor[1] = 1LL;

    for (int i = 2; i<Arsize; i++) {
        factor[i] = factor[i-1] *i;
    }

    for (long long i : factor) {
        cout << i << "! = " << i << endl;
        
    }
}

 

 11. 枚举和switch的组合使用

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 
 5 using namespace std;
 6 
 7 enum {
 8     red, orange, yellow, green, blue, violet, indigo
 9 };
10 
11 int main() {
12     cout << "请输入你的选择(0-6): \n";
13 
14     int code;
15 
16     while (cin >> code && code >= 0 && code <= 6) {
17         switch (code) {
18             case red:
19                 cout << 0 << endl;
20                 break;
21             case orange:
22                 cout << 1 << endl;
23                 break;
24             case yellow:
25                 cout << 2 << endl;
26                 break;
27             case green:
28                 cout << 3 << endl;
29                 break;
30             case blue:
31                 cout << 4 << endl;
32                 break;
33             case violet:
34                 cout << 5 << endl;
35                 break;
36             case indigo:
37                 cout << 6 << endl;
38                 break;
39             default:
40                 cout << "输入有误,退出" << endl;
41                 break;
42         }
43         cout << "跳出选择\n";
44     }
45     cout << "跳出循环\n";
46     return 0;
47 }

 

12. 带回车的字符串输入

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     string input{};
10     int count{};
11 
12     cout << "请输入你需要的字符,用*结尾" << endl;
13     getline(cin, input, '*');
14     count = static_cast<int>(input.length());
15 
16     cout << "字符个数为:" << input << endl;
17 
18 }

 

转载于:https://www.cnblogs.com/geoffreyone/p/9899742.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值