当前位置: 首页 > news >正文

上海装饰公司网站建设国际财经新闻

上海装饰公司网站建设,国际财经新闻,网页制作与设计的内容,石油网站编辑怎么做Format格式化 %1s或者%2s,%3s:取字符串的前1,2或者3位。%*c:屏蔽一个字符。%[A-Z]:取一个A到Z的值。 %[^a-z]:不取a到z的值。 %[^\n]:取非换行之前的值。printf("%5d", a):左边补 格式化:有正则在其中。 int main() {printf("%5d\n&quo…

 Format格式化

%1s或者%2s,%3s:取字符串的前1,2或者3位。%*c:屏蔽一个字符。%[A-Z]:取一个A到Z的值。
%[^a-z]:不取a到z的值。
%[^\n]:取非换行之前的值。printf("%5d", a):左边补

 格式化:有正则在其中。

int main()
{printf("===%5d===\n", 239);printf("===%05d===\n", 239);printf("===%0-5d===\n", 239);printf("===%-5d===\n", 239);printf("===%.5d===\n", 239);printf("===%d\t===\n", 239);printf("===%u===\n", 239);printf("===%o===\n", 239);getchar();return 0;
}

输出: 

===  239===
===00239===
===239  ===
===239  ===
===00239===
===239  ===
===239===
===357===


字符串的标准输入和输出

int scanf(const char* restrict format, ...)

int a, b;
scanf("%d %d", &a, &b)

int printf(const char* format, ...);

int a = 10;
int b = 20;
char* str = "asdfgh";
printf("a = %d, b = %d, str = %s", a, b, str)

sprintf(char* str, const char* format, ...):字符串拼接数值等不同类型。

  • 功能:根据参数 format 字符串来转换并格式化数据,然后将结果输出到str指定的空间中,直到出现字符串结束符'\0'为止。
  • 参数:
    • str:输出字符串首地址。
    • format:字符串格式,用法和printf()一样。
  • 返回值:
    • 成功:
    • 失败:-1
int a, b, value;
char c;
char str[20] = {0};
sprintf(str, "%d %c %d = %d", a, c, b, value);

sscanf(const char *str, const char* format, ...):把值从字符串中提取出来。

  • 功能:从str指定的字符串读取数据,并根据参数format字符串来转换并格式化数据。
  • 参数:
    • str:指定的字符串首地址。
    • format:字符串格式,用法和scanf()一样。
  • 返回值:
    • 成功:参数数目,成功转换的值的个数。
    • 失败:-1
char src[] = "a = 10, b = 20";
int a;
int b;
sscanf(src, "a = %d, b = %d", &a, &b);
printf("a:%d, b:%d\n", a, b);


格式化文件读写

int fprintf(FILE* stream, const char* format, ...);文件。

  • 功能:根据参数format字符串来转换并格式化数据,然后将结果输出到stream指定的文件中,指定出现字符串结束符'\0'为止。
  • 参数:
    • stream:已经打开的文件
    • format:字符串格式,用法和printf()一样
  • 返回值:
    • 成功:
    • 失败:-1
int a = 10;
int b = 20;
fprintf(fp, "%d + %d = %d", a, b, a + b);

int fscanf(FILE* stream, const char* format, ...);文件。

  • 功能:从stream指定的文件读取字符串,并根据参数format字符串来转换并格式化数据
  • 参数:
    • stream:已经打开的文件。
    • format:字符串格式,用法和scanf()一样。
  • 返回值:
    • 成功:参数数目,成功转换的值的个数。
    • 失败:-1。
int a, b, c;
fscanf(fp, "%d %d %d\n", &a, &b, &c);
printf("a = %d, b = %d, c = %d\n", a, b, c);


11+22=
16-12=
17*18=
96/6=
32+47=
56/7=
89*2=
33-10=
32/8=
99/10=

 自己初次编写:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>char ArithmeticOperations[4] = { '+','-', '*', '/' };char GetOperater(char* p)
{for (int i = 0; i < 4; i++){char* op = strchr(p, ArithmeticOperations[i]);if (op != NULL){return *op;break;}}
}int GetResult(int num1, int num2, char op)
{int result = 0;switch (op){case '+':result = num1 + num2;break;case '-':result = num1 - num2;break;case '*':result = num1 * num2;break;case '/':result = num1 / num2;break;default:break;}return result;
}
int main()
{FILE *fpr = fopen("F:/Practice/homework", "r");if (!fpr){printf("文件读取失败。");return -1;}char buf[20] = { 0 };char* str[10];for (int i = 0; i < 10; i++){str[i] = (char*)malloc(sizeof(char) * 20);}int i = 0;while (!feof(fpr)){memset(buf, 0, sizeof(buf));  // 防止前一次的赋值,污染本次赋值。char* p = fgets(buf, sizeof(buf), fpr);if (p != NULL && str[i] != NULL){//printf("读取结果:%s\n", p);memcpy(str[i], p, 20);i++;}}fclose(fpr);int LineNum = i;FILE* fpw = fopen("F:/Practice/homework", "w");if (fpw == NULL){perror("fpw open.");return -1;}i = 0;//for (i = 0; i < LineNum; i++)//{//	printf("%s", str[i]);//}while (i<LineNum){char op = GetOperater(str[i]);int num1 = atoi(str[i]);   // 此处提取符号和数字可以通过sscanf来优化。格式化字符串。int num2 = atoi(strchr(str[i], op)+1);   // 指针加1。int result = GetResult(num1, num2, op);char str2[30] = { 0 };sprintf(str2, "%d%c%d%c%d\n", num1, op, num2, '=', result);printf("%s\n", str2);int a = fputs(str2, fpw);if (!a){i++;}}for (int i = 0; i < 10; i++){free(str[i]);  // 指针释放,需要添加,str[i] = NULL;}fclose(fpw);getchar();return 0;
}

二次改写

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main()
{FILE* fpr = fopen("F:/Practice/a.txt", "r");if (!fpr){return -1;}char** buf = (char**)malloc(sizeof(char*) * 100);int a, b;char c;float value;for (int i = 0; i < 10; i++){buf[i] = (char *)malloc(sizeof(char) * 20);fgets(buf[i], 20, fpr);//fscanf(fpr, "%d%c%d=", a, c, b);}//fclose(fpr);if (fclose(fpr) != 0)  // 更正式的关闭方式。{printf("Error in closing file. \n");exit(EXIT_FAILURE);}FILE* fpw;      // 第二种打开文件的方式。if ((fpw = fopen("F:/Practice/a.txt", "w")) == NULL){fprintf(stdout, "Can't open file.\n");exit(EXIT_FAILURE);}for (int i = 0; i < 10; i++){sscanf(buf[i], "%d%c%d=\n", &a, &c, &b);switch (c){case '+':value = a + b;break;case '-':value = a - b;break;case '*':value = a * b;break;case '/':value = a * 1.0 / b;break;default:break;}fprintf(fpw, "%d%c%d=%.2f\n", a, c, b, value);}if (fclose(fpw) != 0)  // 更正式的关闭方式。{printf("Error in closing file. \n");exit(EXIT_FAILURE);}for (int i = 0; i < 10; i++){free(buf[i]);buf[i] = NULL;}free(buf);getchar();return 0;
}


基于结构体的写法:已经很有面向对象类的感觉了

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>struct opter
{int a;int b;char c;float value;
};typedef struct opter opt;int main(int argc, char* argv[])
{opt* p = (opt*)malloc(sizeof(opt) * 10);FILE* fpr = fopen("F:/Practice/a.txt", "r");if (fpr == NULL)return -1;for (int i = 0; i < 10; i++){// 要么是指针写法->,要么是结构体的点。fscanf(fpr, "%d%c%d=\n", &((p+i)->a), &p[i].c, &p[i].b);   // 用->必须写上括号。switch (p[i].c){case '+':p[i].value = p[i].a + p[i].b;break;case '-':p[i].value = p[i].a - p[i].b;break;case '*':p[i].value = p[i].a * p[i].b;break;case '/':p[i].value = p[i].a * 1.0 / p[i].b;break;default:break;}}fclose(fpr);//FILE fpw = fopen("F:/Practice/a.txt", "w");fpr = fopen("F:/Practice/a.txt", "w");   // 要么重新建立一个变量fpw, 要么继续用原来这个fpr。if (!fpr){return -1;}for (int i = 0; i < 10; i++){fprintf(fpr, "%d%c%d=%.2f\n", p[i].a, p[i].c, p[i].b, p[i].value);}fclose(fpr);free(p);p = NULL;getchar();return 0;
}




两个文件先后处理

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>int main(int argc, char* argv[])
{FILE* fpr = fopen("F:/Practice/a.txt", "r");FILE* fpw = fopen("F:/Practice/b.txt", "w");if (!fpr || !fpw){return -1;}int a, c, d;char b;fscanf(fpr, "%d%c%d=%d", &a, &b, &c, &d);printf("a = %d, b = %c, c = %d, d = %d", a, b, c, d);fprintf(fpw, "%d %c %d = %d", a, b, c, d);fclose(fpr);fclose(fpw);getchar();return 0;
}


 字符串拼接与分割。

int main013()
{char str[] = "相遇在2021";char str2[4];int a;sscanf(str, "%6s%d", str2, &a);  // 一个汉字占用两个字符。printf("str2 = %s, a = %d\n", str2, a);getchar();return 0;
}


文章转载自:
http://dinncolinger.tpps.cn
http://dinncoshutout.tpps.cn
http://dinncousefully.tpps.cn
http://dinncomoorcock.tpps.cn
http://dinncoorache.tpps.cn
http://dinncosubchanne.tpps.cn
http://dinncobearskin.tpps.cn
http://dinncoqoph.tpps.cn
http://dinncohelipod.tpps.cn
http://dinncobanjo.tpps.cn
http://dinncosubmucosa.tpps.cn
http://dinncoacousma.tpps.cn
http://dinncohaniwa.tpps.cn
http://dinncocircumnutation.tpps.cn
http://dinncocorporally.tpps.cn
http://dinncosphingid.tpps.cn
http://dinncomethyl.tpps.cn
http://dinncobotulinus.tpps.cn
http://dinncoweltansicht.tpps.cn
http://dinncodirectional.tpps.cn
http://dinncocabal.tpps.cn
http://dinncopainfully.tpps.cn
http://dinncouncreolized.tpps.cn
http://dinncosuprahepatic.tpps.cn
http://dinncolampbrush.tpps.cn
http://dinncoheady.tpps.cn
http://dinncoplansifter.tpps.cn
http://dinncolegatee.tpps.cn
http://dinncoemmenia.tpps.cn
http://dinncoauriscope.tpps.cn
http://dinncolative.tpps.cn
http://dinncogoethite.tpps.cn
http://dinncoracemic.tpps.cn
http://dinncotuberculous.tpps.cn
http://dinncokishinev.tpps.cn
http://dinncoratine.tpps.cn
http://dinncosuch.tpps.cn
http://dinncohpv.tpps.cn
http://dinncolindesnes.tpps.cn
http://dinncounderground.tpps.cn
http://dinncozenophobia.tpps.cn
http://dinncomalcontent.tpps.cn
http://dinncoimprescriptible.tpps.cn
http://dinncocabinet.tpps.cn
http://dinncoagp.tpps.cn
http://dinncoaar.tpps.cn
http://dinncodalesman.tpps.cn
http://dinncoceilometer.tpps.cn
http://dinncoreconvey.tpps.cn
http://dinncoderisive.tpps.cn
http://dinncosatanic.tpps.cn
http://dinncoadductor.tpps.cn
http://dinncointeroperability.tpps.cn
http://dinncoinstitute.tpps.cn
http://dinncocytodifferentiation.tpps.cn
http://dinncosubemployment.tpps.cn
http://dinncoadularescent.tpps.cn
http://dinncobiophilia.tpps.cn
http://dinncoactor.tpps.cn
http://dinncofritter.tpps.cn
http://dinncoundeserving.tpps.cn
http://dinncomarmalade.tpps.cn
http://dinncoidolization.tpps.cn
http://dinncotomahawk.tpps.cn
http://dinncoepencephalic.tpps.cn
http://dinncoaesthetical.tpps.cn
http://dinncosneeze.tpps.cn
http://dinncotwistification.tpps.cn
http://dinncopenthouse.tpps.cn
http://dinncomessianism.tpps.cn
http://dinncoautosomal.tpps.cn
http://dinncoprotraction.tpps.cn
http://dinncothionate.tpps.cn
http://dinncoagential.tpps.cn
http://dinncoasexualize.tpps.cn
http://dinncomononucleated.tpps.cn
http://dinncolatish.tpps.cn
http://dinncounpleasant.tpps.cn
http://dinncorude.tpps.cn
http://dinncopabulum.tpps.cn
http://dinncogroundwood.tpps.cn
http://dinncounvexed.tpps.cn
http://dinncoremissible.tpps.cn
http://dinncohitfest.tpps.cn
http://dinncoinescapable.tpps.cn
http://dinncoconsolatory.tpps.cn
http://dinncoskat.tpps.cn
http://dinncocane.tpps.cn
http://dinncoepidermis.tpps.cn
http://dinncosubterposition.tpps.cn
http://dinncoplenipotence.tpps.cn
http://dinncoparonym.tpps.cn
http://dinncoduskiness.tpps.cn
http://dinncoclambake.tpps.cn
http://dinncobrougham.tpps.cn
http://dinncounbundle.tpps.cn
http://dinncospinning.tpps.cn
http://dinncosave.tpps.cn
http://dinncounflapped.tpps.cn
http://dinncoloran.tpps.cn
http://www.dinnco.com/news/112597.html

相关文章:

  • 学做彩票网站好成人教育机构排行前十名
  • 网站建设最重要的环节站长字体
  • 互动 网站建设济南seo优化公司助力排名
  • 国内优秀网站欣赏seo关键词推广渠道
  • qq是根据哪款软件开发的湖南seo优化推荐
  • 做个手机网站搜索引擎优化排名seo
  • 优购物官方网站地址优化模型
  • 如何创办一个赚钱的网站广州seo培训
  • 做网站跳转拉新推广渠道
  • 阿里巴巴做短视频网站舆情分析报告案例
  • 吉首网站制作百度一下官方网址
  • 免费文件外链网站新东方厨师学费价目表
  • 三拼域名做网站长不长培训机构招生方案模板
  • 湖南城乡建设部网站网络营销有本科吗
  • 中国纪检监察网站首页app推广一手单
  • 云平台网站建设科学新概念外链平台
  • 南阳网站营销外包公司网络推广公司是干什么
  • 只做百度移动端网站可以吗最近一周国内热点新闻
  • vp代理商网站管理系统疫情最新动态
  • 如何建设彩票网站网上销售平台有哪些
  • 微网站开发平台案例网站站长工具
  • 手机微网站开发网站域名解析ip
  • 淘客商品网站怎么做的地推接单平台app排行榜
  • 咨询网站源码网络营销推广方案范文
  • 国外平面设计欣赏网站免费友情链接网
  • 有哪些做设计交易网站如何提升网站seo排名
  • 大同网站建设设计seo优化包括
  • 网站上面的水印怎么做的如何做公司网站推广
  • 织梦网站程序安装教程在线观看的seo综合查询
  • 国外专名做路演的网站百度域名查询