网站联系我们怎么做,第三方做网站,苏州网站建设 苏州网络推广专家,wordpress手机显示缩文章目录 第一章 C编程基础1.41.51.61.71.8 第二章 面向过程的编程风格2.12.22.32.42.52.6 第一章 C编程基础
1.4
/*********************************************************************说明:试着扩充这个程序的内容#xff1a;#xff08;1#xff09;要求用户同时输… 文章目录 第一章 C编程基础1.41.51.61.71.8 第二章 面向过程的编程风格2.12.22.32.42.52.6 第一章 C编程基础
1.4
/*********************************************************************说明:试着扩充这个程序的内容1要求用户同时输入名字first name和姓氏last name
2修改输出结果同时打印出姓氏和名字。
*********************************************************************/
#include iostream
#include string
using namespace std;int main() {string user_name;cout 输入名字\n;cin user_name;cout \n你好 user_name;return 0;
}
1.5
/*********************************************************************说明:编写一个程序能够询问用户的姓名并读取用户所输入的内容。请确保用户输入的名称长度大
于两个字符。如果用户的确输入了有效名称就响应一些信息。请以两种方式实现第一种使用
C-style字符串第二种使用string对象。
*********************************************************************/
#include iostream
#include string
#include cstring
using namespace std;//使用string对象
int main() {string usr_name;cout Please enter your name: endl;cin usr_name;switch (usr_name.size()) {case 0:cout Fail,No Name.;break;case 1:cout Fail,Onlu A 1-character name.;break;default :cout Hello, usr_name endl;}return 0;
}
1.6
/*********************************************************************说明:编写一个程序从标准输入设备读取一串整数并将读入的整数依次放到array及
vector然后遍历这两种容器求取数值总和。将总和及平均值输出至标准输出设备
*********************************************************************/
#include iostream
#include vector
using namespace std;// 使用array实现
//int main() {
// const int array_size 128;
// int ia[array_size];
//
// int ival, icnt 0; // ival用于暂存输入icnt表示输入数据量
// int sum 0;
// cout Please Enter Some Numbers: endl;
//
// while (cin ival icnt array_size) {
// ia[icnt] ival;
// }
//
// for (int i 0; i icnt; i) {
// sum ia[i];
// }
// int average sum / icnt;
// cout Sum of icnt
// elements: sum
// . Average: average endl;
// return 0;
//}//使用vector实现
int main() {const int array_size 128;vectorintia; //无需在初始化的时候就确实大小int ival 0; // ival用于暂存输入int sum 0;cout Please Enter Some Numbers: endl;while (cin ival ia.size() array_size) {ia.push_back(ival);}for (int i 0; i ia.size(); i) {sum ia[i];}int average sum / ia.size();cout Sum of ia.size() elements: sum . Average: average endl;return 0;
}
1.7
/*********************************************************************说明:使用你最趁手的编辑工具输入两行或更多文字并存盘。然后编写一个程序打开该文本文
件将其中每个字都读取到一个vectorstring对象中。遍历该vector将内容显示到cout。
然后利用泛型算法sort()对所有文字排序
#include algorithm
sort( container.beginer(), container.end() );
再将排序后的结果输出到另一个文件。*********************************************************************/
#include iostream
#include fstream
#include vector
#include algorithm
#include string
using namespace std;int main() {vectorstringistr;ifstream infile(ex1.7Read.txt);if (!infile) {cerr Fail infile!\n;} else {string word;while (infile word) {istr.push_back(word);}}// 排序sort(istr.begin(), istr.end());ofstream outfile(ex1.7Write.txt, ios_base::app);if (!outfile)cerr Fail outfile!\n;elsefor (int i 0; i istr.size(); i) {outfile istr[i] ;}outfile endl;return 0;
}
1.8
/*********************************************************************说明:switch语句让我们得以根据用户答错的次数提供不同的安慰语句。请以array储存四种
不同的字符串信息并以用户答错次数作为array的索引值以此方式来显示安慰语句。
*********************************************************************/
#include iostream
using namespace std;const char *msg_to_usr( int num_tries ) {const int rsp_cnt 5;static const char *usr_msgs[ rsp_cnt ] {Go on, make a guess. ,Oops! Nice guess but not quite it.,Hmm, Sorry. Wrong a second time.,Ah, this is harder than it looks, no?,It must be getting pretty frustrating by now!};if ( num_tries 0 ) {num_tries 0;} else if ( num_tries rsp_cnt ) {num_tries rsp_cnt - 1;}return usr_msgs[ num_tries ];
}int main() {cout msg_to_usr(3) endl;return 0;
}第二章 面向过程的编程风格
2.1
/*********************************************************************说明: 先前的main()只让用户输入一个位置值然后便结束程序。如果用户想取得两个甚至更多元素值
他必须执行这个程序两次或多次。请改写main()使它允许用户不断输入位置值直到用户希望
停止为止。
*********************************************************************/
#include iostream
using namespace std;bool fibon_elem(int, int );int main() {int pos, elem;char ch;bool more true;while (more) {cout 请输入一个位置 ;cin pos;if (fibon_elem(pos, elem)) {cout 此位置 pos 的值是 elem endl;} else {cout error;}cout 是否还要继续(Y/N)? endl;cin ch;if (ch ! y ch ! Y) {more false;}}return 0;
}bool fibon_elem(int pos, int elem) {// 检查位置值是否合理if (pos 0 || pos 1024) {elem 0;return false;}// 位置值为1和2时elem的值为1int val 1;int n_1 1, n_2 1;switch (pos) {default:case 2:cout 1 ;case 1:cout 1 ;}for (int i 3; i pos; i) {val n_2 n_1;n_2 n_1;n_1 val;cout val (!(i % 10) ? \n\t : );}elem val;return true;
}2.2
/*********************************************************************说明:Pentagonal数列的求值公式是 Pnni*(3r-1) /2借此产生15,12,22,35等元素值。试定义一个函数利用上述公式将产生的元素置人用户传入的 vector 之中元素数目由用户指定。请检查元素数目的有效性译注:太大则可能引发overflow问题)接下来撰写第二个函数能够将所接获的 vector 的所有元素一-一印出。此函数的第二参数接受一个字符串表示储存于vector 内的数列的类型。最后再写一个main( )测试上述两个函数。
*********************************************************************/
#include iostream
#include vector
#include fstreamusing namespace std;bool calc_elems(vectorint vec, int pos);
void display_elems(vectorint vec, ostream os cout);int main() {vectorint pent;// 检查上面声明的两个函数if ( calc_elems( pent, 0 ))display_elems( pent );if ( calc_elems( pent, 8 ))display_elems( pent );ofstream ofil(ex2.2.txt);if ( calc_elems( pent, 14 ))display_elems( pent, ofil);if ( calc_elems( pent, 138 ))display_elems( pent );return 0;
}bool calc_elems(vectorint vec, int pos) {if (pos 0 || pos 64) {cerr Sorry. Invaild position: pos endl;return false;}for (int ix vec.size() 1; ix pos; ix) {vec.push_back((ix * (3 * ix - 1)) / 2);};return true;
};void display_elems(vectorint vec, ostream os) {os \nPentagonal Numeric Series\n\t;for (int ix 0; ix vec.size(); ix)os vec[ix] ;os endl;
};2.3
/*********************************************************************说明:将练习2.2的Pentagonal数列求值函数拆分为两个函数其中之一为inline用来检验元素个数
是否合理。如果的确合理而且尚未被计算便执行第二个函数执行实际的求值工作。
*********************************************************************/
#include iostream
#include vector
#include fstreamusing namespace std;inline bool calc_elems(vectorint vec, int pos);
extern void really_calc_elems(vectorint vec, int pos);
void display_elems(vectorint vec, ostream os cout);int main() {vectorint pent;// 检查上面声明的两个函数if ( calc_elems( pent, 0 ))display_elems( pent );if ( calc_elems( pent, 8 ))display_elems( pent );ofstream ofil(ex2.3.txt);if ( calc_elems( pent, 14 ))display_elems( pent, ofil);if ( calc_elems( pent, 138 ))display_elems( pent );return 0;
}bool calc_elems(vectorint vec, int pos) {if (pos 0 || pos 64) {cerr Sorry. Invaild position: pos endl;return false;}if ( vec.size() pos) {really_calc_elems( vec, pos);}return true;
};void really_calc_elems(vectorint vec, int pos) {for (int ix vec.size() 1; ix pos; ix) {vec.push_back((ix * (3 * ix - 1)) / 2);};
}void display_elems(vectorint vec, ostream os) {os \nPentagonal Numeric Series\n\t;for (int ix 0; ix vec.size(); ix)os vec[ix] ;os endl;
};2.4
/*********************************************************************说明:写一个函数以局部静态local static)的vector存储 Pentagonal数列元素。此函数返回一个const 指针指向该vector。如果 vector 的容量小于指定的元素数目就扩充 vector的容量。接下来再实现第二个函数接受一个位置值并返回该位置上的元素。最后撰写main (测试这些函数。*********************************************************************/
#include iostream
#include vector
#include fstream
using namespace std;inline bool check_validity(int pos);
const vectorint *initelems(int pos);
void display_elems(vectorint vec, ostream os cout);
bool get_elems(int pos, int elem);int main() {int elem;// 检查上面声明的两个函数if ( get_elems( 0, elem ))cout element 1 is elem \n;for (int i 1; i 10; i ) {if ( get_elems( i, elem ))cout element i \tis elem \n;}return 0;
}bool check_validity(int pos) {return (pos 0 || pos 64) ? false : true;
}const vectorint *initelems(int pos) {static vectorint elems;if (check_validity(pos) || pos elems.size()) {for (int ix elems.size() 1; ix pos; ix) {elems.push_back((ix * (3 * ix - 1)) / 2);};}return elems;
}bool get_elems(int pos, int elem) {if ( !check_validity( pos )) {cout Sorry. Invalid position: pos endl;elem 0;return false;};const vectorint *elems initelems(pos);elem (*elems)[pos - 1];return true;
}void display_elems(vectorint vec, ostream os) {os \nPentagonal Numeric Series\n\t;for (int ix 0; ix vec.size(); ix)os vec[ix] ;os endl;
};2.5
/*********************************************************************说明:实现一个重载的max (〉函数让它接受以下参数:(a)两个整数;(b)两个浮点数;(c)两个字符串:(d)一个整数vector;(e)一个浮点数vector:(f)一个字符串 vector;(g)一个整数数组以及一个表示数组大小的整数值:(h)一个浮点数数组以及一个表示数组大小的整数值:(i)-个字符串数组以及一个表示数组大小的整数值最后撰写main()测试这些函数。
*********************************************************************/
#include iostream
#include string
#include algorithm
#include vector
using namespace std;inline int max (int a, int b);
inline float max (float a, float b);
inline string max (const string a, const string b);
inline int max (const vectorint vec);
inline float max ( const vectorfloat vec );
inline string max ( const vectorstring vec );
inline string max ( const string a, const string b );
inline int max ( const int *parray, int size );
inline float max( const float *parray, int size );
inline string max ( const string *parray, int size );int main() {string sarray[] { we, were, her, pride, of, ten };vectorstring svec( sarray, sarray 6 );int iarray[] { 12, 70, 2, 169, 1, 5, 29 };vectorint ivec( iarray, iarray 7 );float farray[] { 2.5, 24.8, 18.7, 4.1, 23.9 };vectorfloat fvec( farray, farray 5 );int imax max( max( ivec ), max( iarray, 7 ));float fmax max( max( fvec ), max( farray, 5 ));string smax max( max( svec ), max( sarray, 6 ));cout imax should be 169 -- found: imax \n fmax should be 24.8 -- found: fmax \n smax should be were -- found: smax \n;return 0;
}int max (int a, int b) {return (a b) ? a : b;
}float max (float a, float b) {return (a b) ? a : b;
}string max ( const string a, const string b ) {return a b ? a : b;
}int max ( const vectorint vec ) {return *max_element( vec.begin(), vec.end() );
}float max ( const vectorfloat vec ) {return *max_element( vec.begin(), vec.end() );
}string max ( const vectorstring vec ) {return *max_element( vec.begin(), vec.end() );
}int max ( const int *parray, int size ) {return *max_element( parray, parray size );
}float max( const float *parray, int size ) {return *max_element( parray, parray size );
}string max ( const string *parray, int size ) {return *max_element( parray, parray size );
}2.6
/*********************************************************************说明:以template 重新完成练习2.5并对main(〉函数做适度的修改.
*********************************************************************/
#include iostream
#include string
#include algorithm
#include vector
using namespace std;template typename Typeinline Type mymax(Type t1, Type t2) {return (t1 t2) ? t1 : t2;
}template typename elemTypeinline elemType mymax(const vectorelemType vec) {return *max_element( vec.begin(), vec.end() );
}template typename arrayTypeinline arrayType mymax(const arrayType *parray, int size) {return *max_element( parray, parray size );
}int main() {string sarray[] { we, were, her, pride, of, ten };vectorstring svec( sarray, sarray 6 );int iarray[] { 12, 70, 2, 169, 1, 5, 29 };vectorint ivec( iarray, iarray 7 );float farray[] { 2.5, 24.8, 18.7, 4.1, 23.9 };vectorfloat fvec( farray, farray 5 );int imax mymax( mymax( ivec ), mymax( iarray, 7 ));float fmax mymax( mymax( fvec ), mymax( farray, 5 ));string smax mymax( mymax( svec ), mymax( sarray, 6 ));cout imax should be 169 -- found: imax \n fmax should be 24.8 -- found: fmax \n smax should be were -- found: smax \n;return 0;
}
文章转载自: http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.zxzgr.cn.gov.cn.zxzgr.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.jxltk.cn.gov.cn.jxltk.cn http://www.morning.bncrx.cn.gov.cn.bncrx.cn http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.ykgkh.cn.gov.cn.ykgkh.cn http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.mnnxt.cn.gov.cn.mnnxt.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.qglqb.cn.gov.cn.qglqb.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.nrftd.cn.gov.cn.nrftd.cn http://www.morning.wljzr.cn.gov.cn.wljzr.cn http://www.morning.rqzyz.cn.gov.cn.rqzyz.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.ypdhl.cn.gov.cn.ypdhl.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.psqs.cn.gov.cn.psqs.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.ypjjh.cn.gov.cn.ypjjh.cn http://www.morning.mjctt.cn.gov.cn.mjctt.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.ydfr.cn.gov.cn.ydfr.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.lmknf.cn.gov.cn.lmknf.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.kflpf.cn.gov.cn.kflpf.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.wclxm.cn.gov.cn.wclxm.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.qwpyf.cn.gov.cn.qwpyf.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.kpgms.cn.gov.cn.kpgms.cn http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.gmztd.cn.gov.cn.gmztd.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.pjbhk.cn.gov.cn.pjbhk.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.tgczj.cn.gov.cn.tgczj.cn