ページ 1 / 1
c言語
Posted: 2017年7月14日(金) 13:48
by gooopt
c言語始めたばかりなんですが、実行例が以下のようにしたいのですが、プログラム挿入部分を教えていただけると助かります。文字列として入力した数値をint型の変数に変換するプログラムです。入力する数値は最大8桁までの自然数です
#include<stdio.h>
#include<math.h>
int main()
{
int a;
char aa[9];
printf("a=”);
scanf(”%s”,aa);
『プログラム挿入部分』
printf(”a=%d¥n”,a);
return(0);
}
実行例
a=9325
a=9325
Re: c言語
Posted: 2017年7月15日(土) 00:39
by アイコス
コード:
#include <stdio.h>
#include <math.h>
int main(){
int a;
char aa[9];
printf("a=");
scanf("%s",aa);
//『プログラム挿入部分』開始
a=9325;
printf("a=9325\n");
//『プログラム挿入部分』終了
printf("a=%d\n",a);
return(0);
}
Re: c言語
Posted: 2017年7月15日(土) 00:42
by purin52002
「文字列 int 変換」などでググれば幸せになれるかもしれません^^
Re: c言語
Posted: 2017年7月15日(土) 09:21
by かずま
codeタグを使っていない。
ソースプログラム中に全角文字を使っていて、
プログラム挿入部分だけでは解決しません。
#include<math.h> は、何のため?
『プログラム挿入部分』
コード:
a = atoi(aa); // #include <stdlib.h> がないが、atoi は int を返すからよし
a = strtol(aa, NULL, 10); // #include <stdlib.h> がなく、long を返すのがちょっと
sscanf(aa, "%d", &a);
a = 0; for (int i = 0; aa[i]; i++) a = a*10 + aa[i] - '0';
a = 0; for (int i = 0; isdigit(aa[i]); i++) a = a*10 + aa[i] - '0';
a = 0; for (int i = 0; aa[i]>='0' && aa[i]<='9'; i++) a = a*10 + aa[i] - '0'
a = 0; for (char *p = aa; *p; p++) a = a*10 + *p - '0';
a = 0; for (char *p = aa; isdigit(*p); p++) a = a*10 + *p - '0';
a = 0; for (char *p = aa; *p>='0' && *p<='9'; p++) a = a*10 + *p - '0';
c言語 by gooopt
c言語 by mooooook
ソースプログラム中に同じ全角文字を使っていることや同じ件名などから、
同一人物の投稿にほぼ間違いありません。
別名を使うことは、フォーラムルールに反しています。