技术标签: c语言 visual studio
在vs2022的编译环境中不能调用scanf函数,只能调用scanf_s函数用于输入
For语句中的间隔用分号“;”例如for(i=1;i<=n;i++)
scanf_s后边对应的数值应该取址符“&”
#include <stdio.h>
int multiply(int a,int b ) 即用户自定义函数 multiply(int a,int b)
{ 定义函数功能为 return (a*b)
return (a * b);
}
int main() 有且仅有一个main函数
{
int x, y, product; printf("please input two integers:"); scanf_s(" % d % d", &x, &y); 将转换后的数据送到变量地址列表所对应的变量中 product = multiply(x , y); 调用用户自定义函数multiply printf("The product is %d\n", product); return 0; 无返回值
}
可修改为
#include<stdio.h>
int main()
{
int x, y, product; printf("please input two integers:"); scanf_s("%d%d", &x, &y); product = x * y; printf("The product is %d\n", product); return 0;
}
#include<stdio.h>
int main()
{
int year, month, day; printf("请按标准格式输入一个日期(YYYY-MM-DD):"); scanf_s("%d-%d-%d", &year, &month, &day); printf("中国日期格式:%d年%d月%d日\n",year,month,day); printf("美国日期格式:%d/%d/%d\n", month, day, year); printf("英国日期格式:%d/%d/%d\n", day, month, year); return 0;
}
#include <stdio.h>
int main()
{
char grade1, grade2; <变量>=getchar(); grade1 = getchar(); 函数getchar用于从键盘读入一个用户输入的字符 getchar(); getchar()表示系统从输入缓冲区提取一个的字符但不赋给任何变量 grade2 = getchar(); putchar(<参数>); printf("The first grade is:");函数putchar是将给定的参数以单个字符的形式输出到显示器 putchar(grade1); 屏幕的当前位置上,其参数可以是字符常量、变量或表达式 putchar('\n'); printf("The second grade is:"); putchar(grade2); putchar('\n'); return 0;
}
#include <stdio.h>
int main()
{
const double pi = 3.14159; const <数据类型><只读变量名>; 变量初始化 double r; 用const修饰符限定只读变量 增加了程序的可读性、 scanf_s("%lf", &r); 方便了程序的维护、增强了程序的正确性并减少误操作 printf("area=%.2f\n", pi * r * r); 计算圆的面积并输出 printf("permeter=%.2f\n", 2 * pi * r); 计算圆的周长并输出 return 0;
}
#include <stdio.h>
#include <math.h> 引用数学函数 #include<stdio.h>;
int main()
{
const double pi = 3.14159; const <数据类型> <只读变量名>; const double g = 9.80; 用于定义只读变量 double v0; int theta; double R; printf("Please input v0 (m/s) and theta (degree):\n"); scanf_s("%lf%d", &v0, &theta); R = v0 * v0 * sin(2 * theta / 180.0 * pi)/g; C语言中sin函数的参数是弧度制 printf("The range is:%.2f (m)\n", R); 需要用pi将theta从角度制转换 return 0; 成弧度制。
}
#include <stdio.h>
#include <math.h>
int main()
{
int a, b; int x, y, z; int t; printf("please input a and b:"); scanf_s("%d%d", &a, &b); t = (int)sqrt(2 * a * b); printf("a=%d,b=%d\n", a, b); printf("2ab %s a perfect square number\n", t * t == 2 * a * b ? "is" : "is NOT"); x = a + t; y = b + t; z = a + b + t; printf("x=%d,y=%d,z=%d\n", x, y, z); printf("(%d,%d,%d) %s a solution of the Pythagorean Theorem equation\n", x, y, z, x * x + y * y == z * z ? "is" : "is NOT"); return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, p, s; printf("Please input three edges:"); scanf_s("%lf%lf%lf", &a, &b, &c); p = (a + b + c) / 2; s = sqrt(p * (p - a) * (p - b) * (p - c)); 调用数学函数 该例子我进行了修改 printf("s=%lf\n", s); return 0;
}
#include <stdio.h>
int main()
{
int age1, age2; printf("Enter age of two persons:"); scanf_s("%d%d", &age1, &age2); if (age1 >= age2) { printf("The older age is %d\n:", age1); } if...else语句的使用 Else { printf("The older age is %d\n:", age2); } return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, p, s; printf("Enter three edges of a triangle:"); scanf_s("%f%f%f", &a, &b, &c); if(a > 0 && b > 0 && c > 0 && a + b > c && a + c > b && b + c > a) { p = (a + b + c) / 2; s = sqrt(p * (p - a) * (p - b) * (p - c)); printf("s=%f", s); } else { printf("error input!\n"); } return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c; printf("Enter three edges of triangle:"); scanf_s("%f%f%f", &a, &b, &c); if (a <= 0 || b <= 0 || c <= 0) { printf("error input!\n"); } else { if (a + b > c && a + c > b && b + c > a) { if (fabs(a * a + b * b - c * c) < 1E-2 || fabs(a * a + c * c - b * b) < 1E-2 || fabs(b * b + c * c - a * a) < 1E-2) { printf("%f,%f,%f is a right triangle\n",a,b,c); } else { printf("%f,%f,%f is a ordinary triangle\n",a,b,c); } } else { printf("%f,%f,%f is not a triangle\n",a,b,c); }
} return 0;
}
#include <stdio.h>
int main()
{
int year, month, daysum; printf("Enter the year and the month:"); scanf_s("%d%d", &year, &month); switch (month) { case 1: case 3: switch(表达式) case 5: { case 7: case 常数表达式1: 语句系列1 case 8: case 常数表达式2: 语句系列2 case 10: ... case 12: case 常数表达式n: 语句系列n daysum = 31; } break; 首先计算switch后面表达式的值,然后与各 case 4: case分支的常量进行匹配,与哪个常量相等 case 6: 就该从该分支的语句序列开始执行,直至遇 case 9: 到break或者switch语句块的右大括号。 case 11: daysum = 30; break; case 2: if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { daysum = 29; } else { daysum = 28; } } printf("%d.%d has %d days\n", year, month, daysum); return 0;
}
#include <stdio.h>
int main()
{
int n, i, sum; printf("Enter a positive integer:"); while语句的利用 scanf_s("%d", &n); i = 1; while(表达式) sum = 0; { while (i <= n) 语句块 { { sum += i; 1.计算表达式的值。若为真,则转步骤二 i++; 否则退出循环,执行while的下一条语句 } 2.执行语句块,并返回步骤一 printf("累加和%d=%d", n, sum); return 0;
}
#include <stdio.h>
int main()
{
int n, i; double fac; printf("Enter a positive integer:"); do while语句的利用 scanf_s("%d", &n); i= 1; fac = 1; do Do { { 语句块 fac *= i; }while(表达式); i++; 1.执行语句块,即循环体 } while (i <= n); 2.计算表达式。若为真,则转步骤一; printf("%d!=%lf", n, fac); 否则否则退出循环,执行下一语句。 return 0;
}
#include <stdio.h>
int main()
{
int sign, i; double item, sum; sum=0; sign = 1; for (i = 1; i <= 1000; i++) { item = sign / (2.0*i - 1); * 2.0 重点 double型 must 以小数形式 sum += item; sign = -sign; } printf("sum=%lf", sum);
}
代码一 break的使用
#include <stdio.h>
int main()
{
int i, n; for (i = 1; i <= 5; i++) { printf("Enter n:"); scanf_s("%d", &n); if (n < 0) break; break是结束本层循环体的的运行,并退出本层循环 printf("n=%d\n", n); } printf("The end!\n"); return 0;
}
代码二 continue的用法
#include <stdio.h>
int main()
{
int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= i; j++) { printf("%d+%d=%2d ", i, j, i + j); } printf("\n"); } return 0;
}
#include <stdio.h>
int main()
{
int i, j; for (i = 1; i <= 4; i++) { for (j = 1; j <= 4 - i; j++) { printf(" "); 两个内循环的嵌套和使用 } for (j = 1; j <= 2 * i + 1; j++) { printf("*"); } printf("\n"); } return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
int n, i, k; do { printf("Enter a positive integer:"); scanf_s("%d", &n); } while (n <= 0); if (n == 1) { printf("%d is not a prime\n", n); 无语子,反复嵌套容易混乱。 } else { k = (int)sqrt(n); 使用到了(int)强制类型转换 for (i = 2; i <= k; i++) 和数学函数sqrt(n) { if (n % i == 0) { break; break的使用 } } if (i > k) { printf("%d is a prime", n); } else { printf(" % d is not a prime", n); } } return 0;
}
问公鸡、母鸡、小鸡各几只?
#include <stdio.h>
int main()
{
int a, b, c; for (a = 0; a <= 20; a++) { for (b = 0; b <= 33; b++) { c = 100 - a - b; if (15 * a + 9 * b + c == 300) { printf("%d,%d,%d\n", a, b, c); } } }return 0;
}
#include <stdio.h>
double totalCost (int n, double p);
int main()
{
double price, bill; int number; printf("Enter the number of items purchased:"); scanf_s("%d", &number); printf("Enter the price per item (RMB):"); scanf_s("%lf", &price); bill = totalCost(number, price); printf("The totalCost of the items purchased is:%.lf RMB.\n", bill); return 0;
}
double totalCost(int n, double p)
{
const double DISCOUNT = 0.2; double total; if (n > 1) total = n * p * (1 - DISCOUNT); else total = n * p; return total;
}
int judgeprime(int n)
{
int i, k; int judge = 1; if (n == 1) judge = 0; k = (int)sqrt(n); for (i = 2; judge && i <= k; i++) if (n % i == 0) judge = 0; return judge;
}
void drawline()
{
const int n = 30; int i; for (i = 1; i <= n; i++) printf("-"); printf("\n"); return;
}
#include <stdio.h>
#include <math.h>
int judgeprime(int n);
int judgeprime(int n)
{
int i, k; int judge = 1; if (n == 1) judge = 0; k = (int)sqrt(n); for (i = 2; judge && i <= k; i++) if (n % i == 0) judge = 0; return judge;
}
int main()
{
int m, prime; scanf_s("%d", &m); if (m <= 0) { printf("error input!\n"); return 0; } prime = judgeprime(m); if (prime) printf("%d is a prime!\n", m); else printf("%d is not a prime!\n", m); return 0;
}
#include <stdio.h>
void drawline();
void drawline()
{
const int n = 30; int i; for (i = 1; i <= n; i++) printf("-"); printf("\n"); return;
}
int main()
{
drawline(); printf("C is a beautiful language!\n"); drawline(); return 0;
}
#include <stdio.h>
double Fact(int n);
int main()
{
int n; double t; printf("Plese input n:\n"); scanf_s("%d", &n); if (n < 0) n = -n; t = Fact(n); printf("%d!=%lf", n, t); return 0;
}
double Fact(int n)
{
if (!n) return 1.0; return n * Fact(n - 1);
}
修改后
#include <stdio.h>
double Fact(int n);
int main()
{
int n; double t; printf("Plese input n:\n"); scanf_s("%d", &n); if (n < 0) n = -n; t = Fact(n); printf("%d!=%lf", n, t); return 0;
}
double Fact(int n)
{
if (n==0) { return 1.0; } else { return n * Fact(n - 1); }
}
-
#include <stdio.h>
void multibase(int n, int B);
void multibase(int n, int B)
{
int m; if (n) { multibase(n / B, B); m = n % B; if (m < 10) printf("%d", m); else printf("%c", m + 55); }
}
int main()
{
int n, B; do { scanf_s("%d%d", &n, &B); } while (n <= 0 || B <= 1 || B >16); printf("change result:\n"); multibase(n, B); printf("\n"); return 0;
}
#include <stdio.h>
#include <math.h>
int count;
int judgeprime(int n);
int main()
{
int i; printf("The primes between 2 to 100:\n"); for(i=2;i<100;i++) if (judgeprime(i)) { printf("%d ", i); count++; } printf("The total number of primes:%d\n", count); return 0;
}
int judgeprime(int n)
{
int i; int judge = 1; if (n == 1) judge = 0; { int k = (int)sqrt(n); for (i = 2; judge && i <= k; i++) if (n % i == 0) judge = 0; } return judge;
}
#include <stdio.h>
int fun(int n);
int fun(int n)
{
static int f = 1; f = f * n; return f;
}
int main()
{
int i; for (i = 1; i <= 5; i++) { printf("%d!=%d ", i, fun(i)); }return 0;
}
#include <stdio.h>
#include <math.h>
#define pi 3.14159
double getarea(double r);
double getvolumes(double r);
double getvolumec(double r, double h);
int main()
{
double r, h, s, v1, v2; printf("请输入所需半径 :"); scanf_s("%lf", &r); printf("请输入所需高:"); scanf_s("%lf", &h); s = getarea(r); v1 = getvolumes(r); v2 = getvolumec(r, h); printf("s=%lf\n", s); printf("v1=%lf\n", v1); printf("v2=%lf\n", v2); return 0;
}
double getarea(double r)
{
return (pi * pow(r, 2));
}
double getvolumes(double r)
{
return (pi * pow(r, 3) * 3 / 4);
}
double getvolumec(double r, double h)
{
return (pi * h * pow(r, 2));
}
#include <stdio.h>
int main()
{
float score[50] = { 0 }; int num; float sum = 0, average; int i; do { printf("Input the number of students:"); scanf_s("%d", &num); } while (num <= 0 || num > 50); printf("Input the score :"); for (i = 0; i < num; i++) { scanf_s("%f", &score[i]); sum += score[i]; } average = sum / num; printf("The average is :%5.2f\n", average); return 0;
}
例5.2 Fibonacci数列。有一对小兔子(一公一母),第2个月长大成大兔子,长到第3个月开始每个月就生一对小兔子(一公一母)。等这对兔子长到第3个月又开始生小兔子。假设所有的兔子都不会死,求出n个月后兔子的数目。
|时间|小兔子|大兔子|兔子总数|
|--|--|--|--|
|1 | 1 | 0 | 1 |
|2 | 0 | 1 | 1 |
|3 | 1 | 1 | 2 |
|4 | 1 | 2 | 3 |
|5 | 2 | 3 | 5 |
|..| .. | .. | ..|
| 8| 8 | 13 | 21 |
|..| .. | .. | ..|
功能为 return (a*b)
return (a * b);
}
int main() 有且仅有一个main函数
{
int x, y, product; printf("please input two integers:"); scanf_s(" % d % d", &x, &y); 将转换后的数据送到变量地址列表所对应的变量中 product = multiply(x , y); 调用用户自定义函数multiply printf("The product is %d\n", product); return 0; 无返回值
}
可修改为
#include<stdio.h>
int main()
{
int x, y, product; printf("please input two integers:"); scanf_s("%d%d", &x, &y); product = x * y; printf("The product is %d\n", product); return 0;
}
文章浏览阅读965次,点赞18次,收藏21次。现在我们有了 2021 年和 2023 年的 NDVI 数据帧,我们需要从 2021 年的值中减去 2023 年的值以捕获 NDVI 的差异。该数据集包括像素级别的植被值,我们将编写一个自定义函数来根据红色和绿色波段的表面反射率计算 NDVI。在我的上一篇文章中,我演示了如何将单个多边形分割/镶嵌为一组大小均匀的六边形。现在我们有了植被损失数据,让我们使用 Kepler.gl 可视化每个六边形的植被损失。将地图保存为 HTML 文件,在浏览器中打开 HTML 以获得更好的视图。现在我们将调用该函数并使用、
文章浏览阅读3.3k次,点赞6次,收藏5次。正态分布,又称高斯分布或钟形曲线,是统计学中最为重要和常用的分布之一。_echarts正态分布图
文章浏览阅读217次。首先要在Mainfest.xml中加入所需要的权限:[html] view plain copyprint?uses-permission android:name="android.permission.SEND_SMS"/> uses-permission android:name="android.permission.READ_SMS"/> _android bundle.get("pdus");
文章浏览阅读2.6k次。0、说明最近在学习 Data Assimilation Research Testbed (DART) 相关内容,其软件是在 Unix/Linux 操作系统下编译和运行的 ,由于我的电脑是 Windows 10 的,DART 推荐可以使用 Windows Subsystem For Linux (WSL) 来创建一个 Windows 下的 Linux 子系统。以下的内容主要介绍如何安装 WSL2,以及 WSL2 的联网。1、如何在 Windows 10 下安装WSL具体的安装流程可以在 microso_wsl2 联网
文章浏览阅读1k次。DB_LINK 介绍在本机数据库orcl上创建了一个prod_link的publicdblink(使用远程主机的scott用户连接),则用sqlplus连接到本机数据库,执行select * from scott.emp@prod_link即可以将远程数据库上的scott用户下的emp表中的数据获取到。也可以在本地建一个同义词来指向scott.emp@prod_link,这样取值就方便多了..._添加 database link重复的数据库链接命
文章浏览阅读3.1k次。ylbtech-云-腾讯云-实时音视频:实时音视频(TRTC)支持跨终端、全平台之间互通,从零开始快速搭建实时音视频通信平台1.返回顶部 1、腾讯实时音视频(Tencent Real-Time Communication,TRTC)拥有QQ十几年来在音视频技术上的积累,致力于帮助企业快速搭建低成本、高品质音视频通讯能力的完整解决方案。..._腾讯实时音视频 分享链接
文章浏览阅读534次,点赞10次,收藏8次。编写一个完整的日历表需要处理许多细节,包括公历和农历之间的转换、节气、闰年等。运行程序后,会输出指定年份的日历表。注意,这个程序只是一个简单的示例,还有很多可以改进和扩展的地方,例如添加节气、节日等。_农历库c语言
文章浏览阅读1w次,点赞28次,收藏27次。FL Studio21.1.1.3750中文破解版是最优秀、最繁荣的数字音频工作站 (DAW) 之一,日新月异。它是一款录音机和编辑器,可让您不惜一切代价制作精美的音乐作品并保存精彩的活动画廊。为方便用户,FL Studio 21提供三种不同的版本——Fruity 版、Producer 版和签名版。所有这些版本都是独一无二的,同样具有竞争力。用户可以根据自己的需要选择其中任何一种。FL Studio21.1.1.3750中文版可以说是一站式综合音乐制作单位,可以让您录制、作曲、混音和编辑音乐。_fl studio 21 注册机
文章浏览阅读1.3k次。冯诺依曼计算机工作原理冯 诺依曼计算机工作原理的核心是 和 程序控制世界上不同型号的计算机,就其工作原理而言,一般都是认为冯 诺依曼提出了什么原理冯 诺依曼原理中,计算机硬件系统由那五大部分组成的 急急急急急急急急急急急急急急急急急急急急急急冯诺依曼结构计算机工作原理的核心冯诺依曼结构和现代计算机结构模型 转载重学计算机组成原理 一 冯 诺依曼体系结构从冯.诺依曼的存储程序工作原理及计算机的组成来..._简述冯诺依曼计算机结构及工作原理
文章浏览阅读559次。这次在随机乱下的基础上加上了一些简单的处理,如进营、炸棋、吃子等功能,在和敌方棋子产生碰撞之后会获取敌方棋子大小的一些信息,目前采用的是事件驱动模型,当下完一步棋界面返回结果后会判断是否触发了相关事件,有事件发生则处理相关事件,没有事件发生则仍然是随机下棋。1.事件驱动模型首先定义一个各种事件的枚举变量,目前的事件有工兵吃子,摸暗棋,进营,明确吃子,炸棋。定义如下:enum MoveE..._军棋引擎
文章浏览阅读85次。1, 模板观念与函数模板简单模板: template< typename T > T Function( T a, T b) {… }类模板: template struct Object{……….}; 函数模板 template< class T> inline T Function( T a, T b){……} 不可以使用不同型别的..._geekband 讲义
文章浏览阅读158次。"^\d+$" //非负整数(正整数 + 0)"^[0-9]*[1-9][0-9]*$" //正整数"^((-\d+)|(0+))$" //非正整数(负整数 + 0)"^-[0-9]*[1-9][0-9]*$" //负整数"^-?\d+$" //整数"^\d+(\.\d+)?$" //非负浮点数(正浮点数 + 0)"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0..._vb.net 正则表达式 取html中的herf