1.#include<bits/stdc++.h>
using namespace std;
int main(){
char fisrtname[128],lastname[128];
char grade;
int age;
cout<<“What is your first name”<<endl;
cin.getline(fisrtname,128);
cout<<“What is your last name”<<endl;
cin.getline(lastname,128);
cout<<“What letter grade do you deserve”<<endl;
cin>>grade;
cout<<“What is your age?”<<endl;
cin>>age;
cout<<"Name: “<<lastname<<”, "<<fisrtname<<endl;
cout<<"Grade: "<<char(grade+1)<<endl;
cout<<"Age: "<<age<<endl;
}
cin.getline的两个参数,第一个是字符数组,第二个是读取的字符数,因此如果必须定义为char型的字符数组
getline(cin,str)这种方式可以用string来定义字符串
C++对字符用单引号,对字符串用双引号。
由于A,B,C等是字符,定义的时候,char grade就可以了
字符用AsCII编码,char grade = ‘A’实际grade是int型,为65,可以+1操作
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
struct students
{
string fisrtname;
string lastname;
char grade;
int age;
};
void display(students);
int main(){
students name;
cout<<"What is your first name? ";
getline(cin,name.fisrtname);
cout<<"What is your last name? ";
getline(cin,name.lastname);
cout<<"What letter grade do you deserve? ";
cin>>name.grade;
cout<<"What is your age? ";
cin>>name.age;
display(name);
}
void display(students lcg)
{
cout<<"Name: “<<lcg.lastname<<”, "<<lcg.fisrtname<<endl;
cout<<"Grade: "<<char(lcg.grade+1)<<endl;
cout<<"Age: "<<lcg.age<<endl;
}
声明函数,参数为结构体。函数声明放在结构体之后
2.#include<bits/stdc++.h>
#include<string.h>
using namespace std;
int main(){
string name,dessert;
cout<<“Enter your name:\n”;
getline(cin,name);
cout<<“Enter your favorite dessert”<<endl;
getline(cin,dessert);
cout<<“I have some delicious “<<dessert;
cout<<” for you, “<<name<<”.\n”;
return 0;
}
3.#include<bits/stdc++.h>
#include<string.h>
using namespace std;
const int size=128;
struct students
{
char fisrtname[size];
char lastname[size];
};
void display(students);
int main(){
students name;
cout<<"Enter your first name: ";
cin.get(name.fisrtname,size);
cin.get();
cout<<"Enter your last name: ";
cin.get(name.lastname,size);
display(name);
}
void display(students lcg)
{
cout<<"Here’s the information in a single string: “<<lcg.lastname<<”, "<<lcg.fisrtname<<endl;
}
4.#include<bits/stdc++.h>
#include<string.h>
using namespace std;
struct students
{
string fisrtname;
string lastname;
};
void display(students);
int main(){
students name;
cout<<"Enter your first name: ";
getline(cin,name.fisrtname);
cout<<"Enter your last name: ";
getline(cin,name.lastname);
display(name);
}
void display(students lcg)
{
cout<<"Here’s the information in a single string: “<<lcg.lastname<<”, "<<lcg.fisrtname<<endl;
}
5.#include<bits/stdc++.h>
#include<string.h>
using namespace std;
struct CandyBar
{
string name;
double weight;
int calories;
};
int main(){
CandyBar snack
{
“Mocha Munch”,
2.3,
350
};
cout<<"name is "<<snack.name<<endl;
cout<<"weight is "<<snack.weight<<endl;
cout<<"calories is "<<snack.calories<<endl;
}
6.#include<bits/stdc++.h>
#include<string.h>
using namespace std;
const int size=3;
struct CandyBar
{
string name;
double weight;
int calories;
};
int main(){
CandyBar snack[size]={
{“Mocha Munch”,2.3,350},
{“Pocha Punch”,4.3,550},
{“Nocha Nunch”,3.3,450}
};
for (int i=0;i<size;i++)
{
cout<<"name is "<<snack[i].name<<endl;
cout<<"weight is "<<snack[i].weight<<endl;
cout<<"calories is "<<snack[i].calories<<endl;
}
}
7.#include<bits/stdc++.h>
#include<string.h>
using namespace std;
const int size=3;
struct Pizza
{
string name;
double length;
double weight;
};
void display(Pizza);
int main(){
Pizza spizza;
cout<<“Enter name of pizza:”;
getline(cin,spizza.name);
cout<<“Enter length of pizza:”;
cin>>spizza.length;
cout<<“Enter weight of pizza:”;
cin>>spizza.weight;
display(spizza);
}
void display(Pizza lcg)
{
cout<<“Pizza name is “<<lcg.name<<”,”<<“length is “<<lcg.length<<”,”<<"weight is "<<lcg.weight<<endl;
}
8.#include<bits/stdc++.h>
#include<string.h>
using namespace std;
const int size=3;
struct Pizza
{
string name;
double length;
double weight;
};
void display(Pizza);
int main(){
*Pizza spizza=new Pizza;
cout<<“Enter length of pizza:”;
cin>>spizza->length;
cin.get();
cout<<“Enter name of pizza:”;
getline(cin,spizza->name);
cout<<“Enter weight of pizza:”;
cin>>spizza->weight;
*display(spizza);
delete spizza;
}
void display(Pizza lcg)
{
cout<<“Pizza name is “<<lcg.name<<”,”<<“length is “<<lcg.length<<”,”<<"weight is "<<lcg.weight<<endl;
}
先输入double类型的变量后,需要cin.get()来处理下空格或者换行符,不然会不让输入name
display带的参数*spizza而不是&spizza或者spizza。因为是结构体
注意要及时释放指针内存,防止内存泄漏
9.#include<bits/stdc++.h>
#include<string.h>
using namespace std;
const int size=3;
struct CandyBar
{
string name;
double weight;
int calories;
};
int main(){
CandyBar *snack=new CandyBar[size]{
{“Mocha Munch”,2.3,350},
{“Pocha Punch”,4.3,550},
{“Nocha Nunch”,3.3,450}
};
for (int i=0;i<size;i++)
{
cout<<"name is "<<snack[i].name<<endl;
cout<<"weight is "<<snack[i].weight<<endl;
cout<<"calories is "<<snack[i].calories<<endl;
}
}
注意用new动态声明数组
10.#include<bits/stdc++.h>
#include<string.h>
#include
using namespace std;
const int size=3;
int main(){
array<double,size>result{0.0};
double averageresult=0.0;
cout<<“Enter three results time of runing 40 meters:\n”;
cin>>result[0];
cin>>result[1];
cin>>result[2];
averageresult=(result[0]+result[1]+result[2])/3;
cout<<“average of result is “<<averageresult<<endl;
cout<<“result is “<<result[0]<<”,”<<result[1]<<”,”<<result[2]<<endl;
}
codepath:https://github.com/zhouzora/Learning-C-skills