源自下面网站的若干翻译
源自www.programiz.com
1 C语言的特色和优势
- 程序语言 -C程序中的指令将逐步执行。
- 可移植 -您可以将C程序从一个平台移动到另一个平台,并在不进行任何更改或进行最少更改的情况下运行它。
- 速度 -C编程比Java,Python等大多数编程语言要快。
- 通用 -C编程可用于开发操作系统,嵌入式系统,数据库等。
2 为什么要学习C编程?
- C帮助您了解计算机的内部体系结构,计算机如何存储和检索信息。
- 学习C之后,学习其他编程语言(如Java,Python等)会容易得多。
- 从事开源项目的机会。一些最大的开源项目,例如Linux内核,Python解释器,SQLite数据库等,都是用C编程编写的。
3 如何学习C编程?
- Programiz的C教程 -我们提供逐步的C教程,示例和参考。开始使用C。
- 官方C文档 -对于初学者可能很难理解。请访问官方的C编程文档。
- 编写大量的C编程代码 -学习编程的唯一方法是编写大量的代码。
4 仪式感-必须写的程序
#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
- 这是一个预处理程序命令,它告诉编译器在程序中包含(标准输入和输出)文件的内容。
#includestdio.h
- 该
文件包含诸如stdio.h
和的功能,scanf()
分别用于输入和显示输出。printf()
- 如果不写
就写printf() 则程序将无法编译。#include <stdio.h>
- C
函数的执行从函数开始。main()
-
是一种库函数,用于将格式化的输出发送到屏幕。在此程序中,printf()
显示你好,世界! 屏幕上的文字。printf()
- 该
语句是程序的 “退出状态” 。简单来说,程序以该语句结束。return 0;
5 丰富的开源代码和用户环境
5.1 最短路径算法实现
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("nEnter the adjacency matrix:n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
//initialize pred[],distance[] and visited[]
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
//nextnode gives the node at minimum distance
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
//check if a better path exists through nextnode
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
//print the path and distance of each node
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("nDistance of node%d=%d",i,distance[i]);
printf("nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
https://www.thecrazyprogrammer.com/2014/03/dijkstra-algorithm-for-finding-shortest-path-of-a-graph.htmlwww.thecrazyprogrammer.com