######PC上で書いたプログラムをスマートフォン上でコンパイル,実行したい
もともとPC上で動かしていたプログラムをそのままiPhoneに移植できたら大変便利なので,AppStoreなどで無料で入手できるCのコンパイラをダウンロードしコンパイル,実行したところうまく動かない部分があります.
PCはOS X El Capitan ver. 10.11.6でgccコンパイラを使っています.
うまくいかないのは,コンパイラのせいでしょうか?それとも他に原因がありますか?
いままでこのようなことをしたことがなかったので,つまずいています.
######エラー中身について
アプリ名:CppCode
入力が2回求められて結果が表示されない
アプリ名:モバイルC
数字が入ったものを入力すると
mol.c(174):error: __builtin_huge_val:No symbol found.
数字がないものだと問題なく動作.
アプリ名:C Language
コンパイルが通らない
Insufficient compilations. Please get more on Setting tab.
アプリ名:SketchCFree
math.hが無い
######解決したいこと,知りたいこと
自分のPCでは動くが,よそでは動かないということはよくあること?
char samplename[64] = {};
という宣言の仕方で自分のPC上では問題なく動作するのはなぜでしょう?
このソースコードをWindowsで動かしたらちゃんと動くのか,気になります.
######化学式を入力し,その分子量を求める自作のプログラム
(例)
入力: Fe
出力: 55.84500 g/mol
入力: O2
出力: 31.99880 g/mol
入力: H2O
出力: 18.01536 g/mol
入力: (Fe0.9Mn0.1)3GeTe2
出力: 495.09290 g/mol
入力: YBa2Cu3O6.8
出力: 662.99372 g/mol
C
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <errno.h> 5#include <math.h> 6 7char samplename[64] = {}; 8int isDigit(char); 9typedef struct Material { 10 int type; //0=Atom, 1=Molecule 11} Material_t; 12typedef struct Atom { 13 int material_type; //0=Atom, 1=Molecule 14 char name[3]; 15 double weight; 16} Atom_t; 17typedef struct MaterialValue { 18 Material_t *material; //Atom or Molecule 19 double ratio; 20} MaterialValue_t; 21typedef struct Molecule { 22 int material_type; //0=Atom, 1=Molecule 23 int materials_count; 24 MaterialValue_t materials[64]; 25} Molecule_t; 26Atom_t atoms[] = { 27 { 0, "H" , 1.00798 }, { 0, "He", 4.0026 }, { 0, "Li", 6.968 }, 28 { 0, "Be", 9.01218 }, { 0, "B" , 10.814 }, { 0, "C" , 12.0106 }, 29 { 0, "N" , 14.0069 }, { 0, "O" , 15.9994 }, { 0, "F" , 18.9984 }, 30 { 0, "Ne", 20.1797 }, { 0, "Na", 22.9898 }, { 0, "Mg", 24.306 }, 31 { 0, "Al", 26.9815 }, { 0, "Si", 28.085 }, { 0, "P" , 30.9738 }, 32 { 0, "S" , 32.068 }, { 0, "Cl", 35.452 }, { 0, "Ar", 39.948 }, 33 { 0, "K" , 39.0983 }, { 0, "Ca", 40.078 }, { 0, "Sc", 44.9559 }, 34 { 0, "Ti", 47.867 }, { 0, "V" , 50.9415 }, { 0, "Cr", 51.9961 }, 35 { 0, "Mn", 54.938 }, { 0, "Fe", 55.845 }, { 0, "Co", 58.9332 }, 36 { 0, "Ni", 58.6934 }, { 0, "Cu", 63.546 }, { 0, "Zn", 65.38 }, 37 { 0, "Ga", 69.723 }, { 0, "Ge", 72.630 }, { 0, "As", 74.9216 }, 38 { 0, "Se", 78.971 }, { 0, "Br", 79.904 }, { 0, "Kr", 83.798 }, 39 { 0, "Rb", 85.4678 }, { 0, "Sr", 87.62 }, { 0, "Y" , 88.9058 }, 40 { 0, "Zr", 91.224 }, { 0, "Nb", 92.9064 }, { 0, "Mo", 95.95 }, 41 { 0, "Tc", 99 }, { 0, "Ru", 101.07 }, { 0, "Rh", 102.906 }, 42 { 0, "Pd", 106.42 }, { 0, "Ag", 107.868 }, { 0, "Cd", 112.414 }, 43 { 0, "In", 114.818 }, { 0, "Sn", 118.710 }, { 0, "Sb", 121.760 }, 44 { 0, "Te", 127.60 }, { 0, "I" , 126.904 }, { 0, "Xe", 131.293 }, 45 { 0, "Cs", 132.905 }, { 0, "Ba", 137.327 }, { 0, "La", 138.905 }, 46 { 0, "Ce", 140.116 }, { 0, "Pr", 140.908 }, { 0, "Nd", 144.242 }, 47 { 0, "Pm", 145 }, { 0, "Sm", 150.36 }, { 0, "Eu", 151.964 }, 48 { 0, "Gd", 157.25 }, { 0, "Tb", 158.925 }, { 0, "Dy", 162.500 }, 49 { 0, "Ho", 164.930 }, { 0, "Er", 167.259 }, { 0, "Tm", 168.934 }, 50 { 0, "Yb", 173.045 }, { 0, "Lu", 174.967 }, { 0, "Hf", 178.49 }, 51 { 0, "Ta", 180.948 }, { 0, "W" , 183.84 }, { 0, "Re", 186.207 }, 52 { 0, "Os", 190.23 }, { 0, "Ir", 192.217 }, { 0, "Pt", 195.084 }, 53 { 0, "Au", 196.967 }, { 0, "Hg", 200.592 }, { 0, "Tl", 204.384 }, 54 { 0, "Pb", 207.2 }, { 0, "Bi", 208.980 }, { 0, "Po", 210 }, 55 { 0, "At", 210 }, { 0, "Rn", 222 }, { 0, "Fr", 223 }, 56 { 0, "Ra", 226 }, { 0, "Ac", 227 }, { 0, "Th", 232.038 }, 57 { 0, "Pa", 231.036 }, { 0, "U" , 238.029 }, { 0, "Np", 237 }, 58 { 0, "Pu", 239 }, { 0, "Am", 243 }, { 0, "Cm", 247 }, 59 { 0, "Bk", 247 }, { 0, "Cf", 252 }, { 0, "Es", 252 }, 60 { 0, "Fm", 257 }, { 0, "Md", 258 }, { 0, "No", 259 }, 61 { 0, "Lr", 262 }, { 0, "Rf", 267 }, { 0, "Db", 268 }, 62 { 0, "Sg", 271 }, { 0, "Bh", 272 }, { 0, "Hs", 277 }, 63 { 0, "Mt", 276 }, { 0, "Ds", 281 }, { 0, "Rg", 280 }, 64 { 0, "Cn", 285 }, { 0, "Nh", 278 }, { 0, "Fl", 289 }, 65 { 0, "Mc", 289 }, { 0, "Lv", 293 }, { 0, "Ts", 293 }, 66 { 0, "Og", 294 }, {-1, "" , -1 } //EOD 67}; 68int str_cnt(char str[], char c) { 69 int n = 0; 70 for (int i=0; i<(int)strlen(str); i++) { 71 if (str[i] == c) n++; 72 } 73 return n; 74} 75void JudgeBrackets(void) { 76 char *bra, *ket; 77 bra = strchr(samplename, '('); 78 ket = strchr(samplename, ')'); 79 80 if (bra == NULL && ket == NULL) return; 81 if (str_cnt(samplename, '(') != str_cnt(samplename, ')')) { 82 fprintf(stderr, "The numbers of '(' and ')' do not match.\n"); 83 } else if (++bra == ket) { 84 fprintf(stderr, "Nothing is entered in parenthese.\n"); 85 } else if (isDigit(samplename[bra - samplename])) { 86 fprintf(stderr, "Incorrect input.\n"); 87 } else return; 88 exit(1); 89} 90void Input(void){ 91 printf("Enter sample name.\nChemical formula => "); 92 if (scanf("%63[^\n]%*[^\n]", samplename) != 1) { 93 fprintf(stderr, "%s\n", strerror(errno)); 94 exit(1); 95 } 96 if (isDigit(samplename[0])) { 97 fprintf(stderr, "Incorrect input.\n"); 98 exit(1); 99 } else while (getchar() != '\n'); 100 return JudgeBrackets(); 101} 102Atom_t *search_atom(char *name) { 103 Atom_t *p; 104 for (p=atoms; p->weight>0; p++) { 105 if (strcmp(p->name, name) == 0) return p; 106 } 107 fprintf(stderr, "The entered atom or molecule does not exist!\n"); 108 exit(1); 109} 110Molecule_t *newMolecule() { 111 Molecule_t *molecule = (Molecule_t *)malloc(sizeof(Molecule_t)); 112 molecule->material_type = 1; 113 molecule->materials_count = 0; 114 return molecule; 115} 116void add(Molecule_t *molecule, Material_t *material) { 117 MaterialValue_t *mv = &(molecule->materials[molecule->materials_count++]); 118 mv->material = material; 119 mv->ratio = 1.0; 120} 121void setRatio(Molecule_t *molecule, double ratio) { 122 molecule->materials[molecule->materials_count-1].ratio = ratio; 123} 124double getWeight(Molecule_t *molecule) { 125 double weight = 0; 126 for (int i=0; i<molecule->materials_count; i++) { 127 MaterialValue_t *mv = &molecule->materials[i]; 128 if (mv->material->type == 0) { //Atom 129 weight += ((Atom_t *)(mv->material))->weight * mv->ratio; 130 } else { //Molecule 131 weight += getWeight((Molecule_t *)(mv->material)) * mv->ratio; 132 } 133 } 134 return weight; 135} 136// Stack 137Molecule_t *stack[64]; 138int stack_pointer = 0; 139void stack_push(Molecule_t *molecule) { 140 stack[stack_pointer++] = molecule; 141} 142Molecule_t *stack_pop() { 143 return stack[--stack_pointer]; 144} 145Molecule_t *stack_peek() { 146 return stack[stack_pointer-1]; 147} 148// Analysis 149int isDigit(char c) { 150 return ('0' <= c && c <= '9'); 151} 152int isDigitOrPeriod(char c) { 153 return (isDigit(c) || c == '.'); 154} 155Molecule_t *parse(void) { 156 int i, j, k; 157 char v[64]; 158 stack_push(newMolecule()); 159 for (i=0; i<(int)strlen(samplename); i++) { 160 memset(v, '\0', sizeof(v)); 161 v[0] = samplename[i]; 162 if (isDigit(v[0])) { 163 for (j=i+1, k=1; j<(int)strlen(samplename) && isDigitOrPeriod(samplename[j]); k++, j++, i++) { 164 v[k] = samplename[j]; 165 } 166 if (strtod(v, NULL) == HUGE_VAL) { 167 fprintf(stderr, "OverFlow.\n"); 168 exit(1); 169 } else if (strtod(v, NULL) == 0 && errno == ERANGE) { 170 fprintf(stderr, "UnderFlow.\n"); 171 exit(1); 172 } else setRatio(stack_peek(), strtod(v, NULL)); 173 } else if (strcmp(v, "(") == 0) { 174 stack_push(newMolecule()); 175 } else if (strcmp(v, ")") == 0) { 176 Material_t *material = (Material_t *)stack_pop(); 177 add(stack_peek(), material); 178 } else { 179 // Determine whether the next character is lowercase. 180 // If it is lowercase, it's a two letter atom, otherwise it's a one letter atom. 181 if ((samplename[i+1]>='a') && (samplename[i+1]<='z')) { 182 v[1] = samplename[i+1]; 183 i++; 184 } 185 add(stack_peek(), (Material_t *)search_atom(v)); 186 } 187 } 188 return stack_pop(); 189} 190int main(void) { 191 Input(); 192 printf("%.5lf g/mol\n", getWeight(parse())); 193 return 0; 194}
回答2件
あなたの回答
tips
プレビュー