第一题
要求:接受名字有多个单词;下调成绩(上调字母);
//4.1
#include<iostream>
char changegrade(char n);
//创建一个char函数来往下更改成绩
int main()
{
using namespace std;
cout << "What is your first name ?\n";
char strname[20];
cin.getline(strname, 20);
cout << "What is your last name ?\n";
char strname1[20];
cin.getline(strname1, 20);
cout << "What letter grade do you deserve ?\n";
char n;
cin >> n;
n = changegrade(n);//调用函数
cout << "What is your age ?\n";
int years;
cin >> years;
cout << "Name : " << strname1 << ", " << strname << endl;
cout << "Grade : " << n << endl;
cout << "Age : " << years << endl;
return 0;
}
char changegrade(char n)
{
char m;
if (n == 'A')
m = 'B';
else if (n == 'B')
m = 'C';
else if (n == 'C')
m = 'D';
else
m = 'F';
return m;
}
第二题
要求:使用c++string 类改写程序.
//4.2
#include<iostream>
#include<cstring>
#include<string>
int main()
{
using namespace std;
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin, name);//需要注意,如果输入字符前有其他输入,应该在前加入cin.get().
cout << "Enter your favorite dessert\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert << " for you, " << name;
return 0;
}
第三题
要求:用char数组将输入字符串储存并组合输出。
//4.3
#include<iostream>
#include<cstring>
#pragma warning(disable : 4996)//用warning 忽视编码4996错误
int main()
{
using namespace std;
cout << "Enter your first name : ";
char firstname[20];
cin.get(firstname, 20).get();
cout << "Enter your last name : ";
char lastname[20];
cin.get(lastname, 20).get();//分开写就是
//cin.get(lastname,20);
//cin.get();
strcat(lastname, ", ");
strcat(lastname, firstname);
char Name[20];
strcpy(Name, lastname);
cout << "Here's the information in a single string : "
<< Name << endl;
return 0;
}
第四题
要求:用string类将字符串储存并组合输出。
//4.4
#include<iostream>
#include<string>
int main()
{
using namespace std;
cout << "Enter your first name : ";
string firstname;
getline(cin, firstname);
cout << "Enter your last name : ";
string lastname;
getline(cin, lastname);
lastname += ", ";
lastname += firstname;
string Name = lastname;
cout << "Here's the information in a single string : "
<< Name << endl;
return 0;
}
第五&六题
要求:创建结构,初始化结构,输出结构成员内容
//4.5&4.6
#include <iostream>
struct CandyBar
{
std::string candytype;
int heat;
double weight;
};
int main()
{
using namespace std;
CandyBar snack;
snack.candytype= "Mocha Manch";
snack.heat = 350;
snack.weight = 2.3;
CandyBar Snack =
{
"Manch",250,30.0
};
cout << "type ; " << snack.candytype << endl;
cout << "heat ; " << snack.heat << endl;
cout << "weight : " << snack.weight << endl;
cout << "type : " << Snack.candytype << endl;
cout << "heat : " << Snack.heat << endl;
cout << "weight : " << Snack.weight << endl;
return 0;
}
第七题
要求:对多个披萨的数据进行统计,创造结构输入信息,储存并输出。
4.7
#include<iostream>
#include<string>
struct Pizza
{
std::string companyname;
double diameter;
double weight;
};
int main()
{
using namespace std;//也可以将名称空间前提到结构创造前
cout << "Enter the number of pizzas :\n";
int n,N;
cin >> n;
const int m = 100;
Pizza pizzas[m];
for (N = 0;N <n; N++)
{
cin.get();
cout << "pizza's name : \n";
getline(cin, pizzas[N].companyname);
cout << "pizza's diameter : \n";
cin >> pizzas[N].diameter;
cout << "pizza's weight : \n";
cin >> pizzas[N].weight;
};
for (N=0;N < n; N++)
{
cout << "Name: " << pizzas[N].companyname << endl;
cout << "Diameter: " << pizzas[N].diameter << endl;
cout << "Weight: " << pizzas[N].weight << endl;
}
return 0;
}
第八题
要求:使用new来创建动态结构,并且在输入名称前输入直径(要求在使用getline输入字符串前,注意使用到cin.get()消去数字输入留下的enter。)
//4.8
#include<iostream>
#include<string>
struct Pizza
{
std::string PCname;
double PDmeter;
double PWeight;
};
int main()
{
using namespace std;
Pizza* ps = new Pizza;
cout << "Enter the diameter :\n";
cin >> ps->PDmeter;
cout << "Enter the name :\n";
cin.get();
getline(cin, (*ps).PCname);
cout << "Enter the weight : \n";
cin >> (*ps).PWeight;
cout << "Name: " << ps->PCname << "\nDiameter: " << (*ps).PDmeter
<< "\nWeight: " << ps->PWeight << endl;
delete ps;
return 0;
}
第九题
要求:使用new来创建动态数组
//4.9
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string type;
double heat;
double weight;
};
int main()
{
CandyBar* ps = new CandyBar [3];
ps[0].type= "Mocha Munnch";
ps[0].heat = 2.3;
ps[0].weight = 250;
CandyBar** pa = &ps;
cout << "type : " << pa[0]->type
<< "\nheat : " << (*pa[0]).heat
<< "\nweight : " << ps[0].weight
<< endl;
return 0;
}
第十题
要求:使用array来替代创建固长数组。
//4.10
#include<iostream>
#include<array>
int main()
{
using namespace std;
array<double, 3> time;
cin >> time[0] >> time[1] >> time[2];
cout << "Grade :\n"
<< time[0] << endl;
cout << time[1] << endl;
cout << time[2] << endl;
return 0;
}