C++上机课程序汇编(学长留)

上传人:gbs****77 文档编号:10681526 上传时间:2020-04-13 格式:DOC 页数:30 大小:164KB
返回 下载 相关 举报
C++上机课程序汇编(学长留)_第1页
第1页 / 共30页
C++上机课程序汇编(学长留)_第2页
第2页 / 共30页
C++上机课程序汇编(学长留)_第3页
第3页 / 共30页
点击查看更多>>
资源描述
输配电一班内部资料 C 所有上机实验 实验二 第 1 题 编写程序计算梯形面积 公式为 s a b h 2 其中 a b 分别为梯形上底和下底 的宽 h 为梯形的高 运行该语句后结果是 cout cout 5 2 8 endl cout 5 2 为什么 include void main float a b h float s cout a b h s a b h 2 cout 梯形的面积为 s endl 第 2 题 已知华氏温度和摄氏温度的转换公式为 C F 32 5 9 其中 C 为摄氏温度 F 为华氏温度 假设当前华氏温度为 80 度 求对应的摄氏温度是多少 要求输出最后结果 include void main float F float C cout F C F 32 5 9 cout 摄氏温度为 C endl 第 3 题 假设三角形的三边长为 3 4 5 求其面积 s a b c 2 area sqrt s s a s b s c 上述四题变量的值要求从键盘提供 提示 使用根号 需在文件前方加上独立的一行 include 并使用 sqrt 函数完成 如 x sqrt 2 则 x 的值即为 1 414 include include void main float a b c d float s float area cout a b c if a b c d s s a s b s c area sqrt d cout 三角形的面积为 area endl else cout 无法构成三角形 第 4 题 假设有任意一个 x 为 3 位数 如 x 123 求出其倒序之后的结果 如 x 321 思路 按课堂上所讲解 分别拆出 123 的百位 十位和个位分别用 a b 和 c 来表 示 再组合为 321 十位数为 b x 10 10 include void main int a b c int x cout x a x 100 b x 10 10 c x 10 int y y 100 c 10 b a cout 倒序三位数为 y tmax tmax c tmin tmin c include void main int a b c int tmax int tmin int zjs cout a b c if a b tmax a tmin b else tmax b tmin a zjs c tmax tmax c tmin tmin c cout 中间数为 zjs endl 实验三 第 1 题 用 cin 语句从键盘输入一个整数 要求判断其奇偶性 并输出结果 例 输入的是 3 则输出 3 是奇数 如输入的是 4 则输出 4 是偶数 注意这里输出的时候数值部分应该根据输入的值发生变化 而不是固定写在字符串 当中 关于 cin 和 cout 的具体用法请参阅课本 include void main int i cout i if i 2 0 cout 该整数是偶数 endl else cout 该整数是奇数 endl 第 2 题 用 cin 语句从键盘输入一个整数 要求判断其正负性 并输出结果 例 输入的是 3 则输出 3 是正数 如输入的是 4 则输出 4 是负数 如果输 入的是 0 则输出 该数是零 注意这里输出的时候数值部分应该根据输入的值发生变化 而不是固定写在字符串 当中 关于 cin 和 cout 的具体用法请参阅课本 include void main int i cout i if i 1 0 cout 该整数是0 endl else if i 1 0 cout 该整数是正数 endl else cout 该整数是负数 endl 第 3 题 用 cin 输入一个整数 判断其是否是 3 或 5 的倍数 分别用 15 9 10 8 四个例子 来测验本题的四种不同的结果 都可以 只能被 3 只能被 5 都不能 并输出结果 如 输入的是 9 则输出 9 只能被 3 整除 输入的是 15 则输出 15 可以同时被 3 和 5 整除 include void main int a cout a if a 3 0 cout a是3的倍数 else cout a不是3的倍数 if a 5 0 cout a是5的倍数 else cout a不是5的倍数 第 4 题 完成算术四则运算 a b 为实型 c 为字符型 从键盘输入 a b a b a b a b 用 switch 语句来编写 运行程序后要测试 输入 12 60 10 5 5 5 2 5 0 5 3 5 include void main char c float a b cout 输入两个整数 a c b cout 结果为 endl switch c case cout a b endl break case cout a b endl break case cout a b endl break case cout a b endl break 第 5 题 用 cin 输入三角形的三条边 判断能否构成三角形 注 需要考虑尽可能的情况 如三边是否为负数或者 0 以及两边之和是否大于第 三边等 输出最终的结果 如输入的是 1 2 3 则输出 1 2 3 不能构成三角形 如输入的是 3 4 5 则输出 3 4 5 可以构成三角形 include void main float a b c cin a b c if a 0 b 0 c 0 cout a b c 不能构成三角形 c else cout a b c 不能构成三角形 endl 实验四 第 1 题 用 cin 语句从键盘输入一个月份的值 用 if 语句判断该月的季节并输出结果 假设 1 3 为春 4 6 为夏 7 9 为秋 10 12 为冬 include void main int a cout a if 1 a if 4 a if 7 a if 10 a 第 2 题 用 cin 语句从键盘输入一个月份的值 用 switch 语句判断该月的季节并输出 结果 假设 1 3 为春 4 6 为夏 7 9 为秋 10 12 为冬 include void main int season cout season cout 季节为 switch season case 12 cout 冬 break case 11 cout 冬 break case 10 cout 冬 break case 9 cout 秋 break case 8 cout 秋 break case 7 cout 秋 break case 6 cout 夏 break case 5 cout 夏 break case 4 cout 夏 break case 3 cout 春 break case 2 cout 春 break case 1 cout 春 break 第3题 要求从键盘输入年份 判断该年份 是否为闰年 参教材p90页 include void main int year cout year if year 4 0 else cout year 年不是闰年 n 第4题 用cin从键盘输入一个字符 用if语句判断并输出该字符的种类 大写字母 小写字母 阿拉伯数字以及其他字符 include void main char c cout c if c a else cout 该字符为其他字符 n 第 5 题 从键盘输入三个数 a b c 要求按从小到大进行排序 include void main float a b c t cout 请输入a b c三个数 a b c if a b 如果a小于b的值 那么就交换a b变量的值 t a a b b t if b c t b b c c t if a b t a a b b t cout c b a endl 实验五 第 1 题 用任意的循环语句求出 1 100 之间所有奇数的和 1 3 5 99 并输出结果 include void main int i sum i 1 sum 0 while i 100 sum i sum sum i的C 写法 i i 2 cout 1到100之间所有奇数的和是 cout sum endl 第 2 题 编程输出1 100之内所有能被3整除的数 并输出这些数 本题输出结果应该为 3 6 9 12 99 提示 对i进行100次循环 在每次循环中判断i能否被3整除 如果可以则输出 否则进行下一次循环 include void main int i x x 3 for i 1 i 100 i if i 3 0 cout i endl 第3题 求一个正整数的所有因子 例如 24的因子是1 2 3 4 8 6 12 include void main int i x cout i cout 这个正整数的所有因子为 for x 1 x i x if i x 0 cout x endl 第4题 写一个程序 计算输入的正整数的位数 如输入2008 该数的位数为4 include void main int n i 0 cout n cout 位数为 while n n n 10 i cout 输入正整数的位数为 i endl 第5题 输出所有的顺序三位数 所谓的顺序三位数是指 百位 十位 个位 如123 556都是顺序三位数 而987则不是 include void main int a b c cout 所有的顺序三位数为 for int i 100 i 1000 i a i 100 b i 10 10 c i 10 if a b 实验六 第 1 题 编写函数 squaresum 用来求解两个整数的平方和 从键盘里面用 cin 输入两个整数 调用该函数 并输出结果 例 cout squaresum 3 4 endl 则输出 25 include include int squaresum int a int b int squaresum return squaresum a a b b void main int a b cout a b cout squaresum a b endl 第 2 题 编写函数 mypower 用来求解整数 a 的 n 次方 该函数的声明为 int mypower int a int n 提示 进行 n 次循环 每次累乘 a 即可 例 cout mypower 3 4 endl 则输出 81 include int mypower int a int n int b 1 for int i 1 i a n cout mypower a n endl 第 3 题 编写函数 bool isprime int n 用来判断整数 n 是否为素数 如果是则返回 true 否则返回 false 利用该函数求出 1000 以内所有的素数并显示 include include bool isprime int n bool a 1 for int i 2 i sqrt n i if n i 0 a 0 break return a void main int x for x 2 x 1000 x if isprime x cout x endl continue 第 4 题 编程从键盘输入一个数 判断是奇偶性 要求定义一个判断奇偶数的函数 int even int n 当 n 为偶数时返回 1 否则返回 0 include void main int even int n int x cin x if even x cout 偶数 endl else cout 奇数 r 分别求出 c 5 2 c 8 6 的组合数 阶乘及组合数用函数实现 include int zhs int x if x 0 x 1 return 1 return x zhs x 1 void main int a b c d e cout d e a zhs d b zhs e c zhs d e if d e cout a b c endl 2 求400之内的亲密对数 所谓亲密对数 即A的所有因子之和等于B B的所有因子之 和等于A 要求设计函数int fun int n 该函数完成查找因子的功能 include int fun int n void main for i 2 i 400 i if fun fun i i if i fun i cout i 和 fun i 是亲密对数 endl int fun int n int s 0 for i 1 i n i if n i 0 s i return s 3 打印输出下面图形 第一行 一个 第二行 3个 第三行 5个 第四行 7个 第五行 9个 include void main int j i for i 1 i 5 i for j 1 j 5 i j cout for j 1 j 2 i 1 j cout cout endl 实验八 熟悉数组的基本操作过程 1 定义数组int a 5 2 利用循环从键盘依次输入5个数分别赋给数组的5个元素 3 求出它们的总和 4 在屏幕上显示数组的5个元素以及总和 include void main int a 5 int s 0 cout 请输入五个正整数 n for int i 0 i a i s a i cout 五个数的和为 s endl 第2题 假设数组int a 5 1 2 3 4 5 1 定义数组int b 5 2 将数组a中元素的平方依次付给数组b的对应元素 也就是b 0 1 b 1 4 要求必须使用循环完成 3 在屏幕上显示数组b的5个元素的值 include void main int a 5 1 2 3 4 5 int b 5 for int i 0 i 4 i b i a i a i cout b i t cout endl 第3题 定义数组int x 5 5 2 3 1 4 复习课本当中求解数组最大值的算法 利用循环求出数组x最大值和最小值之 间的差 include void main int x 5 5 2 3 1 4 int i main max min max x 0 for i 1 imax max x i min x 0 for i 1 i 5 i if x i min min x i main max min cout max max main main endl 第4题 复习课本上的冒泡和选择排序算法 任选一种对数组int x 5 5 2 3 1 4 进行排序 并输出排序后的结果 include void main int x 5 5 2 3 1 4 t for int j 1 j 5 j for int i 0 ix i 1 t x i x i x i 1 x i 1 t for int i 0 i 5 i cout x i endl 实验九 第1题 定义数组int a 2 3 运行以下步骤 1 从键盘依次输入6个数存入该数组当中 2 按每行三个的形式打印这6个数的值 如输入的是1 2 3 4 5 6 则输出效果是 1 2 3 4 5 6 include void main cout 请输入六个数 n endl int i j a 2 3 for i 0 i 2 i for j 0 j a i j cout endl for i 0 i 2 i for j 0 j 3 j cout a i j cout endl 第2题 定义数组int a 3 4 1 从键盘输入12个整数 存入该数组中 2 利用循环求出数组中的最大值 最小值和总和 3 打印上述结果 include void main int i j k a 3 4 max min sum 0 cout 请输入12个整数 n for i 0 i 3 i for j 0 j a i j max a 0 0 min a 0 0 for k 0 kmax max a i j if a i j min min a i j cout max max n cout min min n cout sum sum endl 实验十 第1题 编写函数 其功能是求出整型数组元素值的平均值 建议函数原型如下 double getaverage int a int n 其中a是数组 而n表示数组的长度 在main函数中定义数组 调用该函数验证结果 include double getaverage int a int n double s 0 double p for int i 0 i n i s a i p s n return p void main int p m b 50 cout 程序将完成计算整型数组平均值 n cout p cout 请输入一个整形数组以求平均值 n for m 0 m b m cout 平均值为 getaverage b p endl 第2题 编写函数 其功能是将数组内容进行倒序排列 如数组int a 5 的5个元素本 来是1 2 3 4 5 则经过倒序之后变成5 4 3 2 1 函数原型声明建议为void reverse int a int n 其中a为数组而n为数组的 长度 算法提示 将数组的头尾元素交换 然后分别递增和递减 直到两者相遇或者 尾巴超过头就结束循环 假设数组元素是 1 2 3 4 5 则循环第一次之后变成 5 2 3 4 1 第二次 之后变成5 4 3 2 1 第三次的时候因为头尾已经相等则结束 在main函数中定义数组 调用该函数验证结果 include void reverse int a int n int t i j n 1 for i 0 i j i t a i a i a j a j t j void main int m p b 50 cout p cout 请输入一个整型数组 n for m 0 m b m reverse b p for m 0 m p m cout b m cout n 第3题 将课本上的简单查找算法提炼为函数实现 在main函数中定义数组并调用该函 数验证 函数的建议声明int find int a int n int k 其中a为数组 n为数组的 长度 k为要查找的值 如果k在该数组中 则返回对应的下标 如果不在数组中 则返回 1 include find int a int n int k for int i 0 i n i if a i k break if i n return i return 1 int main int n 5 int a 5 1 2 3 4 5 int k 3 int iresult 0 iresult find a n k if 1 iresult cout 没有查到K值 else cout 查到了K值 return 0 实验十一 第1题 定义数组char str 20 从键盘输入一个字符串存入该数组中 要求 将字符串中进行倒序 并输出倒序后的结果 思路 将字符串头尾字母交换 并逐步推进直至头超过尾 例 输入的字符串为ABCdef 则输出的结果为fedCBA include void main char s1 20 s2 20 int i j cout s1 for i 0 s1 i 0 i for j 0 s1 j 0 j s2 i j 1 s1 j s2 i 0 cout 处理后的字符串 s2 第2题 定义数组char str 20 从键盘输入一个包含多种形式的字符串存入该数组中 要求 统计字符串中大写字母 小写字母 阿拉伯数字和其他字符的数量 例 输入的字符串是ABC int i 0 j 0 p 0 n 0 m 0 cin getline str 20 while str i 0 if str i a else p i cout 小写字母有 j endl cout 大写字母有 n endl cout 数字有 m endl cout 其它字母有 p endl 第三题 定义字符数组s1 30 和s2 20 从键盘输入 VC 6 0 和 language存入s1和s2字符数组中 n从键盘上输入 如输入 6 要求将字符串s2的前n个字符复制到字符数组s1中去 并在末尾加 0 include void main char s1 30 VC 6 0 char s2 20 language int i 0 j 0 n while s1 i 0 i cin n for j 0 j n j s1 i s2 j i s1 i 0 cout s1 endl 实验十二 第1题 定义描述平面坐标点的结构体类型point 并定义该类型的两个变量p1和 p2 从键盘输入2个点的坐标数据 求两点间线段的长度 输出最后结果 include include struct point float x y p1 p2 void main cout 请输入两个点的坐标数据 p1 x p1 y cin p2 x p2 y double d d sqrt p1 x p2 x p1 x p2 x p1 y p2 y p1 y p2 y cout 距离为 d endl 第2题 定义金钱数的类型money 其成员包括元 角和分 定义两个money的变量m1 m2 从键盘输入m1和m2的数据 判断m1和m2的大小 如m1的输入为3 4 5 m2的输入为6 7 8 则输出m1比m2小 include struct money int yuan jiao fen m1 m2 void main cout 请输入任意两个金额 m1 yuan m1 jiao m1 fen cin m2 yuan m2 jiao m2 fen int x y x m1 yuan 100 m1 jiao 10 m1 fen y m2 yuan 100 m2 jiao 10 m2 fen if x y cout m1比m2小 endl else cout m2比m1小 endl 第3题 定义金钱数的类型money 其成员包括元 角和分 定义两个money的变量m1 m2 从键盘输入m1和m2的数据 求出m1和m2的和 要求角和分的值 10 并输出结 果 例 m1为1元2角5分 m2为3元4角6分 则m1 m2的值应该是4元7角1分 include struct money int yuan jiao fen m1 m2 void main cout 请输入任意两个金额 m1 yuan m1 jiao m1 fen cin m2 yuan m2 jiao m2 fen int x y z a b c x m1 yuan 100 m1 jiao 10 m1 fen y m2 yuan 100 m2 jiao 10 m2 fen z x y a z 100 b z 10 10 c z 10 cout 总金额为 a 元 b 角 c 分 endl 实验十三 第1题 定义数组int a 10 定义整形指针p 只利用指针p完成10个整数的输入和输出 要求循环当中不允许出现下标运算 符 include void main void int a 10 i int p for i 0 i a i for p a p a 9 p cout p t 第2题 定义数组int a 10 从键盘当中输入10个数放入其中 定义指针p和q分别指向数组的开始及末尾 只用指针 不出现下标运算符 完 成数组元素的倒序 include void main int a 10 x p q while p p for p a p q p q x p p q q x for p a p a 10 p cout p t cout endl 第3题 编写函数int find int p int n int k 用来查找数组当中是否存在整数 k 其中p应该赋值为数组的名字 n则是数组的长度 如果k在数组当中 返回位置 否则返回 1 在main当中任意定义数组 并用find函数来验证某数是否在该数组当中 include int find int p int n int k for int i 0 i n i if p i k return i return 1 void main int m l a 50 int q cout m cout 请输入数组 n for q a q q cout l if find a m l 1 cout 不存在 n else cout l 在数组第 find a m l 个元素 endl 实验十四 第1题 定义数组char str 50 p str 从键盘输入一个字符串存入该数组中 要求 通过指针p来完成字符串的大小写互换 即将原串的大写字符改成小写 小写字符改成大写 其他字符不变 输出互换后的结果 例 输入的字符串是ABCabc123 则输出的内容是abcABC123 include void main char str 50 p str cout 请输入一个字符串 A cout str q停止 例 输入的字符串是ABCabc123 则输出的内容是321cbaCBA include void main char str 50 p str q cout 请输入一个字符串 n cin getline str 50 for int i 0 p i 0 i q for int j 0 j i j cout q j cout endl 第三题 自定义字符串拷贝函数char mystrcpy char s const char t 要求 将字符指针t代表的字符串拷进字符指针s的地址当中 并返回s原来的 地址 要求 在main当中定义变量和数组 验证函数的正确性 include include char mystrcpy char s const char t for int i 0 s i 0 i s i t i s i 0 return s void main char a 10 b 10 cin getline a 10 mystrcpy b a cout b endl 实验十五 第一题 从键盘当中输入一个整数n 申请一个长度为n的动态整形数组 从键盘当中输入n个整数存入该数组当中 求出这n个的平均值并显示 释放该动态数组 include void main int i n p ave s 0 cout 请输入一个整数 n cout 请输入n个整数 n p new int n for i 0 i p i for i 0 i n i s s p i ave s n cout 平均值为 ave endl delete p 第二题 从键盘当中输入一个整数n 申请一个长度为n的动态整形数组 从键盘当中输入n个整数存入该数组当中 将这n个整数进行升序排序后输出 排序方法不限 释放该动态数组 include include void main char str 200 ch int n cout 请输入一个整数 n cout 请输入n个整数 str 200 p1 new int n p2 new int n char p1 p2 cin getline str p1 str p2 p1 strlen p1 1 while p1 p2 ch p1 p1 p2 p2 ch cout str delete p1 p2 实验十六 第一题 定义一个类ctime 用来存放时间 其数据成员包括hour minute和 second 函数成员包括赋值函数void set int h int m int s 和打印函数 void show 完成该类的定义 在main当中建立ctime类的对象 并验证函数set和show的效 果 include class ctime int hour minute second public ctime hour 0 minute 0 second 0 ctime int h int m int s hour h minute m second s void display void ctime display cout hour hour cout minute minute cout second second void main ctime A 1 2 3 B A display B display 第二题 定义一个类mycomplex 用来存放复数 其数据成员包括实部real和虚部 imag 函数成员包括赋值函数void set int r int i 打印函数void show 两个参数的构造函数和不带参数的构造函数各一个 完成该类的定义 在main当中建立mycomplex类的2个对象 分别调用两个构造 函数 并验证函数set和show的效果 include class mycomplex int real imag public mycomplex int r int i real r imag i void show cout real real t cout imag imag endl void main mycomplex A 1 2 A show 实验十七 定义复数类mycomplex 数据成员包括实部和虚部 函数成员包括构造函数 析构函数 复制构造和打印函数 完成该类的定义 在main当中建立mycomplex类的多个对象 并分别验证上面 的每个函数 include class mycomplex float real image public mycomplex float r 0 float i 0 real r image i cout 构造函数被调用 endl mycomplex mycomplex mycomplex cout 析构函数被调用 endl void show cout real real t image image endl mycomplex mycomplex mycomplex image p image cout 复制构造函数被调用 endl void main mycomplex a 1 5 mycomplex b a a show b show 第二题 定义一个类sample 里面有静态数据成员count 用来统计已经定义的对象的 数量 在类外将count初始化为0 在类的构造函数当中将count加1 析构函数当中将 count减1 在打印函数当中输出count的值 定义多个对象 分别打印内容来验证上述函数的内容 include class sample public static int count sample int x sample void print cout count count endl private int y int sample count 0 sample sample int x y x count sample sample count void main sample d1 2 sample d2 6 sample d3 15 sample d4 35 d1 print 第三题 将字符串 Viusal VC 6 0 写入data1 txt 文件中 并要求在屏幕上输出 include include void main fstream outfile data1 txt ios out char s1 Viusal VC 6 0 outfile s1 endl outfile close fstream infile cout s1 endl infile close 实验十八 第一题 求出200以内的所有素数 按每行5个写入文本文件prime txt include include include include void main void fstream outfile prime txt ios out ofstream outfile prime txt fstream outfile outfile open prime txt ios out int m i k n 0 for m 1 m 200 m k int sqrt m for i 2 i k 1 n n 1 cout setw 4 m if n 5 0 cout n outfile outfile close 第二题 求出斐波那契数列的前20项 将每行4个的格式写入文本文件 fib txt include include include void main void fstream outfile fib txt ios out ofstream outfile fib txt fstream outfile outfile open fib txt ios out long int f1 f2 f3 int i f1 1 f2 1 cout setw 10 f1 setw 10 f2 for i 3 i 20 i f3 f1 f2 cout setw 10 f3 f1 f2 f2 f3 if i 4 0 cout n outfile outfile close 第三题 定义数组int a 10 生成10个随机数进行赋值 将其进行升序排序 方法任 选 后写入文件data txt include include include include void main fstream outfile data txt ios out int a 10 i j t for i 0 i 10 i a i rand 100 for j 1 j 10 j for i 0 ia i 1 t a i a i a i 1 a i 1 t for i 0 i 10 i outfile a i outfile close 请大家好好复习 学委能做的就这么多了
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!