c++ prime plus 第四章1~4

来源:互联网 发布:余弦匹配算法 编辑:程序博客网 时间:2024/06/07 14:29

今天本该有一节体育课的,但是下雨了,所以就躲到图书馆看看我的c++。主要看的是指针的一些内容。

1.枚举量,定义符号常量。

2.指针,new和delete,指针和数组的关系。

3.数组的地址,是整个数组的地址。如果想打印字符串的地址,需要强制转换。

//c++ 4.8.3实验.cpp

#include <iostream>using namespace std;int main(){    const char * p = "Hello world!";    cout << p << " at " << (int *) p << endl;//输出指向字符串的地址    int sentence2[4] = {12,45,8,5};    int * p2 = sentence2;    cout << p2 << endl;    cout << &sentence2 << endl;    cout << sentence2+1 << endl;    cout << &sentence2+1 << endl;    return 0;}//char sentence = "Hello world!";//error: invalid conversion from 'const char*' to 'char'//char sentence[15] = "Hello world!";
4.暂时忽略了结构体


//answer

#include <iostream>#include <string>#include <cstring>using namespace std;int main(){    //char firstname[10] = "hell wrld";    //cout << "What is your first name? " ;    //cout << firstname << endl;   不能读一行    char name[20] ;    cout << "What is your name? " ;    cin.getline (name, 20);    char firstname[20] ;    cout << "What is your first name? " ;    cin.get(firstname, 20);    cin.get();//get 和 getline 的不同使用    //cout << "What is your last name? ";    //string lastname ;    //getline(cin, lastname);    //strcat_s脱胎于strcat,用于两个字符串的链接,strcat不支持string类型    char lastname[20] ;    cout << "What is your last name? " ;    cin.getline(lastname, 20);    cout << "What letter grade do you deserve? ";    char grade;    cin >> grade;    grade++;    cout << "What is your age?";    int age;    cin >> age;    cout << "Who do you love?";    char hername[20];    cin.get();    cin.getline(hername,20);    char allname[80] ;    strcpy(allname, firstname);    strcat(allname, " , ");    strcat(allname, lastname);    cout << "Name: " << lastname << " , " << firstname << endl;    cout << allname << endl;    cout << "Grade: " << grade << endl;    cout << "Age: " << age << endl;    cout << "lover: " << hername << endl;    return 0;}
这个没怎么用到指针,好气啊。感觉我看了好长时间,最后写题目没用到。。。

原创粉丝点击