1.打印如下图形

#include <stdio.h>int main(){int i, j, k;for (i = -3; i <= 3; i++){k = abs(i);for (j = 1; j <= 7 - k; j++){if (j > k){printf("*");}else {printf(" ");}}printf("\n");}return 0;}
#include<stdio.h>#include<math.h>int main(){int i, j;for (i = 1; i <= 5; i++){for (j = -4; j <= 4; j++){if (5 - abs(j) > 5 - i)printf("*");else printf(" ");}for (j = -4; j <= 4; j++){if (5 - abs(j) > 5 - i)printf("*");else printf(" ");}printf("\n");}return 0;}
2.打印杨辉三角
#include<stdio.h>//头文件int main()//主函数入口{int i, j;//定义整型变量int array[10][10];//定义二维数组for (i = 0; i < 10; i++)//for循环{array[i][i] = 1;//给二维数组的每一行的最后一个赋值为1array[i][0] = 1;//第二维数组的每一行的开头赋值为1}for (i = 2; i < 10; i++)//外层循环限制行{for (j = 1; j <= i - 1; j++)//内层循环限制列{array[i][j] = array[i - 1][j] + array[i - 1][j - 1];//给中间的数赋值}}for (i = 0; i < 10; i++)//外层循环限制行{for (j = 0; j <= i; j++)//内层循环限制列{printf("%6d", array[i][j]);//输出二维数组,宽度为6}printf("\n");//换行}return 0;//函数返回值为0}
![DNJ7_6@FZ2IXSX]%83RY5FI.png](/uploads/projects/cloudbase@c99/98b2741dbc5573c7f268b9961553385d.png)
#include<stdio.h>int main(){int i, j;int a[6][6];for (i = 0; i < 6; i++){a[i][0] = 1;a[i][i] = 1;}for (i = 2; i < 6; i++){for (j = 1; j < i; j++){a[i][j] = a[i - 1][j] + a[i - 1][j - 1];}}for (i = 0; i < 6; i++){for (j = 0; j <= i; j++){printf("%6d", a[i][j]);}printf("\n");}return 0;}
3.链表相关操作
#include <stdio.h>#include <stdlib.h>struct Node {int data; //数据域struct Node* next; //指针域};struct Note* createList(){struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//headNode 成为结构体变量//变量使用前必须初始话//headNode->data = 1;headNode->next = NULL;return headNode;}//创建节点struct Node* createNode(int data){struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));newNode->data = data;newNode->next = NULL;return newNode;}//打印节点void printList(struct Node* headNode){struct Node* pMove = headNode->next;while (pMove){printf("%d\t", pMove->data);pMove = pMove->next;}}//插入节点,参数:插入那个链表,插入节点的数据是多少void insertNodeByHead(struct Node* headNode, int data){//1.创建插入的节点struct Node* newNode = createNode(data);newNode->next = headNode->next;headNode->next = newNode;}//删除节点void deleteNodeByHead(struct Node* headNode, int data){struct Node* posNode = headNode->next;struct Node* posNodeFront = headNode;if (data == NULL ){printf("无法删除,链表为空\n");}else {while (posNode->data != data){posNodeFront = posNode;posNode = posNodeFront->next;if (posNode == NULL){printf("没有找到相关数据,无法删除\n");return;}}posNodeFront->next = posNode->next;free(posNode);}}int main(){struct Node* list = createList();insertNodeByHead(list, 1);insertNodeByHead(list, 2);insertNodeByHead(list, 3);deleteNodeByHead(list, 1);printList(list);return 0;}
4.将一个正整数逆序输出
例如,若输入 135 ,则输出 531
#include<stdio.h>int main(){int s, n;printf("Enter a number:");scanf("%d", &n);printf("output:");do {s = n % 10;printf("%d", s);n = n / 10;} while (n != 0);return 0;}
5.字符串复制功能
将 p2 所指字符串赋值到 p1 所值内存空间;
可以通过修改 fun(b+strlen(b), a) 实参使其实现字符串连接功能;
#include<stdio.h>void fun(char* p1, char* p2){while ((*p1 = *p2) != '\0'){p1++;p2++;}}int main(){char a[20] = "abc";char b[20] = "cdefg";fun(a, b);printf("%s", a);return 0;}
6.两个字符串连接
#include<stdio.h>#include<stdlib.h>void fun(char* p1, char* p2){while (*p2){*p1 = *p2;p1++;p2++;}}int main(){char a[20] = "how are ";char b[20] = "you?";int n = strlen(a);fun(a+n, b);printf("%s\n", a);return 0;}
7.比较 字符串 a 和字符串 b 的大小
fun(char *a, char * b){while((*a !='\0') && (*b !='\0') && (*a == *b){a++;b++;}return *a-*b;}
8.删除字符串中数字字符
#include<stdio.h>void dele(char* s){int n = 0, i;for (i = 0; s[i]; i++)if (!(s[i] >= '0' && s[i] <= '9'))s[n++] = s[i];s[n] = '\0';}int main() {char a[] = "hello123";dele(a);printf("%s\n", a);return 0;}
9.C语言数据文件
1.文件由数据流形式组成,可按数据的存放形式分为二进制文件和文本文件;
例:将 12345 写入文本文件占用 5 个字节 ,和二进制文件所占用 4 个字节;
文本文件:一个有 5 个数 ;
二进制文件:int 型数据,占用 4 个字节。
2.一个C文件是一个字节流或二进制流。它把数据看作是一连串的字符(字节),即字符序列组成
