作业要求:
实现提示:
代码:
</pre><pre name="code" class="cpp">// homework_1_3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
struct COURSE{
COURSE() = default;
COURSE(std::string &s, unsigned int n) : courseName(s), courseID(n){}
std::string courseName;
unsigned int courseID;
};//there need a ";" behind a struct
int initCourses( std::vector<COURSE> *);
int help();
int printCourses( std::vector<COURSE> *);
int printNum( std::vector<COURSE> *);
int printLongName( std::vector<COURSE> *);
int deleteLast(std::vector<COURSE> *);
int _tmain(int argc, _TCHAR* argv[])
{
std::vector <COURSE> course;
#if 0
course[0].courseName = "English";
course[0].courseID = 001;
course[1].courseName = "English History";
course[1].courseID = 002;
course[2].courseName = "Math";
course[2].courseID = 003;
course[3].courseName = "Chinese";
course[3].courseID = 004;
course[4].courseName = "Physical education";
course[4].courseID = 005;
course[5].courseName = "Psychology";
course[5].courseID = 006;
course[6].courseName = "Study of study";
course[6].courseID = 007;
#endif
initCourses(&course);
std::cout << "Please enter a number and chose the operation: " << "\n" \
<< "0 help" << "\n" \
<< "1 print all courses " << "\n" \
<< "2 get the total number of the courses " << "\n" \
<< "3 print the longest name of the courses " << "\n" \
<< "4 delete the last course " << "\n"\
<< "5 quit the program" << "\n";
int opra = 0;
while (std::cin >> opra){
switch (opra){
case 0:
help();
break;
case 1:
printCourses(&course);
break;
case 2:
printNum(&course);
break;
case 3:
printLongName(&course);
break;
case 4:
deleteLast(&course);
break;
case 5:
goto loop;
break;
default:
std::cout << "Wrong number, please retype in: ";
break;
}
}
loop:return 0;
}
//init the courses
#if 0
int initCourses(std::vector<COURSE> *course){
(*course)[0].courseName = "English";
(*course)[0].courseID = 001;
(*course)[1].courseName = "English History";
(*course)[1].courseID = 002;
(*course)[2].courseName = "Math";
(*course)[2].courseID = 003;
(*course)[3].courseName = "Chinese";
(*course)[3].courseID = 004;
(*course)[4].courseName = "Physical education";
(*course)[4].courseID = 005;
(*course)[5].courseName = "Psychology";
(*course)[5].courseID = 006;
(*course)[6].courseName = "Study of study";
(*course)[6].courseID = 007;
return 0;
}
#endif
int initCourses(std::vector<COURSE> *course){
COURSE eng;
eng.courseID = 1001;
eng.courseName = "English";
(*course).push_back(eng);
eng.courseID = 1002;
eng.courseName = "math";
(*course).push_back(eng);
//错误代码,不能用下标的方式向vector添加元素
#if 0
(*course)[0].courseName = "English";
(*course)[0].courseID = 001;
(*course)[1].courseName = "English History";
(*course)[1].courseID = 002;
(*course)[2].courseName = "Math";
(*course)[2].courseID = 003;
(*course)[3].courseName = "Chinese";
(*course)[3].courseID = 004;
(*course)[4].courseName = "Physical education";
(*course)[4].courseID = 005;
(*course)[5].courseName = "Psychology";
(*course)[5].courseID = 006;
(*course)[6].courseName = "Study of study";
(*course)[6].courseID = 007;
#endif
return 0;
}
//pirnt the help information
int help(){
std::cout << "Please enter a number and chose the operation: " << "\n" \
<< "0 help" << "\n" \
<< "1 print all courses " << "\n" \
<< "2 get the total number of the courses " << "\n" \
<< "3 print the longest name of the courses " << "\n" \
<< "4 delete the last course " << "\n"\
<< "5 quit the program" << "\n";
return 0;
}
//print all courseName and courseID
int printCourses( std::vector<COURSE> *course){
for (std::vector<COURSE>::size_type i = 0, sz = (*course).size(); i != sz; i++){
std::cout << (*course)[i].courseName << " ";
std::cout << (*course)[i].courseID << "\n";
}
return 0;
}
int printNum( std::vector<COURSE> *course){
std::cout << "There are " << (*course).size() << "courses in the table" << "\n";
return 0;
}
int printLongName( std::vector<COURSE> *course){
COURSE longest; //save the longest name
//for cycle to choice the longest name in the vector
longest = (*course)[0]; //save the first one value in vector
if (1 == (*course).size())
;
else if ((*course).size() > 1){
for (std::vector<COURSE>::size_type it = 0;
it < (*course).size() - 1; ++it){
std::string str1 = longest.courseName;
std::string str2 = { (*course)[it + 1].courseName };
if (str1.size()< str2.size())//现存的小于下一个,则更新,否则什么也不做
longest = (*course)[it + 1];
}
}
std::cout << "The longest name of the course is " << longest.courseName << " "\
<< longest.courseID<< "\n";
return 0;
}
int deleteLast(std::vector<COURSE> *course){
std::vector<COURSE>::size_type lst = (*course).size();
std::cout << "there are " << lst << "courses in the vector" << "\n";
(*course).pop_back();//delete the last value in vector
lst = (*course).size();//更新
std::cout << "Now delete the last one,there is " << lst << "courses in the vector" << "\n";
return 0;
}
自己写之前也没看实现提示,但总体思路与提示基本相同,代码可能效率太低,在以后学习过程中自己还得多修改,多完善。
1 整个程序的循环用一个while(cin)语句和 switch 语句作为程序的入口点,简单好操作。
2 在写第一个initCourses的时候,最开始想到用下标的方式初始化vector 对象,编译的时候程序没有报错,但一运行就中止,看了半天觉得没有问题,有病乱投医,在struct 里面加构造函数,把初始化的代码放到main函数里面,但最后都是不行。之后用push_back()的方式添加元素,程序可以正常运行。查了一下书,发现p93~94清晰的写明vector对象不能用下标形式添加元素。光看书不打代码学习效果有限啊。
3 在学习过程中对vector对象在函数间传递的过程也有了一些了解。向函数传递的是地址,在函数内使用时需要先解引用再进行操作。
4 比较课程名长度,最开始直接用(string1 < string2)这种方式来判断,发现结果不正确,才想到 string类型 "<" ">" 比较的并非字符串的长度。一时想不出怎么比较长度,上网搜,发现值得学习的代码:
#include<stdio.h>
char *fun(char *s,char *t)
{
int i,j=0,k=0;
for(i=0;i<20;i++)
{
if(s[i]!='\0')
j++;
if(t[i]!='\0')
k++;
}
if(j>=k)
return s;
else
return t;
}
void main()
{
char a[20],b[20];
printf("input 1th string:");
gets(a);
printf("input 2th string:");
gets(b);
printf("%s\n",fun(a,b));
}
于是我用同样的方式比较两个字符串,运行后再次出现中止错误,原来最外层for循环中 i 超出了string 变量下标范围。又想将string保存在 char ch[20] 字符数组中,失败。邮箱将string 保存在 char ch[string.size()] 中,失败。咦——等等,string.size(),直接用这个就阔以了嘛。
折腾半天终于能够运行,虽然bug众多,但基本的操作还是能够完成。继续学习,强大程序的同时强大自己。
贴几篇学习中看到的文章:
C++编程入门系列之十四(类与对象:构造函数和析构函数)
http://www.jizhuomi.com/software/51.htmlhttp://blog.csdn.net/djf_1985/article/details/7556303
C++ 如何将容器(vector)作为参数传给一个函数?
Vector当做引用在在函数中传递
http://www.blogjava.net/babymouse/archive/2008/10/10/233536.html
C++ 中如何用 vector类作为函数的参数
http://blog.csdn.net/doctormacky/article/details/6068106