1.编写一个c++程序,如下述输出示例请求并显示信息。
注意,改程序应该接受的名字包含多个单词,另外,程序将向下调整成绩,既向上条一个字母。
#include <iostream>
#include <string>
using namespace std;
struct user_1
{
char first_name[20];
char finally_name[20];
char grand_1;
int age;
};
int main()
{
user_1 you ;
cout << "please enter your first name? ";
cin.getline(you.first_name, 20);
cout << "what is your last name? ";
cin.getline(you.finally_name, 20);
cout << "what letter grade do you deserve ? ";
cin >> you.grand_1;
cout << "what is your age? ";
cin >> you.age;
cout << "Name:" << you.first_name << ", " << you.finally_name << endl;
cout << "Grade: " <<char( you.grand_1+1)<< endl;
cout << "Age: " << you.age;
system("pause");
return 0;
}
2.修改程序清单4.4,使用c++string而不是char数组。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favorite dessert:\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " For you ," << name << ".\n";
system("pause");
return 0;
}
4.编写一个程序,它要求用户首先输入其名,然后输入姓;然后程序使用逗号和空格将姓和名组合在一起,并存储和显示组合结果。清使用char数组和头文件string中的函数。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string first_name, last_name;
string name;
cout << "Enter your first name : ";
cin >> first_name;
cout << "Enter your last name : ";
cin >> last_name;
name = first_name + ", " + last_name;
cout << "Here is the information in a single string :" <<name << endl;
system("pause");
return 0;
}
5.结构CandyBar包含3个成员。第一个成员存储糖块的品牌;第二个成员储存糖块的重量包含小数;第三个长远储存糖块的卡路里整数。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化为“Mocha Munch”,2.3和350.初始化应在声明snack时进行。最后,程序显示snack变量内容。
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string name = { "Mocha Munch" };
double weight=2.3;
int kaluli=350;
}snack;
int main()
{
cout << "name " << snack.name << endl;
cout << "weight " << snack.weight << endl;
cout << "Kaluli " << snack.kaluli << endl;
system("pause");
return 0;
}
6.结构CandyBar包含3个成员,请编写一个程序,创建一个包含3个元素的CandyBar数组,并将他们初始化为所选的值,然后显示每个结构的内容。
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string name ;
double weight ;
int kaluli ;
};
CandyBar snack[3]=
{
{ "one", 2.1, 340 },
{ "two", 2.2, 350 },
{"three", 2.3, 360}
};
int main()
{
int i;
cout << "please enter your chance nunber";
cin >> i;
i--;
cout << "name " << snack[i].name << endl;
cout << "weight " << snack[i].weight << endl;
cout << "Kaluli " << snack[i].kaluli << endl;
system("pause");
return 0;
}
7.他从事比萨饼分析服务,对于每个披萨饼,他需要记录下列信息:披萨公司名称,包含多个单词。披萨的直径。披萨的重量。请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序,程序将请求用户输入上述信息,然后显示这些信息。
#include <iostream>
#include <string>
using namespace std;
struct p
{
string name;
double zhijing;
double weight;
}pisa;
int main()
{
cout << "please enter name ";
getline( cin,pisa.name);
cout << "please enter zhijing ";
cin >> pisa.zhijing;
cout << "please enter weiht ";
cin >> pisa.weight;
cout << pisa.name << endl << pisa.zhijing << endl<< pisa.weight;
system("pause");
return 0;
}
8.完成编程练习7,但使用new来动态分配内存,而不是声明一个结构变量,另外,然程序在请求输入披萨饼公司的名称之前输入披萨的直径。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string *name = new string;
double *zhijing = new double;
double *weight = new double;
cout << "please enter name ";
getline(cin,* name);
cout << "please enter zhijing ";
cin >> *zhijing;
cout << "please enter weiht ";
cin >>* weight;
cout << *name << endl << *zhijing << endl << *weight;
system("pause");
return 0;
}
9.完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
#include <iostream>
#include <string>
using namespace std;
struct MyStruct
{
string name;
double zhijing;
double weiht;
};
int main()
{
MyStruct * snack = new MyStruct[3];
snack[0] = { "one", 2.3, 350 };
snack[1] = { "two", 2.4, 360 };
snack[2] = { "three", 2.5, 370 };
cout << snack[0].name << endl << snack[0].zhijing << endl << snack[0].weiht << endl;
cout << snack[1].name << endl << snack[1].zhijing << endl << snack[1].weiht << endl;
cout << snack[2].name << endl << snack[2].zhijing << endl << snack[2].weiht << endl;
system("pause");
return 0;
}
10.编写一个程序,让用户输入三次40码跑的成绩,并显示次数和平均成绩。请输入一个array对象来存储数据。
#include <iostream>
#include <array>
using namespace std;
int main()
{
array < double, 3 >a3= {};
double ave;
cin >> a3[0];
cin >> a3[1];
cin >> a3[2];
ave = (a3[0] + a3[1] + a3[2]) / 3;
cout << "ave = " << ave;
system("pause");
return 0;
}
|