
C++
John的IT之旅
有梦想谁都了不起!
展开
-
C++实现报数
小明和小亮玩报数游戏,小明从1到5报数(即报1 2 3 4 5),小亮从1到10报数(即报1 2 3 4 5 6 7 8 9 10)。若两人同时开始,并以同样的速度报数,不停重复。当两人都报了100个数时,编程计算,两个人同时报相同数的次数是多少。#include<iostream> using namespace std;int main() { int h,m,n,i=0,x=0; do { m=i%5+1; h=i%10+1; if(m==h)x++;原创 2022-05-15 16:27:31 · 1632 阅读 · 0 评论 -
C++之QT按钮实现开关一个窗口
在mywidget.cpp下输入以下代码#include "mywidget.h"#include "ui_mywidget.h"#include<QPushButton>MyWidget::MyWidget(QWidget *parent) : QWidget(parent) , ui(new Ui::MyWidget){ ui->setupUi(this); QPushButton *btn = new QPushButton;//创.原创 2020-05-21 11:30:48 · 843 阅读 · 0 评论 -
利用函数模板实现char和int排序
#include<iostream>using namespace std;//对char 和int数组进行排序排序规则 从大到小 利用选择排序template <class T>void mySwap(T &a,T &b){ T tmp = a; a = b; b = tmp;}template<class T>v...原创 2020-03-31 10:41:19 · 284 阅读 · 0 评论 -
C++友元函数
#include<iostream>#include<string.h>using namespace std;class Building{ friend void GoodGay(Building* building);public: Building() { this->m_BedRoom = "卧室"; this->m_Sitt...原创 2020-03-10 11:36:43 · 151 阅读 · 0 评论 -
c++常函数
#include <iostream>using namespace std;class Person{public: Person() { //构造中修改属性 //this 永远指向本体 this->m_A = 0; this->m_B = 0; } void showInfo() const//常函数 不允许修改指针指向的值 { ...原创 2020-03-09 17:11:25 · 208 阅读 · 0 评论 -
C++利用结构体输入日期时间
#include<iostream>using namespace std;struct MyTimeStruct{ unsigned int year; unsigned int month; unsigned int day; unsigned int hour; unsigned int min; unsigned int sec;};int mai...原创 2020-02-27 11:52:03 · 1485 阅读 · 0 评论 -
C++用switch 语句实现图形面积的选择计算
#include <iostream>using namespace std;const float PI = 3.1416;int main() { int iType; float radius, a, b, area; cout << "图形的类型为?(1-圆形 2-长方形 3-正方形):"; cin >> iType; swit...原创 2020-02-27 11:21:53 · 2218 阅读 · 0 评论 -
C++分别用 do while和for 语句实现前n项和的计算
for语句#include<iostream>using namespace std;int main(){ int sum = 0; for (int n = 1; n <= 10; n++) sum += n; cout << "sum is :" << sum << endl; return 0;}do w...原创 2020-02-27 11:03:09 · 1912 阅读 · 0 评论 -
C++枚举类型实现比赛结果分析
#include<iostream>using namespace std;enum GameResult{WIN,LOSE,TIE,CANCEL};int main(){ GameResult result; enum GameResult omit = CANCEL; for (int count = WIN; count <= CANCEL; count+...原创 2020-02-27 10:32:58 · 331 阅读 · 0 评论 -
C++求一个整数的的所有因子
#include<iostream>using namespace std;int main(){ int n; cout << "Enter a postive integer:"; cin >> n; cout << "Number " << n << " Factors is: "; for (int ...原创 2020-02-27 09:49:57 · 10045 阅读 · 0 评论 -
C++函数的调用只实现幂运算
#include<iostream>using namespace std;double power(double x, int n){ double val = 1.0; while (n--) val *= x; return val;}int main(){ double pow; double pow2; pow = power(5, 2);...原创 2020-02-26 10:47:53 · 289 阅读 · 0 评论 -
C++入门之指针变量遍历二维数组
*p对二维数组中的所有元素进行了引用#include<iostream>#include<iomanip>using namespace std;void main(){ int a[4][3] = {1,2,3,4,5,6,7,8,9,10,11,12}; int* p; p = a[0]; for (int i = 0; i < sizeo...原创 2020-01-15 11:11:34 · 412 阅读 · 0 评论 -
C++入门之多维数组转换为一维数组
主要是核心算法,多维与一维的坐标转换,已经注释#include<iostream>using namespace std;void main(){ int array1[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} }; int array2[12] = { 0 }; int row, col, i; cout <&l...原创 2020-01-15 10:57:37 · 1170 阅读 · 0 评论 -
C++入门之用指针变量获取数组中的元素
如果指针变量p已指向数组中的一个元素,则p+1指向同一数组的下一个元素#include<iostream>using namespace std;void main(){ int i, a[10]; int* p; //利用循环,分别为10个元素赋值 for (i = 0; i < 10; i++) a[i] = i; //将数组中的10个元素输出到显...原创 2020-01-15 10:09:50 · 2144 阅读 · 0 评论 -
C++入门之指针实现比较两个数的大小
简单的用指针实现两个数的大小比较,将a的值赋予指针*p1,将b的值赋予指针*p2#include<iostream>#include<iomanip>using namespace std;void main(){ int* p1, * p2; int* p; int a, b; cout << "Input a:" << e...原创 2020-01-14 10:57:56 · 2016 阅读 · 0 评论 -
C++字符串结束符
打印字符串的时候未加结束符会出现乱码,加上就不后了。#include<iostream>using namespace std;void main(){ int i; char array[12]; array[0] = 'a'; array[1] = 'b'; array[2] = '\0'; printf("%s\n", array);}...原创 2020-01-13 11:28:47 · 2043 阅读 · 0 评论 -
C++入门之二维数组行列对换
二维数组之间的行列对换#include<iostream>#include<iomanip>using namespace std;int fun(int array[3][3]){ int i, j, t; for (i=0;i<3;i++) for (j = 0; j < i; j++) { t = array[i][j]...原创 2020-01-10 09:54:25 · 2941 阅读 · 1 评论 -
C++学习笔记之用C++静态变量实现累加
定义了一个静态变量,每次调用函数add时,静态变量n都保存了前次被调用的值,所以输出的是累加和。#include <iostream>using namespace std;int add(int x){ static int n = 0; n = n + x; return n;}void main(){ int i, j, sum; cout &...原创 2020-01-09 10:14:47 · 1523 阅读 · 0 评论 -
C++实现阶乘
#include<iostream>#include<iomanip>using namespace std;long Fac(int n){ if (n == 0) return 1; else return n * Fac(n - 1);}void main(){ int n; long f; cout << "Pleas...原创 2020-01-07 10:38:20 · 3678 阅读 · 0 评论 -
C++入门
汉诺塔C++实现#include<iostream>#include<iomanip>using namespace std;long ICount;void move(int n, char x, char y, char z){ if (n == 1) cout << "Times:" << ++ICount <&l...原创 2020-01-03 10:10:13 · 111 阅读 · 0 评论