输入两行字符串“domain x 10.0 y 10.0 z 10.0”,“block x 20.0 y 20.0 z 20.0”
如何判断输入是属于domain还是block?如何将数值保存到与x、y、z对应的变量中?另外由于变量是double,小数点也要输入到数值变量中,该如何处理?
c++字符与数字混合输入与读取
答案:3 悬赏:0
解决时间 2021-03-07 12:53
- 提问者网友:单纯说谎家
- 2021-03-07 00:04
最佳答案
- 二级知识专家网友:废途浑身病态
- 2021-03-07 00:31
看你是什么项目类型了,如果是控制台项目,那么
输入用gets()
sscanf处理字符串,根据空格拆分
拆分后第一个字符串进行strstr比较即可知道domain/block
atof等函数将拆分后的字符串转换为double(如果格式固定,sscanf就可以直接到double了)
输入用gets()
sscanf处理字符串,根据空格拆分
拆分后第一个字符串进行strstr比较即可知道domain/block
atof等函数将拆分后的字符串转换为double(如果格式固定,sscanf就可以直接到double了)
全部回答
- 1楼网友:输掉的尊严
- 2021-03-07 01:50
string s;
char t ;
double x , y , z ;
cin >> s ;
cin >> t >> x >> t >> y >> t >> z ;
if (s == 'domain') dosomething();
else doanotherthing();
- 2楼网友:不服输的倔强
- 2021-03-07 01:45
c++ #include <stdio.h> 便可方便地用 scanf( ) 格式化输入。
用 strcmp(s,"domain")==0 判断 属于 domain
完整程序如下:
#include <iostream>
using namespace std;
#include <stdio.h>
int main(){
char s[10];
double x,y,z;
int i;
for (i=0;i<2;i++){
scanf("%s x %lf y %lf z %lf",s,&x,&y,&z);
if (strcmp(s,"domain")==0){
printf("this belongs to domain: %lf %lf %lf\n",x,y,z);
} else if (strcmp(s,"block")==0){
printf("this belongs to block: %lf %lf %lf\n",x,y,z);
} else printf("not belong to domain and block !\n");
}
return 0;
}
// input: domain x 10.0 y 10.0 z 10.0 block x 20.0 y 20.0 z 20.0
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯