免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 发帖
本帖最后由 aaok 于 2020-4-8 17:57 编辑

先写个hello world吧【所有程序员要写的第一个程序】
首先新建一个文件,打开左上角的文件->新建->源代码,然后依次输入以下内容:
  1. #include<stdio.h>
  2. int main(void){
  3. printf("Hello World!");
  4. }
复制代码

让我来依次解释下什么意思吧:
首先,#include代表你要引用一个函数库。函数库又是什么意思?你把它理解成你打游戏的时候的武器库好了,你编程的时候要用什么函数的话只要你引用的库包含了这个函数就可以直接用。其中stdio.h这个库是最为特殊的,最基本的那些函数【比如用以录入你输入内容的scanf函数、用以输出你想输出内容的printf函数】都在这个库里面,所以是一定要引用的。
然后这个int main又是什么意思呢,这就是你的主函数,你们可以理解为这个程序的主干。许多简单的程序往往都只需要一个主函数就足够了。你任何通过屏幕的输入与反映到屏幕的输出都只能通过主函数运行,主函数之外的副函数只能从主函数或者其他副函数那儿取值,并把得到的结果返回给主函数或者另一个副函数。
副函数这个得很后期再讲了,大家暂时把它理解为你在武器库里面没找到趁手的武器,于是现场临时自己打造的一把武器好了。
printf这个函数的作用就是把后面括号里面引号里面的东西给输出出来。
那么程序是如何运行的?大家请看屏幕上方这三个按钮:


它们的意思分别是编译、运行、编译及运行。任何一个编好的代码通过编译就可以成为一个可以运行的程序,而运行后会弹出来一个小黑框,这就是你所编写的程序运行的效果了。
附件: 您需要登录才可以下载或查看附件。没有帐号?注册
敏而不疑,缜而不繁;娇而不横,求而不奢;诤而有益,谋而有断;处危有度,历难有瞻;其节如松,其韧若竹。

TOP

过来学习
不负溪山债

TOP

努力学习..
一起去啊。更远的地方。
没事别在那给我装深沉.

Slytherin will help you on the way to greatness.

TOP

果然学习类型的帖子会很受欢迎的吧。所有的付出都会有回报的。加油呢!
前途似海,来日方长。

TOP

今天再说下scanf函数的使用吧【还是在stdio.h库里面的函数】
首先必须得和大家说,你要创造任意一个数据,都得表明他的类型。就像数学上说的设x,计算机里面得说这个x是个整数、小数,整数是不是特别大的那种,小数又要精确到多少位?一般而言,我们以你要设的数字对应的数据类型加上你要设的数字代表字符,例如:
  1. int a;
复制代码

这个的意思就是,我设了一个未知数a,这个a大小还没确定,但是它是一个整数。int代表的正是整数类型,此外还有代表小数的float,代表高精度小数的double,代表较大整数的long int,代表巨大整数的long long int,还有代表字符的char【字符这个后面专门讲】,还有诸如布尔类型一类的字符我就不一一列举了。
那么说了这么半天,scanf有什么用,又怎么用?scanf能识别你输入的数据并将之储存在你预先设好的未知数中。比如:
  1. scanf("%d",&a);
复制代码


这个括号里面的前半段代表的是你寻找并输入的数据是一个整数【%d代表的就是一个整数,小数是%f,高精度的小数是%lf,大整数是%ld,更大的整数是%lld,字符是%c】,后面的&则是找到a在电脑里对应的地址并把这个值给放了进去。
下面写一个简单的示范,大家可以看一下我怎么实现两个整数相加的:
  1. int main(void){
  2. int a,b,c;
  3. scanf("%d%d",&a,&b);
  4. c=a+b;
  5. printf("The result is %d",c);
  6. }
复制代码

一旦printf里面出现了%之类代表未知数的东西,后面就要补上这个未知数的说明表示你想输出哪个变量。比如这儿我想用%d表达c这个变量,于是在引号完了之后我要加一个c来表明我想让%d对应c。
敏而不疑,缜而不繁;娇而不横,求而不奢;诤而有益,谋而有断;处危有度,历难有瞻;其节如松,其韧若竹。

TOP

以及谢谢楼上几位的支持~
敏而不疑,缜而不繁;娇而不横,求而不奢;诤而有益,谋而有断;处危有度,历难有瞻;其节如松,其韧若竹。

TOP

发一个我不久前学的叫链表的东西【新人劝退向】【你们暂时学不到这么深……真学到这儿……也就习惯了ovo】【就是给你们看下我们近期编的都是些什么】
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node{
  4.     int data;
  5.     struct node *nextPtr;
  6. };
  7. int dataget(struct node*headPtr,int n){
  8.         int i;
  9.         for(i=0;i<n;i++){
  10.                 headPtr=headPtr->nextPtr;
  11.         }
  12.         return headPtr->data;
  13. }
  14. int main(void){
  15.         struct node* headPtr1=NULL;
  16.         struct node* headPtr2=NULL;
  17.         struct node* headPtr3=NULL;
  18.         struct node* headPtr4=NULL;
  19.         struct node* lastPtr=NULL;
  20.         struct node* currentPtr=NULL;
  21.         struct node* lastPtr0=NULL;
  22.         struct node* currentPtr0=NULL;
  23.         int temp;
  24.         int flag1=0,flag1A=0,flag1B=0,flag2=0;
  25.         int i1=0,i2=0;
  26.         int I1=0,I2=0;
  27.         int F1=0,F2=0;
  28.         int n1=0;
  29.         scanf("%d",&temp);
  30.         if(temp==-1){
  31.                 flag1A++;
  32.         }
  33.     if(flag1A==0){
  34.             while(temp!=-1){
  35.             currentPtr=(struct node*)malloc(sizeof(struct node));
  36.             if(currentPtr!=NULL){
  37.                     currentPtr->data=temp;
  38.                 }
  39.                 if(headPtr1==NULL){
  40.                         headPtr1=currentPtr;
  41.                         lastPtr=currentPtr;
  42.                 }
  43.                 else{
  44.                         lastPtr->nextPtr=currentPtr;
  45.                         lastPtr=currentPtr;
  46.                 }
  47.                 scanf("%d",&temp);
  48.                 n1++;
  49.         }
  50.         lastPtr->nextPtr=NULL;
  51.         }
  52.         int n2=0;
  53.         scanf("%d",&temp);
  54.         if(temp==-1){
  55.                 flag1B++;
  56.         }
  57.     if(flag1B==0){
  58.             while(temp!=-1){
  59.             currentPtr=(struct node*)malloc(sizeof(struct node));
  60.             if(currentPtr!=NULL){
  61.                     currentPtr->data=temp;
  62.                 }
  63.                 if(headPtr2==NULL){
  64.                         headPtr2=currentPtr;
  65.                         lastPtr=currentPtr;
  66.                 }
  67.                 else{
  68.                         lastPtr->nextPtr=currentPtr;
  69.                         lastPtr=currentPtr;
  70.                 }
  71.                 scanf("%d",&temp);
  72.                 n2++;
  73.         }
  74.         lastPtr->nextPtr=NULL;
  75.         }
  76.         if(flag1A!=0&&flag1B!=0){
  77.                 printf("There is no item in A list.\nThere is no item in B list.");
  78.                 flag1++;
  79.         }
  80.         if(flag1==0){
  81.                 if(flag1A==0&&flag1B==0){
  82.                                         while(F1==0||F2==0){
  83.                         if(F1!=0||F2!=0){
  84.                                 if(F1!=0){
  85.                                 currentPtr=(struct node*)malloc(sizeof(struct node));
  86.                             if(currentPtr!=NULL){
  87.                             currentPtr->data=dataget(headPtr2,i2);
  88.                                 }
  89.                                 if(headPtr3==NULL){
  90.                                 headPtr3=currentPtr;
  91.                                 lastPtr=currentPtr;
  92.                                 }
  93.                                 else{
  94.                                 lastPtr->nextPtr=currentPtr;
  95.                                 lastPtr=currentPtr;
  96.                                 }
  97.                                 i2++;
  98.                                 if(i2==n2)
  99.                                 F2++;
  100.                                 }
  101.                                 else{
  102.                                 currentPtr=(struct node*)malloc(sizeof(struct node));
  103.                             if(currentPtr!=NULL){
  104.                             currentPtr->data=dataget(headPtr1,i1);
  105.                                 }
  106.                                 if(headPtr3==NULL){
  107.                                 headPtr3=currentPtr;
  108.                                 lastPtr=currentPtr;
  109.                                 }
  110.                                 else{
  111.                                 lastPtr->nextPtr=currentPtr;
  112.                                 lastPtr=currentPtr;
  113.                                 }
  114.                                 i1++;
  115.                                 if(i1==n1)
  116.                                 F1++;
  117.                                 }
  118.                         }
  119.                         else{
  120.                                 if(dataget(headPtr1,i1)<dataget(headPtr2,i2)){
  121.                                 currentPtr=(struct node*)malloc(sizeof(struct node));
  122.                             if(currentPtr!=NULL){
  123.                             currentPtr->data=dataget(headPtr1,i1);
  124.                                 }
  125.                                 if(headPtr3==NULL){
  126.                                 headPtr3=currentPtr;
  127.                                 lastPtr=currentPtr;
  128.                                 }
  129.                                 else{
  130.                                 lastPtr->nextPtr=currentPtr;
  131.                                 lastPtr=currentPtr;
  132.                                 }
  133.                                 i1++;
  134.                                 if(i1==n1)
  135.                                 F1++;
  136.                         }
  137.                         else if(dataget(headPtr1,i1)>dataget(headPtr2,i2)){
  138.                                 currentPtr=(struct node*)malloc(sizeof(struct node));
  139.                             if(currentPtr!=NULL){
  140.                             currentPtr->data=dataget(headPtr2,i2);
  141.                                 }
  142.                                 if(headPtr3==NULL){
  143.                                 headPtr3=currentPtr;
  144.                                 lastPtr=currentPtr;
  145.                                 }
  146.                                 else{
  147.                                 lastPtr->nextPtr=currentPtr;
  148.                                 lastPtr=currentPtr;
  149.                                 }
  150.                                 i2++;
  151.                                 if(i2==n2)
  152.                                 F2++;
  153.                         }
  154.                         else{
  155.                                 currentPtr=(struct node*)malloc(sizeof(struct node));
  156.                             if(currentPtr!=NULL){
  157.                             currentPtr->data=dataget(headPtr1,i1);
  158.                                 }
  159.                                 if(headPtr3==NULL){
  160.                                 headPtr3=currentPtr;
  161.                                 lastPtr=currentPtr;
  162.                                 }
  163.                                 else{
  164.                                 lastPtr->nextPtr=currentPtr;
  165.                                 lastPtr=currentPtr;
  166.                                 }
  167.                                 currentPtr0=(struct node*)malloc(sizeof(struct node));
  168.                             if(currentPtr0!=NULL){
  169.                             currentPtr0->data=dataget(headPtr1,i1);
  170.                                 }
  171.                                 if(headPtr4==NULL){
  172.                                 headPtr4=currentPtr0;
  173.                                 lastPtr0=currentPtr0;
  174.                                 }
  175.                                 else{
  176.                                 lastPtr0->nextPtr=currentPtr0;
  177.                                 lastPtr0=currentPtr0;
  178.                                 }
  179.                                 i1++;
  180.                                 i2++;
  181.                                 flag2++;
  182.                                 I2++;
  183.                                 if(i1==n1)
  184.                                 F1++;
  185.                                 if(i2==n2)
  186.                                 F2++;
  187.                         }
  188.                         }
  189.                         I1++;
  190.                 }
  191.                 }
  192.                 if(flag1A!=0){
  193.                                 while(i2<n2){
  194.                                 currentPtr=(struct node*)malloc(sizeof(struct node));
  195.                             if(currentPtr!=NULL){
  196.                             currentPtr->data=dataget(headPtr2,i2);
  197.                                 }
  198.                                 if(headPtr3==NULL){
  199.                                 headPtr3=currentPtr;
  200.                                 lastPtr=currentPtr;
  201.                                 }
  202.                                 else{
  203.                                 lastPtr->nextPtr=currentPtr;
  204.                                 lastPtr=currentPtr;
  205.                                 }
  206.                                 i2++;
  207.                                 I1++;
  208.                                 }
  209.                 }
  210.                 if(flag1B!=0){
  211.                                 while(i1<n1){
  212.                                 currentPtr=(struct node*)malloc(sizeof(struct node));
  213.                             if(currentPtr!=NULL){
  214.                             currentPtr->data=dataget(headPtr1,i1);
  215.                                 }
  216.                                 if(headPtr3==NULL){
  217.                                 headPtr3=currentPtr;
  218.                                 lastPtr=currentPtr;
  219.                                 }
  220.                                 else{
  221.                                 lastPtr->nextPtr=currentPtr;
  222.                                 lastPtr=currentPtr;
  223.                                 }
  224.                                 i1++;
  225.                                 I1++;
  226.                                 }
  227.                 }
  228.                 int Flag1=0,Flag2=0;
  229.                 printf("The new list A:");
  230.                 for(i1=0;i1<I1;i1++){
  231.                         if(Flag1==0){
  232.                                 printf("%d",dataget(headPtr3,i1));
  233.                                 Flag1++;
  234.                         }
  235.                         else{
  236.                                 printf(" %d",dataget(headPtr3,i1));
  237.                         }
  238.                 }
  239.                 printf("\n");
  240.                 if(flag2==0){
  241.                         printf("There is no item in B list.");
  242.                 }
  243.                 else{
  244.                         printf("The new list B:");
  245.                         for(i2=0;i2<I2;i2++){
  246.                         if(Flag2==0){
  247.                                 printf("%d",dataget(headPtr4,i2));
  248.                                 Flag2++;
  249.                         }
  250.                         else{
  251.                                 printf(" %d",dataget(headPtr4,i2));
  252.                         }
  253.                 }
  254.                 }
  255. }
  256. }
复制代码
敏而不疑,缜而不繁;娇而不横,求而不奢;诤而有益,谋而有断;处危有度,历难有瞻;其节如松,其韧若竹。

TOP

索性把字符变量和几种结构一起讲了吧。
字符变量,顾名思义就是这个变量对应的是一个字符,定义用char表示,scanf中用%c表示。每个字符都有一个与之对应的数字,也就是说,你可以对字符进行数字运算,字符的数字运算其实是它背后的数字进行数字运算,得到的结果再转化为数字对应的字符罢了。具体哪个数字对应什么字符可以参考ASC码表。
至于结构,最常见的应该是if……else类型,写出来是这样的:
  1. if(){
  2. }
  3. else{
  4. }
复制代码

if后面的那个括号里面是判断的条件,一旦满足,就执行第一个大括号里面的语句,否则执行下一个括号里面的。例如:
  1. if(a>b){
  2. c=a;
  3. }
  4. else{
  5. c=b;
  6. }
复制代码

这段代码就能比较a、b的大小并把其中较大的那个赋值给c。
当然,有了if……else……,自然也有if……else if……else这样的存在,具体而言,就是先判断第一个if括号后面的内容,满足条件的话执行第一个大括号里面的内容,不满足的话接着判断下一个else if后面括号的内容,满足的话执行第二个大括号里面的内容,不满足的话执行第三个。这个else if自然也能写好几个后再接一个else,这就不赘述了。
还有一种选择执行的结构,叫做switch结构,具体而言长这样:
  1. switch(){
  2. case :   ;break;
  3. case :   ;break;
  4. ……
  5. default:   ;break;
  6. }
复制代码

switch后面跟的是一个变量的名字,case后面跟的是这个变量的值,一旦满足这个值,就执行case后面的语句,如果都不满足,那么就执行default后面的语句。给大家举个例子:
  1. switch(t){
  2. case 0:c=a+b;break;
  3. case 1:c=a-b;break;
  4. default:c=0;break;
  5. }
复制代码

如果变量t等于0,那么c为a、b二者之和,如果t等于1,c为二者之差,如果t既不为0也不为1,那么c被赋值为0.
接下来给大家讲一下循环结构,循环结构就是如果没有满足某个条件,那么大括号里面的东西就会不断地循环,直到条件满足为止,最常见的循环结构有while结构和for结构。
while结构大概长这样:
  1. while(){

  2. }
复制代码

括号里面是能循环下去的条件,每次循环完毕后进行一次判断,一旦不满足这个条件,即刻结束循环。例如:
  1. while(a!=3){
  2. a++;
  3. }
复制代码

“!=”就是不等于的意思【与此对应的等于表达式是“==”,这里要注意不要写成赋值用的“=”了】,a++是a=a+1的缩写【与此同时a--也可以表示a=a-1】,这个代码能让a不断+1,直到a=3为止。
for循环结构则长这个模样:
  1. for( ; ; ){

  2. }
复制代码

for结构后面的括号里有两个分号,第一个分号前的内容是你在for循环开始前进行的一系列赋值,第一个分号和第二个分号之前则是循环能够维持的条件,第二个分号之后则是每执行一次大括号内容之后再执行的内容,也就是说,一个这样的for循环:
  1. for(代码一;代码二;代码三){
  2. 代码四
  3. }
复制代码

可以改写成这样的while循环:
  1. 代码一
  2. while(代码二){
  3. 代码四;
  4. 代码三;
  5. }
复制代码

具体有这个例子:
  1. for(i=0;i<=5;i++){
  2. a=a+b;
  3. }
复制代码

也就是说我一开始让变量i等于0,只要i还小于等于5,a每次就增加一个b,i每次也增加一个1。i这样的变量在循环结构中非常常见,它可以有效地控制循环的次数来达到程序的目的。
敏而不疑,缜而不繁;娇而不横,求而不奢;诤而有益,谋而有断;处危有度,历难有瞻;其节如松,其韧若竹。

TOP

大家可以去这里面先训练一下:https://www.luogu.com.cn/training/list
【话说你们现在学得咋样啊……能不能给个反馈什么的】
【溜去做大作业了】
敏而不疑,缜而不繁;娇而不横,求而不奢;诤而有益,谋而有断;处危有度,历难有瞻;其节如松,其韧若竹。

TOP

特别不靠谱的反馈(?不存在的)来了:我选择慢慢理解留到明年好好琢磨一下........(捂脸)我只是想打破尴尬不过好像变得更尴尬了/////
如果你害羞 你可以先把我灌醉 然后再说爱我

TOP

返回列表