杨辉三角

      1
      1  1
      1  2  1
      1  3  3  1
      1  4  6  4  1
      1  5  10 10 5  1                                    

int main(void)
{
    int a[10][10],i,j;
    for(i=0;i<10;i++)
    {
//          for(j=10;j>=i;j--)
//            printf("%2c",'*');/*两个空格*/
        for(j=0;j<=i;j++)       //打出直角三角形,
        {
            if(i==j||j==0)      //给对角线和第一列赋值为1
            {
                a[i][j]=1;
            }
            else                //剩下内面的值等于上一行的两数之和
            {
                a[i][j]=a[i-1][j]+a[i-1][j-1];
            }
            printf("%3d ",a[i][j]);     /*%3d后一个空格*/
            
            if(i==j)            //为对角线的时候换行
            {
                printf("
");
            }
        }
    }
    return 0;
}

2pingjiang

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

struct Stuinfo
{
    char ID[4];
    char NAME[10];
    int Score;
};

struct Stuinfo stuInfo[100];//结构体数组

FILE *file;//文件指针,指向物理文件

bool checkId(char Id[]);
bool checkId(char Id[])
{
    bool s = false;
    for (int i = 0; i < strlen(Id); i++)
    {
        if (Id[i] >= '0' && Id[i] <= '9')
        {
            s = true;
        }
        else
        {
            s = false;
            break;   //
        }
    }
    return s;
}

bool checkName(char name[]);
bool checkName(char name[])
{
    bool s = false;
    for (int i = 0; i < strlen(name); i++)
    {
        if ((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= 'A' && name[i] <= 'Z'))
        {
            s = true;
        }
        else
        {
            s = false;
            break;   //
        }
    }
    return s;
}

void createDB();
void createDB()
{
    int i = 0;//代表先在录入第i个学生
    int m = 0;//录入了多少学生
    bool success = 0;
    
    while (1)
    {
        //学号
        printf("请输入第%d个学生的信息
",i+1);
        do
        {
            printf("请输入学号
");
            char tempId[4];
            scanf("%s",tempId);
            
            if (checkId(tempId))
            {
                success = 1;
                strcpy(stuInfo[i].ID, tempId);
            }
            else
            {
                success = 0;
                printf("输入的学号有错误
");
            }
        } while (!success);
        //姓名
        do
        {
            printf("请输入姓名
");
            char tempName[10];
            scanf("%s",tempName);
            
            if (checkName(tempName))
            {
                success = 1;
                strcpy(stuInfo[i].NAME, tempName);
            }
            else
            {
                success = 0;
            }
        } while (!success);
        
        //成绩
        do
        {
            printf("请输入成绩
");
            int score = 0;
            scanf("%d",&score);
            
            if (score >= 0 && score <=100)
            {
                success = 1;
                stuInfo[i].Score = score;
            }
            else
            {
                success = 0;
            }
        } while (!success);
        
        i++;
        m++;
        
        //跳出循环的条件
        printf("是继续录入信息请按y或Y
");
        getchar();//截取

        char ch;
        scanf("%c",&ch);
        if (ch == 'Y' || ch == 'y')
        {
            continue;
        }
        else
        {
            break;
        }
    }
    
    //把数据记录到文件
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt","w+")) == NULL)
    {
        printf("打开文件失败
");
    }
    for (int i = 0 ; i < m; i++)
    {
        fwrite(&stuInfo[i], sizeof(struct Stuinfo), 1, file);//只是写到缓冲区
    }
    
    fclose(file);//写到文件上
}

void printStuInfo();
void printStuInfo()
{
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt","r")) == NULL)
    {
        printf("打开文件失败
");
    }

    int n = 0;
                            //代表文件中几条记录
    for(int i = 0;fread(&stuInfo[i],sizeof(struct Stuinfo), 1, file);i++ )
    {
        n++;
    }

    printf("学号--------姓名-------成绩-----
");
    for(int j = 0;j < n ;j++)
    {
        printf("%-10s %-10s %-10d
",stuInfo[j].ID,stuInfo[j].NAME,stuInfo[j].Score);
    }
    printf("------------------------------
");
    fclose(file);
}

void searchByName();
void searchByName()
{
    int n = 0;//记录文件中有几条记录
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt","r")) == NULL)
    {
        printf("打开文件失败
");
    }
    
    for (int i = 0;fread(&stuInfo[i],sizeof(struct Stuinfo), 1, file);i++)
    {
        n++;
    }
    
    while(1)
    {
        printf("请输入你想查找学生的姓名
");
        char temp[10];
        scanf("%s",temp);
        
        int flag = 0 ;
        printf("学号--------姓名-------成绩-----
");
        for(int i = 0 ;i < n; i++)
        {
            if(strcmp(temp, stuInfo[i].NAME) == 0)
            {
                    printf("%-10s %-10s %-10d
",stuInfo[i].ID,stuInfo[i].NAME,stuInfo[i].Score);
            }
            else
            {
                flag++;
            }
        }
        

        if(flag == n)
        {
            printf("对不起,没查到你需要找到学生
");
        }
        printf("------------------------------
");
        
        //
        printf("按Y或y继续查找
");
        getchar();
        char ch;
        scanf("%c",&ch);
        if(ch == 'Y' || ch == 'y')
        {
            continue;
        }
        else
        {
            break;
        }
    }

}

void insertInfo();
void insertInfo()
{
    
    int n = 0;//记录文件中有几条记录
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt","r")) == NULL)
    {
        printf("打开文件失败
");
    }
    
    for (int i = 0;fread(&stuInfo[i],sizeof(struct Stuinfo), 1, file);i++)
    {
        n++;
    }
    
    //
    int i = 0;//代表先在录入第i个学生
    int m = 0;//录入了多少学生
    bool success = 0;
    
    while (1)
    {
        //学号
        printf("请输入第%d个学生的信息
",n+i+1);
        do
        {
            printf("请输入学号
");
            char tempId[4];
            scanf("%s",tempId);
            
            if (checkId(tempId))
            {
                success = 1;
                strcpy(stuInfo[i].ID, tempId);
            }
            else
            {
                success = 0;
                printf("输入的学号有错误
");
            }
        } while (!success);
        //姓名
        do
        {
            printf("请输入姓名
");
            char tempName[10];
            scanf("%s",tempName);
            
            if (checkName(tempName))
            {
                success = 1;
                strcpy(stuInfo[i].NAME, tempName);
            }
            else
            {
                success = 0;
            }
        } while (!success);
        
        //成绩
        do
        {
            printf("请输入成绩
");
            int score = 0;
            scanf("%d",&score);
            
            if (score >= 0 && score <=100)
            {
                success = 1;
                stuInfo[i].Score = score;
            }
            else
            {
                success = 0;
            }
        } while (!success);
        
        i++;
        m++;
        
        //跳出循环的条件
        printf("是继续录入信息请按y或Y
");
        getchar();//截取

        char ch;
        scanf("%c",&ch);
        if (ch == 'Y' || ch == 'y')
        {
            continue;
        }
        else
        {
            break;
        }
    }
    
    //把数据记录到文件
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt","a+")) == NULL)//追加
    {
        printf("打开文件失败
");
    }
    for (int i = 0 ; i < m; i++)
    {
        fwrite(&stuInfo[i], sizeof(struct Stuinfo), 1, file);//只是写到缓冲区
    }
    
    fclose(file);//写到文件上

}

void deleteByName();
void deleteByName()
{
    int n = 0;
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt", "r")) == NULL)
    {
        printf("打开文件失败
");
    }
    
    for (int i = 0;fread(&stuInfo[i],sizeof(struct Stuinfo), 1, file);i++)
    {
        n++;
    }
    //
    char temp[10];
    int i = 0;
    while(1)
    {
        printf("请输入要删除学生的名字
");
        scanf("%s",temp);
        printf("学号--------姓名-------成绩-----
");
        for (; i < n ; i++)
        {
            if (strcmp(stuInfo[i].NAME, temp) == 0)
            {
                printf("%-10s %-10s %-10d
",stuInfo[i].ID,stuInfo[i].NAME,stuInfo[i].Score);
               
            
                if (i == n - 1)//删除最后1个
                {
                
                }
                else//删除中间
                {
                    for(int j = i;j < n -1;j++)
                    {
                        stuInfo[j] = stuInfo[j+1];
                    }
                }
                n--;
            
                i = -1; //找到删除了i 又从0开始遍历,没找到i不用从0开始
            }
        }
        printf("------------------------------
");
        
        
        //
        printf("按y或Y继续删除
");
        getchar();
        char ch;
        scanf("%c",&ch);
        if(ch == 'Y' || ch == 'y')
        {
            continue;
        }
        else
        {
            break;
        }

    }

    //把数据记录到文件
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt","w")) == NULL)
    {
        printf("打开文件失败
");
    }
    
    for (int i = 0 ; i < n; i++)
    {
        fwrite(&stuInfo[i], sizeof(struct Stuinfo), 1, file);//只是写到缓冲区
    }
    
    fclose(file);//写到文件上

}

void sortByScore();
void sortByScore()
{
    int n = 0;
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt", "r")) == NULL)
    {
        printf("打开失败
");
    }
    for (int i = 0 ; fread(&stuInfo[i],sizeof(struct Stuinfo), 1, file); i++)
    {
        n++;
    }
    
    struct Stuinfo temp;
    for (int j = 0; j < n -1; j++)
    {
        for (int i = 0; i < n - j - 1; i++)
        {
            if (stuInfo[i].Score < stuInfo[i+1].Score)
            {
                temp = stuInfo[i+1];
                stuInfo[i+1] = stuInfo[i];
                stuInfo[i] = temp;
            }
        }
    }
    
    //把数据记录到文件
    if ((file = fopen("/Users/3022/Desktop/code_C/评讲/stuInfo/stu.txt","w")) == NULL)
    {
        printf("打开文件失败
");
    }
    
    for (int i = 0 ; i < n; i++)
    {
        fwrite(&stuInfo[i], sizeof(struct Stuinfo), 1, file);//只是写到缓冲区
    }
    
    fclose(file);//写到文件上
}

int main(int argc, const char * argv[])
{
    printf("1.创建2.输出3.查找4.插入5.删除6.排序
");
    int w;
    scanf("%d",&w);
    
    while (1)
    {
        
        switch (w)
        {
            case 1:
            {
                createDB();
                break;
            }
            case 2:
            {
                printStuInfo();
                break;
            }
            case 3:
            {
                searchByName();
                break;
            }
            case 4:
            {
                insertInfo();
                break;
            }
            case 5:
            {
                deleteByName();
                break;
            }
            case 6:
            {
                sortByScore();
                break;
            }
            default:
                break;
        }

        
        printf("1~6继续操作:
1.创建2.输出3.查找4.插入5.删除6.排序
");
        scanf("%d",&w);
        if(w <=6 && w > 0)
        {
            continue;
        }
        else
        {
            break;
        }

    }
    
    
    
    return 0;
}
原文地址:https://www.cnblogs.com/huen/p/3502987.html