案例: 输入一个整数,判断是奇数还是偶数
先用户输入
奇数: 不能被2整数的是奇数
偶数: 能被2整数的是偶数
数字校验
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int iInput;
cout << "please input a number" << endl;
while (!(cin >> iInput)){
cout << "Wrong, you have inputed a wrong type data\n" << endl;
cin.clear();
while (cin.get() != '\n'){
continue;
}
cout << "please input again" << endl;
}
cout << "The number is: " << iInput << endl;
if (iInput % 2 == 0) {
cout << "这个数字是一个偶数" << endl;
}
else {
cout << "这个数字是一个奇数" << endl;
}
system("pause");
return 0;
}
作业:输入一个正方形的边长,求其面积和周长
#include <stdio.h>
int main()
{
int l = 0;
print("请输入边长:\n");
scanf("%d", &l);
int s = l * l;
int c = 4 * l;
printf("面积:%d 周长:%d\n", s, c);
return 0;
}
输入一个三位数,将每一位取出求和
static int iInput,sum;
static int bai, shi, ge;
cin >> iInput;
cout << "please input a number" << endl;
bai = iInput / 100;
shi = iInput / 10 % 10;
ge = iInput % 10;
sum = bai + ge + shi;
cout << sum;
++,–算术运算
int a=5;
a++; <===>a=a+1 a--:a=a-1;
++a;
若++a,a++单独使用时无任何区别,但和其他运算共同使用时,
int a=5;
int b=0;
b=++a; ++在前,先++后使用
++a 自增为6,将6赋值给b
printf("a=%d\tb=%d\n",a,b);---->a=6,b=6;
int a=5;
int b=0;
b=a++; ? a=? b=? ++在后,先使用后++,先将a的值赋值给b,再对a进行自增
a=6,b=5;
三目运算符
#include "stdafx.h"
#include <stdio.h>
#define Max(a,b) a >b ? a:b
int main()
{
int a = Max(6, 5);
printf("%d\r\n", a);
}
出现段错误的情况:
(1).NULL指针
(2).野指针
(3).更改常量区的内容
二维数组和指针
int brr[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
//定义一个指针指向brr数组的首元素
int (*p)[4] = &brr[0] = brr;
p == &brr[0] ==brr;
*p = *&brr[0] = brr[0] = &brr[0][0] = p[0] = &p[0][0]
**p = *&brr[0][[0] = brr[0][0] = p[0][0]
&brr[0][1] = &brr[0][0]+1 = *p+1
brr[0][1] = *(*p+1)
brr[0][2] = *(*p+2)
p+1 = &brr[0]+1 = brr+1 = &brr[1]
&brr[1][0] = brr[1] = *(p+1)
brr[1][0] = **(p+1)
&brr[1][1] = &brr[1][0]+1 =*(p+1)+1
brr[1][1] = *(*(p+1)+1)
p+2 = brr+2 = &brr[2]
brr[2][3] = *(*(p+2)+3)
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int arr[2][3] = {
{1,2,3},{4,5,6}
};
int(*p)[3] = arr;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++) {
printf("%d", arr[i][j]);
}
}
cout << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++) {
printf("%d", p[i][j]);
}
}
cout << endl;
cout << *(*(p+1)) << endl;
return 0;
}
记得改地磁的时候一个一个输入测试真是傻子,解决办法是二维数组输入打印
tar命令
tar -cvf test.tar 19074 19081 19111 // 将19074 19081 19111三个文件归档到test
tar -cvzf test.tar.gz 19074 19081 1911 //将19074 19081 19111三个文件归档到test,再使用gzip压缩
tar -cvjf test.tar.bz2 19074 19081 1911 //将19074 19081 19111三个文件归档到test,再使用bzip2压缩
tar -xvf test.tar.gz //解压test文件 //解压是-x
字符指针
字符指针数组 //字符指针的数组
char str[4][20] = {“hello”,“world”,“aabb”};
char *pstr[4] = {“hello”,“world”,“aabb”};
面试问题:几个区
栈区,堆区,常量区, 代码区,静态区(全局区)
malloc
动态内存分配 (申请的内存在堆区)
void *malloc(size_t size);
char *p =(char *)malloc(sizeof(char) * 20) //申请20个字节的空间存char变量
if(p == NULL)
{
puts(“malloc error”);
}
(1)malloc不关心空间里面保存什么样的数据,只关心申请的大小,所以在使用这片空间的时候一定要强制转换
(2)申请成功会返回申请这片空间的首地址,如果不成功会返回NULL
(3)使用完申请的空间之后,一定要显式释放这片空间(free)
(4)free之后要给指针赋null,避免它成为一个空悬指针
(5)只申请不释放会造成内存泄漏
(6)不能重复释放同一片内存空间
C高级的练习作业
1.定义学生结构体,学生有姓名,学号,3个分数。
有一个班的5个学生,有3门课程。建议使用上malloc
(1)、求第一门课的平均分;
(2)、找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均分
(3)、5个学生按总成绩进行排序
(4)、找出平均分在90分以上或全部课程成绩在85分以上的学生。
分别编写三个函数来实现以上三个要求
struct person
{
char name[20];
int num;
float score[3] ;
};
先解决输入和打印的问题
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
typedef struct iPerson
{
char name[20];
int num;
int score[3];
}person;
void fristAvager();
int _tmain(int argc, _TCHAR* argv[])
{
person student[5];
person *pstudent = student;
for (int i = 0; i < 5; ++i)
{
cout << "请输入第" << i+1 << "个同学的名字" << endl;
cin >> pstudent[i].name;
cout << "请输入第" << i + 1 << "个同学的学号" << endl;
cin >> pstudent[i].num;
cout << "请输入第" << i + 1 << "个同学的分数" << endl;
for (int j = 0; j < 3;++j)
{
cin >> pstudent[i].score[j];
}
}
pstudent = student;
int len = sizeof(student) / sizeof(struct iPerson);
/*
for (int j = 0; j < 5; ++j)
{
cout << pstudent[j].name <<endl;
cout << pstudent[j].num << endl;
}
*/
//用结构体指针进行打印
for (pstudent = student; pstudent < student + len; pstudent++)
{
/*
cout << pstudent->name << endl;
cout << pstudent->num << endl;
for (int o = 0; o < 3; o++){
cout << pstudent->score[o] << endl;
}
*/
printf("%s学生信息打印如下:\n", pstudent->name);
printf("学号%d:\n", pstudent->num);
printf("高数成绩%d\n", pstudent->score[0]);
printf("英语四级%d\n", pstudent->score[1]);
printf("计算机基础%d\n", pstudent->score[2]);
/*
for (int o = 0; o < 3; o++){
cout << "各科分数" << endl;
cout << pstudent->score[o] << endl;
}
*/
printf("\n");
}
return 0;
}
算出第一个要求平均分
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
typedef struct iPerson
{
char name[20];
int num;
int score[3];
}person;
person student[5];
person *pstudent = student;
void fristAvager();
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < 5; ++i)
{
cout << "请输入第" << i+1 << "个同学的名字" << endl;
cin >> pstudent[i].name;
cout << "请输入第" << i + 1 << "个同学的学号" << endl;
cin >> pstudent[i].num;
cout << "请输入第" << i + 1 << "个同学的分数" << endl;
for (int j = 0; j < 3;++j)
{
cin >> pstudent[i].score[j];
}
}
pstudent = student;
int len = sizeof(student) / sizeof(struct iPerson);
/*
for (int j = 0; j < 5; ++j)
{
cout << pstudent[j].name <<endl;
cout << pstudent[j].num << endl;
}
*/
//用结构体指针进行打印
for (pstudent = student; pstudent < student + len; pstudent++)
{
/*
cout << pstudent->name << endl;
cout << pstudent->num << endl;
for (int o = 0; o < 3; o++){
cout << pstudent->score[o] << endl;
}
*/
printf("%s学生信息打印如下:\n", pstudent->name);
printf("学号%d:\n", pstudent->num);
printf("高数成绩%d\n", pstudent->score[0]);
printf("英语四级%d\n", pstudent->score[1]);
printf("计算机基础%d\n", pstudent->score[2]);
/*
for (int o = 0; o < 3; o++){
cout << "各科分数" << endl;
cout << pstudent->score[o] << endl;
}
*/
printf("\n");
}
//fristAvager();
int tmp = 0;
for (pstudent = student; pstudent < student + len; pstudent++)
{
tmp = tmp + pstudent->score[0];
}
printf("总分\n");
cout << tmp;
printf("平价分\n");
cout << tmp / 5;
return 0;
}
const
数据类型修饰符:const volatile
const : 只读
const int a = 5; //a就只能读
int * const p; //const修饰的是p,所以p的值不能被更改,但是p指向的那片空间的值可以被更改(也就是说p不能修改,p可以修改)
const int p; //const修饰的是p, 所以p不能被更改,但是p可以被修改,就是p可以指向其他变量
int const *p; //同上
const int const p; //p和p都被const修饰,所以p的指向不能被修改,p指向的这片空间的的值也不能被修改
malloc
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *description;
char *func(char *description);
int _tmain(int argc, _TCHAR* argv[])
{
char name[100];
strcpy_s(name, 100,"Zara Ali");
description = func(description);
printf("Name = %s\n", name);
printf("Description: %s\n", description);
free(description);
return 0;
}
char *func(char *description)
{
description = (char *)malloc(30 * sizeof(char));
if (description == NULL)
{
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else
{
strcpy_s(description, 30,"Zara ali a DPS student.");
}
return description;
}
文件IO
获取当前时间并写入到文件当中并实时打印
import time
while(1):
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
a = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
f = open("time.txt", "a+")
f.write(a);
f.write("\n");