纯小白C语言一些学习实例

(注:点击代码块右上角以全屏)

(或者按住句子作复制状,往右拖拉,即可看见过长而不在显示范围的代码)

  • 图书馆系统

可多次输入标题,作者,价格。当输入作者时按下回车即为停止输入,此时将打印所有信息。

#include<stdio.h>
#include<string.h>
char* s_gets(char* st, int n);//比较难理解的部分
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100

struct book {
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

int main(void)
{
    struct book library[MAXBKS];
    int count = 0;
    int index;

    printf("plz enter title\n");
    printf("plz [enter] at the start of a line to stop.\n");

    while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0')//保证有效输入,保证了有输入不为空、输入开头不为\0
    {
        printf("enter author.\n");
        s_gets(library[count].author, MAXAUTL);
        printf("enter value. \n");
        scanf_s("%f", &library[count++].value);

        while (getchar() != '\n')
            continue;

        if (count < MAXBKS)
            printf("Enter the next title. \n");//结束以后又回到while里面要你输入标题了
    }

    if (count > 0)//如果输入完了enter以后,前面的while不通过,结束循环,就进入这
    {
        printf("Here is the list of your books:\n");
        for (index = 0; index < count; index++)
            printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
    }
    else
        printf("NO\n");

    return 0;
}

char* s_gets(char* st, int n)
{
    char* ret_val;
    char* find;

    ret_val = fgets(st, n, stdin);//从键盘里输入n个字符到st字符串里面
    if (ret_val)//如果有输入则进入
    {
        find = strchr(st, '\n');//把st字符串里查找到的\n的位置赋值给find
        if (find)//如果找到了
            *find = '\0';//把这个位置设置成'\0'(为了保证结尾罢)
        else
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}
  • 学生成绩统计系统

可读性:低

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

struct subjectmark
{
	char subject[6][20];
	float mark[6];
	int count;
	float average;
};

struct information
{
	char name[30];
	char StudentID[30];
	int age;
	char Faculty[30];
	struct subjectmark Mark;
};

void Grade(float average)
{
	//输出评价
	if (average >= 90 && average <= 100)
	{
		printf("The grade you got is A+");
	}
	else if (average >= 80 && average < 90)
	{
		printf("The grade you got is A");
	}
	else if (average >= 70 && average <= 80)
	{
		printf("The grade you got is A-");
	}
	else if (average >= 65 &&average <= 70)
	{
		printf("The grade you got is B+");
	}
	else if (average >= 60 &&average <= 65)
	{
		printf("The grade you got is B");
	}
	else if (average >= 55 && average <= 60)
	{
		printf("The grade you got is B-");
	}
	else if (average >= 50 && average <= 55)
	{
		printf("The grade you got is C+");
	}
	else if (average >= 45 && average <= 50)
	{
		printf("The grade you got is C");
	}
	else if (average >= 40 &&average <= 45)
	{
		printf("The grade you got is C-");
	}
	else
	{
		printf("The grade you got is D");
	}
}

int main()
{
	//Welcome
	printf("————————————————WELCOME TO STUDENTS GRADE INFORMATION SYSTEM—————————————————\n\n");
	//student information
	struct information studentA[10];
	int people = 0;
	for (; people < 10; people++)
	{	//个人信息
		printf("\n\nPlease enter your Name\n(Enter 0 if you want to stop input)\n");
		scanf("%s", &studentA[people].name);
		if (studentA[people].name[0] == '0')
			break;
		printf("Please enter your StudentID \n");
		scanf("%s", &studentA[people].StudentID);
		printf("Please enter your Age \n");
		scanf("%d", &studentA[people].age);
		printf("Please enter you Faculty\n");
		scanf("%s", &studentA[people].Faculty);

		int check;
		printf("Do you want to check you information?\n1.Yes\n2.No\n");
		scanf("%d", &check);
		if (check == 1)
		{
			printf("Personal information:\n");
			printf("Name:%s\nStudentID:%s\nAge:%d\nFaculty:%s\n\n\n", studentA[people].name, studentA[people].StudentID, studentA[people].age, studentA[people].Faculty);
		}
		//student Marks
		printf("Please enter maximum 6 subjects and their corresponding marks\n(Valid marks must be 0-100)\n");


		int i = 0;
		while (i < 6)
		{
			printf("\n\nWhat is the NO.%d subject\n(Enter 0 if you want to stop)\n", i + 1);
			scanf("%s", &studentA[people].Mark.subject[i]);

			if (studentA[people].Mark.subject[i][0] == '0')//"字符串’字符.mfujbkyvuk
			{
				break;
			}

			while (1)
			{
				printf("What is the mark of %s\n", studentA[people].Mark.subject[i]);
				scanf("%f", &studentA[people].Mark.mark[i]);
				if (studentA[people].Mark.mark[i] < 0 || studentA[people].Mark.mark[i]>100)
				{
					printf("The mark is invalid.\nPlease enter again\n");
					continue;
				}
				else
				{
					break;
				}
			}
			i++;

		}
		//开始打印
		printf("The number of subject you entered is %d\n\n", i);

		studentA[people].Mark.average = 0;
		studentA[people].Mark.count = i;

		for (int j = 0; j < i; j++)
		{
			printf("The marks of %s is %.2f\n", studentA[people].Mark.subject[j], studentA[people].Mark.mark[j]);
			studentA[people].Mark.average += studentA[people].Mark.mark[j];
			//求平均数
		}

		studentA[people].Mark.average = studentA[people].Mark.average / i;

		Grade(studentA[people].Mark.average);
	}

	//结束程序打印全部
	for (int w = 0; w < people; w++)
	{
		printf("\n\n—————NO.%d Student—————\n", w + 1);
		printf("Name: %s\t Age: %d years old\nStudentID: %s\nFaculty: %s\n", studentA[w].name, studentA[w].age, studentA[w].StudentID, studentA[w].Faculty);
		printf("Subject\t    Mark\t\n");
		for (int z = 0; z < studentA[w].Mark.count; z++)
		{

			printf("%s\t    %.2f\t\n", studentA[w].Mark.subject[z], studentA[w].Mark.mark[z]);
		}
		printf("Avarage is %.2f\n", studentA[w].Mark.average);

		Grade(studentA[w].Mark.average);

	}
	return 0;
}
文章作者为:Cyan

评论

  1. 博主 置顶
    Windows Edge
    7 月前
    2024-4-22 21:41:19

    Markdown在线编辑的网站:
    https://stackedit.io/

  2. 代码搬运工1号
    Windows Edge
    已编辑
    7 月前
    2024-4-22 16:32:21


    P.S.用三个波浪号括住代码块即可(markdown语法)开头波浪~~~后可加语言类型,例如~~~C就是C语言。记得结尾用三波浪对应。

  3. 代码搬运工1号
    Windows Edge
    已编辑
    7 月前
    2024-4-22 16:31:02
    #include<stdio.h>
    #include<limits.h>
    
    #define LL_MAX LLONG_MAX
    //Ensure large capacity to aviod overflowing
    
    long long int func_sumOfElements(int matrix[5][4])
    {
        int i, j;
        long long int sum = 0;
        long long int product = 1;
    
        for (i = 0; i < 5; i++)
        {
            for (j = 0; j < 4; j++)
            {
                sum += matrix[i][j];
                if (product > LL_MAX / matrix[i][j])//if product larger than LL_MAX, return -1 means error
                {
                    printf("The product is going to overflow!!\n");
                    return -1;
                }
                product *= matrix[i][j];
            }
        }
        printf("The sum of all elements is %lld\n", sum);
        printf("The product of all elements is %lld\n", product);
    
        return sum + product;
    }
    
    int main()
    {
        int matrix[5][4];
        int i, j;
    
        //Enter elements into matrix
        printf("Please enter the elements of 5*4 matrix one by one!\n");
        for (i = 0; i < 5; i++);
        {
            for (j = 0; j < 4; j++)
            {
                scanf("%d", &matrix[i][j]);
            }
        }
    
        long long int total = func_sumOfElements(matrix);
    
        if (total != -1)
        {
            printf("Total of sum and product: %lld\n", total);
        }
    
        return 0;
    }
    • 博主
      代码搬运工1号
      Windows Edge
      7 月前
      2024-4-22 21:02:05

      难绷,代码的错误是for循环后面那个分号,居然有个分号,分号仙人

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
T_T
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇