1.编写一个C++程序,如下述输出示例所示的那样请求并显示信息;
第一题
#include<iostream>
#include<string>//不要忘记getline头文件
using namespace std;
int main()
{
string first_name, last_name;
cout << "What is your first name?";
getline(cin, first_name);
cout << "What is your last name?" ;
getline(cin, last_name);
char grade;
int age;
cout << "What letter grade do you deserve?";
cin >> grade;
cout << "What is your age?";
cin >> age;
cout << "Name:" << last_name << "," << first_name<<endl;
cout << "Grade:"<<" "<<++grade << endl;
cout << "Age:" << " " << age << endl;
system("pause");
return 0;
}
第二题
#include<iostream>
#include<string>//不要忘记getline头文件
using namespace std;
int main()
{
string name, desert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favorite dessert:\n" ;
getline(cin, desert);
cout << "I have some delicious"<<" "<<desert<<" "<<"for you,"<<name<<".\n";
system("pause");
return 0;
}
第三题
#include<iostream>
#include<cstring>//不要忘记getline头文件
using namespace std;
int main()
{
const int Arsize = 20;
char first_name[Arsize];
char last_name[Arsize];
char full_name[Arsize * 2];
cout << "Enter your first name:";
cin.getline(first_name, Arsize);
cout << "Enter your last name:";
cin.getline(last_name,Arsize);
//使用函数strcpy()将字符串复制到字符数组中,使用函数strcat()将字符串附加到字符数组末尾
strcpy_s(full_name, last_name);//如果用Strcpy会报错,改用新出的strcpy_s()函数
strcat_s(full_name, ",");
strcat_s(full_name, first_name);
cout << "Here's the information in a single string:"<<" "<<full_name<<".\n";
system("pause");
return 0;
}
第四题
#include<iostream>
#include<string>//不要忘记getline头文件
using namespace std;
int main()
{
string first_name;
string last_name;
string full_name;
cout << "Enter your first name:";
getline(cin,first_name);
cout << "Enter your last name:";
getline(cin,last_name);
full_name = last_name +","+first_name;
cout << "Here's the information in a single string:" << " " << full_name << ".\n";
system("pause");
return 0;
}
第五题
#include<iostream>
#include<cstring>
using namespace std;
struct CandyBar {
string brand;
float weight;
int calorie;
};
int main()
{
CandyBar snake ={ "Mocha Munch",2.3,350};
cout << snake.brand << endl;
cout << snake.weight << endl;
cout << snake.calorie << endl;
system("pause");
return 0;
}
第六题
#include<iostream>
#include<cstring>
using namespace std;
struct CandyBar {
string brand;
float weight;
int calorie;
};
int main()
{
CandyBar snake[3];
snake[0] = { "Mocha Munch",2.3,350 };
snake[1] = { "Chocolate",2.2,320 };
snake[2] = { "crystal",2.1,210 };
cout << snake[0].brand << endl;
cout << snake[1].weight << endl;
cout << snake[2].calorie << endl;
system("pause");
return 0;
}
//主要考察结构体数组
第七题
#include<iostream>
#include<string>
using namespace std;
struct Pizza {
string name;
int diameter;
int weight;
};
int main()
{
Pizza snake;
cout << "Please enter the name of the Pizza Company:";
getline(cin, snake.name);
cout << "Please enter the diameter of the pizza:";
cin>>snake.diameter;
cout << "Please enter the weight of the pizza:";
cin>>snake.weight;
cout << snake.name << endl;
cout << snake.diameter << endl;
cout << snake.weight << endl;
system("pause");
return 0;
}
第八题
#include<iostream>
#include<string>
using namespace std;
struct Pizza {
string name;
int diameter;
int weight;
};
int main()
{
Pizza *snake = new Pizza;//创建动态结构
cout << "Please enter the diameter of the pizza:";
cin>>snake->diameter;
cin.get();
cout << "Please enter the weight of the pizza:";
cin>>snake->weight;
cin.get();
cout << "Please enter the name of the pizza:";
getline(cin, snake->name);
cout << "diameter:" << snake->diameter << " name:" << snake->name << " weight:" << snake->weight << endl;
delete snake;
system("pause");
return 0;
}
第九题
#include<iostream>
#include<string>
using namespace std;
struct Pizza {
string name;
int diameter;
int weight;
};
int main()
{
Pizza *snake = new Pizza[3];//创建动态数组
cout << "Please enter the diameter of the pizza:";
cin>>snake[0].diameter;
cin.get();
cout << "Please enter the weight of the pizza:";
cin>>snake[1].weight;
cin.get();
cout << "Please enter the name of the pizza:";
getline(cin, snake[2].name);
cout << "diameter:" << snake[0].diameter << " name:" << snake[2].name << " weight:" << snake[1].weight << endl;
system("pause");
return 0;
}
第十题
#include<iostream>
#include<array>
using namespace std;
int main()
{
array<int, 3>a1;
cout << "请输入第一次成绩:";
cin >> a1[0];
cout << "请输入第二次成绩:";
cin >> a1[1];
cout << "请输入第三次成绩:";
cin >> a1[2];
cout << "三次平均成绩为:" << (a1[0] + a1[1] + a1[2]) / 3 << endl;
system("pause");
return 0;
}