写在前面的话
这个是楼主大一入门时的课程作业代码,很多当时也写得较为粗糙,
后来逐渐掌握后也没进一步优化的想法和必要,此文纯粹记录,参考作用不大。
实验平台为Visual-Studio-2017
前面的 #include "pch.h"
文件如下:
// 入门提示:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
#ifndef PCH_H
#define PCH_H
// TODO: 添加要在此处预编译的标头
#endif //PCH_H
- 时间的设置与显示(普通结构)
#include "pch.h"
#include <iostream>
using namespace std;
class Time
{
public:
void set_time(void);
void show_time(void);
int hour;
int minute;
int sec;
};
Time t;
void Time::set_time(void)
{
cin >> t.hour;
cin >> t.minute;
cin >> t.sec;
}
void Time::show_time(void)
{
cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}
int main()
{
Time t;
t.set_time();
t.show_time();
system("pause");
}
/*
#include <iostream>
using namespace std;
class Time
{
public: //成员改为公用的
int hour;
int minute;
int sec;
};
Time t;
void set_time(void) //在main函数之前定义
{
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
void show_time(void) //在main函数之前定义
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
int main()
{set_time();
show_time();
return 0;
}
*/
- 多文档组织结构,时间设置与显示增强版
Time.h
#include <iostream>
using namespace std;
class Time
{
private:
int hour;
int minute;
int sec;
public:
void set_time(void);
void show_time(void);
};
Time.cpp
#include "pch.h"
#include "Time.h"
#include <iostream>
using namespace std;
void Time::set_time(void)
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void Time::show_time(void)
{
cout << hour << ":" << minute << ":" << sec << endl;
}
多文档组织结构,时间设置与显示增强版.cpp
#include "pch.h"
#include <iostream>
#include "Time.h"
using namespace std;
int main()
{
Time t;
t.set_time();
t.show_time();
system("pause");
}
- 贪心算法(田忌赛马)
// 贪心算法 田忌赛马.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#include <algorithm>
using namespace std;
bool compete(int A, int B)
{
return A > B;
}
int TianJi[3000];
int QiWang[3000];
int main()
{
int n = 1, num, t_max, t_min, q_max, q_min, a;
while (n!=0)
{
cin >> n;
num = 0;
for (a = 0; a < n; a++)
{
cin >> TianJi[a];
}
for ( a = 0; a < n; a++)
{
cin >> QiWang[a];
}
sort(TianJi, TianJi + n, compete);
sort(QiWang, QiWang + n, compete);//两组马速度由大到小排序
t_max = 0; t_min = n - 1;
q_max = 0; q_min = n - 1;
while (t_max <= t_min && q_max <= q_min)
{
if (TianJi[t_max] > QiWang[q_max])
{
num = num + 1;
t_max = t_max + 1;
q_max = q_max + 1;
}
//强者相遇
else if (TianJi[t_min] > QiWang[q_min])
{
num = num + 1;
t_min = t_min - 1;
q_min = q_min - 1;
}
//菜鸡互啄
//这个else用得好
//两边缩进
else
{
if (TianJi[t_min] < QiWang[q_max])
{
num = num - 1;
t_min = t_min - 1;
q_max = q_max + 1;
}
else if (TianJi[t_min] >= QiWang[q_max])
{
//t_max = t_max + 1;
t_min = t_min - 1;
q_max = q_max + 1;
//q_min = q_min - 1;
} //空过
}
}
cout << 200 * num << endl;
}
system("pause");
}
// 赢在当下,强的不能赢,弱的也不行,就用弱的打掉对面强的,
//再继续用我的强找对面稍强的,我的不那么弱的打对面更弱的
- Student类求总分与平均分
#include "pch.h"
#include <iostream>
using namespace std;
class student
{
public:
void scoretotalcount(double s);
static double sum();
static double average();
private:
double score;
static double total;
static int count; //不能在这初始化为常量
};
double student::total = 0;
int student::count = 0;
void student::scoretotalcount(double s)
{
score = s; //设置分数
total = total + score; //求总分
count = count + 1; //累计学生人数
}
double student::sum() { return total; };
double student::average() { return total / count; };
int main()
{
double s; int c;
student a[50];
cout << "请输入学生人数" << endl;
cin >> c;
cout << "请依次输入学生成绩" << endl;
for (int i = 0; i < c; i++)
{
cin >> s;
a[i].scoretotalcount(s);
}
cout << "全班总分是" << endl;
cout << student::sum() << endl;
cout << "全班平均分是" << endl;
cout << student::average() << endl;
system("pause");
}
- 有理数类与其运算
#include "pch.h"
#include <iostream>
#include "Rational.h"
using namespace std;
int main()
{
Rational K;
Rational &R=K;
double a, b, c, d;
cout << "请输入A的分子与分母:" << endl;
a = R.nenter();
b = R.denter();
cout << endl << endl;
cout << "请输入B的分子与分母:" << endl;
c = R.nenter();
d = R.denter();
cout << endl << endl;
Rational r;
Rational r1(a, b), r2(c, d);
r = r1 + r2;
cout << " A + B = ";
r.show();
r = r1 - r2;
cout << " A - B = ";
r.show();
r = r1 * r2;
cout << " A * B = ";
r.show();
r = r1 / r2;
cout << " A / B = ";
r.show();
system("pause");
}
- m位同为n的数叠加求和
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int m, n, sum = 0, t;
cin >> m >> n;
t = n;
while (m>0)
{
sum = sum + n;
n = n * 10 + t;
m = m - 1;
}
cout << "sum = "<<sum << endl;
system("pause");
}
- 数列求和
txt例子
输入3 6
输出3+33+333+3333+33333+333333
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int A, N,sum=0,X;
cin >> A >> N;
X = A;
while (N>0)
{
sum = sum + A;
A = X + 10 * A;
N = N - 1;
}
cout << sum << endl;
system("pause");
}
- 菱形打印
#include "pch.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int n, a, b, c = 0;
cin >> n;
if (n % 2 != 0)
{
a = n / 2 + 1;
for (a; a > 0; a = a - 1)
{
b = a;
c = c + 1; //c控制输出符号数,b控制空格
for (b; b > 1; b = b - 1)
cout << " ";
for (b; b <= 2 * c - 1; b = b + 1)
cout << "* ";
cout << endl;//例如 n=7,结束时a=0,b=7,c=4
}
a = n / 2 + 1;
for (a; a < n; a = a + 1)
{
b = n / 2;
c = c - 1;
for (b; b < a; b = b + 1)
cout << " ";
for (int d = 0; d < 2 * c - 1; d = d + 1)
cout << "* ";
cout << endl;
}
}
system("pause");
}
更简洁版
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int a, b, c;
cin >> a;
for (b = 0; b < a / 2 + 1; b = b + 1)
{
for (c = b; c < a / 2; c = c + 1)
cout << " ";
for (c = 0; c < b * 2 + 1; c = c + 1)
cout << "* ";
cout << "\n";
}
for (b = 0; b < a / 2; b = b + 1)
{
for (c = b; c >= 0; c = c - 1)
cout << " ";
for (c = 2 * (a / 2 - b) - 1; c > 0; c = c - 1)
cout << "* ";
cout << "\n";
}
return 0;
}
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int n,a,b,c=0;
cin >> n;
if (n % 2 != 0)
{
a = n / 2+1 ;
for (a; a > 0; a = a - 1)
{
b = a;
c = c + 1; //c控制输出符号数,b控制空格
for (b; b > 0; b = b - 1)
cout << " ";
for (b; b < 2 * c - 1; b = b + 1)
cout << "* ";
cout << endl;//例如 n=7,结束时a=0,b=7,c=4
}
a = n / 2+1;
for (a; a < n; a = a + 1)
{
b = n / 2;
c = c - 1;
for (b; b <= a; b = b + 1)
cout << " ";
for (int d = 0; d < 2 * c - 1; d = d + 1)
cout << "* ";
cout << endl;
}
}
system("pause");
}
- 字符串连接(要求是指针与非)但只会用string
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
cout << a << b;
system("pause");
}
- !和sum的高斯算法
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
double n;
int sum;
cin >> n;
sum = (1 + n)*(n / 2);
cout << sum << endl;
return 0;
}
后记:大一上时较为落后的水平了。
代码许多地方不规范,思路不清晰,没注释,变量名起的随意,可读性大打折扣。
仅做记录,也希望后面入坑一开始就要注意规范,学好更多的小套路。