摘要: 从键盘输入x的值(要求为实型),根据以下公式计算并输出x和y 的值要求采用两种解法完成,解法1用if-else语句,解法2主体用switch语句(注意到分段的关键点处,x的值均是偶数,这是可以利用的条件)。提示:在这个公式中,x<0是非法的输入,程序中应该做出处理。测试要求:运行至少5次程序,测试程序在所有可能的执行流程中,是否存在错误的处理。下面是参考的报告格式。上机内容:用if-else语句和switch语句求解分段函数上机目的:学会分支结构程序设计#include <iostream>#include<cmath>using namespace std;in 阅读全文
posted @ 2013-05-21 01:16 StanleyWu 阅读(302) 评论(0) 推荐(0) 编辑
摘要: 编程序用来计算贷款的定期还款额度,比如买车的贷款。输入本金、贷款的时间长度、每年偿还的次数、贷款利率,程序就会计算出每次应该偿还的额度。计算定期还款金额的公式如下这里rate代表利率,principal代表本金,payPerYear代表每年偿还贷款的次数,numYears代表贷款的年限。注意设计出友好的输入输出界面。运行程序时,自拟多组输入数据,通过比较程序输出及手工计算的结果,验证程序是否正确完成计算。如果在完成中遇到的困难,看下面的锦囊是否能帮你。(1)计算涉及到小数的运算,需要使用浮点类型的数据来进行计算,一般常用double类型。(2)本题要使用幂运算,C++中用pow()函数来完成求 阅读全文
posted @ 2013-05-21 00:52 StanleyWu 阅读(331) 评论(0) 推荐(0) 编辑
摘要: 如何输出上面的星号图? #include <iostream> using namespace std; int main( ) { int i,j,n=6; for(i=n;i>=1;--i) //一共要输出n行 { //输出第i行 for(j=1; j<=2*i-1; ++j) //输出2*i-1个星号 cout<<"*"; cout<<endl; ... 阅读全文
posted @ 2013-05-21 00:15 StanleyWu 阅读(190) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;int main(){ int a,b,c,d; cout<<"请输入4个整数,ok?"<< endl; cin>>a>>b>>c>>d; int max=a; if (max<b) max=b; if (max<c) max=c; if (max<d) max=d; cout<<max<<endl; return 0;} 阅读全文
posted @ 2013-05-20 23:42 StanleyWu 阅读(541) 评论(0) 推荐(0) 编辑
摘要: 问题:给定两个正整数,求出两数的正差值并输出。样例输入 样例输出7 10 312 7 5提示:“正差值”意味着无论这两数孰大孰小,输出的差值为非负数。可以自行加入些“请输入……”,“……是:”之类的提示,让你的程序有“友好”的用户界面。#include <iostream>using namespace std;int main(){ int a,b; cout<<"请输入两个正整数: "<<endl; cin>>a>>b; if (a>b) cout<<a-b<<endl; else 阅读全文
posted @ 2013-05-20 23:31 StanleyWu 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 迭代器是一种检查vector内元素并遍历元素的数据类型定义迭代器类型:vector<int>::iterator iter1.begin和end操作begin返回的迭代器指向第一个元素:vector<int>::iterator iter = ivec.begin();end返回的迭代器并不指向vector中任何实际的元素,表示已经处理完vector中所有元素:vector<int>::iterator iter = ivec.end();范例将一个vector<int>型的ivec变量所有元素赋值为0:for (vector<int> 阅读全文
posted @ 2013-04-28 02:07 StanleyWu 阅读(110) 评论(0) 推荐(0) 编辑
摘要: vector是同一种类型的对象的集合,一个容器中的所有对象都必须是同一种类型的。使用vector之前,必须声明:#include <vector>using std::vector1.vector对象的定义和初始化vector<T> v1; vector保存类型为T的对象。默认构造函数,v1为空vector<T> v2(v1); v2是v1的一个副本vector<T> v3(n, i); v3包含n个值为i的元素vector<T> v4(n); v4含有值初始化的元素的n个副本2.初始化如果vector保存内置类型(如int),则初始 阅读全文
posted @ 2013-04-28 01:23 StanleyWu 阅读(124) 评论(0) 推荐(0) 编辑
摘要: 标准库string类型标准库string类型是为了满足对字符串的一般应用,使用string类型对象,必须包含相关头文件:#include <string>using std::string一.string对象的读写1.读入未知数目的stringint main(){ string word; while (cin >> word) cout << word << endl; return 0;}2.用getline读取整行文本int main(){ string line; while (getline(cin, line)) cout... 阅读全文
posted @ 2013-04-28 00:57 StanleyWu 阅读(108) 评论(0) 推荐(0) 编辑
摘要: 1. if is convergt, then 2. = = = 3. 阅读全文
posted @ 2013-01-19 15:52 StanleyWu 阅读(170) 评论(0) 推荐(0) 编辑
摘要: 1. prove that the empty set is a subset of every set. for a element x in a empty set, x is null , so x is a element of every other set since x is null... 阅读全文
posted @ 2013-01-16 15:59 StanleyWu 阅读(1029) 评论(0) 推荐(0) 编辑