第4章复合类型-创新互联
一
分享文章:第4章复合类型-创新互联
本文路径:http://cdiso.cn/article/cecocc.html
//猜测应用到指针的运算,不确定答案
#includeint main()
{using namespace std;
char firname[15], lastname[10], lettergrade, outgrade;
unsigned int age;
cout<< "What is your first name? ";
cin >>firname;
cout<< "What is your lastname? ";
cin >>lastname;
cout<< "What letter grade do you deserve? ";
cin >>lettergrade;
switch (lettergrade) {case 'A':
outgrade = 'B';
case'B':
outgrade = 'C';
case'C':
outgrade = 'D';
case'D':
outgrade = 'A';
}
cout<< "What is your age? ";
cin >>age;
cout<< "Name: "<< lastname<< ", "<< firname<< endl
<< "Grade: "<< outgrade<< endl
<< "Age: "<< age<< endl;
return 0;
}
2023/1/16更新如下:
#include#includeint main()
{using namespace std;
string firname, lastname;
char lettergrade;
unsigned int age;
cout<< "What is your first name? ";
cin >>firname;
cout<< "What is your lastname? ";
cin >>lastname;
cout<< "What letter grade do you deserve? ";
cin >>lettergrade;
cout<< "What is your age? ";
cin >>age;
cout<< "Name: "<< lastname<< ", "<< firname<< endl
<< "Grade: "<< ++lettergrade<< endl
<< "Age: "<< age<< endl;
return 0;
}
二 cin.getline(name,ArSize);报错(待解)//instr2.cpp -- reading more than one word with getline
#include#includeint main()
{using namespace std;
const int ArSize = 20;
string name, dessert;
cout<< "Enter your name:\n";
getline(cin,name);
cout<< "Enter your favorite dessert:\n";
getline(cin,dessert);
cout<< "I hace some delious "<< dessert
<< " for you, "<< name<< ".\n";
return 0;
}
使用cin.getline(name,ArSize);会报错,待解;截图如下
#include#includeint main()
{using namespace std;
char firstname[15],lastname[10];
cout<< "Enter your first name: ";
cin.getline(firstname, 15);
cout<< "Enter your last name: ";
cin.getline(lastname,15);
cout<< "Here's the infoamation in a single: "<< lastname<< ", "<< firstname<
四 string#include#includeint main()
{using namespace std;
string firstname, lastname;
cout<< "Enter your first name: ";
cin >>firstname;
cout<< "Enter your lastname: ";
cin >>lastname;
cout<< "Here's the information in a single string: "<< lastname<< ", "<< firstname;
return 0;
}
五 struct#includeusing namespace std;
struct CandyBar {string brand;
float weight;
int calorie;
};
int main()
{CandyBar snack{"Mocha Munch",2.3,350 };
cout<< snack.brand<< endl<< snack.weight<< endl<< snack.calorie;
return 0;
}
六 struct+数组⑤#includeusing namespace std;
struct CandyBar {string brand;
float weight;
int calorie;
};
int main()
{CandyBar snack[3]
{{"Mocha Munch",2.3,350 },
{"Rose Water",1.2,600},
{"Apple grow",3.0,250}
};
cout<< snack[0].brand<< ", "<< snack[0].weight<< ", "<< snack[0].calorie<
七 struct#includeusing namespace std;
struct pizza{string company;
float length;
float weight;
};
int main()
{pizza quest1;
cout<< "Enter the name of pizza company: ";
cin >>quest1.company;
cout<< "Enter the length of pizza(cm): ";
cin >>quest1.length;
cout<< "Enter the weight of pizza(g): ";
cin >>quest1.weight;
cout<< "Company: "<< quest1.company<< endl
<< "length: "<< quest1.length<<"cm"<
八 new⑦#includeusing namespace std;
struct pizza {string company;
float length;
float weight;
};
int main()
{pizza *quest1=new pizza;
cout<< "Enter the length of pizza(cm): ";
cin >>quest1->length;
cout<< "Enter the name of pizza company: ";
cin >>quest1->company;
cout<< "Enter the weight of pizza(g): ";
cin >>quest1->weight;
cout<< "Company: "<< quest1->company<< endl
<< "length: "<< quest1->length<< "cm"<< endl
<< "weight: "<< quest1->weight<< "g";
delete quest1;
return 0;
}
九 new⑥#includeusing namespace std;
struct CandyBar {string brand;
float weight;
int calorie;
};
int main()
{CandyBar* snack = new CandyBar[3]
{{"Mocha Munch",2.3,350 },
{"Rose Water",1.2,600},
{"Apple grow",3.0,250}
};
cout<< snack[0].brand<< ", "<< snack[0].weight<< ", "<< snack[0].calorie<< endl;
cout<< snack[1].brand<< ", "<< snack[1].weight<< ", "<< snack[1].calorie<< endl;
cout<< snack[2].brand<< ", "<< snack[2].weight<< ", "<< snack[2].calorie<< endl;
delete [] snack;
return 0;
}
十 array#include#include
int main() {using namespace std;
arrayscore;
cout<< "Enter the first score(s/100m): ";
cin >>score[0];
cout<< "Enter the second score(s/100m): ";
cin >>score[1];
cout<< "Enter the third score(s/100m): ";
cin >>score[2];
cout<< "Times: 3"<< endl
<< "Average score: "<< (score[0] + score[1] + score[2]) / 3<< "s/100m";
return 0;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
分享文章:第4章复合类型-创新互联
本文路径:http://cdiso.cn/article/cecocc.html