//求三角形面积
#include "stdafx.h"
#include<stdio.h>
#include<math.h>
float Trianglearea(float a, float b, float c){
float p = (a + b + c) / 2;
float s = sqrt(p*(p - a)*(p - b)*(p - c));
return s;
}
int main(){
float a, b, c, s;
printf("请输入三角形的三条边:\n");
scanf_s("%f %f %f", &a, &b, &c);
s = Trianglearea(a, b, c);
printf("三角形的面积为:%f", s);
}
1.由于在写代码时经常使用整形,所以在应用printf 或者scanf_s函数时第一个想到的或者说写出来的总是%d,刚才找了半天错误没找出来,下次一定注意,浮点数(float double)就用%f (char)用%c
2.scanf_s函数忘记使用取地址值
2.四舍五入函数round--->math.h中的,用C++实现四舍五入
#include "stdafx.h"
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double d;
cin >> d;
int a = round(d);
cout << a << endl;
return 0;
}
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(){
long long sum = 0, total = 0;
for (int i = 1; i <= 10; i++){
sum = sum * 10 + 9;
total = total + sum;
}
cout << total << endl;
}
?1.前面写的算法都没有问题,但是运行结果是一个负值,后来看答案发现int的取值范围只有10位数,而正确的结果有十一位数,应该使用long long型。
2.一下为各种类型的取值范围
?3.保留小数点后几位小数,用#include<iomanip>头文件中的的setprecision函数
如保留一位小数
//保留小数点后一位小数
cout<<fixed<<setprecision(1)<<num<<endl;
4.对数组求长度
int arr[6]={0};
int len=sizeof(arr)/sizeof(int);
5.实现元素的反转
#include <iostream>
using namespace std;
int main() {
int arr[6] = { 0 };
int len = sizeof(arr) / sizeof(int);
for (int i = 0; i < len; i++) {
cin >> arr[i];
}
cout << "[";
for (int i = 0; i < len; i++) {
if (i == len - 1) {
cout << arr[i] << "]" << endl;
break;
}
cout << arr[i] << ", ";
}
// write your code here......
cout << "[";
for (int i = len-1; i >=0; i--) {
if (i == 0) {
cout << arr[i] << "]" << endl;
break;
}
cout << arr[i] << ", ";
}
return 0;
}
5.选择排序
#include <iostream>
using namespace std;
int main() {
int arr[6] = { 0 };
int len = sizeof(arr) / sizeof(int);
for (int i = 0; i < len; i++) {
cin >> arr[i];
}
int mix,temp;
// write your code here......
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++){
if (arr[i] >arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < len; i++){
cout << arr[i] << " ";
}
return 0;
}
6.打印0——99? 一行有十个数
#include "stdafx.h"
#include<iostream>
using namespace std;
int main(){
for (int i = 0; i < 100; i++){
cout << i<<" ";
if ((i+1) % 10 == 0){
puts("");
}
}
7.实现字母序列循环
#include "stdafx.h"
#include<iostream>
using namespace std;
int main(){
char str[] = { "abcdefghigklmnopqrstuvwxyz" };
for (int i = 0; i < 26; i++){
int k = i;
for (int j = 0; j < 26; j++){
cout << str[k];
k = k + 1;
if (k % 26 == 0){
k = 0;
}
}
puts("");
}
return 0;
}
1.求最小公约数(辗转相除法)
欧几里得算法
#include "stdafx.h"
#include<iostream>
using namespace std;
//辗转相除法(欧几里得算法)
int main(){
int a, b;
cin >> a >> b;
while(b != 0){
int tep = a%b;
a = b;
b = tep;
}
cout << a;
return 0;
}
32位操作系统指针占4字节
64位操作系统指针占8字节?
?
?4.结构体
#include "stdafx.h"
#include<iostream>
using namespace std;
struct Student{
char s_id[10];
char s_name[10];
char s_sex[10];
int ege;
};
int main(){
struct Student bling{ "136 ", "wbling", "women",20 };
struct Student *p = &bling;
cout << (*p).s_id << endl << p->s_name << endl << bling.s_sex << endl << p->ege;
return 0;
}
1.在结构体中定义一个变量,采用? 结构体+结构名+定义名称
如:struct Student bling{? ?};
同理,在定义指针时也一样
如:struct Student *p=&bling ;
2.结构体后不跟括号struct Student{
};
3.结构体末尾的花括号后必须紧跟一个分号{};
4.(*p).s_id=p->s_id=bling.s_id
struct Student{
char s_id[10];
char s_name[10];
char s_sex[10];
int ege;
}stud;
struct Student stud;//全局结构体变量
5.EOF是一个宏常量? ?代表值是-1
6.typedef 用于将定义一个类型名称
如:
#include<iostream>
using namespace std;
typedef unsigned int bling;
int main(){
bling w = 520;
cout << w << endl;
return 0;
}
?1.感觉今天弄懂了好多知识i? 嘻嘻嘻
?2.外部关键字 extern 只对
3.static 如果未对其进行赋值,将默认其初始值为0;
static 是静态变量? 每次保存前一次的值? 将数据存在.data中? (作用于不变? 生存期改变)
#include<iostream>
using namespace std;
void fun(){
int a = 1;
static int b = 1;
a++;
b++;
cout << a << " " << b << endl;
}
int main(){
for (int i = 0; i < 5; i++){
fun();
}
return 0;
}
2.全局变量和函数属于文件作用域。
3.static 只能修饰的全局变量或者函数 只能在本文件中使用,即使一个项目中其他的文件加了entern 也无法使用。
在static之前加上extern会出现编译错误? 如同神仙打架 static 声明只能在本文件中使用。而extern代表的意义是可以在外部使用。
const如果想被同一个工程中的其他文件使用的话,需要在const之前加上entern 。
4.字符0?
'0'的ASCII码值为48
'\0'的ACSII码值为0
NULL的ASCII码值为0
空格的ASCII码值为32
#include<iostream>
using namespace std;
//C语言中的空为大写
int main(){
bool x = 0;//false
bool a = '0';//48 true
bool b = '\0';//0 false
bool c = NULL; //0 false
bool d = " ";//32 true
cout << x << endl << a << endl << b << endl << c << endl << d << endl;
return 0;
}
运算符优先级(由高到低)
!非---->算术运算符(+? -)---->关系运算符(== >? <? >=? <=)---->逻辑运算符(||? &&)---->赋值运算符(=)
2022.1.28
1.输出三个数中中间的一个数
#include "stdafx.h"
#include<iostream>
using namespace std;
int main(){
int a, b, c, t;
cin >> a >> b >> c;
if (a > b){
t = a;
a = b;
b = t;
}
if (b > c){
t = b;
b = c;
c = t;
}
if (a > c){
t = a;
a = c;
c = t;
}
cout << b << endl;
}
2.? switch语句中的default? 以及default语句中的break均可省略。
3. switch 可以嵌套
4. bool 类型中,0表示假,非0表示真。
4.有限状态机(判断一个字符串由多少单词构成)
#include "stdafx.h"
#include<iostream>
#include<ctype.h>
#define BEGIN 0
#define IN_WORD 1
#define OUT_WORD 2
using namespace std;
//字符串三件套 [] {""}
int main(){
char str[] = { "wangbuding is a beautiful girl" };
int sum = 0;
int tag = BEGIN;
for (int i = 0; str[i] != '\0'; i++){
switch (tag)
{
case BEGIN:
if (isalpha(str[i])) tag = IN_WORD;
else {
tag = OUT_WORD;
}
break;
case IN_WORD:
if (!isalpha(str[i])) tag = OUT_WORD;
break;
case OUT_WORD:
if (isalpha(str[i])) tag = IN_WORD;
sum = sum + 1;
break;
}
}
if (tag == IN_WORD){
sum = sum + 1;
}
cout << sum << endl;
return 0;
}
//switch语句不加break 语句真的会死
5.判断那一年那一个月有多少天
#include<iostream>
using namespace std;
int main(){
int y, m;
cin >> y >>m;
if ((y % 4 == 0 && y % 100 != 0 || y % 400 == 0)&&m==2)
{
cout << y << "年" << m << "月有29天" << endl;
}
else cout << y << "年" << m << "月有28天" << endl;
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
cout << y << "年" << m << "月有31天" << endl;
}
if (m == 4 || m == 6 || m == 9 || m == 11){
cout << y << "年" << m << "月有30天" << endl;
}
return 0;
}
6.判断是否为闰年
#include "stdafx.h"
#include<iostream>
using namespace std;
void judge_Year_Month_D(int y, int m){
if ((y % 4 == 0 && y % 100 != 0 || y % 400 == 0) && m == 2)
{
cout << y << "年" << m << "月有29天" << endl;
}
else cout << y << "年" << m << "月有28天" << endl;
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
cout << y << "年" << m << "月有31天" << endl;
}
if (m == 4 || m == 6 || m == 9 || m == 11){
cout << y << "年" << m << "月有30天" << endl;
}
}
int main(){
int y, m;
cin >> y >>m;
judge_Year_Month_D(y, m);
return 0;
}
6.输入年月日判断是那一年的哪一天(呜呜呜也算是至今为止自己编过最长的代码吧!)
#include "stdafx.h"
#include<iostream>
using namespace std;
//输入 year month day 判断这一天是这一年的第几天
void judge_YMD_D(int y, int m, int d){
int sum = 0;
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
{//闰年
for (int i = 1; i < m; i++)
{
switch (i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum = sum + 31;
break;
case 2:
sum = sum + 29;
break;
case 4:
case 6:
case 9:
case 11:
sum = sum + 30;
break;
}
}
cout << y << "年" << m << "月" << d << "日为" << y << "年的第" << sum + d << "天" << endl;
}
else{
for (int i = 1; i < m; i++)
{
switch (i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum = sum + 31;
break;
case 2:
sum = sum + 28;
break;
case 4:
case 6:
case 9:
case 11:
sum = sum + 30;
break;
}
}
cout << y << "年" << m << "月" << d << "日为" << y << "年的第" << sum + d << "天" << endl;
}
}
int main(){
int y, m, d;
cin >> y >> m >> d;
judge_YMD_D(y, m, d);
return 0;
}
1.交换元素(1. 值传递? 2.指针传递)
2.判断字符串中有多少控制字符 数字字符 大写 小写字符 其他字符
#include "stdafx.h"
#include<iostream>
#include<ctype.h>
#define KONGZHIZIFU 1
#define SHUZIZIFU 2
#define XIAOXIE 3
#define DAXIE 4
#define QITA 5
using namespace std;
int main(){
char str[] = { "WangBuding Is 100\n ,,, Cute!" };
//控制字符 1 数字字符3 小写16 大写 5 其他 2
int a = 0, b = 0, c = 0, d = 0, e = 0;
for (int i = 0; str[i] != '\0'; i++)
{
char tag;
if (iscntrl(str[i])) tag= KONGZHIZIFU;
else if (isdigit(str[i])) tag = SHUZIZIFU;
else if (islower(str[i])) tag = XIAOXIE;
else if (isupper(str[i])) tag = DAXIE;
else if (ispunct(str[i])) tag = QITA;
switch (tag){
case KONGZHIZIFU:
a++; break;
case SHUZIZIFU:
b++; break;
case XIAOXIE:
c++; break;
case DAXIE:
d++; break;
case QITA:
e++; break;
}
}
cout << "控制字符: " << a << " " << "数字字符: " << b << " " << "小写: " << c << " " << "大写: " << d << " " << "其他: " << e << " " << endl;
return 0;
}
3.清晰易懂的比较三个数中的中间值
#include "stdafx.h"
#include<iostream>
#include<ctype.h>
using namespace std;
void swap(int *a, int *b){
int t = *a;
*a = *b;
*b = t;
}
int main(){
int a, b, c;
cin >> a >> b >> c;
if (a > b){
swap(&a, &b);
}
if (b > c){
swap(&b, &c);
}
else if (a > b){
swap(&a, &b);
}
cout << b << endl;
return 0;
}
|