C
https://fishc.com.cn/
1.hello world
- ubuntu搭建C语言环境
安装vim(编辑器),gcc(编译器)和build-essential(编译程序必须软件包的列表信息)
sudo apt-get install vim
sudo apt-get install gcc
sudo apt-get install build-essential
- 在终端中,输入:vim hello.c 命令,将创建并进入到hello.c文件中
- 按 “i”键, 开始在文件中输入代码
#include <stdio.h>
int main()
{
/* 我的第一个 C 程序 */
printf("Hello! \n");
return 0;
}
- 按“Esc”键(如果不都管用多按几次,如果还不管用,使用shift+q跳出命令模式,然后再按esc键),输入 “ :wq ” 保存退出(注意wq前边有一个的英文冒号)
- 在终端界面输入下面的代码,将hello.c编译成可执行文件hello
在 gcc 后面 加 -Wall ,可看到错误信息, -o hello 是为生成的文件命名为hello,如果不加-o则默认生成的a.out文件
gcc -Wall hello.c -o hello
- 执行文件hello
./hello
4.结构体
一、初始C语言
1. 第一个C语言程序
#include <stdio.h>
int main()
{
/* 我的第一个 C 程序 */
printf("Hello! \n");
return 0;
}
1.1
#include <stdio.h>
//std - 标准
//i - input
//o - output
1.2标准的主函数写法
int main()
{
return 0;
}
//main函数是程序的入口
//仅有一个
2.分支和循环
控制语句用于控制程序的执行流程,以实现程序的各种结构方式,它们由特定的语句定义符组成,C语言有九种控制语句。
可分成以下三类:
- 条件判断语句也叫分支语句: if语句、switch语句;
- 循环执行语句: do while语句、while语句、for语句;
- 转向语句: break语句、goto语句、continue语句、return语句。
分支语句:
- if
- switch
循环语句
- while
- for
- do while
2.1 if语句(分支中多条语句带{})
//多分支
if(表达式1)//表达式为假后续代码不执行
语句1;
else if(表达式2)
语句2;
e1se
语句3;
tips:
- else与最近的if匹配
- 数字在前少写了一个等号会报错,变量在前不会
if(5 == num) 等价于 if(num == 5)
2.2 while循环
while循环其一般式如下:
while(表达式)
{
循环体语句
}
在表达式为真(1)重复执行循环体语句部分
2.3 for循环
for(表达式1;表达式2;表达式3)
循环语句;
表达式1
表达式1为初始化部分,用于初始化循环变量的。
表达式2
表达式2为条件判断部分,用于判断循环时候终止。
表达式3
表达式3为调整部分,用于循环条件的调整。
3.指针
int x = 10;
int *ptr = &x;
printf("The value of ptr is: %p\n", (void *)ptr);
4.结构体
#include <stdio.h>
#include <string.h>
// 定义一个结构体
struct student {
char name[50];
int age;
float gpa;
};
int main() {
// 声明一个结构体变量
struct student s1;
// 为结构体变量赋值
strcpy(s1.name, "John");
s1.age = 20;
s1.gpa = 3.5;
// 输出结构体变量的值
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("GPA: %f\n", s1.gpa);
return 0;
}
4.1 strcpy函数
#include <stdio.h>
#include <string.h>
int main() {
char str1[50], str2[50];
printf("Enter a string: ");
scanf("%s", str1);
// 将str1复制到str2中
strcpy(str2, str1);
printf("String 1: %s\n", str1);
printf("String 2: %s\n", str2);
return 0;
}
我们使用scanf函数从用户那里获取一个字符串,并将其存储在名为str1的字符数组中。然后,我们使用strcpy函数将str1复制到名为str2的另一个字符数组中。最后,我们使用printf函数输出两个字符串的值。
4.2 strncpy函数
需要注意的是,strcpy函数不会检查目标字符串的长度,因此可能会导致缓冲区溢出。为了避免这种情况,您可以使用strncpy函数,它允许您指定要复制的字符数。例如:
strncpy(str2, str1, sizeof(str2));
5.内存对齐
C语言中的内存对齐是一个重要的概念,它指的是数据在内存中的存储方式。在C语言中,数据通常以字节为单位存储在内存中。
然而,一些数据类型,如整数和浮点数,需要多个字节的内存来存储。
内存对齐确保这些数据类型以计算机访问效率高的方式存储在内存中。
具体来说,内存对齐确保数据以其大小的倍数存储在内存中的地址上。
例如,4字节的整数将在4字节边界上对齐,这意味着它在内存中的地址将是4的倍数。
在C语言中,内存对齐通常由编译器自动处理。然而,程序员在处理包含多个数据类型的数据结构时,需要注意内存对齐问题。为了确保数据结构正确对齐,可以使用alignof运算符确定给定数据类型所需的对齐方式。例如,以下代码片段演示了如何使用alignof确定double所需的对齐方式:
#include <stdio.h>
#include <stdalign.h>
int main() {
printf("Alignment of double: %zu\n", alignof(double));
return 0;
}
alignof()是一个C语言中的内置函数,用于获取给定类型的对齐要求。在C语言中,对齐是指将数据存储在内存中的方式,以便于访问和处理。对齐要求是指数据类型在内存中的对齐方式,通常是以字节为单位的倍数。例如,对于一个int类型,通常需要4字节对齐。
还可以使用alignas
来手动控制内存对齐。alignas
可以用于指定给定数据类型或变量的所需对齐方式。例如,以下代码片段演示了如何使用alignas
来指定struct
的所需对齐方式:
#include <stdio.h>
#include <stdalign.h>
struct my_struct {
int a;
double b;
} __attribute__((aligned(16)));
int main() {
printf("Alignment of my_struct: %zu\n", alignof(struct my_struct));
return 0;
}
在这个代码中,我们使用__attribute__((aligned(16)))
语法来指定my_struct
结构体应该在16字节边界上对齐。然后使用alignof
运算符来验证结构体是否正确对齐。
总的来说,内存对齐是C编程中的一个重要概念,可以对程序性能产生重大影响。通过了解内存对齐的工作原理以及如何控制它,您可以编写更高效和有效的C程序。
二.线性表
1. 单链表
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
LinkList createList(int n) {
LinkList head = NULL;
LinkList tail = NULL;
for (int i = 0; i < n; i++) {
LNode *newNode = (LNode*)malloc(sizeof(LNode));
newNode->data = i;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
void printList(LinkList head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
LinkList myList = createList(5);
printList(myList);
return 0;
}
createList函数创建一个包含n个节点的新链表,每个节点包含从0到n-1的值。printList函数只是打印链表中的值。
JAVA
来自尚硅谷老师的学习笔记:
http://www.atguigu.com/
1.hello world
代码:
public class HelloWorld {
public static void main(String[] args) {//main方法为程序入口
System.out.println("Hello World");
}
}
编译 javac HelloWorld.java
运行 java HelloWorld
2.注释
2.1 单行
2.2 多行(不可嵌套)
2.3 文档注释(Java特有)/** */
会被解析
总结:
- 一个Java源文件中只能有一个类public,但可以有多个class
要求申明public的类的类名与源文件名相同 - 程序的入口是main()方法,格式是固定的
- 输出语句:
System.out.println():输出后换行
System.out.print():只输出数据
- 执行语句借位都以“;”结束
- 编译过程:编译完成后会生成一个或多个字节码文件,字节码文件名与源文件中类名相同
3.运算符与标识符
3.1 标识符:所有可以自己命名的都叫标识符
命名规则:
- 严格区分大小写,长度无限制
- 不能包含空格,数字不可以开头
- 可以包含保留字和关键字,但是不能使用
- 由字母大小写,0-9,_,$组成
3.2Java中的名称命名规范:
- 包名:多单词组成时所有字母都小写:xxxyyyzzz
- 类名、接口名:多单词组成时,所有单词的首字母大写:XxxYyyZzz
- 变量名、方法名:多单词组成时,第一个单词首字母小写,第二个单词开始每个单词首字母大写:xxxYyyZzZ
- 常量名:所有字母都大写。多单词时每个单词用下划线连接:xXX_Y_zzZ
3.3变量
- 定义格式:数据类型 变量名 = 变量值
- 分类:
定义float类型时必须以f或F结尾
定义long类型时必须以l或L结尾
通常,定义浮点型使用double
定义char类型,使用一对’ ',内部只能有一个字符
3.4运算符
运算符是一种特殊的符号,用以表示数据的运算、赋值和比较等。算术运算符
-
赋值运算符(= += /= -= *= %= )
支持连续赋值
思考:1编译过不了,2是正常的 -
比较运算符(关系运算符)
比较运算符的运算结果都是布尔型 -
逻辑运算符
逻辑运算符操作的都是bool型的变量
开发中推荐使用|| &&
-
位运算符(用的少)
5. 三元运算符
- 算术运算符
a. 关于除号 /
b. 关于取模运算 %
c. 关于自增++
4.流程控制
4.1 分支结构
4.1.1 else if
岳小鹏参加Java考试,他和父亲岳不群达成承诺:如果:
成绩为100分时,奖励一辆BMN;
成绩为(80,99]时,奖励一台iphone xs max;当成绩为[60,80]时,奖励一个iPad;
其它时,什么奖励也没有。
请从键盘输入岳小鹏的期末成绩,并加以判断
package com.sgg.score;
import java.util.Scanner;
public class score {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入小鹏的成绩:");
int score = scan.nextInt();
if(score == 100){
System.out.println("to a bwm");
}else if (score>80 && score <=99) {
System.out.println("a iphone");
}else if(score >=60 && score <=80) {
System.out.println("a ipad");
}else {
System.out.println("nothing");
}
}
}
说明:
- else结构是可选的
- 针对于多个表达式
多个条件表达式之间为互斥关系(没有交集),判断或执行语句在哪无所谓
若有交集 应该考虑清楚放在哪里
else-if 练习
编写程序:由键盘输入三个整数分别存入变量num1、num2. num3,对它们进行排序(使用if-else if-else),并且从小到大输出。
package com.sgg.number;
import java.util.Scanner;
public class number {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("the first:");
int num1 = scanner.nextInt();
System.out.println("the second:");
int num2 = scanner.nextInt();
System.out.println("the third:");
int num3 = scanner.nextInt();
if(num1 > num2) {
if(num3 >= num1) {
System.out.println(num2+","+num1+","+num3);
}else if(num3 <= num2) {
System.out.println(num3 + "," + num2 + ","+ num1);
}else {
System.out.println(num2 +","+ num3+","+num1);
}
}else {
if(num3 >= num2) {
System.out.println(num1+","+num2+","+num3);
}else if(num3 <= num1) {
System.out.println(num3+","+num1+","+num2);
}else {
System.out.println(num1+","+num3+","+num2);
}
}
}
}
大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出一定的条件:高:180cm以机富:财富1千万以上;帅:是。
如果这三个条件同时满足,则:“我一定要嫁给他!!!”
如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。”
如果三个条件都不满足,则:“不嫁!”
package com.sgg.marry;
import java.util.Scanner;
public class marry {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入你的身高:(cm)");
int height =scan.nextInt();
System.out.println("你的资产:(千万)");
double wealth =scan.nextDouble();
System.out.println("你是否帅气:(true or false)");
boolean ishandsome = scan.nextBoolean();
if(height >=180 && wealth >=1 && ishandsome) {
System.out.println("marry!!!!!!!!");
}else if(height >=180 || wealth >=1 || ishandsome) {
System.out.println("just so so");
}else {
System.out.println("no way");
}
}
}
4.1.2 switch-case
说明:
- 根据switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应case结构中,调用其执行语句。4当调用完执行语句以后,则仍然继续向下执行其他case结构中的执行语句,直到遇到break关键字switch-case结构末尾为止结束。
- break,可以使用在switch-case结构中,表示一旦执行到此关键字,就跳出switch-case结构
- switch结构中的表达式,只能是如下的6种数据类型之一: byte ,short、char、int、枚举类型(jdk5.0新增)、String类型(jdk7.0新增)
- case只能声明常量,不能声明范围(bool)
- default: 相当于if-else 中的 else (备胎)
- 如果switch-case结构中的多个case的执行语句相同,则可以考虑进行合并。
- 凡是可以使用switch-case的结构,都可以转换为if-else。反之,不成立。
public class sc {
public static void main(String[] args) {
int number = 2;
switch(number) {
case 0:
System.out.println("zero");
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
default:
System.out.println("other");
}
}
}
4.2循环结构
题目:
从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。
4.2.1 for循环
遍历100以内所有偶数
public class sc1 {
public static void main(String[] args) {
for(int i =1;i <= 100;i++) {
if(i % 2 == 0) {
System.out.println(i);
}
}
}
}
4.2.2 while 循环
说明:
- 写while循环千万小心不要丢了迭代条件。一旦丢了,就可能导致死循环!
- 写程序要避免死循环
- for循环和while循环可以相互转换
遍历一百以内所有偶数
package com.sgg.sc1;
public class sc1 {
public static void main(String[] args) {
int i = 1;
while(i <= 100) {
if(i % 2 == 0) {
System.out.println(i);
}
i++;
}
}
}
4.2.3 do-while循环
说明:
- do while至少会执行一次循环体
- 开发中for和while更多
遍历一百以内所有的偶数
package com.sgg.sc1;
import java.util.Scanner;
public class sc1 {
public static void main(String[] args) {
int i = 1;
do {
if(i % 2 ==0) {
System.out.println(i);
}
i++;
}while(i<=100);
}
}
eclipse使用方法:
main方法 alt+/ 回车
输出:syso alt+/ 回车
new–>java project
项目名随意
右键src
new --> package (包名都得小写)
命名格式 com.xx.xx
右键package new–> class
命名随意