技术标签: 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;
}
文章浏览阅读2w次,点赞7次,收藏51次。四个步骤1.创建C++ Win32项目动态库dll 2.在Win32项目动态库中添加 外部依赖项 lib头文件和lib库3.导出C接口4.c#调用c++动态库开始你的表演...①创建一个空白的解决方案,在解决方案中添加 Visual C++ , Win32 项目空白解决方案的创建:添加Visual C++ , Win32 项目这......_c#调用lib
文章浏览阅读4.6k次。苹方字体是苹果系统上的黑体,挺好看的。注重颜值的网站都会使用,例如知乎:font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, W..._ubuntu pingfang
文章浏览阅读159次。表单表单概述表单标签表单域按钮控件demo表单标签表单标签基本语法结构<form action="处理数据程序的url地址“ method=”get|post“ name="表单名称”></form><!--action,当提交表单时,向何处发送表单中的数据,地址可以是相对地址也可以是绝对地址--><!--method将表单中的数据传送给服务器处理,get方式直接显示在url地址中,数据可以被缓存,且长度有限制;而post方式数据隐藏传输,_html表单的处理程序有那些
文章浏览阅读1.2k次。使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)https://github.com/PHPGangsta/GoogleAuthenticatorPHP代码示例://引入谷_php otp 验证器
文章浏览阅读4.3k次,点赞5次,收藏11次。matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距
文章浏览阅读2.2k次。①Storage driver 处理各镜像层及容器层的处理细节,实现了多层数据的堆叠,为用户 提供了多层数据合并后的统一视图②所有 Storage driver 都使用可堆叠图像层和写时复制(CoW)策略③docker info 命令可查看当系统上的 storage driver主要用于测试目的,不建议用于生成环境。_docker 保存容器
文章浏览阅读834次,点赞27次,收藏13次。网络拓扑结构是指计算机网络中各组件(如计算机、服务器、打印机、路由器、交换机等设备)及其连接线路在物理布局或逻辑构型上的排列形式。这种布局不仅描述了设备间的实际物理连接方式,也决定了数据在网络中流动的路径和方式。不同的网络拓扑结构影响着网络的性能、可靠性、可扩展性及管理维护的难易程度。_网络拓扑csdn
文章浏览阅读1.8k次,点赞5次,收藏8次。IOS系统Date的坑要创建一个指定时间的new Date对象时,通常的做法是:new Date("2020-09-21 11:11:00")这行代码在 PC 端和安卓端都是正常的,而在 iOS 端则会提示 Invalid Date 无效日期。在IOS年月日中间的横岗许换成斜杠,也就是new Date("2020/09/21 11:11:00")通常为了兼容IOS的这个坑,需要做一些额外的特殊处理,笔者在开发的时候经常会忘了兼容IOS系统。所以就想试着重写Date函数,一劳永逸,避免每次ne_date.prototype 将所有 ios
文章浏览阅读5.3k次。方法一:用PLSQL Developer工具。 1 在PLSQL Developer的sql window里输入select * from test for update; 2 按F8执行 3 打开锁, 再按一下加号. 鼠标点到第一列的列头,使全列成选中状态,然后粘贴,最后commit提交即可。(前提..._excel导入pl/sql
文章浏览阅读83次。Git常用命令速查手册1、初始化仓库git init2、将文件添加到仓库git add 文件名 # 将工作区的某个文件添加到暂存区 git add -u # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,不处理untracked的文件git add -A # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,包括untracked的文件...
文章浏览阅读202次。分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120
文章浏览阅读1.8k次。版权声明:转载请注明出处 http://blog.csdn.net/irean_lau。目录(?)[+]1、缺省构造函数。2、缺省拷贝构造函数。3、 缺省析构函数。4、缺省赋值运算符。5、缺省取址运算符。6、 缺省取址运算符 const。[cpp] view plain copy_空类默认产生哪些类成员函数